subr_fattime.c revision 163611
1163611Sphk/*-
2163611Sphk * Copyright (c) 2006 Poul-Henning Kamp
3163611Sphk * All rights reserved.
4163611Sphk *
5163611Sphk * Redistribution and use in source and binary forms, with or without
6163611Sphk * modification, are permitted provided that the following conditions
7163611Sphk * are met:
8163611Sphk * 1. Redistributions of source code must retain the above copyright
9163611Sphk *    notice, this list of conditions and the following disclaimer.
10163611Sphk * 2. Redistributions in binary form must reproduce the above copyright
11163611Sphk *    notice, this list of conditions and the following disclaimer in the
12163611Sphk *    documentation and/or other materials provided with the distribution.
13163611Sphk *
14163611Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15163611Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16163611Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17163611Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18163611Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19163611Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20163611Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21163611Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22163611Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23163611Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24163611Sphk * SUCH DAMAGE.
25163611Sphk *
26163611Sphk * $FreeBSD: head/sys/kern/subr_fattime.c 163611 2006-10-22 18:19:08Z phk $
27163611Sphk *
28163611Sphk * Convert MS-DOS FAT format timestamps to and from unix timespecs
29163611Sphk *
30163611Sphk * FAT filestamps originally consisted of two 16 bit integers, encoded like
31163611Sphk * this:
32163611Sphk *
33163611Sphk *	yyyyyyymmmmddddd (year - 1980, month, day)
34163611Sphk *
35163611Sphk *      hhhhhmmmmmmsssss (hour, minutes, seconds divided by two)
36163611Sphk *
37163611Sphk * Subsequently even Microsoft realized that files could be accessed in less
38163611Sphk * than two seconds and a byte was added containing:
39163611Sphk *
40163611Sphk *      sfffffff	 (second mod two, 100ths of second)
41163611Sphk *
42163611Sphk * FAT timestamps are in the local timezone, with no indication of which
43163611Sphk * timezone much less if daylight savings time applies.
44163611Sphk *
45163611Sphk * Later on again, in Windows NT, timestamps were defined relative to GMT.
46163611Sphk *
47163611Sphk * Purists will point out that UTC replaced GMT for such uses around
48163611Sphk * a century ago, already then.  Ironically "NT" was an abbreviation of
49163611Sphk * "New Technology".  Anyway...
50163611Sphk *
51163611Sphk * The functions below always assume UTC time, and the calling code
52163611Sphk * must apply the local timezone offset as appropriate.  Unless special
53163611Sphk * conditions apply, the utc_offset() function be used for this.
54163611Sphk *
55163611Sphk * The conversion functions below cut time into four-year leap-second
56163611Sphk * cycles rather than single years and uses table lookups inside those
57163611Sphk * cycles to get the months and years sorted out.
58163611Sphk *
59163611Sphk * Obviously we cannot calculate the correct table index going from
60163611Sphk * a posix seconds count to Y/M/D, but we can get pretty close by
61163611Sphk * dividing the daycount by 32 (giving a too low index), and then
62163611Sphk * adjusting upwards a couple of steps if necessary.
63163611Sphk *
64163611Sphk * FAT timestamps have 7 bits for the year and starts at 1980, so
65163611Sphk * they can represent up to 2107 which means that the non-leap-year
66163611Sphk * 2100 must be handled.
67163611Sphk *
68163611Sphk * XXX: As long as time_t is 32 bits this is not relevant or easily
69163611Sphk * XXX: testable.  Revisit when time_t grows bigger.
70163611Sphk * XXX: grepfodder: 64 bit time_t, y2100, y2.1k, 2100, leap year
71163611Sphk *
72163611Sphk */
73163611Sphk
74163611Sphk#include <sys/param.h>
75163611Sphk#include <sys/types.h>
76163611Sphk#include <sys/time.h>
77163611Sphk#include <sys/clock.h>
78163611Sphk
79163611Sphk#define DAY	(24 * 60 * 60)	/* Length of day in seconds */
80163611Sphk#define YEAR	365		/* Length of normal year */
81163611Sphk#define LYC	(4 * YEAR + 1)	/* Length of 4 year leap-year cycle */
82163611Sphk#define T1980	(10 * 365 + 2)	/* Days from 1970 to 1980 */
83163611Sphk
84163611Sphk/* End of month is N days from start of (normal) year */
85163611Sphk#define JAN	31
86163611Sphk#define FEB	(JAN + 28)
87163611Sphk#define MAR	(FEB + 31)
88163611Sphk#define APR	(MAR + 30)
89163611Sphk#define MAY	(APR + 31)
90163611Sphk#define JUN	(MAY + 30)
91163611Sphk#define JUL	(JUN + 31)
92163611Sphk#define AUG	(JUL + 31)
93163611Sphk#define SEP	(AUG + 30)
94163611Sphk#define OCT	(SEP + 31)
95163611Sphk#define NOV	(OCT + 30)
96163611Sphk#define DEC	(NOV + 31)
97163611Sphk
98163611Sphk/* Table of months in a 4 year leap-year cycle */
99163611Sphk
100163611Sphk#define ENC(y,m)	(((y) << 9) | ((m) << 5))
101163611Sphk
102163611Sphkstatic const struct {
103163611Sphk	uint16_t	days;	/* month start in days relative to cycle */
104163611Sphk	uint16_t	coded;	/* encoded year + month information */
105163611Sphk} mtab[48] = {
106163611Sphk	{   0 + 0 * YEAR,     ENC(0, 1)  },
107163611Sphk
108163611Sphk	{ JAN + 0 * YEAR,     ENC(0, 2)  }, { FEB + 0 * YEAR + 1, ENC(0, 3)  },
109163611Sphk	{ MAR + 0 * YEAR + 1, ENC(0, 4)  }, { APR + 0 * YEAR + 1, ENC(0, 5)  },
110163611Sphk	{ MAY + 0 * YEAR + 1, ENC(0, 6)  }, { JUN + 0 * YEAR + 1, ENC(0, 7)  },
111163611Sphk	{ JUL + 0 * YEAR + 1, ENC(0, 8)  }, { AUG + 0 * YEAR + 1, ENC(0, 9)  },
112163611Sphk	{ SEP + 0 * YEAR + 1, ENC(0, 10) }, { OCT + 0 * YEAR + 1, ENC(0, 11) },
113163611Sphk	{ NOV + 0 * YEAR + 1, ENC(0, 12) }, { DEC + 0 * YEAR + 1, ENC(1, 1)  },
114163611Sphk
115163611Sphk	{ JAN + 1 * YEAR + 1, ENC(1, 2)  }, { FEB + 1 * YEAR + 1, ENC(1, 3)  },
116163611Sphk	{ MAR + 1 * YEAR + 1, ENC(1, 4)  }, { APR + 1 * YEAR + 1, ENC(1, 5)  },
117163611Sphk	{ MAY + 1 * YEAR + 1, ENC(1, 6)  }, { JUN + 1 * YEAR + 1, ENC(1, 7)  },
118163611Sphk	{ JUL + 1 * YEAR + 1, ENC(1, 8)  }, { AUG + 1 * YEAR + 1, ENC(1, 9)  },
119163611Sphk	{ SEP + 1 * YEAR + 1, ENC(1, 10) }, { OCT + 1 * YEAR + 1, ENC(1, 11) },
120163611Sphk	{ NOV + 1 * YEAR + 1, ENC(1, 12) }, { DEC + 1 * YEAR + 1, ENC(2, 1)  },
121163611Sphk
122163611Sphk	{ JAN + 2 * YEAR + 1, ENC(2, 2)  }, { FEB + 2 * YEAR + 1, ENC(2, 3)  },
123163611Sphk	{ MAR + 2 * YEAR + 1, ENC(2, 4)  }, { APR + 2 * YEAR + 1, ENC(2, 5)  },
124163611Sphk	{ MAY + 2 * YEAR + 1, ENC(2, 6)  }, { JUN + 2 * YEAR + 1, ENC(2, 7)  },
125163611Sphk	{ JUL + 2 * YEAR + 1, ENC(2, 8)  }, { AUG + 2 * YEAR + 1, ENC(2, 9)  },
126163611Sphk	{ SEP + 2 * YEAR + 1, ENC(2, 10) }, { OCT + 2 * YEAR + 1, ENC(2, 11) },
127163611Sphk	{ NOV + 2 * YEAR + 1, ENC(2, 12) }, { DEC + 2 * YEAR + 1, ENC(3, 1)  },
128163611Sphk
129163611Sphk	{ JAN + 3 * YEAR + 1, ENC(3, 2)  }, { FEB + 3 * YEAR + 1, ENC(3, 3)  },
130163611Sphk	{ MAR + 3 * YEAR + 1, ENC(3, 4)  }, { APR + 3 * YEAR + 1, ENC(3, 5)  },
131163611Sphk	{ MAY + 3 * YEAR + 1, ENC(3, 6)  }, { JUN + 3 * YEAR + 1, ENC(3, 7)  },
132163611Sphk	{ JUL + 3 * YEAR + 1, ENC(3, 8)  }, { AUG + 3 * YEAR + 1, ENC(3, 9)  },
133163611Sphk	{ SEP + 3 * YEAR + 1, ENC(3, 10) }, { OCT + 3 * YEAR + 1, ENC(3, 11) },
134163611Sphk	{ NOV + 3 * YEAR + 1, ENC(3, 12) }
135163611Sphk};
136163611Sphk
137163611Sphk
138163611Sphkvoid
139163611Sphktimet2fattime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
140163611Sphk{
141163611Sphk	time_t t1;
142163611Sphk	unsigned t2, l, m;
143163611Sphk
144163611Sphk	t1 = tsp->tv_sec;
145163611Sphk
146163611Sphk	if (dhp != NULL)
147163611Sphk		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
148163611Sphk	if (dtp != NULL) {
149163611Sphk		*dtp = (t1 / 2) % 30;
150163611Sphk		*dtp |= ((t1 / 60) % 60) << 5;
151163611Sphk		*dtp |= ((t1 / 3600) % 24) << 11;
152163611Sphk	}
153163611Sphk	if (ddp != NULL) {
154163611Sphk		t2 = t1 / DAY;
155163611Sphk		if (t2 < T1980) {
156163611Sphk			/* Impossible date, truncate to 1980-01-01 */
157163611Sphk			*ddp = 0x0021;
158163611Sphk		} else {
159163611Sphk			t2 -= T1980;
160163611Sphk
161163611Sphk			/*
162163611Sphk			 * 2100 is not a leap year.
163163611Sphk			 * XXX: a 32 bit time_t can not get us here.
164163611Sphk			 */
165163611Sphk			if (t2 >= ((2100 - 1980) / 4 * LYC + FEB))
166163611Sphk				t2++;
167163611Sphk
168163611Sphk			/* Account for full leapyear cycles */
169163611Sphk			l = t2 / LYC;
170163611Sphk			*ddp = (l * 4) << 9;
171163611Sphk			t2 -= l * LYC;
172163611Sphk
173163611Sphk			/* Find approximate table entry */
174163611Sphk			m = t2 / 32;
175163611Sphk
176163611Sphk			/* Find correct table entry */
177163611Sphk			while (m < 47 && mtab[m + 1].days <= t2)
178163611Sphk				m++;
179163611Sphk
180163611Sphk			/* Get year + month from the table */
181163611Sphk			*ddp += mtab[m].coded;
182163611Sphk
183163611Sphk			/* And apply the day in the month */
184163611Sphk			t2 -= mtab[m].days - 1;
185163611Sphk			*ddp |= t2;
186163611Sphk		}
187163611Sphk	}
188163611Sphk}
189163611Sphk
190163611Sphk/*
191163611Sphk * Table indexed by the bottom two bits of year + four bits of the month
192163611Sphk * from the FAT timestamp, returning number of days into 4 year long
193163611Sphk * leap-year cycle
194163611Sphk */
195163611Sphk
196163611Sphk#define DCOD(m, y, l)	((m) + YEAR * (y) + (l))
197163611Sphkstatic const uint16_t daytab[64] = {
198163611Sphk	0, 		 DCOD(  0, 0, 0), DCOD(JAN, 0, 0), DCOD(FEB, 0, 1),
199163611Sphk	DCOD(MAR, 0, 1), DCOD(APR, 0, 1), DCOD(MAY, 0, 1), DCOD(JUN, 0, 1),
200163611Sphk	DCOD(JUL, 0, 1), DCOD(AUG, 0, 1), DCOD(SEP, 0, 1), DCOD(OCT, 0, 1),
201163611Sphk	DCOD(NOV, 0, 1), DCOD(DEC, 0, 1), 0,               0,
202163611Sphk	0, 		 DCOD(  0, 1, 1), DCOD(JAN, 1, 1), DCOD(FEB, 1, 1),
203163611Sphk	DCOD(MAR, 1, 1), DCOD(APR, 1, 1), DCOD(MAY, 1, 1), DCOD(JUN, 1, 1),
204163611Sphk	DCOD(JUL, 1, 1), DCOD(AUG, 1, 1), DCOD(SEP, 1, 1), DCOD(OCT, 1, 1),
205163611Sphk	DCOD(NOV, 1, 1), DCOD(DEC, 1, 1), 0,               0,
206163611Sphk	0,		 DCOD(  0, 2, 1), DCOD(JAN, 2, 1), DCOD(FEB, 2, 1),
207163611Sphk	DCOD(MAR, 2, 1), DCOD(APR, 2, 1), DCOD(MAY, 2, 1), DCOD(JUN, 2, 1),
208163611Sphk	DCOD(JUL, 2, 1), DCOD(AUG, 2, 1), DCOD(SEP, 2, 1), DCOD(OCT, 2, 1),
209163611Sphk	DCOD(NOV, 2, 1), DCOD(DEC, 2, 1), 0,               0,
210163611Sphk	0,		 DCOD(  0, 3, 1), DCOD(JAN, 3, 1), DCOD(FEB, 3, 1),
211163611Sphk	DCOD(MAR, 3, 1), DCOD(APR, 3, 1), DCOD(MAY, 3, 1), DCOD(JUN, 3, 1),
212163611Sphk	DCOD(JUL, 3, 1), DCOD(AUG, 3, 1), DCOD(SEP, 3, 1), DCOD(OCT, 3, 1),
213163611Sphk	DCOD(NOV, 3, 1), DCOD(DEC, 3, 1), 0,               0
214163611Sphk};
215163611Sphk
216163611Sphkvoid
217163611Sphkfattime2timet(unsigned dd, unsigned dt, unsigned dh, struct timespec *tsp)
218163611Sphk{
219163611Sphk	unsigned day;
220163611Sphk
221163611Sphk	/* Unpack time fields */
222163611Sphk	tsp->tv_sec = (dt & 0x1f) << 1;
223163611Sphk	tsp->tv_sec += ((dt & 0x7e0) >> 5) * 60;
224163611Sphk	tsp->tv_sec += ((dt & 0xf800) >> 11) * 3600;
225163611Sphk	tsp->tv_sec += dh / 100;
226163611Sphk	tsp->tv_nsec = (dh % 100) * 10000000;
227163611Sphk
228163611Sphk	/* Day of month */
229163611Sphk	day = (dd & 0x1f) - 1;
230163611Sphk
231163611Sphk	/* Full leap-year cycles */
232163611Sphk	day += LYC * ((dd >> 11) & 0x1f);
233163611Sphk
234163611Sphk	/* Month offset from leap-year cycle */
235163611Sphk	day += daytab[(dd >> 5) & 0x3f];
236163611Sphk
237163611Sphk	/*
238163611Sphk	 * 2100 is not a leap year.
239163611Sphk	 * XXX: a 32 bit time_t can not get us here.
240163611Sphk	 */
241163611Sphk	if (day >= ((2100 - 1980) / 4 * LYC + FEB))
242163611Sphk		day--;
243163611Sphk
244163611Sphk	/* Align with time_t epoch */
245163611Sphk	day += T1980;
246163611Sphk
247163611Sphk	tsp->tv_sec += DAY * day;
248163611Sphk}
249163611Sphk
250163611Sphk#ifdef TEST_DRIVER
251163611Sphk
252163611Sphk#include <stdio.h>
253163611Sphk#include <unistd.h>
254163611Sphk#include <stdlib.h>
255163611Sphk
256163611Sphkint
257163611Sphkmain(int argc __unused, char **argv __unused)
258163611Sphk{
259163611Sphk	int i;
260163611Sphk	struct timespec ts;
261163611Sphk	struct tm tm;
262163611Sphk	double a;
263163611Sphk	u_int16_t d, t;
264163611Sphk	u_int8_t p;
265163611Sphk	char buf[100];
266163611Sphk
267163611Sphk	for (i = 0; i < 10000; i++) {
268163611Sphk		do {
269163611Sphk			ts.tv_sec = random();
270163611Sphk		} while (ts.tv_sec < T1980 * 86400);
271163611Sphk		ts.tv_nsec = random() % 1000000000;
272163611Sphk
273163611Sphk		printf("%10d.%03ld -- ", ts.tv_sec, ts.tv_nsec / 1000000);
274163611Sphk
275163611Sphk		gmtime_r(&ts.tv_sec, &tm);
276163611Sphk		strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm);
277163611Sphk		printf("%s -- ", buf);
278163611Sphk
279163611Sphk		a = ts.tv_sec + ts.tv_nsec * 1e-9;
280163611Sphk		d = t = p = 0;
281163611Sphk		timet2fattime(&ts, &d, &t, &p);
282163611Sphk		printf("%04x %04x %02x -- ", d, t, p);
283163611Sphk		printf("%3d %02d %02d %02d %02d %02d -- ",
284163611Sphk		    ((d >> 9)  & 0x7f) + 1980,
285163611Sphk		    (d >> 5)  & 0x0f,
286163611Sphk		    (d >> 0)  & 0x1f,
287163611Sphk		    (t >> 11) & 0x1f,
288163611Sphk		    (t >> 5)  & 0x3f,
289163611Sphk		    ((t >> 0)  & 0x1f) * 2);
290163611Sphk
291163611Sphk		ts.tv_sec = ts.tv_nsec = 0;
292163611Sphk		fattime2timet(d, t, p, &ts);
293163611Sphk		printf("%10d.%03ld == ", ts.tv_sec, ts.tv_nsec / 1000000);
294163611Sphk		gmtime_r(&ts.tv_sec, &tm);
295163611Sphk		strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm);
296163611Sphk		printf("%s -- ", buf);
297163611Sphk		a -= ts.tv_sec + ts.tv_nsec * 1e-9;
298163611Sphk		printf("%.3f", a);
299163611Sphk		printf("\n");
300163611Sphk	}
301163611Sphk	return (0);
302163611Sphk}
303163611Sphk
304163611Sphk#endif /* TEST_DRIVER */
305