From 93ce4160a4777638690ee8d0309366adedc0dfd0 Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Sat, 21 Dec 2024 11:32:57 -0600 Subject: [PATCH] refactor a few time utils; add Now/Today (#605) --- implot.cpp | 20 ++++++-------------- implot_internal.h | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/implot.cpp b/implot.cpp index 6753a2e8..68addd42 100644 --- a/implot.cpp +++ b/implot.cpp @@ -950,20 +950,6 @@ tm* GetLocTime(const ImPlotTime& t, tm* ptm) { #endif } -inline ImPlotTime MkTime(struct tm *ptm) { - if (GetStyle().UseLocalTime) - return MkLocTime(ptm); - else - return MkGmtTime(ptm); -} - -inline tm* GetTime(const ImPlotTime& t, tm* ptm) { - if (GetStyle().UseLocalTime) - return GetLocTime(t,ptm); - else - return GetGmtTime(t,ptm); -} - ImPlotTime MakeTime(int year, int month, int day, int hour, int min, int sec, int us) { tm& Tm = GImPlot->Tm; @@ -993,6 +979,12 @@ int GetYear(const ImPlotTime& t) { return Tm.tm_year + 1900; } +int GetMonth(const ImPlotTime& t) { + tm& Tm = GImPlot->Tm; + ImPlot::GetTime(t, &Tm); + return Tm.tm_mon; +} + ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count) { tm& Tm = GImPlot->Tm; ImPlotTime t_out = t; diff --git a/implot_internal.h b/implot_internal.h index d54b77a3..bdebbd87 100644 --- a/implot_internal.h +++ b/implot_internal.h @@ -1564,11 +1564,24 @@ IMPLOT_API tm* GetLocTime(const ImPlotTime& t, tm* ptm); // NB: The following functions only work if there is a current ImPlotContext because the // internal tm struct is owned by the context! They are aware of ImPlotStyle.UseLocalTime. +// // Make a UNIX timestamp from a tm struct according to the current ImPlotStyle.UseLocalTime setting. +static inline ImPlotTime MkTime(struct tm *ptm) { + if (GetStyle().UseLocalTime) return MkLocTime(ptm); + else return MkGmtTime(ptm); +} +// Get a tm struct from a UNIX timestamp according to the current ImPlotStyle.UseLocalTime setting. +static inline tm* GetTime(const ImPlotTime& t, tm* ptm) { + if (GetStyle().UseLocalTime) return GetLocTime(t,ptm); + else return GetGmtTime(t,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); // Get year component from timestamp [1970-3000] IMPLOT_API int GetYear(const ImPlotTime& t); +// Get month component from timestamp [0-11] +IMPLOT_API int GetMonth(const ImPlotTime& t); // Adds or subtracts time from a timestamp. #count > 0 to add, < 0 to subtract. IMPLOT_API ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count); @@ -1581,6 +1594,11 @@ 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& time_part); +// Get the current time as a timestamp. +static inline ImPlotTime Now() { return ImPlotTime::FromDouble((double)time(nullptr)); } +// Get the current date as a timestamp. +static inline ImPlotTime Today() { return ImPlot::FloorTime(Now(), ImPlotTimeUnit_Day); } + // Formats the time part of timestamp t into a buffer according to #fmt IMPLOT_API int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt, bool use_24_hr_clk); // Formats the date part of timestamp t into a buffer according to #fmt