~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to common/gettime.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gettime.c - Wrapper for time functions
 
2
 *      Copyright (C) 1998, 2002 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdlib.h>
 
23
#include <time.h>
 
24
#ifdef HAVE_LANGINFO_H
 
25
#include <langinfo.h>
 
26
#endif
 
27
 
 
28
#include "util.h"
 
29
 
 
30
static unsigned long timewarp;
 
31
static enum { NORMAL = 0, FROZEN, FUTURE, PAST } timemode;
 
32
 
 
33
/* Wrapper for the time(3).  We use this here so we can fake the time
 
34
   for tests */
 
35
time_t 
 
36
gnupg_get_time () 
 
37
{
 
38
  time_t current = time (NULL);
 
39
  if (timemode == NORMAL)
 
40
    return current;
 
41
  else if (timemode == FROZEN)
 
42
    return timewarp;
 
43
  else if (timemode == FUTURE)
 
44
    return current + timewarp;
 
45
  else
 
46
    return current - timewarp;
 
47
}
 
48
 
 
49
 
 
50
/* Return the current time (possibly faked) in ISO format. */
 
51
void
 
52
gnupg_get_isotime (gnupg_isotime_t timebuf)
 
53
{
 
54
  time_t atime = gnupg_get_time ();
 
55
    
 
56
  if (atime < 0)
 
57
    *timebuf = 0;
 
58
  else 
 
59
    {
 
60
      struct tm *tp;
 
61
#ifdef HAVE_GMTIME_R
 
62
      struct tm tmbuf;
 
63
      
 
64
      tp = gmtime_r (&atime, &tmbuf);
 
65
#else
 
66
      tp = gmtime (&atime);
 
67
#endif
 
68
      sprintf (timebuf,"%04d%02d%02dT%02d%02d%02d",
 
69
               1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
 
70
               tp->tm_hour, tp->tm_min, tp->tm_sec);
 
71
    }
 
72
}
 
73
 
 
74
 
 
75
/* set the time to NEWTIME so that gnupg_get_time returns a time
 
76
   starting with this one.  With FREEZE set to 1 the returned time
 
77
   will never change.  Just for completeness, a value of (time_t)-1
 
78
   for NEWTIME gets you back to rality. Note that this is obviously
 
79
   not thread-safe but this is not required. */
 
80
void
 
81
gnupg_set_time (time_t newtime, int freeze)
 
82
{
 
83
  time_t current = time (NULL);
 
84
 
 
85
  if ( newtime == (time_t)-1 || current == newtime)
 
86
    {
 
87
      timemode = NORMAL;
 
88
      timewarp = 0;
 
89
    }
 
90
  else if (freeze)
 
91
    {
 
92
      timemode = FROZEN;
 
93
      timewarp = current;
 
94
    }
 
95
  else if (newtime > current)
 
96
    {
 
97
      timemode = FUTURE;
 
98
      timewarp = newtime - current;
 
99
    }
 
100
  else
 
101
    {
 
102
      timemode = PAST;
 
103
      timewarp = current - newtime;
 
104
    }
 
105
}
 
106
 
 
107
/* Returns true when we are in timewarp mode */
 
108
int
 
109
gnupg_faked_time_p (void)
 
110
{
 
111
  return timemode;
 
112
}
 
113
 
 
114
 
 
115
/* This function is used by gpg because OpenPGP defines the timestamp
 
116
   as an unsigned 32 bit value. */
 
117
u32 
 
118
make_timestamp (void)
 
119
{
 
120
  time_t t = gnupg_get_time ();
 
121
 
 
122
  if (t == (time_t)-1)
 
123
    log_fatal ("gnupg_get_time() failed\n");
 
124
  return (u32)t;
 
125
}
 
126
 
 
127
 
 
128
 
 
129
/****************
 
130
 * Scan a date string and return a timestamp.
 
131
 * The only supported format is "yyyy-mm-dd"
 
132
 * Returns 0 for an invalid date.
 
133
 */
 
134
u32
 
135
scan_isodatestr( const char *string )
 
