1 /* dostime.c - convert dos time to/from time_t.
3 Copyright (C) 2002 Free Software Foundation
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 * The specification to which this was written. From Joe Buck.
30 * The DOS format appears to have only 2 second resolution. It is an
31 * unsigned long, and ORs together
34 * month<<21 (month is tm_mon + 1, 1=Jan through 12=Dec)
35 * day<<16 (day is tm_mday, 1-31)
36 * hour<<11 (hour is tm_hour, 0-23)
37 * min<<5 (min is tm_min, 0-59)
38 * sec>>1 (sec is tm_sec, 0-59, that's right, we throw away the LSB)
40 * DOS uses local time, so the localtime() call is used to turn the time_t
45 dos2unixtime (unsigned long dostime
)
48 time_t now
= time (NULL
);
50 /* Call localtime to initialize timezone in TIME. */
51 ltime
= *localtime (&now
);
53 ltime
.tm_year
= (dostime
>> 25) + 80;
54 ltime
.tm_mon
= ((dostime
>> 21) & 0x0f) - 1;
55 ltime
.tm_mday
= (dostime
>> 16) & 0x1f;
56 ltime
.tm_hour
= (dostime
>> 11) & 0x0f;
57 ltime
.tm_min
= (dostime
>> 5) & 0x3f;
58 ltime
.tm_sec
= (dostime
& 0x1f) << 1;
64 return mktime (<ime
);
68 unix2dostime (time_t *time
)
70 struct tm
*ltime
= localtime (time
);
71 int year
= ltime
->tm_year
- 80;
76 | (ltime
->tm_mon
+ 1) << 21
77 | ltime
->tm_mday
<< 16
78 | ltime
->tm_hour
<< 11
80 | ltime
->tm_sec
>> 1);