2004-03-12 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / gettime.c
blob93e4ba113e2d62faf2a506073df16abdbc80e314
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
21 #include <config.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #ifdef HAVE_LANGINFO_H
25 #include <langinfo.h>
26 #endif
28 #include "util.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
34 for tests */
35 time_t
36 gnupg_get_time ()
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;
50 /* Return the current time (possibly faked) in ISO format. */
51 void
52 gnupg_get_isotime (gnupg_isotime_t timebuf)
54 time_t atime = gnupg_get_time ();
56 if (atime < 0)
57 *timebuf = 0;
58 else
60 struct tm *tp;
61 #ifdef HAVE_GMTIME_R
62 struct tm tmbuf;
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);
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)
83 time_t current = time (NULL);
85 if ( newtime == (time_t)-1 || current == newtime)
87 timemode = NORMAL;
88 timewarp = 0;
90 else if (freeze)
92 timemode = FROZEN;
93 timewarp = current;
95 else if (newtime > current)
97 timemode = FUTURE;
98 timewarp = newtime - current;
100 else
102 timemode = PAST;
103 timewarp = current - newtime;
107 /* Returns true when we are in timewarp mode */
109 gnupg_faked_time_p (void)
111 return timemode;
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)
120 time_t t = gnupg_get_time ();
122 if (t == (time_t)-1)
123 log_fatal ("gnupg_get_time() failed\n");
124 return (u32)t;
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.
135 scan_isodatestr( const char *string )
137 int year, month, day;
138 struct tm tmbuf;
139 time_t stamp;
140 int i;
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;
170 add_days_to_timestamp( u32 stamp, u16 days )
172 return stamp + days*86400L;
176 /****************
177 * Return a string with a time value in the form: x Y, n D, n H
180 const char *
181 strtimevalue( u32 value )
183 static char buffer[30];
184 unsigned int years, days, hours, minutes;
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;
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;
204 /****************
205 * Note: this function returns GMT
207 const char *
208 strtimestamp( u32 stamp )
210 static char buffer[11+5];
211 struct tm *tp;
212 time_t atime = stamp;
214 if (atime < 0) {
215 strcpy (buffer, "????" "-??" "-??");
217 else {
218 tp = gmtime( &atime );
219 sprintf(buffer,"%04d-%02d-%02d",
220 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
222 return buffer;
225 /****************
226 * Note: this function returns local time
228 const char *
229 asctimestamp( u32 stamp )
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;
238 if (atime < 0) {
239 strcpy (buffer, "????" "-??" "-??");
240 return buffer;
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
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;