CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <time.h>
7#include "cx/cx.h"
8
11
13#define timeForever 9223372036854775807LL
14
18#define timeS(s) timeFromSeconds(s)
19
23#define timeMS(ms) timeFromMsec(ms)
24
26
32
43
45extern strref timeDayName[];
46
48extern strref timeDayAbbrev[];
49
51extern strref timeMonthName[];
52
54extern strref timeMonthAbbrev[];
55
57typedef struct TimeParts {
58 uint32 usec;
59 int32 year;
60 uint8 month;
61 uint8 day;
62 uint8 hour;
63 uint8 minute;
64 uint8 second;
65
66 // Information-only fields, not used for composition
67 uint8 wday;
68 uint16 yday;
70
79bool timeDecompose(_Out_ TimeParts* out, _In_range_(0, timeForever) int64 time);
80
88_Ret_range_(0, timeForever) int64 timeCompose(_In_ TimeParts* parts);
89
98_Success_(return > 0) int64 timeLocal(int64 time, _Out_opt_ int64* offset);
99
101// end of time_manip group
102
108
112_meta_inline int64 timeToSeconds(int64 time)
113{
114 return time / 1000000;
115}
116
120_meta_inline int64 timeFromSeconds(int64 seconds)
121{
122 return seconds * 1000000;
123}
124
128_meta_inline int64 timeToMsec(int64 time)
129{
130 return time / 1000;
131}
132
136_meta_inline int64 timeFromMsec(int64 msec)
137{
138 return msec * 1000;
139}
140
148_Ret_range_(0, timeForever) _meta_inline int64 timeFromRelTimespec(_In_ struct timespec* ts)
149{
150 int64 ret = (int64)ts->tv_sec * 1000000;
151 ret += ts->tv_nsec / 1000;
152 return ret;
153}
154
162_meta_inline void timeToRelTimespec(_Out_ struct timespec* ts, int64 time)
163{
164 ts->tv_sec = time / 1000000;
165 ts->tv_nsec = (time % 1000000) * 1000;
166}
167
178_Ret_range_(0, timeForever) _meta_inline int64 timeFromAbsTimespec(_In_ struct timespec* ts)
179{
180 int64 ret = (int64)ts->tv_sec * 1000000;
181 ret += ts->tv_nsec / 1000;
182
183 // Unix epoch is midnight on Jan 1, 1970
184 // Which is a julian date of 2440587.50000
185 // That's 210866760000 in seconds, or in microseconds...
186
187 ret += 210866760000000000LL; // adjust epoch
188
189 // This still leaves us over 280,000 years before overflow
190
191 return ret;
192}
193
201_meta_inline void timeToAbsTimespec(_Out_ struct timespec* ts, int64 time)
202{
203 time -= 210866760000000000LL; // adjust epoch
204
205 ts->tv_sec = time / 1000000;
206 ts->tv_nsec = (time % 1000000) * 1000;
207}
208
216_Ret_range_(0, timeForever) _meta_inline int64 timeFromTimeT(time_t tt)
217{
218 int64 ret = (int64)tt * 1000000;
219 ret += 210866760000000000LL; // adjust epoch
220 return ret;
221}
222
230_meta_inline time_t timeToTimeT(int64 time)
231{
232 time -= 210866760000000000LL; // adjust epoch
233 return (time_t)(time / 1000000);
234}
235
237// end of time_convert group
int64 timeToMsec(int64 time)
Definition time.h:128
int64 timeFromAbsTimespec(struct timespec *ts)
Definition time.h:178
int64 timeFromMsec(int64 msec)
Definition time.h:136
int64 timeFromSeconds(int64 seconds)
Definition time.h:120
time_t timeToTimeT(int64 time)
Definition time.h:230
int64 timeFromRelTimespec(struct timespec *ts)
Definition time.h:148
int64 timeFromTimeT(time_t tt)
Definition time.h:216
void timeToAbsTimespec(struct timespec *ts, int64 time)
Definition time.h:201
int64 timeToSeconds(int64 time)
Definition time.h:112
void timeToRelTimespec(struct timespec *ts, int64 time)
Definition time.h:162
strref timeMonthName[]
Full names of months (indexed 1-12, index 0 is empty string)
WEEKDAYS
Days of the week (0-6)
Definition time.h:34
int64 timeLocal(int64 time, int64 *offset)
bool timeDecompose(TimeParts *out, _In_range_(0, timeForever) int64 time)
strref timeDayAbbrev[]
Three-letter abbreviations of days of the week (indexed by WEEKDAYS enum)
strref timeMonthAbbrev[]
Three-letter abbreviations of months (indexed 1-12, index 0 is empty string)
strref timeDayName[]
Full names of days of the week (indexed by WEEKDAYS enum)
int64 timeCompose(TimeParts *parts)
@ Wednesday
Wednesday.
Definition time.h:38
@ Monday
Monday.
Definition time.h:36
@ Sunday
Sunday.
Definition time.h:35
@ Tuesday
Tuesday.
Definition time.h:37
@ Saturday
Saturday.
Definition time.h:41
@ Thursday
Thursday.
Definition time.h:39
@ Friday
Friday.
Definition time.h:40
#define timeForever
Maximum representable time value (approximately year 294,276 CE)
Definition time.h:13
Decomposed time structure with individual date/time components.
Definition time.h:57
uint8 day
Day of month (1-31)
Definition time.h:61
uint8 month
Month (1-12)
Definition time.h:60
uint16 yday
Day of year (0-365)
Definition time.h:68
uint8 minute
Minute (0-59)
Definition time.h:63
uint8 second
Second (0-59)
Definition time.h:64
int32 year
Year (negative for BCE, no year 0)
Definition time.h:59
uint8 wday
Day of week (0-6, Sunday=0)
Definition time.h:67
uint32 usec
Microseconds (0-999999)
Definition time.h:58
uint8 hour
Hour (0-23)
Definition time.h:62
Time manipulation and conversion functions.