| 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 <stddef.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "dostime.h"
|
|---|
| 27 |
|
|---|
| 28 | /*
|
|---|
| 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
|
|---|
| 32 | *
|
|---|
| 33 | * (year-1980)<<25
|
|---|
| 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)
|
|---|
| 39 | *
|
|---|
| 40 | * DOS uses local time, so the localtime() call is used to turn the time_t
|
|---|
| 41 | * into a struct tm.
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | time_t
|
|---|
| 45 | dos2unixtime (unsigned long dostime)
|
|---|
| 46 | {
|
|---|
| 47 | struct tm ltime;
|
|---|
| 48 | time_t now = time (NULL);
|
|---|
| 49 |
|
|---|
| 50 | /* Call localtime to initialize timezone in TIME. */
|
|---|
| 51 | ltime = *localtime (&now);
|
|---|
| 52 |
|
|---|
| 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;
|
|---|
| 59 |
|
|---|
| 60 | ltime.tm_wday = -1;
|
|---|
| 61 | ltime.tm_yday = -1;
|
|---|
| 62 | ltime.tm_isdst = -1;
|
|---|
| 63 |
|
|---|
| 64 | return mktime (<ime);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | unsigned long
|
|---|
| 68 | unix2dostime (time_t *time)
|
|---|
| 69 | {
|
|---|
| 70 | struct tm *ltime = localtime (time);
|
|---|
| 71 | int year = ltime->tm_year - 80;
|
|---|
| 72 | if (year < 0)
|
|---|
| 73 | year = 0;
|
|---|
| 74 |
|
|---|
| 75 | return (year << 25
|
|---|
| 76 | | (ltime->tm_mon + 1) << 21
|
|---|
| 77 | | ltime->tm_mday << 16
|
|---|
| 78 | | ltime->tm_hour << 11
|
|---|
| 79 | | ltime->tm_min << 5
|
|---|
| 80 | | ltime->tm_sec >> 1);
|
|---|
| 81 | }
|
|---|