From 49ca4b3e5e8876bf34d86eb0970385848d4a167e Mon Sep 17 00:00:00 2001 From: James Buren Date: Wed, 7 Jul 2021 17:26:24 +0000 Subject: timefuncs: add dostime_localtime function This does the opposite of dostime_mktime, converting time_t back to the two dos date time values. We use gmtime_r for native because that is what is available and acts the same as localtime_r on other platforms with a regular libc available. Change-Id: If79469d0aae2d7c5dcdd905fbf04963669aa1138 --- firmware/common/timefuncs.c | 17 +++++++++++++++++ firmware/include/timefuncs.h | 1 + 2 files changed, 18 insertions(+) diff --git a/firmware/common/timefuncs.c b/firmware/common/timefuncs.c index d87e6b67f2..66854dea13 100644 --- a/firmware/common/timefuncs.c +++ b/firmware/common/timefuncs.c @@ -46,6 +46,23 @@ time_t dostime_mktime(uint16_t dosdate, uint16_t dostime) return mktime(&tm); } +void dostime_localtime(time_t time, uint16_t* dosdate, uint16_t* dostime) +{ + struct tm tm; +#if (CONFIG_PLATFORM & PLATFORM_NATIVE) + gmtime_r(&time, &tm); +#else + localtime_r(&time, &tm); +#endif + + *dostime = ((tm.tm_sec / 2) << 0)| + ((tm.tm_min ) << 5)| + ((tm.tm_hour ) << 11); + *dosdate = ((tm.tm_mday ) << 0)| + ((tm.tm_mon + 1) << 5)| + ((tm.tm_year - 80) << 9); +} + #if !CONFIG_RTC static inline bool rtc_dirty(void) { diff --git a/firmware/include/timefuncs.h b/firmware/include/timefuncs.h index 2e8ef01ca6..25e041b576 100644 --- a/firmware/include/timefuncs.h +++ b/firmware/include/timefuncs.h @@ -28,6 +28,7 @@ #include "time.h" time_t dostime_mktime(uint16_t dosdate, uint16_t dostime); +void dostime_localtime(time_t time, uint16_t* dosdate, uint16_t* dostime); struct tm *get_time(void); int set_time(const struct tm *tm); #if CONFIG_RTC -- cgit v1.2.3