2010-02-26 19:44:36 +01:00
|
|
|
/* t-timestuff.c - Regression tests for time functions
|
|
|
|
* Copyright (C) 2007 Free Software Foundation, Inc.
|
|
|
|
*
|
2011-09-30 12:52:11 +02:00
|
|
|
* This file is part of JNLIB, which is a subsystem of GnuPG.
|
2010-02-26 19:44:36 +01:00
|
|
|
*
|
|
|
|
* JNLIB is free software; you can redistribute it and/or modify it
|
2011-09-30 12:52:11 +02:00
|
|
|
* under the terms of either
|
|
|
|
*
|
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
2010-02-26 19:44:36 +01:00
|
|
|
*
|
|
|
|
* JNLIB is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2011-09-30 12:52:11 +02:00
|
|
|
* General Public License for more details.
|
2010-02-26 19:44:36 +01:00
|
|
|
*
|
2011-09-30 12:52:11 +02:00
|
|
|
* You should have received a copies of the GNU General Public License
|
|
|
|
* and the GNU Lesser General Public License along with this program;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
2010-02-26 19:44:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "mischelp.h"
|
|
|
|
|
|
|
|
#include "t-support.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmp_time_s (struct tm *a, struct tm *b)
|
|
|
|
{
|
|
|
|
if (a->tm_year != b->tm_year
|
|
|
|
|| a->tm_mon != b->tm_mon
|
|
|
|
|| a->tm_mday != b->tm_mday
|
|
|
|
|| a->tm_hour != b->tm_hour
|
2011-02-04 12:57:53 +01:00
|
|
|
|| a->tm_min != b->tm_min
|
|
|
|
|| a->tm_sec != b->tm_sec
|
2010-02-26 19:44:36 +01:00
|
|
|
|| a->tm_wday != b->tm_wday
|
|
|
|
|| a->tm_yday != b->tm_yday
|
|
|
|
|| !a->tm_isdst != !b->tm_isdst)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_timegm (void)
|
|
|
|
{
|
|
|
|
static struct {
|
2011-02-04 12:57:53 +01:00
|
|
|
int year, mon, mday, hour, min, sec;
|
2010-02-26 19:44:36 +01:00
|
|
|
} tvalues[] = {
|
|
|
|
{ -1 },
|
|
|
|
{ -2, 1 },
|
|
|
|
{ -2, 2 },
|
|
|
|
{ -2, 86399 },
|
|
|
|
{ -2, 86400 },
|
|
|
|
{ -2, 0x7ffffffe },
|
|
|
|
{ -2, 0x7fffffff },
|
|
|
|
/* Note: Because we use mktime below we can only start with the
|
|
|
|
day after Epoch. */
|
2012-03-27 19:46:20 +02:00
|
|
|
{ 1970, 0, 2, 0, 0 , 1},
|
|
|
|
{ 1970, 0, 2, 0, 0 , 2},
|
|
|
|
{ 1970, 0, 2, 12, 0 , 0},
|
|
|
|
{ 1970, 0, 2, 23, 59 , 59},
|
|
|
|
{ 1999, 11, 31, 23, 59 , 59},
|
|
|
|
{ 2000, 0, 1, 0, 0, 0},
|
|
|
|
{ 2000, 0, 1, 0, 0, 1},
|
|
|
|
{ 2010, 11, 31, 23, 59 , 59},
|
|
|
|
{ 2010, 0, 1, 0, 0, 0},
|
|
|
|
{ 2010, 0, 1, 0, 0, 1},
|
|
|
|
/* On GNU based 32 bit systems the end of all ticks will be on
|
|
|
|
20380119T031408 (unless Uli takes compassion on us and changes
|
|
|
|
time_t to a u64). We check that the previous day is okay. */
|
|
|
|
{ 2038, 0, 18, 23, 59, 59}
|
2010-02-26 19:44:36 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
int tidx;
|
2011-08-10 14:11:30 +02:00
|
|
|
time_t now, atime;
|
2010-02-26 19:44:36 +01:00
|
|
|
struct tm tbuf, tbuf2, *tp;
|
|
|
|
|
|
|
|
for (tidx=0; tidx < DIM (tvalues); tidx++)
|
|
|
|
{
|
|
|
|
if (tvalues[tidx].year == -1)
|
|
|
|
{
|
|
|
|
now = time (NULL);
|
|
|
|
}
|
|
|
|
else if (tvalues[tidx].year == -2)
|
|
|
|
{
|
|
|
|
now = tvalues[tidx].mon;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset (&tbuf, 0, sizeof tbuf);
|
|
|
|
tbuf.tm_year = tvalues[tidx].year - 1900;
|
|
|
|
tbuf.tm_mon = tvalues[tidx].mon;
|
|
|
|
tbuf.tm_mday = tvalues[tidx].mday;
|
|
|
|
tbuf.tm_hour = tvalues[tidx].hour;
|
|
|
|
tbuf.tm_min = tvalues[tidx].min;
|
2011-02-04 12:57:53 +01:00
|
|
|
tbuf.tm_sec = tvalues[tidx].sec;
|
2014-06-25 14:33:34 +02:00
|
|
|
#ifdef HAVE_TIMEGM
|
|
|
|
now = timegm (&tbuf);
|
|
|
|
#else
|
2010-02-26 19:44:36 +01:00
|
|
|
now = mktime (&tbuf);
|
2014-06-25 14:33:34 +02:00
|
|
|
#endif
|
2010-02-26 19:44:36 +01:00
|
|
|
}
|
|
|
|
if (now == (time_t)(-1))
|
|
|
|
fail (tidx);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-02-26 19:44:36 +01:00
|
|
|
tp = gmtime (&now);
|
|
|
|
if (!tp)
|
|
|
|
fail (tidx);
|
|
|
|
tbuf = *tp;
|
|
|
|
tbuf2 = tbuf;
|
2014-06-25 14:33:34 +02:00
|
|
|
#ifdef HAVE_TIMEGM
|
2010-02-26 19:44:36 +01:00
|
|
|
atime = timegm (&tbuf);
|
2014-06-25 14:33:34 +02:00
|
|
|
#else
|
|
|
|
atime = mktime (&tbuf);
|
|
|
|
#endif
|
2010-02-26 19:44:36 +01:00
|
|
|
if (atime == (time_t)(-1))
|
|
|
|
fail (tidx);
|
|
|
|
if (atime != now)
|
|
|
|
fail (tidx);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-02-26 19:44:36 +01:00
|
|
|
tp = gmtime (&atime);
|
|
|
|
if (!tp)
|
|
|
|
fail (tidx);
|
|
|
|
if (cmp_time_s (tp, &tbuf))
|
|
|
|
fail (tidx);
|
|
|
|
if (cmp_time_s (tp, &tbuf2))
|
|
|
|
fail (tidx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
2014-06-25 14:33:34 +02:00
|
|
|
/* If we do not have timegm, we use mktime. However, we need to use
|
|
|
|
UTC in this case so that the 20380118T235959 test does not fail
|
|
|
|
for other timezones. */
|
|
|
|
#ifndef HAVE_TIMEGM
|
2014-11-11 10:13:10 +01:00
|
|
|
# ifdef HAVE_SETENV
|
2014-06-25 14:33:34 +02:00
|
|
|
setenv ("TZ", "UTC", 1);
|
2014-11-11 10:13:10 +01:00
|
|
|
#else
|
|
|
|
putenv (xstrdup ("TZ=UTC"));
|
|
|
|
#endif
|
2014-06-25 14:33:34 +02:00
|
|
|
tzset ();
|
|
|
|
#endif
|
|
|
|
|
2010-02-26 19:44:36 +01:00
|
|
|
test_timegm ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|