33 proc_start_time = clock();
55 return ((
f64(now-proc_start_time))/CLOCKS_PER_SEC);
62 clock_t proc_start_time;
65 static inline struct tm Epoch() {
67 tmval.tm_sec = tmval.tm_min = tmval.tm_hour = 0;
78 static inline f64 time_t_ScaleFactor() {
79 static bool set =
false;
82 struct tm tmval = Epoch();
84 epoch = mktime(&tmval);
86 plus1 = mktime(&tmval);
87 factor = plus1 - epoch;
93 static inline f64 time_t_Offset() {
94 static bool set =
false;
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();
116 GetSystemTimeAsFileTime(&ft);
118 (((
u64(ft.dwHighDateTime))<<
u64(32)) + ft.dwLowDateTime));
119 time_100ns -= 116444736000000000ull;
120 retval = (
f64(time_100ns)) * 1e-7L;
123 clock_gettime(CLOCK_REALTIME, &tp);
124 retval = tp.tv_sec + 1e-9L*tp.tv_nsec;
131 return tt / time_t_ScaleFactor() - time_t_Offset();
141 struct tm tmepoch = Epoch();
143 return f64(difftime(mktime(&tmval), mktime(&tmepoch)));
153 if (min < RC::MAX_VAL<f64>()) {
return min; }
157 for (
u32 tries=0; tries<10;) {
160 f64 diff = t - last_t;
163 min = (min <= diff) ? min : diff;
172 time_t tt = time(NULL);
173 struct tm tmloc =
Get_tm(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; }
187 retval[0] = offset / 3600;
188 retval[1] = offset / 60 - 60*retval[0];
189 retval[2] = offset - 3600*retval[0] - 60*retval[1];
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];
206 static inline struct tm
UTC2Local(struct tm tmval) {
207 struct tm retval = tmval;
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;
225 return time_t((val + time_t_Offset()) * time_t_ScaleFactor());
229 return mktime(&tmval);
239 if (mktime(&tmval) == -1) {
246 static inline struct tm
Get_tm() {
247 return Get_tm(time(NULL));
250 static inline struct tm
Get_tm(time_t tt) {
255 tmptmval = localtime(&tt);
256 if (tmptmval != NULL) {
260 localtime_r(&tt, &tmval);
267 static inline struct tm
Get_tm(
f64 val) {
277 static inline struct tm
Get_tm_UTC(time_t tt) {
282 tmptmval = gmtime(&tt);
283 if (tmptmval != NULL) {
287 gmtime_r(&tt, &tmval);
300 static inline struct tm
Get_tm(struct tm tmval) {
316 ascval = asctime(&tmval);
319 asctime_r(&tmval, ascval);
322 RStr retval = ascval;
343 if (strftime(ascval, 2048, format.
c_str(), &tmval) == 0) {
359 return GetStr(
"%Y-%m-%d");
363 return GetStr(
"%Y-%m-%d", tmval);
372 return GetStr(
"%H:%M:%S");
376 return GetStr(
"%H:%M:%S", tmval);
386 return GetStr(
"%Y-%m-%d_%H-%M-%S");
391 return GetStr(
"%Y-%m-%d_%H-%M-%S", tmval);
401 static inline struct tm
ParseDate(const
RStr& date,
struct tm tmval) {
404 tmval.tm_sec = tmval.tm_min = tmval.tm_hour = 0;
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);
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);
421 static inline struct tm
ParseDate(const RStr& date) {
432 static inline struct tm
ParseTime(const RStr& date,
struct tm tmval) {
433 Data1D<RStr> parts = date.SplitAny(
"-:");
435 tmval.tm_hour = parts[0].Get_i32(10);
436 tmval.tm_min = parts[1].Get_i32(10);
437 if (parts.size() < 3) {
441 tmval.tm_sec = parts[2].Get_i32(10);
448 static inline struct tm
ParseTime(const RStr& date) {
459 static inline struct tm
ParseDateTime(const RStr& date,
struct tm tmval) {
460 Data1D<RStr> parts = date.SplitAny(
"_ ");
486 req.tv_sec = time_t(seconds);
487 req.tv_nsec = long(1e9*(seconds-
u64(seconds)));
488 nanosleep(&req, NULL);
518 : sec(sec + 60*min + 3600*hr) {
524 sec = tmval.tm_sec + 60*tmval.tm_min + 3600*tmval.tm_hour;
531 if (sec < other.sec) {
561 #define TIMEOFDAY_COMP(OP) \
563 inline bool operator OP \
564 (const TimeOfDay& other) const {\
565 return sec OP other.sec;\
577 u32 thissec = ((*this) - left).sec;
578 u32 rightsec = (right - left).sec;
579 return (thissec < rightsec);
583 inline u32 Hr()
const {
return sec / 3600; }
585 inline u32 Min()
const {
return (sec / 60) % 60; }
587 inline u32 Sec()
const {
return sec % 60; }
601 (AmPm ? (
Hr() < 12 ?
"am" :
"pm") :
"");
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