2009-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / gettime.c
blob1b4e435a1eb15b620e11a33b04141edd5f2d42fb
1 /* gettime.c - Wrapper for time functions
2 * Copyright (C) 1998, 2002, 2007 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 3 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <ctype.h>
24 #ifdef HAVE_LANGINFO_H
25 #include <langinfo.h>
26 #endif
28 #include "util.h"
29 #include "i18n.h"
31 static unsigned long timewarp;
32 static enum { NORMAL = 0, FROZEN, FUTURE, PAST } timemode;
34 /* Correction used to map to real Julian days. */
35 #define JD_DIFF 1721060L
38 /* Wrapper for the time(3). We use this here so we can fake the time
39 for tests */
40 time_t
41 gnupg_get_time ()
43 time_t current = time (NULL);
44 if (timemode == NORMAL)
45 return current;
46 else if (timemode == FROZEN)
47 return timewarp;
48 else if (timemode == FUTURE)
49 return current + timewarp;
50 else
51 return current - timewarp;
55 /* Return the current time (possibly faked) in ISO format. */
56 void
57 gnupg_get_isotime (gnupg_isotime_t timebuf)
59 time_t atime = gnupg_get_time ();
61 if (atime < 0)
62 *timebuf = 0;
63 else
65 struct tm *tp;
66 #ifdef HAVE_GMTIME_R
67 struct tm tmbuf;
69 tp = gmtime_r (&atime, &tmbuf);
70 #else
71 tp = gmtime (&atime);
72 #endif
73 snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
74 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
75 tp->tm_hour, tp->tm_min, tp->tm_sec);
80 /* Set the time to NEWTIME so that gnupg_get_time returns a time
81 starting with this one. With FREEZE set to 1 the returned time
82 will never change. Just for completeness, a value of (time_t)-1
83 for NEWTIME gets you back to reality. Note that this is obviously
84 not thread-safe but this is not required. */
85 void
86 gnupg_set_time (time_t newtime, int freeze)
88 time_t current = time (NULL);
90 if ( newtime == (time_t)-1 || current == newtime)
92 timemode = NORMAL;
93 timewarp = 0;
95 else if (freeze)
97 timemode = FROZEN;
98 timewarp = current;
100 else if (newtime > current)
102 timemode = FUTURE;
103 timewarp = newtime - current;
105 else
107 timemode = PAST;
108 timewarp = current - newtime;
112 /* Returns true when we are in timewarp mode */
114 gnupg_faked_time_p (void)
116 return timemode;
120 /* This function is used by gpg because OpenPGP defines the timestamp
121 as an unsigned 32 bit value. */
122 u32
123 make_timestamp (void)
125 time_t t = gnupg_get_time ();
127 if (t == (time_t)-1)
128 log_fatal ("gnupg_get_time() failed\n");
129 return (u32)t;
134 /****************
135 * Scan a date string and return a timestamp.
136 * The only supported format is "yyyy-mm-dd"
137 * Returns 0 for an invalid date.
140 scan_isodatestr( const char *string )
142 int year, month, day;
143 struct tm tmbuf;
144 time_t stamp;
145 int i;
147 if( strlen(string) != 10 || string[4] != '-' || string[7] != '-' )
148 return 0;
149 for( i=0; i < 4; i++ )
150 if( !digitp (string+i) )
151 return 0;
152 if( !digitp (string+5) || !digitp(string+6) )
153 return 0;
154 if( !digitp(string+8) || !digitp(string+9) )
155 return 0;
156 year = atoi(string);
157 month = atoi(string+5);
158 day = atoi(string+8);
159 /* some basic checks */
160 if( year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 )
161 return 0;
162 memset( &tmbuf, 0, sizeof tmbuf );
163 tmbuf.tm_mday = day;
164 tmbuf.tm_mon = month-1;
165 tmbuf.tm_year = year - 1900;
166 tmbuf.tm_isdst = -1;
167 stamp = mktime( &tmbuf );
168 if( stamp == (time_t)-1 )
169 return 0;
170 return stamp;
173 /* Scan am ISO timestamp and return an Epoch based timestamp. The only
174 supported format is "yyyymmddThhmmss" delimited by white space, nul, a
175 colon or a comma. Returns (time_t)(-1) for an invalid string. */
176 time_t
177 isotime2epoch (const char *string)
179 const char *s;
180 int year, month, day, hour, minu, sec;
181 struct tm tmbuf;
182 int i;
184 if (!*string)
185 return (time_t)(-1);
186 for (s=string, i=0; i < 8; i++, s++)
187 if (!digitp (s))
188 return (time_t)(-1);
189 if (*s != 'T')
190 return (time_t)(-1);
191 for (s++, i=9; i < 15; i++, s++)
192 if (!digitp (s))
193 return (time_t)(-1);
194 if ( !(!*s || (isascii (*s) && isspace(*s)) || *s == ':' || *s == ','))
195 return (time_t)(-1); /* Wrong delimiter. */
197 year = atoi_4 (string);
198 month = atoi_2 (string + 4);
199 day = atoi_2 (string + 6);
200 hour = atoi_2 (string + 9);
201 minu = atoi_2 (string + 11);
202 sec = atoi_2 (string + 13);
204 /* Basic checks. */
205 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31
206 || hour > 23 || minu > 59 || sec > 61 )
207 return (time_t)(-1);
209 memset (&tmbuf, 0, sizeof tmbuf);
210 tmbuf.tm_sec = sec;
211 tmbuf.tm_min = minu;
212 tmbuf.tm_hour = hour;
213 tmbuf.tm_mday = day;
214 tmbuf.tm_mon = month-1;
215 tmbuf.tm_year = year - 1900;
216 tmbuf.tm_isdst = -1;
217 return timegm (&tmbuf);
221 /* Convert an Epoch time to an iso time stamp. */
222 void
223 epoch2isotime (gnupg_isotime_t timebuf, time_t atime)
225 if (atime < 0)
226 *timebuf = 0;
227 else
229 struct tm *tp;
230 #ifdef HAVE_GMTIME_R
231 struct tm tmbuf;
233 tp = gmtime_r (&atime, &tmbuf);
234 #else
235 tp = gmtime (&atime);
236 #endif
237 snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
238 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
239 tp->tm_hour, tp->tm_min, tp->tm_sec);
247 add_days_to_timestamp( u32 stamp, u16 days )
249 return stamp + days*86400L;
253 /****************
254 * Return a string with a time value in the form: x Y, n D, n H
257 const char *
258 strtimevalue( u32 value )
260 static char buffer[30];
261 unsigned int years, days, hours, minutes;
263 value /= 60;
264 minutes = value % 60;
265 value /= 60;
266 hours = value % 24;
267 value /= 24;
268 days = value % 365;
269 value /= 365;
270 years = value;
272 sprintf(buffer,"%uy%ud%uh%um", years, days, hours, minutes );
273 if( years )
274 return buffer;
275 if( days )
276 return strchr( buffer, 'y' ) + 1;
277 return strchr( buffer, 'd' ) + 1;
282 * Note: this function returns GMT
284 const char *
285 strtimestamp( u32 stamp )
287 static char buffer[11+5];
288 struct tm *tp;
289 time_t atime = stamp;
291 if (atime < 0) {
292 strcpy (buffer, "????" "-??" "-??");
294 else {
295 tp = gmtime( &atime );
296 sprintf(buffer,"%04d-%02d-%02d",
297 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
299 return buffer;
304 * Note: this function returns GMT
306 const char *
307 isotimestamp (u32 stamp)
309 static char buffer[25+5];
310 struct tm *tp;
311 time_t atime = stamp;
313 if (atime < 0)
315 strcpy (buffer, "????" "-??" "-??" " " "??" ":" "??" ":" "??");
317 else
319 tp = gmtime ( &atime );
320 sprintf (buffer,"%04d-%02d-%02d %02d:%02d:%02d",
321 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
322 tp->tm_hour, tp->tm_min, tp->tm_sec);
324 return buffer;
328 /****************
329 * Note: this function returns local time
331 const char *
332 asctimestamp( u32 stamp )
334 static char buffer[50];
335 #if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
336 static char fmt[50];
337 #endif
338 struct tm *tp;
339 time_t atime = stamp;
341 if (atime < 0) {
342 strcpy (buffer, "????" "-??" "-??");
343 return buffer;
346 tp = localtime( &atime );
347 #ifdef HAVE_STRFTIME
348 #if defined(HAVE_NL_LANGINFO)
349 mem2str( fmt, nl_langinfo(D_T_FMT), DIM(fmt)-3 );
350 if( strstr( fmt, "%Z" ) == NULL )
351 strcat( fmt, " %Z");
352 /* NOTE: gcc -Wformat-noliteral will complain here. I have
353 found no way to suppress this warning .*/
354 strftime (buffer, DIM(buffer)-1, fmt, tp);
355 #else
356 /* FIXME: we should check whether the locale appends a " %Z"
357 * These locales from glibc don't put the " %Z":
358 * fi_FI hr_HR ja_JP lt_LT lv_LV POSIX ru_RU ru_SU sv_FI sv_SE zh_CN
360 strftime( buffer, DIM(buffer)-1, "%c %Z", tp );
361 #endif
362 buffer[DIM(buffer)-1] = 0;
363 #else
364 mem2str( buffer, asctime(tp), DIM(buffer) );
365 #endif
366 return buffer;
371 static int
372 days_per_year (int y)
374 int s ;
376 s = !(y % 4);
377 if ( !(y % 100))
378 if ((y%400))
379 s = 0;
380 return s ? 366 : 365;
383 static int
384 days_per_month (int y, int m)
386 int s;
388 switch(m)
390 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
391 return 31 ;
392 case 2:
393 s = !(y % 4);
394 if (!(y % 100))
395 if ((y % 400))
396 s = 0;
397 return s? 29 : 28 ;
398 case 4: case 6: case 9: case 11:
399 return 30;
401 BUG();
405 /* Convert YEAR, MONTH and DAY into the Julian date. We assume that
406 it is already noon. We do not support dates before 1582-10-15. */
407 static unsigned long
408 date2jd (int year, int month, int day)
410 unsigned long jd;
412 jd = 365L * year + 31 * (month-1) + day + JD_DIFF;
413 if (month < 3)
414 year-- ;
415 else
416 jd -= (4 * month + 23) / 10;
418 jd += year / 4 - ((year / 100 + 1) *3) / 4;
420 return jd ;
423 /* Convert a Julian date back to YEAR, MONTH and DAY. Return day of
424 the year or 0 on error. This function uses some more or less
425 arbitrary limits, most important is that days before 1582 are not
426 supported. */
427 static int
428 jd2date (unsigned long jd, int *year, int *month, int *day)
430 int y, m, d;
431 long delta;
433 if (!jd)
434 return 0 ;
435 if (jd < 1721425 || jd > 2843085)
436 return 0;
438 y = (jd - JD_DIFF) / 366;
439 d = m = 1;
441 while ((delta = jd - date2jd (y, m, d)) > days_per_year (y))
442 y++;
444 m = (delta / 31) + 1;
445 while( (delta = jd - date2jd (y, m, d)) > days_per_month (y,m))
446 if (++m > 12)
448 m = 1;
449 y++;
452 d = delta + 1 ;
453 if (d > days_per_month (y, m))
455 d = 1;
456 m++;
458 if (m > 12)
460 m = 1;
461 y++;
464 if (year)
465 *year = y;
466 if (month)
467 *month = m;
468 if (day)
469 *day = d ;
471 return (jd - date2jd (y, 1, 1)) + 1;
475 /* Check that the 15 bytes in ATIME represent a valid ISO time. Note
476 that this function does not expect a string but a plain 15 byte
477 isotime buffer. */
478 gpg_error_t
479 check_isotime (const gnupg_isotime_t atime)
481 int i;
482 const char *s;
484 if (!*atime)
485 return gpg_error (GPG_ERR_NO_VALUE);
487 for (s=atime, i=0; i < 8; i++, s++)
488 if (!digitp (s))
489 return gpg_error (GPG_ERR_INV_TIME);
490 if (*s != 'T')
491 return gpg_error (GPG_ERR_INV_TIME);
492 for (s++, i=9; i < 15; i++, s++)
493 if (!digitp (s))
494 return gpg_error (GPG_ERR_INV_TIME);
495 return 0;
499 void
500 dump_isotime (const gnupg_isotime_t t)
502 if (!t || !*t)
503 log_printf (_("[none]"));
504 else
505 log_printf ("%.4s-%.2s-%.2s %.2s:%.2s:%s",
506 t, t+4, t+6, t+9, t+11, t+13);
510 /* Add SECONDS to ATIME. SECONDS may not be negative and is limited
511 to about the equivalent of 62 years which should be more then
512 enough for our purposes. */
513 gpg_error_t
514 add_seconds_to_isotime (gnupg_isotime_t atime, int nseconds)
516 gpg_error_t err;
517 int year, month, day, hour, minute, sec, ndays;
518 unsigned long jd;
520 err = check_isotime (atime);
521 if (err)
522 return err;
524 if (nseconds < 0 || nseconds >= (0x7fffffff - 61) )
525 return gpg_error (GPG_ERR_INV_VALUE);
527 year = atoi_4 (atime+0);
528 month = atoi_2 (atime+4);
529 day = atoi_2 (atime+6);
530 hour = atoi_2 (atime+9);
531 minute= atoi_2 (atime+11);
532 sec = atoi_2 (atime+13);
534 if (year <= 1582) /* The julian date functions don't support this. */
535 return gpg_error (GPG_ERR_INV_VALUE);
537 sec += nseconds;
538 minute += sec/60;
539 sec %= 60;
540 hour += minute/60;
541 minute %= 60;
542 ndays = hour/24;
543 hour %= 24;
545 jd = date2jd (year, month, day) + ndays;
546 jd2date (jd, &year, &month, &day);
548 if (year > 9999 || month > 12 || day > 31
549 || year < 0 || month < 1 || day < 1)
550 return gpg_error (GPG_ERR_INV_VALUE);
552 snprintf (atime, 16, "%04d%02d%02dT%02d%02d%02d",
553 year, month, day, hour, minute, sec);
554 return 0;
558 gpg_error_t
559 add_days_to_isotime (gnupg_isotime_t atime, int ndays)
561 gpg_error_t err;
562 int year, month, day, hour, minute, sec;
563 unsigned long jd;
565 err = check_isotime (atime);
566 if (err)
567 return err;
569 if (ndays < 0 || ndays >= 9999*366 )
570 return gpg_error (GPG_ERR_INV_VALUE);
572 year = atoi_4 (atime+0);
573 month = atoi_2 (atime+4);
574 day = atoi_2 (atime+6);
575 hour = atoi_2 (atime+9);
576 minute= atoi_2 (atime+11);
577 sec = atoi_2 (atime+13);
579 if (year <= 1582) /* The julian date functions don't support this. */
580 return gpg_error (GPG_ERR_INV_VALUE);
582 jd = date2jd (year, month, day) + ndays;
583 jd2date (jd, &year, &month, &day);
585 if (year > 9999 || month > 12 || day > 31
586 || year < 0 || month < 1 || day < 1)
587 return gpg_error (GPG_ERR_INV_VALUE);
589 snprintf (atime, 16, "%04d%02d%02dT%02d%02d%02d",
590 year, month, day, hour, minute, sec);
591 return 0;