From 04dc2c6be96b13f302a697e78c077ec895e91ab1 Mon Sep 17 00:00:00 2001 From: epezent Date: Wed, 9 Sep 2020 19:17:19 -0500 Subject: [PATCH] fix bugs in date picker --- implot.cpp | 28 +++++++++------------------- implot_internal.h | 6 ++---- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/implot.cpp b/implot.cpp index 23de207..345ffe1 100644 --- a/implot.cpp +++ b/implot.cpp @@ -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) diff --git a/implot_internal.h b/implot_internal.h index 4341286..29ac6be 100644 --- a/implot_internal.h +++ b/implot_internal.h @@ -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); //-----------------------------------------------------------------------------