blob: 8a807cef82263d3605f698f80a552075390b0467 [file] [log] [blame]
Simon Glass2cf99e12016-06-10 20:58:24 -06001/*
2 * This file is part of the coreboot project.
3 *
Simon Glass2cf99e12016-06-10 20:58:24 -06004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * From U-Boot 2016.05
16 */
17
18#include <console/console.h>
19#include <rtc.h>
20
21#define FEBRUARY 2
22#define STARTOFTIME 1970
23#define SECDAY 86400L
24#define SECYR (SECDAY * 365)
25#define LEAP_YEAR(year) ((year) % 4 == 0)
26#define DAYS_IN_YEAR(a) (LEAP_YEAR(a) ? 366 : 365)
27#define DAYS_IN_MONTH(a) (month_days[(a) - 1])
28
Elyes HAOUASc92f5f22018-07-08 12:36:18 +020029static const char *const weekdays[] = {
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070030 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"
Simon Glass2cf99e12016-06-10 20:58:24 -060031};
32
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070033/* Zeller's rule */
Simon Glass2cf99e12016-06-10 20:58:24 -060034static int rtc_calc_weekday(struct rtc_time *tm)
35{
Simon Glass2cf99e12016-06-10 20:58:24 -060036 if (tm->year < 1971)
37 return -1;
38
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070039 /* In Zeller's rule, January and February are treated as if they
40 are months 13 and 14 of the previous year (March is still month 3) */
41 const int zyear = ((tm->mon < 3) ? tm->year - 1 : tm->year);
42 const int q = tm->mday;
43 const int m = (tm->mon < 3) ? tm->mon + 12 : tm->mon;
44 const int K = zyear % 100;
45 const int J = zyear / 100;
Simon Glass2cf99e12016-06-10 20:58:24 -060046
47 /*
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070048 * Because of the way the modulo operator works with negative numbers,
49 * the traditional formulation of Zeller's rule must be modified
50 * slightly to make the numerator positive (i.e., add 5J instead of
51 * subtracting 2J). Also subtract 1 so that Sunday is day 0.
Simon Glass2cf99e12016-06-10 20:58:24 -060052 */
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070053 const int h = (q + (13 * (m + 1)) / 5
54 + K + (K / 4) + (J / 4) + (5 * J) - 1) % 7;
Simon Glass2cf99e12016-06-10 20:58:24 -060055
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070056 tm->wday = h;
Simon Glass2cf99e12016-06-10 20:58:24 -060057 return 0;
58}
59
60int rtc_to_tm(int tim, struct rtc_time *tm)
61{
62 int month_days[12] = {
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070063 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
Simon Glass2cf99e12016-06-10 20:58:24 -060064 };
65 register int i;
66 register long hms, day;
67
68 day = tim / SECDAY;
69 hms = tim % SECDAY;
70
71 /* Hours, minutes, seconds are easy */
72 tm->hour = hms / 3600;
73 tm->min = (hms % 3600) / 60;
74 tm->sec = (hms % 3600) % 60;
75
76 /* Number of years in days */
77 for (i = STARTOFTIME; day >= DAYS_IN_YEAR(i); i++)
78 day -= DAYS_IN_YEAR(i);
79 tm->year = i;
80
81 /* Number of months in days left */
82 if (LEAP_YEAR(tm->year))
83 DAYS_IN_MONTH(FEBRUARY) = 29;
84 for (i = 1; day >= DAYS_IN_MONTH(i); i++)
85 day -= DAYS_IN_MONTH(i);
86 DAYS_IN_MONTH(FEBRUARY) = 28;
87 tm->mon = i;
88
89 /* Days are what is left over (+1) from all that */
90 tm->mday = day + 1;
91
92 /* Determine the day of week */
93 return rtc_calc_weekday(tm);
94}
95
96/*
97 * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -070098 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
Simon Glass2cf99e12016-06-10 20:58:24 -060099 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
100 *
101 * [For the Julian calendar (which was used in Russia before 1917,
102 * Britain & colonies before 1752, anywhere else before 1582,
103 * and is still in use by some communities) leave out the
104 * -year / 100 + year / 400 terms, and add 10.]
105 *
106 * This algorithm was first published by Gauss (I think).
107 *
108 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
109 * machines where long is 32-bit! (However, as time_t is signed, we
110 * will already get problems at other places on 2038-01-19 03:14:08)
111 */
112unsigned long rtc_mktime(const struct rtc_time *tm)
113{
114 int mon = tm->mon;
115 int year = tm->year;
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -0700116 int days, hours;
Simon Glass2cf99e12016-06-10 20:58:24 -0600117
118 mon -= 2;
119 if (0 >= (int)mon) { /* 1..12 -> 11, 12, 1..10 */
120 mon += 12; /* Puts Feb last since it has leap day */
121 year -= 1;
122 }
123
124 days = (unsigned long)(year / 4 - year / 100 + year / 400 +
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -0700125 367 * mon / 12 + tm->mday) +
126 year * 365 - 719499;
Simon Glass2cf99e12016-06-10 20:58:24 -0600127 hours = days * 24 + tm->hour;
128 return (hours * 60 + tm->min) * 60 + tm->sec;
129}
130
131void rtc_display(const struct rtc_time *tm)
132{
Tim Wawrzynczakb47c6332019-02-21 15:02:26 -0700133 printk(BIOS_INFO, "Date: %5d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
Simon Glass2cf99e12016-06-10 20:58:24 -0600134 tm->year, tm->mon, tm->mday,
135 (tm->wday < 0 || tm->wday > 6) ? "unknown " : weekdays[tm->wday],
136 tm->hour, tm->min, tm->sec);
137}