1 /*
2 	time.c (03.02.12)
3 	exFAT file system implementation library.
4 
5 	Free exFAT implementation.
6 	Copyright (C) 2010-2018  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)56 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
57 {
58 	time_t unix_time = EPOCH_DIFF_SEC;
59 	uint16_t ndate = le16_to_cpu(date);
60 	uint16_t ntime = le16_to_cpu(time);
61 
62 	uint16_t day    = ndate & 0x1f;      /* 5 bits, 1-31 */
63 	uint16_t month  = ndate >> 5 & 0xf;  /* 4 bits, 1-12 */
64 	uint16_t year   = ndate >> 9;        /* 7 bits, 1-127 (+1980) */
65 
66 	uint16_t twosec = ntime & 0x1f;      /* 5 bits, 0-29 (2 sec granularity) */
67 	uint16_t min    = ntime >> 5 & 0x3f; /* 6 bits, 0-59 */
68 	uint16_t hour   = ntime >> 11;       /* 5 bits, 0-23 */
69 
70 	if (day == 0 || month == 0 || month > 12)
71 	{
72 		exfat_error("bad date %u-%02hu-%02hu",
73 				year + EXFAT_EPOCH_YEAR, month, day);
74 		return 0;
75 	}
76 	if (hour > 23 || min > 59 || twosec > 29)
77 	{
78 		exfat_error("bad time %hu:%02hu:%02u",
79 				hour, min, twosec * 2);
80 		return 0;
81 	}
82 	if (centisec > 199)
83 	{
84 		exfat_error("bad centiseconds count %hhu", centisec);
85 		return 0;
86 	}
87 
88 	/* every 4th year between 1904 and 2096 is leap */
89 	unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
90 	unix_time += days_in_year[month] * SEC_IN_DAY;
91 	/* if it's leap year and February has passed we should add 1 day */
92 	if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
93 		unix_time += SEC_IN_DAY;
94 	unix_time += (day - 1) * SEC_IN_DAY;
95 
96 	unix_time += hour * SEC_IN_HOUR;
97 	unix_time += min * SEC_IN_MIN;
98 	/* exFAT represents time with 2 sec granularity */
99 	unix_time += twosec * 2;
100 	unix_time += centisec / 100;
101 
102 	/* exFAT stores timestamps in local time, so we correct it to UTC */
103 	unix_time += exfat_timezone;
104 
105 	return unix_time;
106 }
107 
exfat_unix2exfat(time_t unix_time,le16_t * date,le16_t * time,uint8_t * centisec)108 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
109 		uint8_t* centisec)
110 {
111 	time_t shift = EPOCH_DIFF_SEC + exfat_timezone;
112 	uint16_t day, month, year;
113 	uint16_t twosec, min, hour;
114 	int days;
115 	int i;
116 
117 	/* time before exFAT epoch cannot be represented */
118 	if (unix_time < shift)
119 		unix_time = shift;
120 
121 	unix_time -= shift;
122 
123 	days = unix_time / SEC_IN_DAY;
124 	year = (4 * days) / (4 * 365 + 1);
125 	days -= year * 365 + LEAP_YEARS(year);
126 	month = 0;
127 	for (i = 1; i <= 12; i++)
128 	{
129 		int leap_day = (IS_LEAP_YEAR(year) && i == 2);
130 		int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
131 
132 		if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
133 		{
134 			month = i;
135 			days -= days_in_year[i] + leap_sub;
136 			break;
137 		}
138 	}
139 	day = days + 1;
140 
141 	hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
142 	min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
143 	twosec = (unix_time % SEC_IN_MIN) / 2;
144 
145 	*date = cpu_to_le16(day | (month << 5) | (year << 9));
146 	*time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
147 	if (centisec)
148 		*centisec = (unix_time % 2) * 100;
149 }
150 
exfat_tzset(void)151 void exfat_tzset(void)
152 {
153 	time_t now;
154 	struct tm* utc;
155 
156 	tzset();
157 	now = time(NULL);
158 	utc = gmtime(&now);
159 	/* gmtime() always sets tm_isdst to 0 because daylight savings never
160 	   affect UTC. Setting tm_isdst to -1 makes mktime() to determine whether
161 	   summer time is in effect. */
162 	utc->tm_isdst = -1;
163 	exfat_timezone = mktime(utc) - now;
164 }
165