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
This commit is contained in:
James Buren 2021-07-07 17:26:24 +00:00
parent bce6771730
commit 49ca4b3e5e
2 changed files with 18 additions and 0 deletions

View file

@ -46,6 +46,23 @@ time_t dostime_mktime(uint16_t dosdate, uint16_t dostime)
return mktime(&tm); 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 #if !CONFIG_RTC
static inline bool rtc_dirty(void) static inline bool rtc_dirty(void)
{ {

View file

@ -28,6 +28,7 @@
#include "time.h" #include "time.h"
time_t dostime_mktime(uint16_t dosdate, uint16_t dostime); 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); struct tm *get_time(void);
int set_time(const struct tm *tm); int set_time(const struct tm *tm);
#if CONFIG_RTC #if CONFIG_RTC