File Coverage

tstr_datetime.h
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine n/a
pod n/a
total 13 13 100.0


line stmt bran cond sub pod time code
1             #ifndef TSTR_DATETIME_H
2             #define TSTR_DATETIME_H
3              
4             #include
5             #include "tstr_calendar.h"
6              
7             typedef struct {
8             int32_t year; // 1-9999
9             int32_t month; // 1-12
10             int32_t day; // 1-31
11             int32_t hour; // 0-23
12             int32_t minute; // 0-59
13             int32_t second; // 0-60 (60 for leap second)
14             int32_t nanosecond; // 0-999999999
15             int32_t offset; // UTC offset in minutes, -1439..1439
16             uint32_t rdn; // Rata Die Number (local date)
17             } tstr_datetime_t;
18              
19             #define TSTR_SECS_PER_DAY 86400
20             #define TSTR_SECS_PER_HOUR 3600
21             #define TSTR_SECS_PER_MIN 60
22              
23 122           static inline void tstr_datetime_from_epoch(tstr_datetime_t* dt,
24             int64_t epoch,
25             int32_t offset,
26             int32_t nanosecond) {
27             uint64_t local, days, sod;
28              
29 122           local = (uint64_t)(epoch + (int64_t)offset * TSTR_SECS_PER_MIN
30 122           + (int64_t)TSTR_CALENDAR_RDN_UNIX_EPOCH * TSTR_SECS_PER_DAY);
31              
32 122           days = local / TSTR_SECS_PER_DAY;
33 122           sod = local % TSTR_SECS_PER_DAY;
34              
35 122           dt->rdn = (uint32_t)days;
36 122           tstr_calendar_rdn_to_ymd(dt->rdn, &dt->year, &dt->month, &dt->day);
37              
38 122           dt->hour = (int32_t)(sod / TSTR_SECS_PER_HOUR);
39 122           dt->minute = (int32_t)((sod % TSTR_SECS_PER_HOUR) / TSTR_SECS_PER_MIN);
40 122           dt->second = (int32_t)(sod % TSTR_SECS_PER_MIN);
41 122           dt->nanosecond = nanosecond;
42 122           dt->offset = offset;
43 122           }
44              
45             #endif /* TSTR_DATETIME_H */