| 1 | #include "git-compat-util.h" |
| 2 | #include "tr2_tbuf.h" |
| 3 | |
| 4 | void tr2_tbuf_local_time(struct tr2_tbuf *tb) |
| 5 | { |
| 6 | struct timeval tv = { 0 }; |
| 7 | struct tm tm = { 0 }; |
| 8 | time_t secs; |
| 9 | int len; |
| 10 | |
| 11 | gettimeofday(&tv, NULL); |
| 12 | secs = tv.tv_sec; |
| 13 | localtime_r(&secs, &tm); |
| 14 | |
| 15 | len = snprintf(tb->buf, sizeof(tb->buf), "%02d:%02d:%02d.%06ld", |
| 16 | tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec); |
| 17 | |
| 18 | if (len < 0 || (size_t)len >= sizeof(tb->buf)) { |
| 19 | const char *blank = "00:00:00.000000"; |
| 20 | strlcpy(tb->buf, blank, sizeof(tb->buf)); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | void tr2_tbuf_utc_datetime_extended(struct tr2_tbuf *tb) |
| 25 | { |
| 26 | struct timeval tv = { 0 }; |
| 27 | struct tm tm = { 0 }; |
| 28 | time_t secs; |
| 29 | int len; |
| 30 | |
| 31 | gettimeofday(&tv, NULL); |
| 32 | secs = tv.tv_sec; |
| 33 | gmtime_r(&secs, &tm); |
| 34 | |
| 35 | len = snprintf(tb->buf, sizeof(tb->buf), |
| 36 | "%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ", |
| 37 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 38 | tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec); |
| 39 | |
| 40 | if (len < 0 || (size_t)len >= sizeof(tb->buf)) { |
| 41 | const char *blank = "1900-00-00T00:00:00.000000Z"; |
| 42 | strlcpy(tb->buf, blank, sizeof(tb->buf)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void tr2_tbuf_utc_datetime(struct tr2_tbuf *tb) |
| 47 | { |
| 48 | struct timeval tv = { 0 }; |
| 49 | struct tm tm = { 0 }; |
| 50 | time_t secs; |
| 51 | int len; |
| 52 | |
| 53 | gettimeofday(&tv, NULL); |
| 54 | secs = tv.tv_sec; |
| 55 | gmtime_r(&secs, &tm); |
| 56 | |
| 57 | len = snprintf(tb->buf, sizeof(tb->buf), |
| 58 | "%4d%02d%02dT%02d%02d%02d.%06ldZ", |
| 59 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 60 | tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec); |
| 61 | |
| 62 | if (len < 0 || (size_t)len >= sizeof(tb->buf)) { |
| 63 | const char *blank = "19000000T000000.000000Z"; |
| 64 | strlcpy(tb->buf, blank, sizeof(tb->buf)); |
| 65 | } |
| 66 | } |