OSDN Git Service

2000-12-11 Alexandre Petit-Bianco <apbianco@cygnus.com>
[pf3gnuchains/gcc-fork.git] / fastjar / dostime.c
1 /*
2   dostime.c - routines for converting UNIX time to MS-DOS time.  
3
4   Borrowed from Info-zip's unzip
5
6   Copyright (C) 1999 Bryan Burns
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 */
22
23 /* $Id: dostime.c,v 1.1.1.1 1999/12/06 03:09:12 toast Exp $
24
25    $Log: dostime.c,v $
26    Revision 1.1.1.1  1999/12/06 03:09:12  toast
27    initial checkin..
28
29
30
31    Revision 1.6  1999/05/10 08:32:26  burnsbr
32    added dos2unixtime
33
34    Revision 1.5  1999/04/27 10:03:50  burnsbr
35    configure support
36
37    Revision 1.4  1999/04/26 21:55:19  burnsbr
38    switched from sys/time.h to time.h for better portability
39
40    Revision 1.3  1999/04/20 08:54:30  burnsbr
41    added GPL comment
42
43    Revision 1.2  1999/04/20 05:10:53  burnsbr
44    added RCS tags
45
46
47 */
48 #include "config.h"
49
50 #ifdef TM_IN_SYS_TIME
51 #include <sys/time.h>
52 #else
53 #include <time.h>
54 #endif
55
56 #include "dostime.h"
57
58 static char rcsid[] = "$Id: dostime.c,v 1.1.1.1 1999/12/06 03:09:12 toast Exp $";
59
60 /*
61
62  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
63  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
64  Permission is granted to any individual or institution to use, copy, or
65  redistribute this software so long as all of the original files are included,
66  that it is not sold for profit, and that this copyright notice is retained.
67
68 */
69
70
71 time_t dos2unixtime(dostime)
72      unsigned long dostime;            /* DOS time to convert */
73      /* Return the Unix time_t value (GMT/UTC time) for the DOS format (local)
74       * time dostime, where dostime is a four byte value (date in most
75       * significant word, time in least significant word), see dostime() 
76       * function.
77       */
78 {
79   struct tm *t;         /* argument for mktime() */
80   time_t clock = time(NULL);
81
82   t = localtime(&clock);
83   t->tm_isdst = -1;     /* let mktime() determine if DST is in effect */
84   /* Convert DOS time to UNIX time_t format */
85   t->tm_sec  = (((int)dostime) <<  1) & 0x3e;
86   t->tm_min  = (((int)dostime) >>  5) & 0x3f;
87   t->tm_hour = (((int)dostime) >> 11) & 0x1f;
88   t->tm_mday = (int)(dostime >> 16) & 0x1f;
89   t->tm_mon  = ((int)(dostime >> 21) & 0x0f) - 1;
90   t->tm_year = ((int)(dostime >> 25) & 0x7f) + 80;
91
92   return mktime(t);
93 }
94
95 unsigned long dostime(y, n, d, h, m, s)
96 int y;                  /* year */
97 int n;                  /* month */
98 int d;                  /* day */
99 int h;                  /* hour */
100 int m;                  /* minute */
101 int s;                  /* second */
102 /* Convert the date y/n/d and time h:m:s to a four byte DOS date and
103    time (date in high two bytes, time in low two bytes allowing magnitude
104    comparison). */
105 {
106   return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) :
107     (((unsigned long)y - 1980) << 25) | ((unsigned long)n << 21) | 
108     ((unsigned long)d << 16) | ((unsigned long)h << 11) | 
109     ((unsigned long)m << 5) | ((unsigned long)s >> 1);
110 }
111
112
113 unsigned long unix2dostime(t)
114 time_t *t;             /* unix time to convert */
115 /* Return the Unix time t in DOS format, rounded up to the next two
116    second boundary. */
117 {
118   time_t t_even;
119   struct tm *s;         /* result of localtime() */
120
121   t_even = (*t + 1) & (~1);     /* Round up to even seconds. */
122   s = localtime(&t_even);       /* Use local time since MSDOS does. */
123   return dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
124                  s->tm_hour, s->tm_min, s->tm_sec);
125 }
126