1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-22 18:28:53 -05:00

fix bugs in date picker

This commit is contained in:
epezent 2020-09-09 19:17:19 -05:00
parent 3cdf7add04
commit 04dc2c6be9
2 changed files with 11 additions and 23 deletions

View File

@ -665,6 +665,8 @@ ImPlotTime MkGmtTime(struct tm *ptm) {
#else
t.S = timegm(ptm);
#endif
if (t.S < 0)
t.S = 0;
return t;
}
@ -683,6 +685,8 @@ tm* GetGmtTime(const ImPlotTime& t, tm* ptm)
ImPlotTime MkLocTime(struct tm *ptm) {
ImPlotTime t;
t.S = mktime(ptm);
if (t.S < 0)
t.S = 0;
return t;
}
@ -734,20 +738,6 @@ ImPlotTime MakeTime(int year, int month, int day, int hour, int min, int sec, in
return t;
}
ImPlotTime MakeYear(int year) {
int yr = year - 1900;
if (yr < 0)
yr = 0;
tm& Tm = GImPlot->Tm;
Tm.tm_sec = 0;
Tm.tm_min = 0;
Tm.tm_hour = 0;
Tm.tm_mday = 1;
Tm.tm_mon = 0;
Tm.tm_year = yr;
return MkTime(&Tm);
}
int GetYear(const ImPlotTime& t) {
tm& Tm = GImPlot->Tm;
GetTime(t, &Tm);
@ -860,7 +850,7 @@ int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt) {
case ImPlotTimeFmt_DayMoYr: return snprintf(buffer, size, "%d/%d/%02d", mon, day, yr);
case ImPlotTimeFmt_DayMoYrHrMin: return snprintf(buffer, size, "%d/%d/%02d %d:%02d%s", mon, day, yr, hr, min, ap);
case ImPlotTimeFmt_DayMoYrHrMinS: return snprintf(buffer, size, "%d/%d/%02d %d:%02d:%02d%s", mon, day, yr, hr, min, sec, ap);
case ImPlotTimeFmt_DayMoYrHrMinSUs: return snprintf(buffer, size, "%d/%d/%d %d:%02d:%02d.%03d%03d%s", mon, day, yr, hr, min, sec, ms, us, ap);
case ImPlotTimeFmt_DayMoYrHrMinSUs: return snprintf(buffer, size, "%d/%d/%d %d:%02d:%02d.%03d%03d%s", mon, day, year, hr, min, sec, ms, us, ap);
case ImPlotTimeFmt_MoYr: return snprintf(buffer, size, "%s %d", mnames[Tm.tm_mon], year);
case ImPlotTimeFmt_Mo: return snprintf(buffer, size, "%s", mnames[Tm.tm_mon]);
case ImPlotTimeFmt_Yr: return snprintf(buffer, size, "%d", year);
@ -1043,7 +1033,7 @@ void AddTicksTime(const ImPlotRange& range, float plot_width, ImPlotTickCollecti
const int step = (int)interval <= 0 ? 1 : (int)interval;
for (int y = graphmin; y < graphmax; y += step) {
ImPlotTime t = MakeYear(y);
ImPlotTime t = MakeTime(y);
if (t >= t_min && t <= t_max) {
ImPlotTick tick(t.ToDouble(), true, true);
tick.Level = 0;
@ -3373,12 +3363,12 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime*
ImGui::SameLine(5*cell_size.x);
BeginDisabledControls(yr <= min_yr);
if (ImGui::ArrowButtonEx("##Up",ImGuiDir_Up,cell_size))
*t = MakeYear(yr-20);
*t = MakeTime(yr-20);
EndDisabledControls(yr <= min_yr);
ImGui::SameLine();
BeginDisabledControls(yr + 20 >= max_yr);
if (ImGui::ArrowButtonEx("##Down",ImGuiDir_Down,cell_size))
*t = MakeYear(yr+20);
*t = MakeTime(yr+20);
EndDisabledControls(yr+ 20 >= max_yr);
// ImGui::Dummy(cell_size);
cell_size.x *= 7.0f/4.0f;
@ -3393,7 +3383,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime*
ImGui::Dummy(cell_size);
}
else if (ImGui::Button(buff,cell_size)) {
*t = MakeYear(yr);
*t = MakeTime(yr);
*level = 1;
}
if (t1_or_t2)

View File

@ -830,8 +830,6 @@ IMPLOT_API tm* GetLocTime(const ImPlotTime& t, tm* ptm);
// Make a timestamp from time components.
// year[1970-3000], month[0-11], day[1-31], hour[0-23], min[0-59], sec[0-59], us[0,999999]
IMPLOT_API ImPlotTime MakeTime(int year, int month = 0, int day = 1, int hour = 0, int min = 0, int sec = 0, int us = 0);
// Make a timestamp starting at the first day of a year [1970-3000]
IMPLOT_API ImPlotTime MakeYear(int year);
// Get year component from timestamp [1970-3000]
IMPLOT_API int GetYear(const ImPlotTime& t);
@ -844,7 +842,7 @@ IMPLOT_API ImPlotTime CeilTime(const ImPlotTime& t, ImPlotTimeUnit unit);
// Rounds a timestamp up or down to the nearest unit.
IMPLOT_API ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit);
// Combines the date of one timestamp with the time-of-day of another timestamp.
IMPLOT_API ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& tod_part);
IMPLOT_API ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& time_part);
// Formulates a timestamp t into a buffer according to fmt.
IMPLOT_API int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt);
@ -857,7 +855,7 @@ IMPLOT_API void PrintTime(const ImPlotTime& t, ImPlotTimeFmt fmt = ImPlotTimeFmt
// #t1 and #t2 are optional dates to highlight.
IMPLOT_API bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* t1 = NULL, const ImPlotTime* t2 = NULL);
// Shows a time picker widget block (hour/min/sec).
// #t will be set when a new hour, minute, or sec is selected and the function will return true.
// #t will be set when a new hour, minute, or sec is selected or am/pm is toggled, and the function will return true.
IMPLOT_API bool ShowTimePicker(const char* id, ImPlotTime* t);
//-----------------------------------------------------------------------------