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 #define SEC_IN_MIN 60ll
30 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
31 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
32 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
33 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
34 #define UNIX_EPOCH_YEAR 1970
35 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
36 #define EXFAT_EPOCH_YEAR 1980
37 /* number of years from Unix epoch to exFAT epoch */
38 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
39 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
40 #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 #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 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
46 		- (EXFAT_EPOCH_YEAR - 1) / 4)
47 /* checks whether the specified year is leap */
48 #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 
exfat_exfat2unix(le16_t date,le16_t time,uint8_t centisec,uint8_t tzoffset)56 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec,
57 		uint8_t tzoffset)
58 {
59 	time_t unix_time = EPOCH_DIFF_SEC;
60 	uint16_t ndate = le16_to_cpu(date);
61 	uint16_t ntime = le16_to_cpu(time);
62 
63 	uint16_t day    = ndate & 0x1f;      /* 5 bits, 1-31 */
64 	uint16_t month  = ndate >> 5 & 0xf;  /* 4 bits, 1-12 */
65 	uint16_t year   = ndate >> 9;        /* 7 bits, 1-127 (+1980) */
66 
67 	uint16_t twosec = ntime & 0x1f;      /* 5 bits, 0-29 (2 sec granularity) */
68 	uint16_t min    = ntime >> 5 & 0x3f; /* 6 bits, 0-59 */
69 	uint16_t hour   = ntime >> 11;       /* 5 bits, 0-23 */
70 
71 	if (day == 0 || month == 0 || month > 12)
72 	{
73 		exfat_error("bad date %u-%02hu-%02hu",
74 				year + EXFAT_EPOCH_YEAR, month, day);
75 		return 0;
76 	}
77 	if (hour > 23 || min > 59 || twosec > 29)
78 	{
79 		exfat_error("bad time %hu:%02hu:%02u",
80 				hour, min, twosec * 2);
81 		return 0;
82 	}
83 	if (centisec > 199)
84 	{
85 		exfat_error("bad centiseconds count %hhu", centisec);
86 		return 0;
87 	}
88 
89 	/* every 4th year between 1904 and 2096 is leap */
90 	unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
91 	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 	if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
94 		unix_time += SEC_IN_DAY;
95 	unix_time += (day - 1) * SEC_IN_DAY;
96 
97 	unix_time += hour * SEC_IN_HOUR;
98 	unix_time += min * SEC_IN_MIN;
99 	/* exFAT represents time with 2 sec granularity */
100 	unix_time += twosec * 2;
101 	unix_time += centisec / 100;
102 
103 	/* exFAT stores timestamps in local time, so we correct it to UTC */
104 	if (tzoffset & 0x80)
105 		/* lower 7 bits are signed timezone offset in 15 minute increments */
106 		unix_time -= (int8_t)(tzoffset << 1) * 15 * 60 / 2;
107 	else
108 		/* timezone offset not present, assume our local timezone */
109 		unix_time += exfat_timezone;
110 
111 	return unix_time;
112 }
113 
exfat_unix2exfat(time_t unix_time,le16_t * date,le16_t * time,uint8_t * centisec,uint8_t * tzoffset)114 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
115 		uint8_t* centisec, uint8_t* tzoffset)
116 {
117 	time_t shift = EPOCH_DIFF_SEC + exfat_timezone;
118 	uint16_t day, month, year;
119 	uint16_t twosec, min, hour;
120 	int days;
121 	int i;
122 
123 	/* time before exFAT epoch cannot be represented */
124 	if (unix_time < shift)
125 		unix_time = shift;
126 
127 	unix_time -= shift;
128 
129 	days = unix_time / SEC_IN_DAY;
130 	year = (4 * days) / (4 * 365 + 1);
131 	days -= year * 365 + LEAP_YEARS(year);
132 	month = 0;
133 	for (i = 1; i <= 12; i++)
134 	{
135 		int leap_day = (IS_LEAP_YEAR(year) && i == 2);
136 		int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
137 
138 		if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
139 		{
140 			month = i;
141 			days -= days_in_year[i] + leap_sub;
142 			break;
143 		}
144 	}
145 	day = days + 1;
146 
147 	hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
148 	min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
149 	twosec = (unix_time % SEC_IN_MIN) / 2;
150 
151 	*date = cpu_to_le16(day | (month << 5) | (year << 9));
152 	*time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
153 	if (centisec)
154 		*centisec = (unix_time % 2) * 100;
155 
156 	/* record our local timezone offset in exFAT (15 minute increment) format */
157 	*tzoffset = (uint8_t)(-exfat_timezone / 60 / 15) | 0x80;
158 }
159 
exfat_tzset(void)160 void exfat_tzset(void)
161 {
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 }
176