1 // SPDX-License-Identifier: GPL-2.0
3 * rtc and date/time utility functions
5 * Copyright (C) 2005-06 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
8 * U-Boot rtc_time differs from Linux rtc_time:
9 * - The year field takes the actual value, not year - 1900.
10 * - January is month 1.
14 #include <linux/math64.h>
16 static const unsigned char rtc_days_in_month[] = {
17 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
20 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
23 * The number of days in the month.
25 int rtc_month_days(unsigned int month, unsigned int year)
27 return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
31 * rtc_to_tm - Converts u64 to rtc_time.
32 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
34 * This function is copied from rtc_time64_to_tm() in the Linux kernel.
35 * But in U-Boot January is month 1 and we do not subtract 1900 from the year.
37 void rtc_to_tm(u64 time, struct rtc_time *tm)
39 unsigned int month, year, secs;
42 days = div_u64_rem(time, 86400, &secs);
44 /* day of the week, 1970-01-01 was a Thursday */
45 tm->tm_wday = (days + 4) % 7;
47 year = 1970 + days / 365;
48 days -= (year - 1970) * 365
49 + LEAPS_THRU_END_OF(year - 1)
50 - LEAPS_THRU_END_OF(1970 - 1);
53 days += 365 + is_leap_year(year);
55 tm->tm_year = year; /* Not year - 1900 */
56 tm->tm_yday = days + 1;
58 for (month = 0; month < 11; month++) {
61 newdays = days - rtc_month_days(month, year);
66 tm->tm_mon = month + 1; /* January = 1 */
67 tm->tm_mday = days + 1;
69 tm->tm_hour = secs / 3600;
70 secs -= tm->tm_hour * 3600;
71 tm->tm_min = secs / 60;
72 tm->tm_sec = secs - tm->tm_min * 60;
74 /* Zero unused fields */