Skip to content

Commit

Permalink
refactor a few time utils; add Now/Today (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
epezent authored Dec 21, 2024
1 parent 77674d2 commit 93ce416
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
20 changes: 6 additions & 14 deletions implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions implot_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 93ce416

Please sign in to comment.