/src/barebox/common/date.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * (C) Copyright 2001 |
4 | | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
5 | | */ |
6 | | |
7 | | /** |
8 | | * @file |
9 | | * @brief Date & Time support |
10 | | */ |
11 | | |
12 | | #include <common.h> |
13 | | #include <command.h> |
14 | | #include <rtc.h> |
15 | | |
16 | | #define FEBRUARY 2 |
17 | 0 | #define STARTOFTIME 1970 |
18 | 0 | #define SECDAY 86400L |
19 | | #define SECYR (SECDAY * 365) |
20 | 0 | #define leapyear(year) ((year) % 4 == 0) |
21 | 0 | #define days_in_year(a) (leapyear(a) ? 366 : 365) |
22 | 0 | #define days_in_month(a) (month_days[(a) - 1]) |
23 | | |
24 | | static int month_days[12] = { |
25 | | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
26 | | }; |
27 | | |
28 | | /* |
29 | | * This only works for the Gregorian calendar - i.e. after 1752 (in the UK) |
30 | | */ |
31 | | void GregorianDay(struct rtc_time * tm) |
32 | 0 | { |
33 | 0 | int leapsToDate; |
34 | 0 | int lastYear; |
35 | 0 | int day; |
36 | 0 | int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 }; |
37 | |
|
38 | 0 | lastYear=tm->tm_year-1; |
39 | | |
40 | | /* |
41 | | * Number of leap corrections to apply up to end of last year |
42 | | */ |
43 | 0 | leapsToDate = lastYear/4 - lastYear/100 + lastYear/400; |
44 | | |
45 | | /* |
46 | | * This year is a leap year if it is divisible by 4 except when it is |
47 | | * divisible by 100 unless it is divisible by 400 |
48 | | * |
49 | | * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be |
50 | | */ |
51 | 0 | if((tm->tm_year%4==0) && |
52 | 0 | ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) && |
53 | 0 | (tm->tm_mon>2)) { |
54 | | /* |
55 | | * We are past Feb. 29 in a leap year |
56 | | */ |
57 | 0 | day=1; |
58 | 0 | } else { |
59 | 0 | day=0; |
60 | 0 | } |
61 | |
|
62 | 0 | day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday; |
63 | |
|
64 | 0 | tm->tm_wday=day%7; |
65 | 0 | } |
66 | | |
67 | | void to_tm(int tim, struct rtc_time * tm) |
68 | 0 | { |
69 | 0 | register int i; |
70 | 0 | register long hms, day; |
71 | |
|
72 | 0 | day = tim / SECDAY; |
73 | 0 | hms = tim % SECDAY; |
74 | | |
75 | | /* Hours, minutes, seconds are easy */ |
76 | 0 | tm->tm_hour = hms / 3600; |
77 | 0 | tm->tm_min = (hms % 3600) / 60; |
78 | 0 | tm->tm_sec = (hms % 3600) % 60; |
79 | | |
80 | | /* Number of years in days */ |
81 | 0 | for (i = STARTOFTIME; day >= days_in_year(i); i++) { |
82 | 0 | day -= days_in_year(i); |
83 | 0 | } |
84 | 0 | tm->tm_year = i; |
85 | | |
86 | | /* Number of months in days left */ |
87 | 0 | if (leapyear(tm->tm_year)) { |
88 | 0 | days_in_month(FEBRUARY) = 29; |
89 | 0 | } |
90 | 0 | for (i = 1; day >= days_in_month(i); i++) { |
91 | 0 | day -= days_in_month(i); |
92 | 0 | } |
93 | 0 | days_in_month(FEBRUARY) = 28; |
94 | 0 | tm->tm_mon = i; |
95 | | |
96 | | /* Days are what is left over (+1) from all that. */ |
97 | 0 | tm->tm_mday = day + 1; |
98 | | |
99 | | /* |
100 | | * Determine the day of week |
101 | | */ |
102 | 0 | GregorianDay(tm); |
103 | 0 | } |
104 | | EXPORT_SYMBOL(to_tm); |
105 | | |
106 | | /* Converts Gregorian date to seconds since 1970-01-01 00:00:00. |
107 | | * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 |
108 | | * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. |
109 | | * |
110 | | * [For the Julian calendar (which was used in Russia before 1917, |
111 | | * Britain & colonies before 1752, anywhere else before 1582, |
112 | | * and is still in use by some communities) leave out the |
113 | | * -year/100+year/400 terms, and add 10.] |
114 | | * |
115 | | * This algorithm was first published by Gauss (I think). |
116 | | * |
117 | | * WARNING: this function will overflow on 2106-02-07 06:28:16 on |
118 | | * machines were long is 32-bit! (However, as time_t is signed, we |
119 | | * will already get problems at other places on 2038-01-19 03:14:08) |
120 | | */ |
121 | | unsigned long |
122 | | mktime (unsigned int year, unsigned int mon, |
123 | | unsigned int day, unsigned int hour, |
124 | | unsigned int min, unsigned int sec) |
125 | 0 | { |
126 | 0 | if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */ |
127 | 0 | mon += 12; /* Puts Feb last since it has leap day */ |
128 | 0 | year -= 1; |
129 | 0 | } |
130 | |
|
131 | 0 | return ((( |
132 | 0 | (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) + |
133 | 0 | year*365 - 719499 |
134 | 0 | )*24 + hour /* now have hours */ |
135 | 0 | )*60 + min /* now have minutes */ |
136 | 0 | )*60 + sec; /* finally seconds */ |
137 | 0 | } |
138 | | |
139 | | const char *time_str(struct rtc_time *tm) |
140 | 0 | { |
141 | 0 | const char *weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
142 | 0 | const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", |
143 | 0 | "Sep", "Oct", "Nov", "Dec" }; |
144 | 0 | static char buf[128]; |
145 | |
|
146 | 0 | sprintf(buf, "%s %02d %s %4d %02d:%02d:%02d", |
147 | 0 | weekdays[tm->tm_wday], |
148 | 0 | tm->tm_mday, |
149 | 0 | months[tm->tm_mon], |
150 | 0 | tm->tm_year + 1900, |
151 | 0 | tm->tm_hour, |
152 | 0 | tm->tm_min, |
153 | 0 | tm->tm_sec); |
154 | 0 | return buf; |
155 | 0 | } |