72 assert_int_not_equal(ts1.
sec, 0);
75 struct timespec waittime = {0, 300 * 1000 * 1000L};
76 while(nanosleep(&waittime, NULL) != 0) {
83 int64_t diff_us = ts2.
nsec - ts1.
nsec;
84 int64_t diff = (ts2.
sec - ts1.
sec) * 1000 * 1000 * 1000L + diff_us;
85 assert_true(diff > 300 * 1000 * 1000L);
90 const char* formats[] = {
91 "1970-07-28T15:45:00Z",
92 "0001-01-01T00:00:00Z",
93 "2020-02-29T16:16:16+02:00",
94 "2020-02-29T16:16:16.123+02:00",
95 "2020-02-29T16:16:16-01:15",
96 "2020-02-29T16:16:16.123456-01:15",
97 "2000-02-28T23:14:00Z",
98 "1970-01-01T00:00:00Z",
99 "1970-01-01t00:00:00Z",
100 "1970-01-01 00:00:00Z",
101 "1970-01-01T00:00:00z",
105 const int64_t timestamps[] = {
119 for(
int i = 0; formats[i] != NULL; i++) {
120 assert_int_equal(
amxc_ts_parse(&ts, formats[i], strlen(formats[i])), 0);
121 assert_int_equal(ts.
sec, timestamps[i]);
127 const char* formats[] = {
128 "1970-14-28T15:45:00Z",
129 "0001-01-32T00:00:00Z",
130 "2020-02-30T16:16:16+02:00",
131 "2000-02-31T16:16:16+02:00",
132 "0000-02-29T16:16:16.123+02:00",
133 "2019-02-29T16:16:16.123+02:00",
134 "1984-04-31T00:00:00Z",
135 "1984-04-30T26:00:00Z",
136 "1984-04-30T13:70:00Z",
137 "1984-04-30T13:10:70Z",
138 "1984-04-30T13:12:11.9999999999Z",
139 "1984-04-30T13:12:11.999999999+24:00",
140 "1984-0A-30T13:10:70Z",
141 "1984-A0-30T12:10:70Z",
142 "Q984-01-30T12:10:70Z",
143 "1Q84-01-30T12:10:70Z",
144 "19Q4-01-30T12:10:70Z",
145 "198Q-01-30T12:10:70Z",
146 "184-01-30T10:10:10.9999999-11:00",
147 "1984-01-30Z10:10:20.9999999-11:00",
148 "1984-01-30T10:10:30Q-11:00",
149 "2000.02-28T23:14:00Z",
150 "2000-02/28T23:14:00Z",
151 "2000-02-28T23.14:00Z",
152 "2000-02-28T23:14-00Z",
153 "2000-02-28T23:14:00,123Z",
154 "2000-00-28T23:14:00.123Z",
155 "2000-01-01T24:00:00.000Z",
156 "2000-01-01T23:60:00.000Z",
157 "2000-01-01T23:00:60.000Z",
161 for(
int i = 0; formats[i] != NULL; i++) {
162 printf(
"Checking [%s]\n", formats[i]);
163 assert_int_not_equal(
amxc_ts_parse(&ts, formats[i], strlen(formats[i])), 0);
166 assert_int_not_equal(
amxc_ts_parse(NULL, formats[0], strlen(formats[0])), 0);
167 assert_int_not_equal(
amxc_ts_parse(&ts, NULL, strlen(formats[0])), 0);
174 const char* formats[] = {
175 "1970-07-28T15:45:00Z",
176 "0001-01-01T00:00:00Z",
177 "2020-02-29T16:16:16+02:00",
178 "2020-02-29T16:16:16-01:15",
179 "2000-02-28T23:14:00Z",
180 "1970-01-01T00:00:00Z",
181 "2017-11-11T11:11:11.123+02:00",
182 "2017-11-11T11:11:11.123456Z",
183 "1994-10-01T00:00:00.123456789-02:30",
187 for(
int i = 0; formats[i] != NULL; i++) {
188 printf(
"Checking [%s]\n", formats[i]);
189 assert_int_equal(
amxc_ts_parse(&ts, formats[i], strlen(formats[i])), 0);
190 assert_int_equal(
amxc_ts_format(&ts, str_ts, 40), strlen(formats[i]));
191 assert_string_equal(str_ts, formats[i]);
199 assert_int_equal(
amxc_ts_parse(&ts,
"1970-07-28T15:45:00Z", strlen(
"1970-07-28T15:45:00Z")), 0);
219 const char* formats[] = {
220 "1970-07-28T15:45:00Z",
221 "0001-01-01T00:00:00Z",
222 "2020-02-29T16:16:16+02:00",
223 "2020-02-29T16:16:16-01:15",
224 "2000-02-28T23:14:00Z",
225 "1970-01-01T00:00:00Z",
226 "2017-11-11T11:11:11.123+02:00",
227 "2017-11-11T11:11:11.123456Z",
228 "1994-10-01T00:00:00.123456789-02:30",
232 for(
int i = 0; formats[i] != NULL; i++) {
233 assert_int_equal(
amxc_ts_parse(&ts, formats[i], strlen(formats[i])), 0);
252 ts.
sec = INT64_C(-62135596800) - 1;
254 ts.
sec = INT64_C(253402300799) + 1;
260 ts.
nsec = 1999999999;
278 assert_int_equal(
amxc_ts_parse(&ts_a,
"1970-07-28T15:45:00Z", strlen(
"1970-07-28T15:45:00Z")), 0);
279 assert_int_equal(
amxc_ts_parse(&ts_b,
"1970-08-28T15:45:00Z", strlen(
"1970-08-28T15:45:00Z")), 0);
285 assert_int_equal(
amxc_ts_parse(&ts_a,
"1970-07-28T15:45:00.123Z", strlen(
"1970-07-28T15:45:00Z.123")), 0);
286 assert_int_equal(
amxc_ts_parse(&ts_b,
"1970-07-28T15:45:00.456Z", strlen(
"1970-07-28T15:45:00Z.456")), 0);
300 assert_int_equal(
amxc_ts_parse(&ts,
"2020-06-17T15:35:22+02:00", strlen(
"2020-06-17T15:35:22+02:00")), 0);
302 assert_int_equal(tm.tm_hour, 13);
303 assert_int_equal(tm.tm_min, 35);
304 assert_int_equal(tm.tm_sec, 22);
305 assert_int_equal(tm.tm_mday, 17);
306 assert_int_equal(tm.tm_mon, 5);
307 assert_int_equal(tm.tm_year, 120);
310 assert_int_equal(tm.tm_hour, 15);
311 assert_int_equal(tm.tm_min, 35);
312 assert_int_equal(tm.tm_sec, 22);
313 assert_int_equal(tm.tm_mday, 17);
314 assert_int_equal(tm.tm_mon, 5);
315 assert_int_equal(tm.tm_year, 120);
326 char* current_tz = getenv(
"TZ");
332 timeinfo = localtime(&rawtime);
334 setenv(
"TZ",
"Europe/Brussels",
true);
335 assert_int_equal(
amxc_ts_parse(&ts,
"2020-06-17T15:35:22Z", strlen(
"2020-06-17T15:35:22Z")), 0);
336 assert_int_equal(ts.
offset, 0);
338 if(timeinfo->tm_isdst == 0) {
339 assert_int_equal(ts.
offset, 60);
340 assert_int_equal(
amxc_ts_format(&ts, str_ts, 40), strlen(
"2020-06-17T16:35:22+01:00"));
341 assert_string_equal(str_ts,
"2020-06-17T16:35:22+01:00");
343 assert_int_equal(ts.
offset, 120);
344 assert_int_equal(
amxc_ts_format(&ts, str_ts, 40), strlen(
"2020-06-17T17:35:22+02:00"));
345 assert_string_equal(str_ts,
"2020-06-17T17:35:22+02:00");
350 if(current_tz != NULL) {
351 setenv(
"TZ", current_tz,
true);
360 struct tm* timeinfo = NULL;
363 timeinfo = localtime(&rawtime);
366 assert_int_equal(rawtime, ts.
sec);
Ambiorix timestamp header file.
size_t amxc_ts_format(const amxc_ts_t *tsp, char *dst, size_t len)
Transforms unix epoch time to a string.
int amxc_ts_compare(const amxc_ts_t *tsp1, const amxc_ts_t *tsp2)
Checks if tsp1 comes after tsp2.
int amxc_ts_from_tm(amxc_ts_t *const tsp, struct tm *tmp)
Converts a broken down time in a struct tm to a timestamp structure.
int amxc_ts_to_local(amxc_ts_t *tsp)
Adds the local time offset to the timestamp structure.
int amxc_ts_parse(amxc_ts_t *tsp, const char *str, size_t len)
Transforms the given string in to unix epoch time.
bool amxc_ts_is_valid(const amxc_ts_t *tsp)
Checks if a timestamp is valid.
size_t amxc_ts_format_precision(const amxc_ts_t *tsp, char *dst, size_t len, int precision)
Transforms unix epoch time to a string.
int amxc_ts_to_tm_utc(const amxc_ts_t *tsp, struct tm *tmp)
Converts timestamp in unix epoch time to a struct tm type in UTC time.
int amxc_ts_now(amxc_ts_t *tsp)
Takes current time as unix epoch time.
int amxc_ts_to_tm_local(const amxc_ts_t *tsp, struct tm *tmp)
Converts timestamp in unix epoch time to a struct tm type in local time.
The timestamp structure (unix epoch time).
void test_amxc_ts_compare(UNUSED void **state)
void test_amxc_ts_format_invalid(UNUSED void **state)
void test_amxc_ts_from_tm(UNUSED void **state)
void test_amxc_ts_format_valid(UNUSED void **state)
void test_amxc_ts_parse_valid(UNUSED void **state)
void test_amxc_ts_now(UNUSED void **state)
void test_amxc_ts_to_tm(UNUSED void **state)
void test_amxc_ts_parse_invalid(UNUSED void **state)
void test_amxc_ts_to_local(UNUSED void **state)
void test_amxc_ts_is_valid(UNUSED void **state)
void test_amxc_ts_format_precision_valid(UNUSED void **state)