Blame time/tst-mktime2.c

Packit 6c4009
/* Test program from Paul Eggert and Tony Leneis.  */
Packit 6c4009
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* True if the arithmetic type T is signed.  */
Packit 6c4009
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
Packit 6c4009
Packit 6c4009
/* The maximum and minimum values for the integer type T.  These
Packit 6c4009
   macros have undefined behavior if T is signed and has padding bits.
Packit 6c4009
   If this is a problem for you, please let us know how to fix it for
Packit 6c4009
   your host.  */
Packit 6c4009
#define TYPE_MINIMUM(t) \
Packit 6c4009
  ((t) (! TYPE_SIGNED (t) \
Packit 6c4009
	? (t) 0 \
Packit 6c4009
	: ~ TYPE_MAXIMUM (t)))
Packit 6c4009
#define TYPE_MAXIMUM(t) \
Packit 6c4009
  ((t) (! TYPE_SIGNED (t) \
Packit 6c4009
	? (t) -1 \
Packit 6c4009
	: ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
Packit 6c4009
Packit 6c4009
#ifndef TIME_T_MIN
Packit 6c4009
# define TIME_T_MIN TYPE_MINIMUM (time_t)
Packit 6c4009
#endif
Packit 6c4009
#ifndef TIME_T_MAX
Packit 6c4009
# define TIME_T_MAX TYPE_MAXIMUM (time_t)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Values we'll use to set the TZ environment variable.  */
Packit 6c4009
static const char *tz_strings[] =
Packit 6c4009
  {
Packit 6c4009
    (const char *) 0, "GMT0", "JST-9",
Packit 6c4009
    "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
Packit 6c4009
  };
Packit 6c4009
#define N_STRINGS ((int) (sizeof (tz_strings) / sizeof (tz_strings[0])))
Packit 6c4009
Packit 6c4009
/* Fail if mktime fails to convert a date in the spring-forward gap.
Packit 6c4009
   Based on a problem report from Andreas Jaeger.  */
Packit 6c4009
static void
Packit 6c4009
spring_forward_gap (void)
Packit 6c4009
{
Packit 6c4009
  /* glibc (up to about 1998-10-07) failed this test. */
Packit 6c4009
  struct tm tm;
Packit 6c4009
Packit 6c4009
  /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
Packit 6c4009
     instead of "TZ=America/Vancouver" in order to detect the bug even
Packit 6c4009
     on systems that don't support the Olson extension, or don't have the
Packit 6c4009
     full zoneinfo tables installed.  */
Packit 6c4009
  setenv ("TZ", "PST8PDT,M4.1.0,M10.5.0", 1);
Packit 6c4009
Packit 6c4009
  tm.tm_year = 98;
Packit 6c4009
  tm.tm_mon = 3;
Packit 6c4009
  tm.tm_mday = 5;
Packit 6c4009
  tm.tm_hour = 2;
Packit 6c4009
  tm.tm_min = 0;
Packit 6c4009
  tm.tm_sec = 0;
Packit 6c4009
  tm.tm_isdst = -1;
Packit 6c4009
  if (mktime (&tm) == (time_t)-1)
Packit 6c4009
    exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
mktime_test1 (time_t now)
Packit 6c4009
{
Packit 6c4009
  struct tm *lt = localtime (&now;;
Packit 6c4009
  if (lt && mktime (lt) != now)
Packit 6c4009
    exit (2);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
mktime_test (time_t now)
Packit 6c4009
{
Packit 6c4009
  mktime_test1 (now);
Packit 6c4009
  mktime_test1 ((time_t) (TIME_T_MAX - now));
Packit 6c4009
  mktime_test1 ((time_t) (TIME_T_MIN + now));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
irix_6_4_bug (void)
Packit 6c4009
{
Packit 6c4009
  /* Based on code from Ariel Faigon.  */
Packit 6c4009
  struct tm tm;
Packit 6c4009
  tm.tm_year = 96;
Packit 6c4009
  tm.tm_mon = 3;
Packit 6c4009
  tm.tm_mday = 0;
Packit 6c4009
  tm.tm_hour = 0;
Packit 6c4009
  tm.tm_min = 0;
Packit 6c4009
  tm.tm_sec = 0;
Packit 6c4009
  tm.tm_isdst = -1;
Packit 6c4009
  mktime (&tm;;
Packit 6c4009
  if (tm.tm_mon != 2 || tm.tm_mday != 31)
Packit 6c4009
    exit (3);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
bigtime_test (int j)
Packit 6c4009
{
Packit 6c4009
  struct tm tm;
Packit 6c4009
  time_t now;
Packit 6c4009
  tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
Packit 6c4009
  tm.tm_isdst = -1;
Packit 6c4009
  now = mktime (&tm;;
Packit 6c4009
  if (now != (time_t) -1)
Packit 6c4009
    {
Packit 6c4009
      struct tm *lt = localtime (&now;;
Packit 6c4009
      if (! (lt
Packit 6c4009
	     && lt->tm_year == tm.tm_year
Packit 6c4009
	     && lt->tm_mon == tm.tm_mon
Packit 6c4009
	     && lt->tm_mday == tm.tm_mday
Packit 6c4009
	     && lt->tm_hour == tm.tm_hour
Packit 6c4009
	     && lt->tm_min == tm.tm_min
Packit 6c4009
	     && lt->tm_sec == tm.tm_sec
Packit 6c4009
	     && lt->tm_yday == tm.tm_yday
Packit 6c4009
	     && lt->tm_wday == tm.tm_wday
Packit 6c4009
	     && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
Packit 6c4009
		  == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
Packit 6c4009
	exit (4);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  time_t t, delta;
Packit 6c4009
  int i;
Packit 6c4009
  unsigned int j;
Packit 6c4009
Packit 6c4009
  setenv ("TZ", "America/Sao_Paulo", 1);
Packit 6c4009
  /* This test makes some buggy mktime implementations loop.
Packit 6c4009
     Give up after 60 seconds; a mktime slower than that
Packit 6c4009
     isn't worth using anyway.  */
Packit 6c4009
  alarm (60);
Packit 6c4009
Packit 6c4009
  delta = TIME_T_MAX / 997; /* a suitable prime number */
Packit 6c4009
  for (i = 0; i < N_STRINGS; i++)
Packit 6c4009
    {
Packit 6c4009
      if (tz_strings[i])
Packit 6c4009
	setenv ("TZ", tz_strings[i], 1);
Packit 6c4009
Packit 6c4009
      for (t = 0; t <= TIME_T_MAX - delta; t += delta)
Packit 6c4009
	mktime_test (t);
Packit 6c4009
      mktime_test ((time_t) 1);
Packit 6c4009
      mktime_test ((time_t) (60 * 60));
Packit 6c4009
      mktime_test ((time_t) (60 * 60 * 24));
Packit 6c4009
Packit 6c4009
      for (j = 1; j <= INT_MAX; j *= 2)
Packit 6c4009
	bigtime_test (j);
Packit 6c4009
      bigtime_test (j - 1);
Packit 6c4009
    }
Packit 6c4009
  irix_6_4_bug ();
Packit 6c4009
  spring_forward_gap ();
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"