From b363e1cae190e2f64f28694d5cc63c774c370a36 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 15 Aug 2020 10:40:11 -0400 Subject: add call to tzset() --- src/time/ctime.c | 4 ++++ src/time/localtime.c | 4 ++++ src/time/mktime.c | 4 ++++ src/time/strftime.c | 14 +++++++++++++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/time/ctime.c b/src/time/ctime.c index 5619e316..fd40655f 100644 --- a/src/time/ctime.c +++ b/src/time/ctime.c @@ -4,6 +4,10 @@ char * ctime(const time_t * timer) { + #ifdef _POSIX_SOURCE + tzset(); + #endif + return asctime(localtime(timer)); } diff --git a/src/time/localtime.c b/src/time/localtime.c index 72040d00..ddfecab5 100644 --- a/src/time/localtime.c +++ b/src/time/localtime.c @@ -8,6 +8,10 @@ struct tm * localtime(const time_t * timer) static struct tm tm = {0}; ASSERT_NONNULL(timer); + #ifdef _POSIX_SOURCE + tzset(); + #endif + tm = *gmtime(timer); /* TODO: adjust for timezone */ diff --git a/src/time/mktime.c b/src/time/mktime.c index d19ad8d6..3b415b4c 100644 --- a/src/time/mktime.c +++ b/src/time/mktime.c @@ -10,6 +10,10 @@ time_t mktime(struct tm * timeptr) ASSERT_NONNULL(timeptr); + #ifdef _POSIX_SOURCE + tzset(); + #endif + while (timeptr->tm_sec < 0) { timeptr->tm_min--; timeptr->tm_sec += SEC_PER_MIN; diff --git a/src/time/strftime.c b/src/time/strftime.c index 4d3f78c8..c229e26d 100644 --- a/src/time/strftime.c +++ b/src/time/strftime.c @@ -16,6 +16,10 @@ size_t strftime(char * restrict s, size_t maxsize, const char * restrict format, ASSERT_NONNULL(format); ASSERT_NONNULL(timeptr); + #ifdef _POSIX_SOURCE + tzset(); + #endif + for (i = 0; format[i] != '\0' && converted < maxsize; i++) { if (format[i] != '%') { s[converted++] = format[i]; @@ -48,6 +52,10 @@ size_t strftime(char * restrict s, size_t maxsize, const char * restrict format, sprintf(buf, "%02d", timeptr->tm_mday); break; + case 'e': + sprintf(buf, "%2d", timeptr->tm_mday); + break; + case 'H': sprintf(buf, "%02d", timeptr->tm_hour); break; @@ -68,6 +76,10 @@ size_t strftime(char * restrict s, size_t maxsize, const char * restrict format, sprintf(buf, "%02d", timeptr->tm_min); break; + case 'n': + sprintf(buf, "\n"); + break; + case 'p': sprintf(buf, "%s", lc->lc_time.am_pm[timeptr->tm_hour < 12 ? 0 : 1]); break; @@ -117,8 +129,8 @@ size_t strftime(char * restrict s, size_t maxsize, const char * restrict format, break; default: - sprintf(buf, "UNDEFINED"); /* undefined behavior */ + sprintf(buf, "-strftime(%%%c) is undefined-", format[i]); break; } -- cgit v1.2.1