blob: 258f4ac25ce3803fcfc4e03066b74aab484d9a24 [file] [log] [blame]
Elyes HAOUAS081c4d52020-05-07 07:53:59 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
Simon Glass2cf99e12016-06-10 20:58:24 -06003/*
Simon Glass2cf99e12016-06-10 20:58:24 -06004 * From U-Boot 2016.05
5 */
6
7#include <console/console.h>
8#include <rtc.h>
9
10#define FEBRUARY 2
11#define STARTOFTIME 1970
12#define SECDAY 86400L
13#define SECYR (SECDAY * 365)
14#define LEAP_YEAR(year) ((year) % 4 == 0)
15#define DAYS_IN_YEAR(a) (LEAP_YEAR(a) ? 366 : 365)
16#define DAYS_IN_MONTH(a) (month_days[(a) - 1])
17
Elyes HAOUASc92f5f22018-07-08 12:36:18 +020018static const char *const weekdays[] = {
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070019 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"
Simon Glass2cf99e12016-06-10 20:58:24 -060020};
21
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070022/* Zeller's rule */
Simon Glass2cf99e12016-06-10 20:58:24 -060023static int rtc_calc_weekday(struct rtc_time *tm)
24{
Simon Glass2cf99e12016-06-10 20:58:24 -060025 if (tm->year < 1971)
26 return -1;
27
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070028 /* In Zeller's rule, January and February are treated as if they
29 are months 13 and 14 of the previous year (March is still month 3) */
30 const int zyear = ((tm->mon < 3) ? tm->year - 1 : tm->year);
31 const int q = tm->mday;
32 const int m = (tm->mon < 3) ? tm->mon + 12 : tm->mon;
33 const int K = zyear % 100;
34 const int J = zyear / 100;
Simon Glass2cf99e12016-06-10 20:58:24 -060035
36 /*
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070037 * Because of the way the modulo operator works with negative numbers,
38 * the traditional formulation of Zeller's rule must be modified
39 * slightly to make the numerator positive (i.e., add 5J instead of
40 * subtracting 2J). Also subtract 1 so that Sunday is day 0.
Simon Glass2cf99e12016-06-10 20:58:24 -060041 */
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070042 const int h = (q + (13 * (m + 1)) / 5
43 + K + (K / 4) + (J / 4) + (5 * J) - 1) % 7;
Simon Glass2cf99e12016-06-10 20:58:24 -060044
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070045 tm->wday = h;
Simon Glass2cf99e12016-06-10 20:58:24 -060046 return 0;
47}
48
49int rtc_to_tm(int tim, struct rtc_time *tm)
50{
51 int month_days[12] = {
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070052 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
Simon Glass2cf99e12016-06-10 20:58:24 -060053 };
54 register int i;
55 register long hms, day;
56
57 day = tim / SECDAY;
58 hms = tim % SECDAY;
59
60 /* Hours, minutes, seconds are easy */
61 tm->hour = hms / 3600;
62 tm->min = (hms % 3600) / 60;
63 tm->sec = (hms % 3600) % 60;
64
65 /* Number of years in days */
66 for (i = STARTOFTIME; day >= DAYS_IN_YEAR(i); i++)
67 day -= DAYS_IN_YEAR(i);
68 tm->year = i;
69
70 /* Number of months in days left */
71 if (LEAP_YEAR(tm->year))
72 DAYS_IN_MONTH(FEBRUARY) = 29;
73 for (i = 1; day >= DAYS_IN_MONTH(i); i++)
74 day -= DAYS_IN_MONTH(i);
75 DAYS_IN_MONTH(FEBRUARY) = 28;
76 tm->mon = i;
77
78 /* Days are what is left over (+1) from all that */
79 tm->mday = day + 1;
80
81 /* Determine the day of week */
82 return rtc_calc_weekday(tm);
83}
84
85/*
86 * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070087 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
Simon Glass2cf99e12016-06-10 20:58:24 -060088 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
89 *
90 * [For the Julian calendar (which was used in Russia before 1917,
91 * Britain & colonies before 1752, anywhere else before 1582,
92 * and is still in use by some communities) leave out the
93 * -year / 100 + year / 400 terms, and add 10.]
94 *
95 * This algorithm was first published by Gauss (I think).
96 *
97 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
98 * machines where long is 32-bit! (However, as time_t is signed, we
99 * will already get problems at other places on 2038-01-19 03:14:08)
100 */
101unsigned long rtc_mktime(const struct rtc_time *tm)
102{
103 int mon = tm->mon;
104 int year = tm->year;
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -0700105 int days, hours;
Simon Glass2cf99e12016-06-10 20:58:24 -0600106
107 mon -= 2;
108 if (0 >= (int)mon) { /* 1..12 -> 11, 12, 1..10 */
109 mon += 12; /* Puts Feb last since it has leap day */
110 year -= 1;
111 }
112
113 days = (unsigned long)(year / 4 - year / 100 + year / 400 +
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -0700114 367 * mon / 12 + tm->mday) +
115 year * 365 - 719499;
Simon Glass2cf99e12016-06-10 20:58:24 -0600116 hours = days * 24 + tm->hour;
117 return (hours * 60 + tm->min) * 60 + tm->sec;
118}
119
120void rtc_display(const struct rtc_time *tm)
121{
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -0700122 printk(BIOS_INFO, "Date: %5d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
Simon Glass2cf99e12016-06-10 20:58:24 -0600123 tm->year, tm->mon, tm->mday,
124 (tm->wday < 0 || tm->wday > 6) ? "unknown " : weekdays[tm->wday],
125 tm->hour, tm->min, tm->sec);
126}