Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/fs/exfat/time.c
Line
Count
Source
1
/*
2
  time.c (03.02.12)
3
  exFAT file system implementation library.
4
5
  Free exFAT implementation.
6
  Copyright (C) 2010-2023  Andrew Nayenko
7
8
  This program is free software; you can redistribute it and/or modify
9
  it under the terms of the GNU General Public License as published by
10
  the Free Software Foundation, either version 2 of the License, or
11
  (at your option) any later version.
12
13
  This program is distributed in the hope that it will be useful,
14
  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
  GNU General Public License for more details.
17
18
  You should have received a copy of the GNU General Public License along
19
  with this program; if not, write to the Free Software Foundation, Inc.,
20
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
*/
22
23
#include "exfat.h"
24
25
/* timezone offset from UTC in seconds; positive for western timezones,
26
   negative for eastern ones */
27
static long exfat_timezone;
28
29
0
#define SEC_IN_MIN 60ll
30
0
#define SEC_IN_HOUR (60 * SEC_IN_MIN)
31
0
#define SEC_IN_DAY (24 * SEC_IN_HOUR)
32
0
#define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
33
/* Unix epoch started at 0:00:00 UTC 1 January 1970 */
34
0
#define UNIX_EPOCH_YEAR 1970
35
/* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
36
0
#define EXFAT_EPOCH_YEAR 1980
37
/* number of years from Unix epoch to exFAT epoch */
38
0
#define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
39
/* number of days from Unix epoch to exFAT epoch (considering leap days) */
40
0
#define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
41
/* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
42
0
#define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
43
/* number of leap years passed from exFAT epoch to the specified year
44
   (excluding the specified year itself) */
45
0
#define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
46
0
    - (EXFAT_EPOCH_YEAR - 1) / 4)
47
/* checks whether the specified year is leap */
48
0
#define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
49
50
static const time_t days_in_year[] =
51
{
52
  /* Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec */
53
  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334
54
};
55
56
time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec,
57
    uint8_t tzoffset)
58
0
{
59
0
  time_t unix_time = EPOCH_DIFF_SEC;
60
0
  uint16_t ndate = le16_to_cpu(date);
61
0
  uint16_t ntime = le16_to_cpu(time);
62
63
0
  uint16_t day    = ndate & 0x1f;      /* 5 bits, 1-31 */
64
0
  uint16_t month  = ndate >> 5 & 0xf;  /* 4 bits, 1-12 */
65
0
  uint16_t year   = ndate >> 9;        /* 7 bits, 1-127 (+1980) */
66
67
0
  uint16_t twosec = ntime & 0x1f;      /* 5 bits, 0-29 (2 sec granularity) */
68
0
  uint16_t min    = ntime >> 5 & 0x3f; /* 6 bits, 0-59 */
69
0
  uint16_t hour   = ntime >> 11;       /* 5 bits, 0-23 */
70
71
0
  if (day == 0 || month == 0 || month > 12)
72
0
  {
73
0
    exfat_error("bad date %u-%02hu-%02hu",
74
0
        year + EXFAT_EPOCH_YEAR, month, day);
75
0
    return 0;
76
0
  }
77
0
  if (hour > 23 || min > 59 || twosec > 29)
78
0
  {
79
0
    exfat_error("bad time %hu:%02hu:%02u",
80
0
        hour, min, twosec * 2);
81
0
    return 0;
82
0
  }
83
0
  if (centisec > 199)
84
0
  {
85
0
    exfat_error("bad centiseconds count %hhu", centisec);
86
0
    return 0;
87
0
  }
88
89
  /* every 4th year between 1904 and 2096 is leap */
90
0
  unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
91
0
  unix_time += days_in_year[month] * SEC_IN_DAY;
92
  /* if it's leap year and February has passed we should add 1 day */
93
0
  if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
94
0
    unix_time += SEC_IN_DAY;
95
0
  unix_time += (day - 1) * SEC_IN_DAY;
96
97
0
  unix_time += hour * SEC_IN_HOUR;
98
0
  unix_time += min * SEC_IN_MIN;
99
  /* exFAT represents time with 2 sec granularity */
100
0
  unix_time += twosec * 2;
101
0
  unix_time += centisec / 100;
102
103
  /* exFAT stores timestamps in local time, so we correct it to UTC */
104
0
  if (tzoffset & 0x80)
105
    /* lower 7 bits are signed timezone offset in 15 minute increments */
106
0
    unix_time -= (int8_t)(tzoffset << 1) * 15 * 60 / 2;
107
0
  else
108
    /* timezone offset not present, assume our local timezone */
109
0
    unix_time += exfat_timezone;
110
111
0
  return unix_time;
112
0
}
113
114
void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
115
    uint8_t* centisec, uint8_t* tzoffset)
116
0
{
117
0
  time_t shift = EPOCH_DIFF_SEC + exfat_timezone;
118
0
  uint16_t day, month, year;
119
0
  uint16_t twosec, min, hour;
120
0
  int days;
121
0
  int i;
122
123
  /* time before exFAT epoch cannot be represented */
124
0
  if (unix_time < shift)
125
0
    unix_time = shift;
126
127
0
  unix_time -= shift;
128
129
0
  days = unix_time / SEC_IN_DAY;
130
0
  year = (4 * days) / (4 * 365 + 1);
131
0
  days -= year * 365 + LEAP_YEARS(year);
132
0
  month = 0;
133
0
  for (i = 1; i <= 12; i++)
134
0
  {
135
0
    int leap_day = (IS_LEAP_YEAR(year) && i == 2);
136
0
    int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
137
138
0
    if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
139
0
    {
140
0
      month = i;
141
0
      days -= days_in_year[i] + leap_sub;
142
0
      break;
143
0
    }
144
0
  }
145
0
  day = days + 1;
146
147
0
  hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
148
0
  min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
149
0
  twosec = (unix_time % SEC_IN_MIN) / 2;
150
151
0
  *date = cpu_to_le16(day | (month << 5) | (year << 9));
152
0
  *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
153
0
  if (centisec)
154
0
    *centisec = (unix_time % 2) * 100;
155
156
  /* record our local timezone offset in exFAT (15 minute increment) format */
157
0
  *tzoffset = (uint8_t)(-exfat_timezone / 60 / 15) | 0x80;
158
0
}
159
160
void exfat_tzset(void)
161
0
{
162
#ifndef __UBOOT__
163
  time_t now;
164
  struct tm* utc;
165
166
  tzset();
167
  now = time(NULL);
168
  utc = gmtime(&now);
169
  /* gmtime() always sets tm_isdst to 0 because daylight savings never
170
     affect UTC. Setting tm_isdst to -1 makes mktime() to determine whether
171
     summer time is in effect. */
172
  utc->tm_isdst = -1;
173
  exfat_timezone = mktime(utc) - now;
174
#endif
175
0
}