rcolyer.net
RC Lib  Version 202403231100
RTime.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2014, Ryan A. Colyer
4 // Distributed under the Boost Software License, v1.0. (LICENSE.txt)
5 //
6 /// \file RTime.h
7 /// Provides classes for accessing and working with dates and times.
8 /////////////////////////////////////////////////////////////////////
9 
10 #ifndef RC_TIME_H
11 #define RC_TIME_H
12 
13 
14 #include "Types.h"
15 #include "RStr.h"
16 #include "Data1D.h"
17 #include <ctime>
18 #ifdef WIN32
19 #include <winsock2.h>
20 #include <windows.h>
21 #include <winbase.h>
22 #endif
23 
24 
25 namespace RC {
26  /// Accesses and formats the system date and time, and provides high
27  /// precision timing.
28  class Time {
29  public:
30 
31  /// Start the timer.
32  inline void Start() {
33  proc_start_time = clock();
34  real_start_time = Time::Get();
35  }
36 
37 
38  /// Default constructor. Starts the timer.
39  inline Time() {
40  Start();
41  }
42 
43 
44  /// Returns seconds since start in real time.
45  inline f64 SinceStart() {
46  return (Time::Get() - real_start_time);
47  }
48 
49 
50  /// Returns seconds since start in process time.
51  inline f64 ProcSinceStart() {
52  clock_t now;
53 
54  now = clock();
55  return ((f64(now-proc_start_time))/CLOCKS_PER_SEC);
56  }
57 
58 
59  protected:
60  /// @cond PROTECTED
61 
62  clock_t proc_start_time;
63  f64 real_start_time;
64 
65  static inline struct tm Epoch() {
66  struct tm tmval;
67  tmval.tm_sec = tmval.tm_min = tmval.tm_hour = 0;
68  tmval.tm_mday = 1;
69  tmval.tm_mon = 0;
70  tmval.tm_year = 70;
71  tmval.tm_wday = 4;
72  tmval.tm_yday = 0;
73  tmval.tm_isdst = 0;
74  return tmval;
75  }
76 
77  /// Returns the amount time_t changes for one second.
78  static inline f64 time_t_ScaleFactor() {
79  static bool set = false;
80  static f64 factor;
81  if (!set) {
82  struct tm tmval = Epoch();
83  time_t epoch, plus1;
84  epoch = mktime(&tmval);
85  tmval.tm_sec = 1;
86  plus1 = mktime(&tmval);
87  factor = plus1 - epoch;
88  set = true;
89  }
90  return factor;
91  }
92 
93  static inline f64 time_t_Offset() {
94  static bool set = false;
95  static f64 offset;
96  if (!set) {
97  struct tm tmval = Epoch();
98  tmval.tm_isdst = Get_tm().tm_isdst;
99  time_t epoch = mktime(&tmval);
100  offset = epoch / time_t_ScaleFactor() + GetTimezone();
101  set = true;
102  }
103  return offset;
104  }
105 
106  /// @endcond
107  public:
108 
109  /// Returns seconds since 1970-01-01 epoch to a precision between
110  /// 100 nanoseconds and 1 microsecond.
111  static inline f64 Get() {
112  f64 retval;
113 #ifdef WIN32
114  i64 time_100ns;
115  FILETIME ft;
116  GetSystemTimeAsFileTime(&ft);
117  time_100ns = i64(
118  (((u64(ft.dwHighDateTime))<<u64(32)) + ft.dwLowDateTime));
119  time_100ns -= 116444736000000000ull; // 1601 epoch to 1970 epoch
120  retval = (f64(time_100ns)) * 1e-7L;
121 #else // POSIX
122  struct timespec tp;
123  clock_gettime(CLOCK_REALTIME, &tp);
124  retval = tp.tv_sec + 1e-9L*tp.tv_nsec;
125 #endif
126  return retval;
127  }
128 
129  /// Converts a time_t value into seconds from the 1970-01-01 epoch.
130  static inline f64 Get(time_t tt) {
131  return tt / time_t_ScaleFactor() - time_t_Offset();
132  }
133 
134  /// Returns the seconds from the 1970-01-01 epoch for tmval.
135  static inline f64 Get(struct tm tmval) {
136  return Get_UTC(Local2UTC(tmval));
137  }
138 
139  /// Given a UTC struct tm, returns the seconds since epoch portably.
140  static inline f64 Get_UTC(struct tm tmval) {
141  struct tm tmepoch = Epoch();
142  // Actually gives UTC diff, since mktime local, and real epoch is UTC.
143  return f64(difftime(mktime(&tmval), mktime(&tmepoch)));
144  }
145 
146 
147  /// Empirically determines the precision of Get(), and stores this value
148  /// for future calls to this.
149  /** @return The smallest amount by which Get() can increment.
150  */
151  static inline f64 GetPrecision() {
152  static RC_THREAD_LOCAL f64 min = RC::MAX_VAL<f64>();
153  if (min < RC::MAX_VAL<f64>()) { return min; }
154 
155  f64 last_t, t;
156  t = RC::Time::Get();
157  for (u32 tries=0; tries<10;) {
158  last_t = t;
159  t = RC::Time::Get();
160  f64 diff = t - last_t;
161  if (diff > 0) {
162  tries++;
163  min = (min <= diff) ? min : diff;
164  }
165  }
166  return min;
167  }
168 
169 
170  /// Returns the number of seconds offset from UTC
171  static inline i32 GetTimezone() {
172  time_t tt = time(NULL);
173  struct tm tmloc = Get_tm(tt);
174  struct tm tmutc = Get_tm_UTC(tt);
175  i32 offset = 3600*(tmloc.tm_hour - tmutc.tm_hour)
176  + 60*(tmloc.tm_min - tmutc.tm_min)
177  + tmloc.tm_sec - tmutc.tm_sec;
178  if (offset > 3600*12) { offset -= 3600*24; }
179  else if (offset <= -3600*12) { offset += 3600*24; }
180  return offset;
181  }
182 
183  /// Returns { hours, min, sec } offset from UTC.
184  static inline Data1D<i32> GetTimezoneData() {
185  Data1D<i32> retval(3);
186  i32 offset = GetTimezone();
187  retval[0] = offset / 3600;
188  retval[1] = offset / 60 - 60*retval[0];
189  retval[2] = offset - 3600*retval[0] - 60*retval[1];
190  return retval;
191  }
192 
193  /// Returns the local time given UTC time tmval.
194  static inline struct tm Local2UTC(struct tm tmval) {
195  struct tm retval = tmval;
197  retval.tm_hour -= tz[0];
198  retval.tm_min -= tz[1];
199  retval.tm_sec -= tz[2];
200  retval.tm_isdst = 0;
201  mktime(&retval);
202  return retval;
203  }
204 
205  /// Returns the UTC time given local time tmval.
206  static inline struct tm UTC2Local(struct tm tmval) {
207  struct tm retval = tmval;
208  Data1D<i32> tz = GetTimezoneData();
209  retval.tm_hour += tz[0];
210  retval.tm_min += tz[1];
211  retval.tm_sec += tz[2];
212  retval.tm_isdst = Get_tm().tm_isdst;
213  mktime(&retval);
214  return retval;
215  }
216 
217 
218  /// Returns time_t as number of seconds since 1970-01-01 00:00:00 UTC.
219  static inline time_t Get_time_t() {
220  return time(NULL);
221  }
222 
223  /// Portably converts val as seconds since 1970-01-01 epoch to time_t.
224  static inline time_t Get_time_t(f64 val) {
225  return time_t((val + time_t_Offset()) * time_t_ScaleFactor());
226  }
227  /// Converts tmval in local timezone to time_t.
228  static inline time_t Get_time_t(struct tm tmval) {
229  return mktime(&tmval);
230  }
231  /// Converts tmval in UTC to time_t.
232  static inline time_t Get_time_t_UTC(struct tm tmval) {
233  return Get_time_t(UTC2Local(tmval));
234  }
235 
236 
237  /// Normalizes the input tmval.
238  static inline void Normalize(struct tm &tmval) {
239  if (mktime(&tmval) == -1) {
240  Throw_RC_Error("Invalid calendar time");
241  }
242  }
243 
244 
245  /// Provides a struct tm in the local timezone for the current time.
246  static inline struct tm Get_tm() {
247  return Get_tm(time(NULL));
248  }
249  /// Provides a struct tm in the current timezone for the given time_t.
250  static inline struct tm Get_tm(time_t tt) {
251  struct tm tmval;
252  tzset();
253 #ifdef WIN32
254  struct tm* tmptmval;
255  tmptmval = localtime(&tt);
256  if (tmptmval != NULL) {
257  tmval = *tmptmval;
258  }
259 #else
260  localtime_r(&tt, &tmval);
261 #endif
262 
263  return tmval;
264  }
265  /// Provides a struct tm in the local timezone for val seconds from
266  /// the 1970-01-01 epoch.
267  static inline struct tm Get_tm(f64 val) {
268  return Get_tm(Get_time_t(val));
269  }
270 
271 
272  /// Provides a struct tm in UTC for the current time.
273  static inline struct tm Get_tm_UTC() {
274  return Get_tm_UTC(time(NULL));
275  }
276  /// Provides a struct tm in UTC for the given time_t.
277  static inline struct tm Get_tm_UTC(time_t tt) {
278  struct tm tmval;
279  tzset();
280 #ifdef WIN32
281  struct tm* tmptmval;
282  tmptmval = gmtime(&tt);
283  if (tmptmval != NULL) {
284  tmval = *tmptmval;
285  }
286 #else
287  gmtime_r(&tt, &tmval);
288 #endif
289 
290  return tmval;
291  }
292 
293  /// Provides a struct tm in the local timezone for val seconds from
294  /// the 1970-01-01 epoch.
295  static inline struct tm Get_tm_UTC(f64 val) {
296  return Get_tm_UTC(Get_time_t(val));
297  }
298 
299  /// Returns a normalized struct tm.
300  static inline struct tm Get_tm(struct tm tmval) {
301  Normalize(tmval);
302  return tmval;
303  }
304 
305 
306  /// Provides the current time in the format "Fri Aug 1 17:05:25 2014"
307  static inline RStr GetStr() {
308  return GetStr(Get_tm());
309  }
310 
311  /// Provides the time in the format "Fri Aug 1 17:05:25 2014"
312  static inline RStr GetStr(struct tm tmval) {
313  mktime(&tmval);
314 #ifdef WIN32
315  char *ascval;
316  ascval = asctime(&tmval);
317 #else
318  char ascval[256];
319  asctime_r(&tmval, ascval);
320 #endif
321 
322  RStr retval = ascval;
323  retval.Chomp();
324 
325  return retval;
326  }
327 
328  /// Provides the UTC time in the format "Fri Aug 1 17:05:25 2014"
329  static inline RStr GetStr_UTC() {
330  return GetStr(Get_tm_UTC());
331  }
332 
333 
334  /// Provides the local time as a string with the format processed by
335  /// strftime.
336  static inline RStr GetStr(const RStr& format) {
337  return GetStr(format, Get_tm());
338  }
339  /// Provides the tmval as a string with the format processed by strftime.
340  static inline RStr GetStr(const RStr& format, struct tm tmval) {
341  char ascval[2048];
342  mktime(&tmval); // Must sanitize invalid values or strftime segfaults.
343  if (strftime(ascval, 2048, format.c_str(), &tmval) == 0) {
344  return RStr();
345  }
346  else {
347  return RStr(ascval);
348  }
349  }
350 
351  /// Provides the UTC time as a string with the format processed by
352  /// strftime.
353  static inline RStr GetStr_UTC(const RStr& format) {
354  return GetStr(format, Get_tm_UTC());
355  }
356 
357  /// Returns a local date string formatted like "2011-07-28"
358  static inline RStr GetDate() {
359  return GetStr("%Y-%m-%d");
360  }
361  /// Returns a date string for tmval formatted like "2011-07-28"
362  static inline RStr GetDate(struct tm tmval) {
363  return GetStr("%Y-%m-%d", tmval);
364  }
365  /// Returns a UTC date string formatted like "2011-07-28"
366  static inline RStr GetDate_UTC() {
367  return GetStr_UTC("%Y-%m-%d");
368  }
369 
370  /// Returns a local time string formatted like "19:49:18"
371  static inline RStr GetTime() {
372  return GetStr("%H:%M:%S");
373  }
374  /// Returns a time string for tmval formatted like "19:49:18"
375  static inline RStr GetTime(struct tm tmval) {
376  return GetStr("%H:%M:%S", tmval);
377  }
378  /// Returns a UTC time string formatted like "19:49:18"
379  static inline RStr GetTime_UTC() {
380  return GetStr_UTC("%H:%M:%S");
381  }
382 
383  /// Returns a local date-time string formatted like "2011-07-28_19-49-18",
384  /// which is suitable for filenames.
385  static inline RStr GetDateTime() {
386  return GetStr("%Y-%m-%d_%H-%M-%S");
387  }
388  /// Returns a date-time string from tmval formatted like
389  /// "2011-07-28_19-49-18", which is suitable for filenames.
390  static inline RStr GetDateTime(struct tm tmval) {
391  return GetStr("%Y-%m-%d_%H-%M-%S", tmval);
392  }
393  /// Returns a UTC date-time string formatted like "2011-07-28_19-49-18",
394  /// which is suitable for filenames.
395  static inline RStr GetDateTime_UTC() {
396  return GetStr_UTC("%Y-%m-%d_%H-%M-%S");
397  }
398 
399  /// Parses a date string like 2011-07-28, 2011-7-28, 07-28, 7-28, or
400  /// "/" instead of "-", into base tmval.
401  static inline struct tm ParseDate(const RStr& date, struct tm tmval) {
402  Data1D<RStr> parts = date.SplitAny("-/");
403  if (parts.size() < 2) { Throw_RC_Error("Invalid date"); }
404  tmval.tm_sec = tmval.tm_min = tmval.tm_hour = 0;
405  tmval.tm_isdst = -1;
406  if (parts.size() < 3) {
407  tmval.tm_year = Get_tm().tm_year;
408  tmval.tm_mon = parts[0].Get_i32(10) - 1;
409  tmval.tm_mday = parts[1].Get_i32(10);
410  }
411  else {
412  tmval.tm_year = parts[0].Get_i32(10) - 1900;
413  tmval.tm_mon = parts[1].Get_i32(10) - 1;
414  tmval.tm_mday = parts[2].Get_i32(10);
415  }
416  mktime(&tmval);
417  return tmval;
418  }
419  /// Parses a date string like 2011-07-28, 2011-7-28, 07-28, 7-28, or
420  /// "/" instead of "-", into base local time.
421  static inline struct tm ParseDate(const RStr& date) {
422  return ParseDate(date, Get_tm());
423  }
424  /// Parses a date string like 2011-07-28, 2011-7-28, 07-28, 7-28, or
425  /// "/" instead of "-", into base UTC time.
426  static inline struct tm ParseDate_UTC(const RStr& date) {
427  return ParseDate(date, Get_tm_UTC());
428  }
429 
430  /// Parses a time string like 19:49:18, 19:49, or "-" instead of ":",
431  /// into base tmval.
432  static inline struct tm ParseTime(const RStr& date, struct tm tmval) {
433  Data1D<RStr> parts = date.SplitAny("-:");
434  if (parts.size() < 2) { Throw_RC_Error("Invalid time"); }
435  tmval.tm_hour = parts[0].Get_i32(10);
436  tmval.tm_min = parts[1].Get_i32(10);
437  if (parts.size() < 3) {
438  tmval.tm_sec = 0;
439  }
440  else {
441  tmval.tm_sec = parts[2].Get_i32(10);
442  }
443  mktime(&tmval);
444  return tmval;
445  }
446  /// Parses a time string like 19:49:18, 19:49, or "-" instead of ":",
447  /// into base local time.
448  static inline struct tm ParseTime(const RStr& date) {
449  return ParseTime(date, Get_tm());
450  }
451  /// Parses a time string like 19:49:18, 19:49, or "-" instead of ":",
452  /// into base UTC time.
453  static inline struct tm ParseTime_UTC(const RStr& date) {
454  return ParseTime(date, Get_tm_UTC());
455  }
456 
457  /// Parses a string like ParseDate and ParseTime separated by "_" or " ",
458  /// into base tmval.
459  static inline struct tm ParseDateTime(const RStr& date, struct tm tmval) {
460  Data1D<RStr> parts = date.SplitAny("_ ");
461  if (parts.size() < 2) { Throw_RC_Error("Invalid date-time"); }
462  return ParseTime(parts[1], ParseDate(parts[0], tmval));
463  }
464  /// Parses a string like ParseDate and ParseTime separated by "_" or " ",
465  /// into base local time.
466  static inline struct tm ParseDateTime(const RStr& date) {
467  return ParseDateTime(date, Get_tm());
468  }
469  /// Parses a string like ParseDate and ParseTime separated by "_" or " ",
470  /// into base UTC time.
471  static inline struct tm ParseDateTime_UTC(const RStr& date) {
472  return ParseDateTime(date, Get_tm_UTC());
473  }
474 
475 
476  /// Sleeps for a floating point number of seconds, with sub-second
477  /// precision to the limit of the system.
478  static inline void Sleep(f64 seconds) {
479  if (seconds <= 0) {
480  return;
481  }
482 #ifdef WIN32
483  ::Sleep(seconds*1000);
484 #else // POSIX
485  struct timespec req;
486  req.tv_sec = time_t(seconds);
487  req.tv_nsec = long(1e9*(seconds-u64(seconds)));
488  nanosleep(&req, NULL);
489 #endif
490  }
491  };
492 
493 
494  /// A class which obeys periodic time-of-day boundaries, and can manage
495  /// times being within a daily time frame.
496  class TimeOfDay {
497  protected:
498  /// @cond PROTECTED
499  u32 sec;
500 
501  inline void Wrap() {
502  sec = sec % 86400;
503  }
504  /// @endcond
505  public:
506  /// Default constructor, initializes to local time.
507  inline TimeOfDay() {
508  (*this) = Time::Get_tm();
509  }
510 
511  /// Initializes to tmval.
512  inline TimeOfDay(struct tm tmval) {
513  (*this) = tmval;
514  }
515 
516  /// Initializes to the given hr, min, sec from midnight.
517  inline TimeOfDay(u32 hr, u32 min=0, u32 sec=0)
518  : sec(sec + 60*min + 3600*hr) {
519  Wrap();
520  }
521 
522  /// Initializes to tmval.
523  inline TimeOfDay& operator=(struct tm tmval) {
524  sec = tmval.tm_sec + 60*tmval.tm_min + 3600*tmval.tm_hour;
525  Wrap();
526  return *this;
527  }
528 
529  /// Subtracts another TimeOfDay amount, obeying periodic boundaries.
530  inline TimeOfDay& operator-=(const TimeOfDay& other) {
531  if (sec < other.sec) {
532  sec += 86400;
533  }
534  sec -= other.sec;
535  Wrap();
536  return *this;
537  }
538 
539  /// Subtracts two TimeOfDay amounts, obeying periodic boundaries.
540  inline TimeOfDay operator-(const TimeOfDay& other) const {
541  TimeOfDay retval(*this);
542  retval -= other;
543  return retval;
544  }
545 
546  /// Adds another TimeOfDay amount, obeying periodic boundaries.
547  inline TimeOfDay& operator+=(const TimeOfDay& other) {
548  sec += other.sec;
549  Wrap();
550  return *this;
551  }
552 
553  /// Adds two TimeOfDay amounts, obeying periodic boundaries.
554  inline TimeOfDay operator+(const TimeOfDay& other) const {
555  TimeOfDay retval(*this);
556  retval += other;
557  return retval;
558  }
559 
560  /// Creates TimeOfDay comparitors.
561 #define TIMEOFDAY_COMP(OP) \
562  /** \brief True if this TimeOfDay is OP the other. */ \
563  inline bool operator OP \
564  (const TimeOfDay& other) const {\
565  return sec OP other.sec;\
566  }
572 
573 
574  /// True if this TimeOfDay is later than left and earlier than right,
575  /// obeying periodic boundaries.
576  inline bool Between(const TimeOfDay& left, const TimeOfDay& right) const {
577  u32 thissec = ((*this) - left).sec;
578  u32 rightsec = (right - left).sec;
579  return (thissec < rightsec);
580  }
581 
582  /// Returns the 0-23 hour value after midnight.
583  inline u32 Hr() const { return sec / 3600; }
584  /// Returns the 0-59 minute value after midnight.
585  inline u32 Min() const { return (sec / 60) % 60; }
586  /// Returns the 0-59 second value after midnight.
587  inline u32 Sec() const { return sec % 60; }
588 
589  /// Returns the total seconds after midnight.
590  inline u32 AsSeconds() const { return sec; }
591  /// Returns the total minutes after midnight.
592  inline u32 AsMinutes() const { return sec / 60; }
593  /// Returns the total hours after midnight.
594  inline u32 AsHours() const { return sec / 3600; }
595 
596  /// Returns a formatted string representation.
597  inline RStr ToString(bool AmPm=false) const {
598  return RStr(AmPm ? (Hr()+11)%12+1 : Hr()).PadLeft(2, '0') + ":" +
599  RStr(Min()).PadLeft(2, '0') + ":" +
600  RStr(Sec()).PadLeft(2, '0') +
601  (AmPm ? (Hr() < 12 ? "am" : "pm") : "");
602  }
603 
604  /// Initializes from a number of seconds after midnight.
605  inline void FromSeconds(u32 newsec) { sec = newsec; Wrap(); }
606  /// Initializes from a number of minutes after midnight.
607  inline void FromMinutes(u32 newmin) { sec = newmin * 60; Wrap(); }
608  };
609 }
610 
611 
612 #endif // RC_TIME_H
613 
Provides a one-dimensional vector-like structure.
#define Throw_RC_Error(err)
Use this to throw an RC:ErrorMsg exception.
Definition: Errors.h:274
Provides a robust value-added wrapper for std::string.
#define TIMEOFDAY_COMP(OP)
Creates TimeOfDay comparitors.
Definition: RTime.h:561
Provides typedefs and routines for working with primitives.
uint64_t u64
64-bit unsigned integer.
Definition: Types.h:31
int32_t i32
32-bit signed integer.
Definition: Types.h:28
#define RC_THREAD_LOCAL
Provides thread_local if available.
Definition: Types.h:61
int64_t i64
64-bit signed integer.
Definition: Types.h:30
double f64
64-bit float.
Definition: Types.h:34
uint32_t u32
32-bit unsigned integer.
Definition: Types.h:29
A bounds-safe one-dimensional vector-like structure.
Definition: Data1D.h:49
A bounds-safe string class which provides an identical interface to std::string plus many convenience...
Definition: RStr.h:110
RStr & Chomp(const RStr &chomp_chars="\r\n")
Remove all trailing newline characters, or provided char set.
Definition: RStr.h:1575
RStr & PadLeft(const size_t pad_to, const char pad_with=' ')
If the length is less than pad_to, add pad_width to the left until it reaches that size.
Definition: RStr.h:1385
const char * c_str() const
Provides a null-terminated C style string corresponding to RStr.
Definition: RStr.h:622
A class which obeys periodic time-of-day boundaries, and can manage times being within a daily time f...
Definition: RTime.h:496
void FromSeconds(u32 newsec)
Initializes from a number of seconds after midnight.
Definition: RTime.h:605
u32 Hr() const
Returns the 0-23 hour value after midnight.
Definition: RTime.h:583
u32 Sec() const
Returns the 0-59 second value after midnight.
Definition: RTime.h:587
TimeOfDay(u32 hr, u32 min=0, u32 sec=0)
Initializes to the given hr, min, sec from midnight.
Definition: RTime.h:517
u32 AsSeconds() const
Returns the total seconds after midnight.
Definition: RTime.h:590
u32 Min() const
Returns the 0-59 minute value after midnight.
Definition: RTime.h:585
TimeOfDay(struct tm tmval)
Initializes to tmval.
Definition: RTime.h:512
TimeOfDay operator-(const TimeOfDay &other) const
Subtracts two TimeOfDay amounts, obeying periodic boundaries.
Definition: RTime.h:540
u32 AsHours() const
Returns the total hours after midnight.
Definition: RTime.h:594
TimeOfDay & operator+=(const TimeOfDay &other)
Adds another TimeOfDay amount, obeying periodic boundaries.
Definition: RTime.h:547
u32 AsMinutes() const
Returns the total minutes after midnight.
Definition: RTime.h:592
TimeOfDay operator+(const TimeOfDay &other) const
Adds two TimeOfDay amounts, obeying periodic boundaries.
Definition: RTime.h:554
TimeOfDay()
Default constructor, initializes to local time.
Definition: RTime.h:507
bool Between(const TimeOfDay &left, const TimeOfDay &right) const
True if this TimeOfDay is later than left and earlier than right, obeying periodic boundaries.
Definition: RTime.h:576
TimeOfDay & operator=(struct tm tmval)
Initializes to tmval.
Definition: RTime.h:523
void FromMinutes(u32 newmin)
Initializes from a number of minutes after midnight.
Definition: RTime.h:607
TimeOfDay & operator-=(const TimeOfDay &other)
Subtracts another TimeOfDay amount, obeying periodic boundaries.
Definition: RTime.h:530
RStr ToString(bool AmPm=false) const
Returns a formatted string representation.
Definition: RTime.h:597
Accesses and formats the system date and time, and provides high precision timing.
Definition: RTime.h:28
static f64 Get()
Returns seconds since 1970-01-01 epoch to a precision between 100 nanoseconds and 1 microsecond.
Definition: RTime.h:111
static struct tm ParseTime_UTC(const RStr &date)
Parses a time string like 19:49:18, 19:49, or "-" instead of ":", into base UTC time.
Definition: RTime.h:453
static RStr GetDate()
Returns a local date string formatted like "2011-07-28".
Definition: RTime.h:358
static RStr GetDateTime_UTC()
Returns a UTC date-time string formatted like "2011-07-28_19-49-18", which is suitable for filenames.
Definition: RTime.h:395
static RStr GetDate(struct tm tmval)
Returns a date string for tmval formatted like "2011-07-28".
Definition: RTime.h:362
static i32 GetTimezone()
Returns the number of seconds offset from UTC.
Definition: RTime.h:171
static RStr GetStr_UTC(const RStr &format)
Provides the UTC time as a string with the format processed by strftime.
Definition: RTime.h:353
static RStr GetStr()
Provides the current time in the format "Fri Aug 1 17:05:25 2014".
Definition: RTime.h:307
static RStr GetDateTime(struct tm tmval)
Returns a date-time string from tmval formatted like "2011-07-28_19-49-18", which is suitable for fil...
Definition: RTime.h:390
static f64 Get(struct tm tmval)
Returns the seconds from the 1970-01-01 epoch for tmval.
Definition: RTime.h:135
static RStr GetStr(const RStr &format, struct tm tmval)
Provides the tmval as a string with the format processed by strftime.
Definition: RTime.h:340
void Start()
Start the timer.
Definition: RTime.h:32
static struct tm Local2UTC(struct tm tmval)
Returns the local time given UTC time tmval.
Definition: RTime.h:194
static f64 Get(time_t tt)
Converts a time_t value into seconds from the 1970-01-01 epoch.
Definition: RTime.h:130
static struct tm ParseDate(const RStr &date, struct tm tmval)
Parses a date string like 2011-07-28, 2011-7-28, 07-28, 7-28, or "/" instead of "-",...
Definition: RTime.h:401
static struct tm Get_tm_UTC()
Provides a struct tm in UTC for the current time.
Definition: RTime.h:273
static f64 GetPrecision()
Empirically determines the precision of Get(), and stores this value for future calls to this.
Definition: RTime.h:151
static RStr GetStr(struct tm tmval)
Provides the time in the format "Fri Aug 1 17:05:25 2014".
Definition: RTime.h:312
static RStr GetDateTime()
Returns a local date-time string formatted like "2011-07-28_19-49-18", which is suitable for filename...
Definition: RTime.h:385
static struct tm Get_tm()
Provides a struct tm in the local timezone for the current time.
Definition: RTime.h:246
static Data1D< i32 > GetTimezoneData()
Returns { hours, min, sec } offset from UTC.
Definition: RTime.h:184
static RStr GetTime()
Returns a local time string formatted like "19:49:18".
Definition: RTime.h:371
static RStr GetTime(struct tm tmval)
Returns a time string for tmval formatted like "19:49:18".
Definition: RTime.h:375
static struct tm UTC2Local(struct tm tmval)
Returns the UTC time given local time tmval.
Definition: RTime.h:206
static RStr GetDate_UTC()
Returns a UTC date string formatted like "2011-07-28".
Definition: RTime.h:366
static time_t Get_time_t_UTC(struct tm tmval)
Converts tmval in UTC to time_t.
Definition: RTime.h:232
static struct tm ParseTime(const RStr &date, struct tm tmval)
Parses a time string like 19:49:18, 19:49, or "-" instead of ":", into base tmval.
Definition: RTime.h:432
static RStr GetStr_UTC()
Provides the UTC time in the format "Fri Aug 1 17:05:25 2014".
Definition: RTime.h:329
static struct tm ParseDateTime_UTC(const RStr &date)
Parses a string like ParseDate and ParseTime separated by "_" or " ", into base UTC time.
Definition: RTime.h:471
f64 ProcSinceStart()
Returns seconds since start in process time.
Definition: RTime.h:51
static struct tm ParseDateTime(const RStr &date, struct tm tmval)
Parses a string like ParseDate and ParseTime separated by "_" or " ", into base tmval.
Definition: RTime.h:459
static RStr GetTime_UTC()
Returns a UTC time string formatted like "19:49:18".
Definition: RTime.h:379
static struct tm ParseDate_UTC(const RStr &date)
Parses a date string like 2011-07-28, 2011-7-28, 07-28, 7-28, or "/" instead of "-",...
Definition: RTime.h:426
static RStr GetStr(const RStr &format)
Provides the local time as a string with the format processed by strftime.
Definition: RTime.h:336
static f64 Get_UTC(struct tm tmval)
Given a UTC struct tm, returns the seconds since epoch portably.
Definition: RTime.h:140
static time_t Get_time_t(struct tm tmval)
Converts tmval in local timezone to time_t.
Definition: RTime.h:228
Time()
Default constructor. Starts the timer.
Definition: RTime.h:39
static void Normalize(struct tm &tmval)
Normalizes the input tmval.
Definition: RTime.h:238
static time_t Get_time_t(f64 val)
Portably converts val as seconds since 1970-01-01 epoch to time_t.
Definition: RTime.h:224
f64 SinceStart()
Returns seconds since start in real time.
Definition: RTime.h:45
static time_t Get_time_t()
Returns time_t as number of seconds since 1970-01-01 00:00:00 UTC.
Definition: RTime.h:219
static void Sleep(f64 seconds)
Sleeps for a floating point number of seconds, with sub-second precision to the limit of the system.
Definition: RTime.h:478
Definition: APtr.h:25
email address
— (c) 2015