xref: /freebsd/sys/kern/subr_fattime.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Poul-Henning Kamp
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Convert MS-DOS FAT format timestamps to and from unix timespecs
29  *
30  * FAT filestamps originally consisted of two 16 bit integers, encoded like
31  * this:
32  *
33  *	yyyyyyymmmmddddd (year - 1980, month, day)
34  *
35  *      hhhhhmmmmmmsssss (hour, minutes, seconds divided by two)
36  *
37  * Subsequently even Microsoft realized that files could be accessed in less
38  * than two seconds and a byte was added containing:
39  *
40  *      sfffffff	 (second mod two, 100ths of second)
41  *
42  * FAT timestamps are in the local timezone, with no indication of which
43  * timezone much less if daylight savings time applies.
44  *
45  * Later on again, in Windows NT, timestamps were defined relative to GMT.
46  *
47  * Purists will point out that UTC replaced GMT for such uses around
48  * half a century ago, already then.  Ironically "NT" was an abbreviation of
49  * "New Technology".  Anyway...
50  *
51  * The 'utc' argument determines if the resulting FATTIME timestamp
52  * should be on the UTC or local timezone calendar.
53  *
54  * The conversion functions below cut time into four-year leap-year
55  * cycles rather than single years and uses table lookups inside those
56  * cycles to get the months and years sorted out.
57  *
58  * Obviously we cannot calculate the correct table index going from
59  * a posix seconds count to Y/M/D, but we can get pretty close by
60  * dividing the daycount by 32 (giving a too low index), and then
61  * adjusting upwards a couple of steps if necessary.
62  *
63  * FAT timestamps have 7 bits for the year and starts at 1980, so
64  * they can represent up to 2107 which means that the non-leap-year
65  * 2100 must be handled.
66  */
67 
68 #include <sys/param.h>
69 #include <sys/types.h>
70 #include <sys/time.h>
71 #include <sys/clock.h>
72 
73 #ifdef TEST_DRIVER
74 /* stub for testing */
75 #define utc_offset() 0
76 #endif
77 
78 #define DAY	(24 * 60 * 60)	/* Length of day in seconds */
79 #define YEAR	365		/* Length of normal year */
80 #define LYC	(4 * YEAR + 1)	/* Length of 4 year leap-year cycle */
81 #define T1980	(10 * 365 + 2)	/* Days from 1970 to 1980 */
82 #define T2108	(138 * 365 + 33) /* Days from 1970 to 2108 */
83 
84 /* End of month is N days from start of (normal) year */
85 #define JAN	31
86 #define FEB	(JAN + 28)
87 #define MAR	(FEB + 31)
88 #define APR	(MAR + 30)
89 #define MAY	(APR + 31)
90 #define JUN	(MAY + 30)
91 #define JUL	(JUN + 31)
92 #define AUG	(JUL + 31)
93 #define SEP	(AUG + 30)
94 #define OCT	(SEP + 31)
95 #define NOV	(OCT + 30)
96 #define DEC	(NOV + 31)
97 
98 /* Table of months in a 4 year leap-year cycle */
99 
100 #define ENC(y,m)	(((y) << 9) | ((m) << 5))
101 
102 static const struct {
103 	uint16_t	days;	/* month start in days relative to cycle */
104 	uint16_t	coded;	/* encoded year + month information */
105 } mtab[48] = {
106 	{   0 + 0 * YEAR,     ENC(0, 1)  },
107 
108 	{ JAN + 0 * YEAR,     ENC(0, 2)  }, { FEB + 0 * YEAR + 1, ENC(0, 3)  },
109 	{ MAR + 0 * YEAR + 1, ENC(0, 4)  }, { APR + 0 * YEAR + 1, ENC(0, 5)  },
110 	{ MAY + 0 * YEAR + 1, ENC(0, 6)  }, { JUN + 0 * YEAR + 1, ENC(0, 7)  },
111 	{ JUL + 0 * YEAR + 1, ENC(0, 8)  }, { AUG + 0 * YEAR + 1, ENC(0, 9)  },
112 	{ SEP + 0 * YEAR + 1, ENC(0, 10) }, { OCT + 0 * YEAR + 1, ENC(0, 11) },
113 	{ NOV + 0 * YEAR + 1, ENC(0, 12) }, { DEC + 0 * YEAR + 1, ENC(1, 1)  },
114 
115 	{ JAN + 1 * YEAR + 1, ENC(1, 2)  }, { FEB + 1 * YEAR + 1, ENC(1, 3)  },
116 	{ MAR + 1 * YEAR + 1, ENC(1, 4)  }, { APR + 1 * YEAR + 1, ENC(1, 5)  },
117 	{ MAY + 1 * YEAR + 1, ENC(1, 6)  }, { JUN + 1 * YEAR + 1, ENC(1, 7)  },
118 	{ JUL + 1 * YEAR + 1, ENC(1, 8)  }, { AUG + 1 * YEAR + 1, ENC(1, 9)  },
119 	{ SEP + 1 * YEAR + 1, ENC(1, 10) }, { OCT + 1 * YEAR + 1, ENC(1, 11) },
120 	{ NOV + 1 * YEAR + 1, ENC(1, 12) }, { DEC + 1 * YEAR + 1, ENC(2, 1)  },
121 
122 	{ JAN + 2 * YEAR + 1, ENC(2, 2)  }, { FEB + 2 * YEAR + 1, ENC(2, 3)  },
123 	{ MAR + 2 * YEAR + 1, ENC(2, 4)  }, { APR + 2 * YEAR + 1, ENC(2, 5)  },
124 	{ MAY + 2 * YEAR + 1, ENC(2, 6)  }, { JUN + 2 * YEAR + 1, ENC(2, 7)  },
125 	{ JUL + 2 * YEAR + 1, ENC(2, 8)  }, { AUG + 2 * YEAR + 1, ENC(2, 9)  },
126 	{ SEP + 2 * YEAR + 1, ENC(2, 10) }, { OCT + 2 * YEAR + 1, ENC(2, 11) },
127 	{ NOV + 2 * YEAR + 1, ENC(2, 12) }, { DEC + 2 * YEAR + 1, ENC(3, 1)  },
128 
129 	{ JAN + 3 * YEAR + 1, ENC(3, 2)  }, { FEB + 3 * YEAR + 1, ENC(3, 3)  },
130 	{ MAR + 3 * YEAR + 1, ENC(3, 4)  }, { APR + 3 * YEAR + 1, ENC(3, 5)  },
131 	{ MAY + 3 * YEAR + 1, ENC(3, 6)  }, { JUN + 3 * YEAR + 1, ENC(3, 7)  },
132 	{ JUL + 3 * YEAR + 1, ENC(3, 8)  }, { AUG + 3 * YEAR + 1, ENC(3, 9)  },
133 	{ SEP + 3 * YEAR + 1, ENC(3, 10) }, { OCT + 3 * YEAR + 1, ENC(3, 11) },
134 	{ NOV + 3 * YEAR + 1, ENC(3, 12) }
135 };
136 
137 void
138 timespec2fattime(const struct timespec *tsp, int utc, uint16_t *ddp,
139     uint16_t *dtp, uint8_t *dhp)
140 {
141 	time_t t1;
142 	unsigned t2, l, m;
143 
144 	t1 = tsp->tv_sec;
145 	if (!utc)
146 		t1 -= utc_offset();
147 
148 	if (dhp != NULL)
149 		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
150 	if (dtp != NULL) {
151 		*dtp = (t1 / 2) % 30;
152 		*dtp |= ((t1 / 60) % 60) << 5;
153 		*dtp |= ((t1 / 3600) % 24) << 11;
154 	}
155 	if (ddp != NULL) {
156 		t2 = t1 / DAY;
157 		if (t2 < T1980) {
158 			/* Impossible date, truncate to 1980-01-01 */
159 			*ddp = 0x0021;
160 		} else {
161 			t2 -= T1980;
162 
163 			/* 2100 is not a leap year */
164 			if (t2 >= ((2100 - 1980) / 4 * LYC + FEB))
165 				t2++;
166 
167 			/* Account for full leapyear cycles */
168 			l = t2 / LYC;
169 			*ddp = (l * 4) << 9;
170 			t2 -= l * LYC;
171 
172 			/* Find approximate table entry */
173 			m = t2 / 32;
174 
175 			/* Find correct table entry */
176 			while (m < 47 && mtab[m + 1].days <= t2)
177 				m++;
178 
179 			/* Get year + month from the table */
180 			*ddp += mtab[m].coded;
181 
182 			/* And apply the day in the month */
183 			t2 -= mtab[m].days - 1;
184 			*ddp |= t2;
185 		}
186 	}
187 }
188 
189 /*
190  * Table indexed by the bottom two bits of year + four bits of the month
191  * from the FAT timestamp, returning number of days into 4 year long
192  * leap-year cycle
193  */
194 
195 #define DCOD(m, y, l)	((m) + YEAR * (y) + (l))
196 static const uint16_t daytab[64] = {
197 	0, 		 DCOD(  0, 0, 0), DCOD(JAN, 0, 0), DCOD(FEB, 0, 1),
198 	DCOD(MAR, 0, 1), DCOD(APR, 0, 1), DCOD(MAY, 0, 1), DCOD(JUN, 0, 1),
199 	DCOD(JUL, 0, 1), DCOD(AUG, 0, 1), DCOD(SEP, 0, 1), DCOD(OCT, 0, 1),
200 	DCOD(NOV, 0, 1), DCOD(DEC, 0, 1), 0,               0,
201 	0, 		 DCOD(  0, 1, 1), DCOD(JAN, 1, 1), DCOD(FEB, 1, 1),
202 	DCOD(MAR, 1, 1), DCOD(APR, 1, 1), DCOD(MAY, 1, 1), DCOD(JUN, 1, 1),
203 	DCOD(JUL, 1, 1), DCOD(AUG, 1, 1), DCOD(SEP, 1, 1), DCOD(OCT, 1, 1),
204 	DCOD(NOV, 1, 1), DCOD(DEC, 1, 1), 0,               0,
205 	0,		 DCOD(  0, 2, 1), DCOD(JAN, 2, 1), DCOD(FEB, 2, 1),
206 	DCOD(MAR, 2, 1), DCOD(APR, 2, 1), DCOD(MAY, 2, 1), DCOD(JUN, 2, 1),
207 	DCOD(JUL, 2, 1), DCOD(AUG, 2, 1), DCOD(SEP, 2, 1), DCOD(OCT, 2, 1),
208 	DCOD(NOV, 2, 1), DCOD(DEC, 2, 1), 0,               0,
209 	0,		 DCOD(  0, 3, 1), DCOD(JAN, 3, 1), DCOD(FEB, 3, 1),
210 	DCOD(MAR, 3, 1), DCOD(APR, 3, 1), DCOD(MAY, 3, 1), DCOD(JUN, 3, 1),
211 	DCOD(JUL, 3, 1), DCOD(AUG, 3, 1), DCOD(SEP, 3, 1), DCOD(OCT, 3, 1),
212 	DCOD(NOV, 3, 1), DCOD(DEC, 3, 1), 0,               0
213 };
214 
215 void
216 fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc,
217     struct timespec *tsp)
218 {
219 	unsigned day;
220 
221 	/* Unpack time fields */
222 	tsp->tv_sec = (dt & 0x1f) << 1;
223 	tsp->tv_sec += ((dt & 0x7e0) >> 5) * 60;
224 	tsp->tv_sec += ((dt & 0xf800) >> 11) * 3600;
225 	tsp->tv_sec += dh / 100;
226 	tsp->tv_nsec = (dh % 100) * 10000000;
227 
228 	/* Day of month */
229 	day = (dd & 0x1f) - 1;
230 
231 	/* Full leap-year cycles */
232 	day += LYC * ((dd >> 11) & 0x1f);
233 
234 	/* Month offset from leap-year cycle */
235 	day += daytab[(dd >> 5) & 0x3f];
236 
237 	/* 2100 is not a leap year */
238 	if (day >= ((2100 - 1980) / 4 * LYC + FEB))
239 		day--;
240 
241 	/* Align with time_t epoch */
242 	day += T1980;
243 
244 	tsp->tv_sec += (time_t) DAY * day;
245 	if (!utc)
246 		tsp->tv_sec += utc_offset();
247 }
248 
249 #ifdef TEST_DRIVER
250 
251 #include <stdio.h>
252 #include <unistd.h>
253 #include <stdlib.h>
254 
255 int
256 main(int argc __unused, char **argv __unused)
257 {
258 	int i;
259 	struct timespec ts;
260 	struct tm tm;
261 	double a;
262 	uint16_t d, t;
263 	uint8_t p;
264 	char buf[100];
265 
266 	for (i = 0; i < 10000; i++) {
267 		do {
268 			/*
269 			 * 32-bits gets us to 2106-02-07 06:28:15, but we
270 			 * need to get to the end of 2107.  So, we generate
271 			 * a 36-bit second count to get us way past 2106.
272 			 */
273 			ts.tv_sec = ((time_t) arc4random() << 4) ^ arc4random();
274 		} while ((ts.tv_sec < T1980 * 86400) || (ts.tv_sec >= T2108 * 86400ull));
275 
276 		ts.tv_nsec = random() % 1000000000;
277 
278 		printf("%10jd.%03ld -- ", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
279 
280 		gmtime_r(&ts.tv_sec, &tm);
281 		strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm);
282 		printf("%s -- ", buf);
283 
284 		a = ts.tv_sec + ts.tv_nsec * 1e-9;
285 		d = t = p = 0;
286 		timespec2fattime(&ts, 1, &d, &t, &p);
287 		printf("%04x %04x %02x -- ", d, t, p);
288 		printf("%3d %02d %02d %02d %02d %02d -- ",
289 		    ((d >> 9)  & 0x7f) + 1980,
290 		    (d >> 5)  & 0x0f,
291 		    (d >> 0)  & 0x1f,
292 		    (t >> 11) & 0x1f,
293 		    (t >> 5)  & 0x3f,
294 		    ((t >> 0)  & 0x1f) * 2);
295 
296 		ts.tv_sec = ts.tv_nsec = 0;
297 		fattime2timespec(d, t, p, 1, &ts);
298 		printf("%10jd.%03ld == ", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
299 		gmtime_r(&ts.tv_sec, &tm);
300 		strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm);
301 		printf("%s -- ", buf);
302 		a -= ts.tv_sec + ts.tv_nsec * 1e-9;
303 		printf("%.3f", a);
304 		printf("\n");
305 	}
306 	return (0);
307 }
308 
309 #endif /* TEST_DRIVER */
310