summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorzylx <[email protected]>2018-10-24 11:11:17 +0800
committerzylx <[email protected]>2018-10-24 11:11:43 +0800
commitab5f1d49ce03e520427c7909eda16fdf5ff66ed2 (patch)
tree6d2c6f005533eef9b22692e0a3b82ca8bd1a3680 /src
parentfbaa86933dd5aeb65b36db78a210e386fb8a6732 (diff)
[kernel][kservice] add rt_gmtime_r api
Diffstat (limited to 'src')
-rw-r--r--src/kservice.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/kservice.c b/src/kservice.c
index dbc4a2fb0e..0a60fa0142 100644
--- a/src/kservice.c
+++ b/src/kservice.c
@@ -1177,6 +1177,61 @@ void rt_kprintf(const char *fmt, ...)
RTM_EXPORT(rt_kprintf);
#endif
+struct tm *rt_gmtime_r(const time_t *timep, struct tm *result)
+{
+#define IS_LEAP_YEAR(year) (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
+
+ const rt_uint32_t mon_table[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+ rt_uint32_t year = 1970, month = 0;
+ rt_uint32_t daycount = 0, second = 0, number = 0;
+
+ second = *timep % (24 * 60 * 60);
+ result->tm_hour = second / 3600;
+ result->tm_min = (second % 3600) / 60;
+ result->tm_sec = (second % 3600) % 60;
+
+ daycount = *timep / (24 * 60 * 60);
+ result->tm_wday = (daycount + 4) % 7;
+ if (daycount != 0)
+ {
+ while (daycount >= 365)
+ {
+ number = IS_LEAP_YEAR(year) ? 366 : 365;
+ if (daycount >= number)
+ {
+ daycount -= number;
+ year++;
+ }
+ else
+ break;
+ }
+ result->tm_year = year - 1900;
+
+ while (daycount >= 28)
+ {
+ if (month == 1 && IS_LEAP_YEAR(year))
+ {
+ if (daycount >= 29)
+ daycount -= 29;
+ else
+ break;
+ }
+ else
+ {
+ if (daycount >= mon_table[month])
+ daycount -= mon_table[month];
+ else
+ break;
+ }
+ month++;
+ }
+ result->tm_mon = month + 1;
+ result->tm_mday = daycount + 1;
+ }
+ return result;
+}
+RTM_EXPORT(rt_gmtime_r);
+
#ifdef RT_USING_HEAP
/**
* This function allocates a memory block, which address is aligned to the