1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <rtc.h>
15#include <linux/math64.h>
16
17static const unsigned char rtc_days_in_month[] = {
18 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
19};
20
21#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
22
23
24
25
26int rtc_month_days(unsigned int month, unsigned int year)
27{
28 return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
29}
30
31
32
33
34
35
36
37
38void rtc_to_tm(u64 time, struct rtc_time *tm)
39{
40 unsigned int month, year, secs;
41 int days;
42
43 days = div_u64_rem(time, 86400, &secs);
44
45
46 tm->tm_wday = (days + 4) % 7;
47
48 year = 1970 + days / 365;
49 days -= (year - 1970) * 365
50 + LEAPS_THRU_END_OF(year - 1)
51 - LEAPS_THRU_END_OF(1970 - 1);
52 while (days < 0) {
53 year -= 1;
54 days += 365 + is_leap_year(year);
55 }
56 tm->tm_year = year;
57 tm->tm_yday = days + 1;
58
59 for (month = 0; month < 11; month++) {
60 int newdays;
61
62 newdays = days - rtc_month_days(month, year);
63 if (newdays < 0)
64 break;
65 days = newdays;
66 }
67 tm->tm_mon = month + 1;
68 tm->tm_mday = days + 1;
69
70 tm->tm_hour = secs / 3600;
71 secs -= tm->tm_hour * 3600;
72 tm->tm_min = secs / 60;
73 tm->tm_sec = secs - tm->tm_min * 60;
74
75
76 tm->tm_isdst = 0;
77}
78