1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/isofs/util.c
4  */
5 
6 #include <linux/time.h>
7 #include "isofs.h"
8 
9 /*
10  * We have to convert from a MM/DD/YY format to the Unix ctime format.
11  * We have to take into account leap years and all of that good stuff.
12  * Unfortunately, the kernel does not have the information on hand to
13  * take into account daylight savings time, but it shouldn't matter.
14  * The time stored should be localtime (with or without DST in effect),
15  * and the timezone offset should hold the offset required to get back
16  * to GMT.  Thus  we should always be correct.
17  */
18 
19 struct timespec64 iso_date(u8 *p, int flags)
20 {
21 	int year, month, day, hour, minute, second, tz;
22 	struct timespec64 ts;
23 
24 	if (flags & ISO_DATE_LONG_FORM) {
25 		year = (p[0] - '0') * 1000 +
26 		       (p[1] - '0') * 100 +
27 		       (p[2] - '0') * 10 +
28 		       (p[3] - '0') - 1900;
29 		month = ((p[4] - '0') * 10 + (p[5] - '0'));
30 		day = ((p[6] - '0') * 10 + (p[7] - '0'));
31 		hour = ((p[8] - '0') * 10 + (p[9] - '0'));
32 		minute = ((p[10] - '0') * 10 + (p[11] - '0'));
33 		second = ((p[12] - '0') * 10 + (p[13] - '0'));
34 		ts.tv_nsec = ((p[14] - '0') * 10 + (p[15] - '0')) * 10000000;
35 		tz = p[16];
36 	} else {
37 		year = p[0];
38 		month = p[1];
39 		day = p[2];
40 		hour = p[3];
41 		minute = p[4];
42 		second = p[5];
43 		ts.tv_nsec = 0;
44 		/* High sierra has no time zone */
45 		tz = flags & ISO_DATE_HIGH_SIERRA ? 0 : p[6];
46 	}
47 
48 	if (year < 0) {
49 		ts.tv_sec = 0;
50 	} else {
51 		ts.tv_sec = mktime64(year+1900, month, day, hour, minute, second);
52 
53 		/* sign extend */
54 		if (tz & 0x80)
55 			tz |= (-1 << 8);
56 
57 		/*
58 		 * The timezone offset is unreliable on some disks,
59 		 * so we make a sanity check.  In no case is it ever
60 		 * more than 13 hours from GMT, which is 52*15min.
61 		 * The time is always stored in localtime with the
62 		 * timezone offset being what get added to GMT to
63 		 * get to localtime.  Thus we need to subtract the offset
64 		 * to get to true GMT, which is what we store the time
65 		 * as internally.  On the local system, the user may set
66 		 * their timezone any way they wish, of course, so GMT
67 		 * gets converted back to localtime on the receiving
68 		 * system.
69 		 *
70 		 * NOTE: mkisofs in versions prior to mkisofs-1.10 had
71 		 * the sign wrong on the timezone offset.  This has now
72 		 * been corrected there too, but if you are getting screwy
73 		 * results this may be the explanation.  If enough people
74 		 * complain, a user configuration option could be added
75 		 * to add the timezone offset in with the wrong sign
76 		 * for 'compatibility' with older discs, but I cannot see how
77 		 * it will matter that much.
78 		 *
79 		 * Thanks to kuhlmav@elec.canterbury.ac.nz (Volker Kuhlmann)
80 		 * for pointing out the sign error.
81 		 */
82 		if (-52 <= tz && tz <= 52)
83 			ts.tv_sec -= tz * 15 * 60;
84 	}
85 	return ts;
86 }
87