PatchworkOS
Loading...
Searching...
No Matches
localtime_r.c
Go to the documentation of this file.
1#include <stddef.h>
2#include <time.h>
3
4#include "common/time_utils.h"
5
6struct tm* localtime_r(const time_t* timer, struct tm* buf)
7{
8 if (!timer)
9 {
10 return NULL;
11 }
12
14
15 time_t seconds = *timer + timeZone->secondsOffset;
16 int64_t daysElapsed = seconds / 86400;
17 int32_t secondsOfDay = seconds % 86400;
18
19 int32_t year = 1970;
20 while (1)
21 {
22 int32_t daysInYear = 365 + (_time_is_leap_year(year) ? 1 : 0);
23 if (daysElapsed < daysInYear)
24 {
25 break;
26 }
27 daysElapsed -= daysInYear;
28 year++;
29 }
30
31 buf->tm_year = year - 1900;
32 buf->tm_yday = daysElapsed;
33
34 int32_t month = 0;
35 while (daysElapsed >= _time_days_in_month(month, year))
36 {
37 daysElapsed -= _time_days_in_month(month, year);
38 month++;
39 }
40
41 buf->tm_mon = month;
42 buf->tm_mday = daysElapsed + 1;
43
44 buf->tm_hour = secondsOfDay / 3600;
45 buf->tm_min = (secondsOfDay % 3600) / 60;
46 buf->tm_sec = secondsOfDay % 60;
47
48 buf->tm_wday = (daysElapsed + 4) % 7;
49 buf->tm_isdst = 0;
50
51 return buf;
52}
#define NULL
Pointer error value.
Definition NULL.h:23
struct tm * localtime_r(const time_t *timer, struct tm *buf)
Definition localtime_r.c:6
__INT32_TYPE__ int32_t
Definition stdint.h:14
__INT64_TYPE__ int64_t
Definition stdint.h:16
int64_t secondsOffset
Definition time_utils.h:10
Definition time.h:21
int tm_mon
Definition time.h:26
int tm_year
Definition time.h:27
int tm_hour
Definition time.h:24
int tm_sec
Definition time.h:22
int tm_isdst
Definition time.h:30
int tm_yday
Definition time.h:29
int tm_mday
Definition time.h:25
int tm_min
Definition time.h:23
int tm_wday
Definition time.h:28
long long unsigned time_t
Definition time_t.h:4
bool _time_is_leap_year(int32_t year)
Definition time_utils.c:24
_time_zone_t * _time_zone(void)
Definition time_utils.c:13
int32_t _time_days_in_month(int32_t month, int32_t year)
Definition time_utils.c:29
_time_zone_t timeZone
Definition time_utils.c:3