1 | /* dostime.c - convert dos time to/from time_t. |
---|
2 | |
---|
3 | Copyright (C) 2002 Free Software Foundation |
---|
4 | |
---|
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. |
---|
9 | |
---|
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. |
---|
14 | |
---|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | */ |
---|
19 | |
---|
20 | #include <config.h> |
---|
21 | |
---|
22 | #include <time.h> |
---|
23 | |
---|
24 | #include "dostime.h" |
---|
25 | |
---|
26 | /* |
---|
27 | * The specification to which this was written. From Joe Buck. |
---|
28 | * The DOS format appears to have only 2 second resolution. It is an |
---|
29 | * unsigned long, and ORs together |
---|
30 | * |
---|
31 | * (year-1980)<<25 |
---|
32 | * month<<21 (month is tm_mon + 1, 1=Jan through 12=Dec) |
---|
33 | * day<<16 (day is tm_mday, 1-31) |
---|
34 | * hour<<11 (hour is tm_hour, 0-23) |
---|
35 | * min<<5 (min is tm_min, 0-59) |
---|
36 | * sec>>1 (sec is tm_sec, 0-59, that's right, we throw away the LSB) |
---|
37 | * |
---|
38 | * DOS uses local time, so the localtime() call is used to turn the time_t |
---|
39 | * into a struct tm. |
---|
40 | */ |
---|
41 | |
---|
42 | time_t |
---|
43 | dos2unixtime (unsigned long dostime) |
---|
44 | { |
---|
45 | struct tm ltime; |
---|
46 | time_t now = time (NULL); |
---|
47 | |
---|
48 | /* Call localtime to initialize timezone in TIME. */ |
---|
49 | ltime = *localtime (&now); |
---|
50 | |
---|
51 | ltime.tm_year = (dostime >> 25) + 80; |
---|
52 | ltime.tm_mon = ((dostime >> 21) & 0x0f) - 1; |
---|
53 | ltime.tm_mday = (dostime >> 16) & 0x1f; |
---|
54 | ltime.tm_hour = (dostime >> 11) & 0x0f; |
---|
55 | ltime.tm_min = (dostime >> 5) & 0x3f; |
---|
56 | ltime.tm_sec = (dostime & 0x1f) << 1; |
---|
57 | |
---|
58 | ltime.tm_wday = -1; |
---|
59 | ltime.tm_yday = -1; |
---|
60 | ltime.tm_isdst = -1; |
---|
61 | |
---|
62 | return mktime (<ime); |
---|
63 | } |
---|
64 | |
---|
65 | unsigned long |
---|
66 | unix2dostime (time_t *time) |
---|
67 | { |
---|
68 | struct tm *ltime = localtime (time); |
---|
69 | int year = ltime->tm_year - 80; |
---|
70 | if (year < 0) |
---|
71 | year = 0; |
---|
72 | |
---|
73 | return (year << 25 |
---|
74 | | (ltime->tm_mon + 1) << 21 |
---|
75 | | ltime->tm_mday << 16 |
---|
76 | | ltime->tm_hour << 11 |
---|
77 | | ltime->tm_min << 5 |
---|
78 | | ltime->tm_sec >> 1); |
---|
79 | } |
---|