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
9CX_C_BEGIN
10
13
15#define timeForever 9223372036854775807LL
16
20#define timeS(s) timeFromSeconds(s)
21
25#define timeMS(ms) timeFromMsec(ms)
26
28
34
45
47extern strref timeDayName[];
48
50extern strref timeDayAbbrev[];
51
53extern strref timeMonthName[];
54
56extern strref timeMonthAbbrev[];
57
59typedef struct TimeParts {
60 uint32 usec;
61 int32 year;
62 uint8 month;
63 uint8 day;
64 uint8 hour;
65 uint8 minute;
66 uint8 second;
67
68 // Information-only fields, not used for composition
69 uint8 wday;
70 uint16 yday;
72
81bool timeDecompose(_Out_ TimeParts* out, _In_range_(0, timeForever) int64 time);
82
90_Ret_range_(0, timeForever) int64 timeCompose(_In_ const TimeParts* parts);
91
100_Success_(return > 0) int64 timeLocal(int64 time, _Out_opt_ int64* offset);
101
103// end of time_manip group
104
110
114_meta_inline int64 timeToSeconds(int64 time)
115{
116 return time / 1000000;
117}
118
122_meta_inline int64 timeFromSeconds(int64 seconds)
123{
124 return seconds * 1000000;
125}
126
130_meta_inline int64 timeToMsec(int64 time)
131{
132 return time / 1000;
133}
134
138_meta_inline int64 timeFromMsec(int64 msec)
139{
140 return msec * 1000;
141}
142
150_Ret_range_(0, timeForever) _meta_inline int64 timeFromRelTimespec(_In_ const struct timespec* ts)
151{
152 int64 ret = (int64)ts->tv_sec * 1000000;
153 ret += ts->tv_nsec / 1000;
154 return ret;
155}
156
164_meta_inline void timeToRelTimespec(_Out_ struct timespec* ts, int64 time)
165{
166 ts->tv_sec = time / 1000000;
167 ts->tv_nsec = (time % 1000000) * 1000;
168}
169
180_Ret_range_(0, timeForever) _meta_inline int64 timeFromAbsTimespec(_In_ const struct timespec* ts)
181{
182 int64 ret = (int64)ts->tv_sec * 1000000;
183 ret += ts->tv_nsec / 1000;
184
185 // Unix epoch is midnight on Jan 1, 1970
186 // Which is a julian date of 2440587.50000
187 // That's 210866760000 in seconds, or in microseconds...
188
189 ret += 210866760000000000LL; // adjust epoch
190
191 // This still leaves us over 280,000 years before overflow
192
193 return ret;
194}
195
203_meta_inline void timeToAbsTimespec(_Out_ struct timespec* ts, int64 time)
204{
205 time -= 210866760000000000LL; // adjust epoch
206
207 ts->tv_sec = time / 1000000;
208 ts->tv_nsec = (time % 1000000) * 1000;
209}
210
218_Ret_range_(0, timeForever) _meta_inline int64 timeFromTimeT(time_t tt)
219{
220 int64 ret = (int64)tt * 1000000;
221 ret += 210866760000000000LL; // adjust epoch
222 return ret;
223}
224
232_meta_inline time_t timeToTimeT(int64 time)
233{
234 time -= 210866760000000000LL; // adjust epoch
235 return (time_t)(time / 1000000);
236}
237
239// end of time_convert group
240
241CX_C_END
int64 timeToMsec(int64 time)
Definition time.h:130
int64 timeFromMsec(int64 msec)
Definition time.h:138
int64 timeFromAbsTimespec(const struct timespec *ts)
Definition time.h:180
int64 timeFromRelTimespec(const struct timespec *ts)
Definition time.h:150
int64 timeFromSeconds(int64 seconds)
Definition time.h:122
time_t timeToTimeT(int64 time)
Definition time.h:232
int64 timeFromTimeT(time_t tt)
Definition time.h:218
void timeToAbsTimespec(struct timespec *ts, int64 time)
Definition time.h:203
int64 timeToSeconds(int64 time)
Definition time.h:114
void timeToRelTimespec(struct timespec *ts, int64 time)
Definition time.h:164
int64 timeCompose(const TimeParts *parts)
strref timeMonthName[]
Full names of months (indexed 1-12, index 0 is empty string)
WEEKDAYS
Days of the week (0-6)
Definition time.h:36
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)
@ Wednesday
Wednesday.
Definition time.h:40
@ Monday
Monday.
Definition time.h:38
@ Sunday
Sunday.
Definition time.h:37
@ Tuesday
Tuesday.
Definition time.h:39
@ Saturday
Saturday.
Definition time.h:43
@ Thursday
Thursday.
Definition time.h:41
@ Friday
Friday.
Definition time.h:42
#define timeForever
Maximum representable time value (approximately year 294,276 CE)
Definition time.h:15
Decomposed time structure with individual date/time components.
Definition time.h:59
uint8 day
Day of month (1-31)
Definition time.h:63
uint8 month
Month (1-12)
Definition time.h:62
uint16 yday
Day of year (0-365)
Definition time.h:70
uint8 minute
Minute (0-59)
Definition time.h:65
uint8 second
Second (0-59)
Definition time.h:66
int32 year
Year (negative for BCE, no year 0)
Definition time.h:61
uint8 wday
Day of week (0-6, Sunday=0)
Definition time.h:69
uint32 usec
Microseconds (0-999999)
Definition time.h:60
uint8 hour
Hour (0-23)
Definition time.h:64
Time manipulation and conversion functions.