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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #ifdef HAVE_LANGINFO_H
31 static unsigned long timewarp
;
32 static enum { NORMAL
= 0, FROZEN
, FUTURE
, PAST
} timemode
;
34 /* Wrapper for the time(3). We use this here so we can fake the time
39 time_t current
= time (NULL
);
40 if (timemode
== NORMAL
)
42 else if (timemode
== FROZEN
)
44 else if (timemode
== FUTURE
)
45 return current
+ timewarp
;
47 return current
- timewarp
;
51 /* Return the current time (possibly faked) in ISO format. */
53 gnupg_get_isotime (gnupg_isotime_t timebuf
)
55 time_t atime
= gnupg_get_time ();
65 tp
= gmtime_r (&atime
, &tmbuf
);
69 sprintf (timebuf
,"%04d%02d%02dT%02d%02d%02d",
70 1900 + tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
,
71 tp
->tm_hour
, tp
->tm_min
, tp
->tm_sec
);
76 /* set the time to NEWTIME so that gnupg_get_time returns a time
77 starting with this one. With FREEZE set to 1 the returned time
78 will never change. Just for completeness, a value of (time_t)-1
79 for NEWTIME gets you back to rality. Note that this is obviously
80 not thread-safe but this is not required. */
82 gnupg_set_time (time_t newtime
, int freeze
)
84 time_t current
= time (NULL
);
86 if ( newtime
== (time_t)-1 || current
== newtime
)
96 else if (newtime
> current
)
99 timewarp
= newtime
- current
;
104 timewarp
= current
- newtime
;
108 /* Returns true when we are in timewarp mode */
110 gnupg_faked_time_p (void)
116 /* This function is used by gpg because OpenPGP defines the timestamp
117 as an unsigned 32 bit value. */
119 make_timestamp (void)
121 time_t t
= gnupg_get_time ();
124 log_fatal ("gnupg_get_time() failed\n");
131 * Scan a date string and return a timestamp.
132 * The only supported format is "yyyy-mm-dd"
133 * Returns 0 for an invalid date.
136 scan_isodatestr( const char *string
)
138 int year
, month
, day
;
143 if( strlen(string
) != 10 || string
[4] != '-' || string
[7] != '-' )
145 for( i
=0; i
< 4; i
++ )
146 if( !digitp (string
+i
) )
148 if( !digitp (string
+5) || !digitp(string
+6) )
150 if( !digitp(string
+8) || !digitp(string
+9) )
153 month
= atoi(string
+5);
154 day
= atoi(string
+8);
155 /* some basic checks */
156 if( year
< 1970 || month
< 1 || month
> 12 || day
< 1 || day
> 31 )
158 memset( &tmbuf
, 0, sizeof tmbuf
);
160 tmbuf
.tm_mon
= month
-1;
161 tmbuf
.tm_year
= year
- 1900;
163 stamp
= mktime( &tmbuf
);
164 if( stamp
== (time_t)-1 )
171 add_days_to_timestamp( u32 stamp
, u16 days
)
173 return stamp
+ days
*86400L;
178 * Return a string with a time value in the form: x Y, n D, n H
182 strtimevalue( u32 value
)
184 static char buffer
[30];
185 unsigned int years
, days
, hours
, minutes
;
188 minutes
= value
% 60;
196 sprintf(buffer
,"%uy%ud%uh%um", years
, days
, hours
, minutes
);
200 return strchr( buffer
, 'y' ) + 1;
201 return strchr( buffer
, 'd' ) + 1;
206 * Note: this function returns GMT
209 strtimestamp( u32 stamp
)
211 static char buffer
[11+5];
213 time_t atime
= stamp
;
216 strcpy (buffer
, "????" "-??" "-??");
219 tp
= gmtime( &atime
);
220 sprintf(buffer
,"%04d-%02d-%02d",
221 1900+tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
);
228 * Note: this function returns GMT
231 isotimestamp (u32 stamp
)
233 static char buffer
[25+5];
235 time_t atime
= stamp
;
239 strcpy (buffer
, "????" "-??" "-??" " " "??" ":" "??" ":" "??");
243 tp
= gmtime ( &atime
);
244 sprintf (buffer
,"%04d-%02d-%02d %02d:%02d:%02d",
245 1900+tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
,
246 tp
->tm_hour
, tp
->tm_min
, tp
->tm_sec
);
253 * Note: this function returns local time
256 asctimestamp( u32 stamp
)
258 static char buffer
[50];
259 #if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
263 time_t atime
= stamp
;
266 strcpy (buffer
, "????" "-??" "-??");
270 tp
= localtime( &atime
);
272 #if defined(HAVE_NL_LANGINFO)
273 mem2str( fmt
, nl_langinfo(D_T_FMT
), DIM(fmt
)-3 );
274 if( strstr( fmt
, "%Z" ) == NULL
)
276 /* NOTE: gcc -Wformat-noliteral will complain here. I have
277 found no way to suppress this warning .*/
278 strftime (buffer
, DIM(buffer
)-1, fmt
, tp
);
280 /* FIXME: we should check whether the locale appends a " %Z"
281 * These locales from glibc don't put the " %Z":
282 * fi_FI hr_HR ja_JP lt_LT lv_LV POSIX ru_RU ru_SU sv_FI sv_SE zh_CN
284 strftime( buffer
, DIM(buffer
)-1, "%c %Z", tp
);
286 buffer
[DIM(buffer
)-1] = 0;
288 mem2str( buffer
, asctime(tp
), DIM(buffer
) );