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