136
{
 
137
    int year, month, day;
 
138
    struct tm tmbuf;
 
139
    time_t stamp;
 
140
    int i;
 
141
 
 
142
    if( strlen(string) != 10 || string[4] != '-' || string[7] != '-' )
 
143
        return 0;
 
144
    for( i=0; i < 4; i++ )
 
145
        if( !digitp (string+i) )
 
146
            return 0;
 
147
    if( !digitp (string+5) || !digitp(string+6) )
 
148
        return 0;
 
149
    if( !digitp(string+8) || !digitp(string+9) )
 
150
        return 0;
 
151
    year = atoi(string);
 
152
    month = atoi(string+5);
 
153
    day = atoi(string+8);
 
154
    /* some basic checks */
 
155
    if( year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 )
 
156
        return 0;
 
157
    memset( &tmbuf, 0, sizeof tmbuf );
 
158
    tmbuf.tm_mday = day;
 
159
    tmbuf.tm_mon = month-1;
 
160
    tmbuf.tm_year = year - 1900;
 
161
    tmbuf.tm_isdst = -1;
 
162
    stamp = mktime( &tmbuf );
 
163
    if( stamp == (time_t)-1 )
 
164
        return 0;
 
165
    return stamp;
 
166
}
 
167
 
 
168
 
 
169
u32
 
170
add_days_to_timestamp( u32 stamp, u16 days )
 
171
{
 
172
    return stamp + days*86400L;
 
173
}
 
174
 
 
175
 
 
176
/****************
 
177
 * Return a string with a time value in the form: x Y, n D, n H
 
178
 */
 
179
 
 
180
const char *
 
181
strtimevalue( u32 value )
 
182
{
 
183
    static char buffer[30];
 
184
    unsigned int years, days, hours, minutes;
 
185
 
 
186
    value /= 60;
 
187
    minutes = value % 60;
 
188
    value /= 60;
 
189
    hours = value % 24;
 
190
    value /= 24;
 
191
    days = value % 365;
 
192
    value /= 365;
 
193
    years = value;
 
194
 
 
195
    sprintf(buffer,"%uy%ud%uh%um", years, days, hours, minutes );
 
196
    if( years )
 
197
        return buffer;
 
198
    if( days )
 
199
        return strchr( buffer, 'y' ) + 1;
 
200
    return strchr( buffer, 'd' ) + 1;
 
201
}
 
202
 
 
203
 
 
204
/****************
 
205
 * Note: this function returns GMT
 
206
 */
 
207
const char *
 
208
strtimestamp( u32 stamp )
 
209
{
 
210
    static char buffer[11+5];
 
211
    struct tm *tp;
 
212
    time_t atime = stamp;
 
213
    
 
214
    if (atime < 0) {
 
215
        strcpy (buffer, "????" "-??" "-??");
 
216
    }
 
217
    else {
 
218
        tp = gmtime( &atime );
 
219
        sprintf(buffer,"%04d-%02d-%02d",
 
220
                1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
 
221
    }
 
222
    return buffer;
 
223
}
 
224
 
 
225
/****************
 
226
 * Note: this function returns local time
 
227
 */
 
228
const char *
 
229
asctimestamp( u32 stamp )
 
230
{
 
231
    static char buffer[50];
 
232
#if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
 
233
      static char fmt[50];
 
234
#endif
 
235
    struct tm *tp;
 
236
    time_t atime = stamp;
 
237
 
 
238
    if (atime < 0) {
 
239
        strcpy (buffer, "????" "-??" "-??");
 
240
        return buffer;
 
241
    }
 
242
 
 
243
    tp = localtime( &atime );
 
244
#ifdef HAVE_STRFTIME
 
245
#if defined(HAVE_NL_LANGINFO)
 
246
      mem2str( fmt, nl_langinfo(D_T_FMT), DIM(fmt)-3 );
 
247
      if( strstr( fmt, "%Z" ) == NULL )
 
248
        strcat( fmt, " %Z");
 
249
      /* NOTE: gcc -Wformat-noliteral will complain here.  I have
 
250
         found no way to suppress this warning .*/
 
251
      strftime (buffer, DIM(buffer)-1, fmt, tp);
 
252
#else
 
253
      /* FIXME: we should check whether the locale appends a " %Z"
 
254
       * These locales from glibc don't put the " %Z":
 
255
       * fi_FI hr_HR ja_JP lt_LT lv_LV POSIX ru_RU ru_SU sv_FI sv_SE zh_CN
 
256
       */
 
257
      strftime( buffer, DIM(buffer)-1, "%c %Z", tp );
 
258
#endif
 
259
    buffer[DIM(buffer)-1] = 0;
 
260
#else
 
261
    mem2str( buffer, asctime(tp), DIM(buffer) );
 
262
#endif
 
263
    return buffer;
 
264
}
 
265
 
 
266
 
 
267
 
 
268
 
 
269
 
 
270
 
 
271
 
 
272
 
 
273
 
 
274
 
 
275
 
 
276
 
 
277
 
 
278