66
70
tp = gmtime (&atime);
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);
72
snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
73
1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
74
tp->tm_hour, tp->tm_min, tp->tm_sec);
75
/* set the time to NEWTIME so that gnupg_get_time returns a time
79
/* Set the time to NEWTIME so that gnupg_get_time returns a time
76
80
starting with this one. With FREEZE set to 1 the returned time
77
81
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
82
for NEWTIME gets you back to reality. Note that this is obviously
79
83
not thread-safe but this is not required. */
81
85
gnupg_set_time (time_t newtime, int freeze)
172
/* Scan am ISO timestamp and return an Epoch based timestamp. The only
173
supported format is "yyyymmddThhmmss" delimited by white space, nul, a
174
colon or a comma. Returns (time_t)(-1) for an invalid string. */
176
isotime2epoch (const char *string)
179
int year, month, day, hour, minu, sec;
185
for (s=string, i=0; i < 8; i++, s++)
190
for (s++, i=9; i < 15; i++, s++)
193
if ( !(!*s || (isascii (*s) && isspace(*s)) || *s == ':' || *s == ','))
194
return (time_t)(-1); /* Wrong delimiter. */
196
year = atoi_4 (string);
197
month = atoi_2 (string + 4);
198
day = atoi_2 (string + 6);
199
hour = atoi_2 (string + 9);
200
minu = atoi_2 (string + 11);
201
sec = atoi_2 (string + 13);
204
if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31
205
|| hour > 23 || minu > 59 || sec > 61 )
208
memset (&tmbuf, 0, sizeof tmbuf);
211
tmbuf.tm_hour = hour;
213
tmbuf.tm_mon = month-1;
214
tmbuf.tm_year = year - 1900;
216
return timegm (&tmbuf);
220
/* Convert an Epoch time to an iso time stamp. */
222
epoch2isotime (gnupg_isotime_t timebuf, time_t atime)
232
tp = gmtime_r (&atime, &tmbuf);
234
tp = gmtime (&atime);
236
snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
237
1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
238
tp->tm_hour, tp->tm_min, tp->tm_sec);
170
246
add_days_to_timestamp( u32 stamp, u16 days )
371
days_per_year (int y)
379
return s ? 366 : 365;
383
days_per_month (int y, int m)
389
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
397
case 4: case 6: case 9: case 11:
404
/* Convert YEAR, MONTH and DAY into the Julian date. We assume that
405
it is already noon; we dont; support dates before 1582-10-15. */
407
date2jd (int year, int month, int day)
411
jd = 365L * year + 31 * (month-1) + day + JD_DIFF;
415
jd -= (4 * month + 23) / 10;
417
jd += year / 4 - ((year / 100 + 1) *3) / 4;
422
/* Convert a Julian date back to YEAR, MONTH and DAY. Return day of
423
the year or 0 on error. This function uses some more or less
424
arbitrary limits, most important is that days before 1582 are not
427
jd2date (unsigned long jd, int *year, int *month, int *day)
434
if (jd < 1721425 || jd > 2843085)
437
y = (jd - JD_DIFF) / 366;
440
while ((delta = jd - date2jd (y, m, d)) > days_per_year (y))
443
m = (delta / 31) + 1;
444
while( (delta = jd - date2jd (y, m, d)) > days_per_month (y,m))
452
if (d > days_per_month (y, m))
470
return (jd - date2jd (y, 1, 1)) + 1;
474
/* Check that the 15 bytes in ATIME represent a valid ISO time. Note
475
that this function does not expect a string but a plain 15 byte
478
check_isotime (const gnupg_isotime_t atime)
484
return gpg_error (GPG_ERR_NO_VALUE);
486
for (s=atime, i=0; i < 8; i++, s++)
488
return gpg_error (GPG_ERR_INV_TIME);
490
return gpg_error (GPG_ERR_INV_TIME);
491
for (s++, i=9; i < 15; i++, s++)
493
return gpg_error (GPG_ERR_INV_TIME);
499
/* Add SECONDS to ATIME. SECONDS may not be negative and is limited
500
to about the equivalent of 62 years which should be more then
501
enough for our purposes. */
503
add_seconds_to_isotime (gnupg_isotime_t atime, int nseconds)
506
int year, month, day, hour, minute, sec, ndays;
509
err = check_isotime (atime);
513
if (nseconds < 0 || nseconds >= (0x7fffffff - 61) )
514
return gpg_error (GPG_ERR_INV_VALUE);
516
year = atoi_4 (atime+0);
517
month = atoi_2 (atime+4);
518
day = atoi_2 (atime+6);
519
hour = atoi_2 (atime+9);
520
minute= atoi_2 (atime+11);
521
sec = atoi_2 (atime+13);
523
if (year <= 1582) /* The julian date functions don't support this. */
524
return gpg_error (GPG_ERR_INV_VALUE);
534
jd = date2jd (year, month, day) + ndays;
535
jd2date (jd, &year, &month, &day);
537
if (year > 9999 || month > 12 || day > 31
538
|| year < 0 || month < 1 || day < 1)
539
return gpg_error (GPG_ERR_INV_VALUE);
541
snprintf (atime, 16, "%04d%02d%02dT%02d%02d%02d",
542
year, month, day, hour, minute, sec);
548
add_days_to_isotime (gnupg_isotime_t atime, int ndays)
551
int year, month, day, hour, minute, sec;
554
err = check_isotime (atime);
558
if (ndays < 0 || ndays >= 9999*366 )
559
return gpg_error (GPG_ERR_INV_VALUE);
561
year = atoi_4 (atime+0);
562
month = atoi_2 (atime+4);
563
day = atoi_2 (atime+6);
564
hour = atoi_2 (atime+9);
565
minute= atoi_2 (atime+11);
566
sec = atoi_2 (atime+13);
568
if (year <= 1582) /* The julian date functions don't support this. */
569
return gpg_error (GPG_ERR_INV_VALUE);
571
jd = date2jd (year, month, day) + ndays;
572
jd2date (jd, &year, &month, &day);
574
if (year > 9999 || month > 12 || day > 31
575
|| year < 0 || month < 1 || day < 1)
576
return gpg_error (GPG_ERR_INV_VALUE);
578
snprintf (atime, 16, "%04d%02d%02dT%02d%02d%02d",
579
year, month, day, hour, minute, sec);