1
/* gettime.c - Wrapper for time functions
2
* Copyright (C) 1998, 2002 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
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.
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.
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
24
#ifdef HAVE_LANGINFO_H
30
static unsigned long timewarp;
31
static enum { NORMAL = 0, FROZEN, FUTURE, PAST } timemode;
33
/* Wrapper for the time(3). We use this here so we can fake the time
38
time_t current = time (NULL);
39
if (timemode == NORMAL)
41
else if (timemode == FROZEN)
43
else if (timemode == FUTURE)
44
return current + timewarp;
46
return current - timewarp;
50
/* Return the current time (possibly faked) in ISO format. */
52
gnupg_get_isotime (gnupg_isotime_t timebuf)
54
time_t atime = gnupg_get_time ();
64
tp = gmtime_r (&atime, &tmbuf);
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);
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. */
81
gnupg_set_time (time_t newtime, int freeze)
83
time_t current = time (NULL);
85
if ( newtime == (time_t)-1 || current == newtime)
95
else if (newtime > current)
98
timewarp = newtime - current;
103
timewarp = current - newtime;
107
/* Returns true when we are in timewarp mode */
109
gnupg_faked_time_p (void)
115
/* This function is used by gpg because OpenPGP defines the timestamp
116
as an unsigned 32 bit value. */
118
make_timestamp (void)
120
time_t t = gnupg_get_time ();
123
log_fatal ("gnupg_get_time() failed\n");
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.
135
scan_isodatestr( const char *string )
137
int year, month, day;
142
if( strlen(string) != 10 || string[4] != '-' || string[7] != '-' )
144
for( i=0; i < 4; i++ )
145
if( !digitp (string+i) )
147
if( !digitp (string+5) || !digitp(string+6) )
149
if( !digitp(string+8) || !digitp(string+9) )
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 )
157
memset( &tmbuf, 0, sizeof tmbuf );
159
tmbuf.tm_mon = month-1;
160
tmbuf.tm_year = year - 1900;
162
stamp = mktime( &tmbuf );
163
if( stamp == (time_t)-1 )
170
add_days_to_timestamp( u32 stamp, u16 days )
172
return stamp + days*86400L;
177
* Return a string with a time value in the form: x Y, n D, n H
181
strtimevalue( u32 value )
183
static char buffer[30];
184
unsigned int years, days, hours, minutes;
187
minutes = value % 60;
195
sprintf(buffer,"%uy%ud%uh%um", years, days, hours, minutes );
199
return strchr( buffer, 'y' ) + 1;
200
return strchr( buffer, 'd' ) + 1;
205
* Note: this function returns GMT
208
strtimestamp( u32 stamp )
210
static char buffer[11+5];
212
time_t atime = stamp;
215
strcpy (buffer, "????" "-??" "-??");
218
tp = gmtime( &atime );
219
sprintf(buffer,"%04d-%02d-%02d",
220
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
226
* Note: this function returns local time
229
asctimestamp( u32 stamp )
231
static char buffer[50];
232
#if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
236
time_t atime = stamp;
239
strcpy (buffer, "????" "-??" "-??");
243
tp = localtime( &atime );
245
#if defined(HAVE_NL_LANGINFO)
246
mem2str( fmt, nl_langinfo(D_T_FMT), DIM(fmt)-3 );
247
if( strstr( fmt, "%Z" ) == NULL )
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);
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
257
strftime( buffer, DIM(buffer)-1, "%c %Z", tp );
259
buffer[DIM(buffer)-1] = 0;
261
mem2str( buffer, asctime(tp), DIM(buffer) );