Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/rtc/rtc-lib.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * rtc and date/time utility functions
4
 *
5
 * This code was ported from linux-3.15 kernel by Antony Pavlov.
6
 *
7
 * Copyright (C) 2005-06 Tower Technologies
8
 * Author: Alessandro Zummo <a.zummo@towertech.it>
9
 *
10
 * based on arch/arm/common/rtctime.c and other bits
11
 */
12
13
#include <rtc.h>
14
#include <linux/rtc.h>
15
16
static const unsigned char rtc_days_in_month[] = {
17
  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
18
};
19
20
0
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
21
22
/*
23
 * The number of days in the month.
24
 */
25
int rtc_month_days(unsigned int month, unsigned int year)
26
0
{
27
0
  return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
28
0
}
29
EXPORT_SYMBOL(rtc_month_days);
30
31
/*
32
 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
33
 */
34
void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
35
0
{
36
0
  unsigned int month, year;
37
0
  int days;
38
39
0
  days = time / 86400;
40
0
  time -= (unsigned int) days * 86400;
41
42
  /* day of the week, 1970-01-01 was a Thursday */
43
0
  tm->tm_wday = (days + 4) % 7;
44
45
0
  year = 1970 + days / 365;
46
0
  days -= (year - 1970) * 365
47
0
    + LEAPS_THRU_END_OF(year - 1)
48
0
    - LEAPS_THRU_END_OF(1970 - 1);
49
0
  if (days < 0) {
50
0
    year -= 1;
51
0
    days += 365 + is_leap_year(year);
52
0
  }
53
0
  tm->tm_year = year - 1900;
54
0
  tm->tm_yday = days + 1;
55
56
0
  for (month = 0; month < 11; month++) {
57
0
    int newdays;
58
59
0
    newdays = days - rtc_month_days(month, year);
60
0
    if (newdays < 0)
61
0
      break;
62
0
    days = newdays;
63
0
  }
64
0
  tm->tm_mon = month;
65
0
  tm->tm_mday = days + 1;
66
67
0
  tm->tm_hour = time / 3600;
68
0
  time -= tm->tm_hour * 3600;
69
0
  tm->tm_min = time / 60;
70
0
  tm->tm_sec = time - tm->tm_min * 60;
71
72
0
  tm->tm_isdst = 0;
73
0
}
74
EXPORT_SYMBOL(rtc_time_to_tm);
75
76
// SPDX-SnippetBegin
77
// SPDX-Snippet-Comment: Origin-URL: https://github.com/u-boot/u-boot/blob/467382ca03758e4f3f13107e3a83669e93a7461e/lib/date.c
78
79
static int month_offset[] = {
80
  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
81
};
82
83
/*
84
 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
85
 */
86
int rtc_calc_weekday(struct rtc_time *tm)
87
0
{
88
0
  int leaps_to_date;
89
0
  int last_year;
90
0
  int day;
91
92
0
  if (tm->tm_year < 1753)
93
0
    return -1;
94
0
  last_year = tm->tm_year - 1;
95
96
  /* Number of leap corrections to apply up to end of last year */
97
0
  leaps_to_date = last_year / 4 - last_year / 100 + last_year / 400;
98
99
  /*
100
   * This year is a leap year if it is divisible by 4 except when it is
101
   * divisible by 100 unless it is divisible by 400
102
   *
103
   * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 is.
104
   */
105
0
  if (tm->tm_year % 4 == 0 &&
106
0
      ((tm->tm_year % 100 != 0) || (tm->tm_year % 400 == 0)) &&
107
0
      tm->tm_mon > 2) {
108
    /* We are past Feb. 29 in a leap year */
109
0
    day = 1;
110
0
  } else {
111
0
    day = 0;
112
0
  }
113
114
0
  day += last_year * 365 + leaps_to_date + month_offset[tm->tm_mon - 1] +
115
0
      tm->tm_mday;
116
0
  tm->tm_wday = day % 7;
117
118
0
  return 0;
119
0
}
120
EXPORT_SYMBOL(rtc_calc_weekday);
121
122
// SPDX-SnippetEnd
123
124
/*
125
 * Does the rtc_time represent a valid date/time?
126
 */
127
int rtc_valid_tm(struct rtc_time *tm)
128
0
{
129
0
  if (tm->tm_year < 70
130
0
    || ((unsigned)tm->tm_mon) >= 12
131
0
    || tm->tm_wday < 0
132
0
    || tm->tm_wday > 6
133
0
    || tm->tm_mday < 1
134
0
    || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
135
0
    || ((unsigned)tm->tm_hour) >= 24
136
0
    || ((unsigned)tm->tm_min) >= 60
137
0
    || ((unsigned)tm->tm_sec) >= 60)
138
0
    return -EINVAL;
139
140
0
  return 0;
141
0
}
142
EXPORT_SYMBOL(rtc_valid_tm);
143
144
/*
145
 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
146
 */
147
int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
148
0
{
149
0
  *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
150
0
      tm->tm_hour, tm->tm_min, tm->tm_sec);
151
0
  return 0;
152
0
}
153
EXPORT_SYMBOL(rtc_tm_to_time);