2009-06-15 23:49:18 +08:00
|
|
|
/*
|
|
|
|
** This file is in the public domain, so clarified as of
|
|
|
|
** 2009-05-17 by Arthur David Olson.
|
|
|
|
*/
|
|
|
|
|
2012-11-10 05:20:57 +08:00
|
|
|
#include "version.h"
|
1995-02-18 09:27:10 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This code has been made independent of the rest of the time
|
|
|
|
** conversion package to increase confidence in the verification it provides.
|
|
|
|
** You can use this code to help in verifying other implementations.
|
2016-01-07 19:45:07 +08:00
|
|
|
** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
|
1995-02-18 09:27:10 +08:00
|
|
|
*/
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
#ifndef NETBSD_INSPIRED
|
|
|
|
# define NETBSD_INSPIRED 1
|
|
|
|
#endif
|
|
|
|
#ifndef USE_LTZ
|
|
|
|
# define USE_LTZ 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_LTZ
|
2013-12-20 21:10:07 +08:00
|
|
|
# include "private.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
/* Enable tm_gmtoff and tm_zone on GNUish systems. */
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
/* Enable strtoimax on Solaris 10. */
|
|
|
|
#define __EXTENSIONS__ 1
|
|
|
|
|
Wed Mar 8 13:38:13 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
* posix/glob/configure.bat: Fixes from DJ.
* time/backward, time/europe, time/northamerica, time/pacificnew,
time/zdump.c, time/zic.c, time/tzfile.h, time/private.h,
time/ialloc.c: Code and data updated from ADO's 95b.
* time/emkdir.c: File removed.
* time/Makefile (distribute, extra-objs, zic): Omit it.
* time/localtime.c: Deansideclized. Never #define __tzname et al
to non-__ names.
* locale/lc-ctype.c (__ctype_tolower, __ctype_toupper): Use int *
instead of short int *.
* ctype/ctype-info.c: Likewise.
* ctype/ctype.h: Likewise.
* locale/langinfo.h (_NL_CTYPE_CLASS): Use this (just one) instead
of EB and EL versions.
1995-03-09 18:00:12 +08:00
|
|
|
#include "stdio.h" /* for stdout, stderr, perror */
|
1995-02-18 09:27:10 +08:00
|
|
|
#include "string.h" /* for strcpy */
|
|
|
|
#include "sys/types.h" /* for time_t */
|
|
|
|
#include "time.h" /* for struct tm */
|
Wed Mar 8 13:38:13 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
* posix/glob/configure.bat: Fixes from DJ.
* time/backward, time/europe, time/northamerica, time/pacificnew,
time/zdump.c, time/zic.c, time/tzfile.h, time/private.h,
time/ialloc.c: Code and data updated from ADO's 95b.
* time/emkdir.c: File removed.
* time/Makefile (distribute, extra-objs, zic): Omit it.
* time/localtime.c: Deansideclized. Never #define __tzname et al
to non-__ names.
* locale/lc-ctype.c (__ctype_tolower, __ctype_toupper): Use int *
instead of short int *.
* ctype/ctype-info.c: Likewise.
* ctype/ctype.h: Likewise.
* locale/langinfo.h (_NL_CTYPE_CLASS): Use this (just one) instead
of EB and EL versions.
1995-03-09 18:00:12 +08:00
|
|
|
#include "stdlib.h" /* for exit, malloc, atoi */
|
2012-11-10 05:20:57 +08:00
|
|
|
#include "limits.h" /* for CHAR_BIT, LLONG_MAX */
|
2016-01-07 19:45:07 +08:00
|
|
|
#include <errno.h>
|
2005-01-26 08:01:20 +08:00
|
|
|
|
2013-12-20 21:10:07 +08:00
|
|
|
/*
|
|
|
|
** Substitutes for pre-C99 compilers.
|
|
|
|
** Much of this section of code is stolen from private.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HAVE_STDINT_H
|
|
|
|
# define HAVE_STDINT_H \
|
2014-06-26 00:51:17 +08:00
|
|
|
(199901 <= __STDC_VERSION__ \
|
|
|
|
|| 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
|
|
|
|
|| __CYGWIN__)
|
2013-12-20 21:10:07 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_STDINT_H
|
|
|
|
# include "stdint.h"
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_INTTYPES_H
|
|
|
|
# define HAVE_INTTYPES_H HAVE_STDINT_H
|
|
|
|
#endif
|
|
|
|
#if HAVE_INTTYPES_H
|
|
|
|
# include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INT_FAST32_MAX
|
|
|
|
# if INT_MAX >> 31 == 0
|
|
|
|
typedef long int_fast32_t;
|
|
|
|
# else
|
|
|
|
typedef int int_fast32_t;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
|
|
|
|
#if !defined LLONG_MAX && defined __LONG_LONG_MAX__
|
|
|
|
# define LLONG_MAX __LONG_LONG_MAX__
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 21:10:07 +08:00
|
|
|
#ifndef INTMAX_MAX
|
2016-01-07 19:45:07 +08:00
|
|
|
# ifdef LLONG_MAX
|
2013-12-20 21:10:07 +08:00
|
|
|
typedef long long intmax_t;
|
|
|
|
# define strtoimax strtoll
|
2016-01-07 19:45:07 +08:00
|
|
|
# define INTMAX_MAX LLONG_MAX
|
2013-12-20 21:10:07 +08:00
|
|
|
# else
|
|
|
|
typedef long intmax_t;
|
|
|
|
# define strtoimax strtol
|
|
|
|
# define INTMAX_MAX LONG_MAX
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
#ifndef PRIdMAX
|
|
|
|
# if INTMAX_MAX == LLONG_MAX
|
|
|
|
# define PRIdMAX "lld"
|
|
|
|
# else
|
|
|
|
# define PRIdMAX "ld"
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Infer TM_ZONE on systems where this information is known, but suppress
|
|
|
|
guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
|
|
|
|
#if (defined __GLIBC__ \
|
|
|
|
|| defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
|
|
|
|
|| (defined __APPLE__ && defined __MACH__))
|
|
|
|
# if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
|
|
|
|
# define TM_GMTOFF tm_gmtoff
|
|
|
|
# endif
|
|
|
|
# if !defined TM_ZONE && !defined NO_TM_ZONE
|
|
|
|
# define TM_ZONE tm_zone
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_LOCALTIME_R
|
|
|
|
# define HAVE_LOCALTIME_R 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_LOCALTIME_RZ
|
|
|
|
# ifdef TM_ZONE
|
|
|
|
# define HAVE_LOCALTIME_RZ (NETBSD_INSPIRED && USE_LTZ)
|
|
|
|
# else
|
|
|
|
# define HAVE_LOCALTIME_RZ 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_TZSET
|
|
|
|
# define HAVE_TZSET 1
|
|
|
|
#endif
|
2013-12-20 21:10:07 +08:00
|
|
|
|
2005-01-26 08:01:20 +08:00
|
|
|
#ifndef ZDUMP_LO_YEAR
|
|
|
|
#define ZDUMP_LO_YEAR (-500)
|
|
|
|
#endif /* !defined ZDUMP_LO_YEAR */
|
|
|
|
|
|
|
|
#ifndef ZDUMP_HI_YEAR
|
|
|
|
#define ZDUMP_HI_YEAR 2500
|
|
|
|
#endif /* !defined ZDUMP_HI_YEAR */
|
1995-02-18 09:27:10 +08:00
|
|
|
|
|
|
|
#ifndef MAX_STRING_LENGTH
|
|
|
|
#define MAX_STRING_LENGTH 1024
|
|
|
|
#endif /* !defined MAX_STRING_LENGTH */
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
#if __STDC_VERSION__ < 199901
|
|
|
|
# define true 1
|
|
|
|
# define false 0
|
|
|
|
# define bool int
|
|
|
|
#else
|
|
|
|
# include <stdbool.h>
|
|
|
|
#endif
|
1995-02-18 09:27:10 +08:00
|
|
|
|
|
|
|
#ifndef EXIT_SUCCESS
|
|
|
|
#define EXIT_SUCCESS 0
|
|
|
|
#endif /* !defined EXIT_SUCCESS */
|
|
|
|
|
|
|
|
#ifndef EXIT_FAILURE
|
|
|
|
#define EXIT_FAILURE 1
|
|
|
|
#endif /* !defined EXIT_FAILURE */
|
|
|
|
|
|
|
|
#ifndef SECSPERMIN
|
|
|
|
#define SECSPERMIN 60
|
|
|
|
#endif /* !defined SECSPERMIN */
|
|
|
|
|
|
|
|
#ifndef MINSPERHOUR
|
|
|
|
#define MINSPERHOUR 60
|
|
|
|
#endif /* !defined MINSPERHOUR */
|
|
|
|
|
|
|
|
#ifndef SECSPERHOUR
|
|
|
|
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
|
|
|
|
#endif /* !defined SECSPERHOUR */
|
|
|
|
|
|
|
|
#ifndef HOURSPERDAY
|
|
|
|
#define HOURSPERDAY 24
|
|
|
|
#endif /* !defined HOURSPERDAY */
|
|
|
|
|
|
|
|
#ifndef EPOCH_YEAR
|
|
|
|
#define EPOCH_YEAR 1970
|
|
|
|
#endif /* !defined EPOCH_YEAR */
|
|
|
|
|
|
|
|
#ifndef TM_YEAR_BASE
|
|
|
|
#define TM_YEAR_BASE 1900
|
|
|
|
#endif /* !defined TM_YEAR_BASE */
|
|
|
|
|
|
|
|
#ifndef DAYSPERNYEAR
|
|
|
|
#define DAYSPERNYEAR 365
|
|
|
|
#endif /* !defined DAYSPERNYEAR */
|
|
|
|
|
|
|
|
#ifndef isleap
|
2005-01-26 08:01:20 +08:00
|
|
|
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
|
1995-02-18 09:27:10 +08:00
|
|
|
#endif /* !defined isleap */
|
|
|
|
|
2005-01-26 08:01:20 +08:00
|
|
|
#ifndef isleap_sum
|
|
|
|
/*
|
|
|
|
** See tzfile.h for details on isleap_sum.
|
|
|
|
*/
|
|
|
|
#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
|
|
|
|
#endif /* !defined isleap_sum */
|
|
|
|
|
2013-12-20 21:10:07 +08:00
|
|
|
#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
|
2005-01-26 08:01:20 +08:00
|
|
|
#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
|
|
|
|
#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
|
2013-12-20 21:10:07 +08:00
|
|
|
#define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
|
|
|
|
+ SECSPERLYEAR * (intmax_t) (100 - 3))
|
|
|
|
|
|
|
|
/*
|
|
|
|
** True if SECSPER400YEARS is known to be representable as an
|
|
|
|
** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
|
|
|
|
** even if SECSPER400YEARS is representable, because when that happens
|
|
|
|
** the code merely runs a bit more slowly, and this slowness doesn't
|
|
|
|
** occur on any practical platform.
|
|
|
|
*/
|
|
|
|
enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
|
2005-01-26 08:01:20 +08:00
|
|
|
|
2008-04-11 15:01:57 +08:00
|
|
|
#ifndef HAVE_GETTEXT
|
|
|
|
#define HAVE_GETTEXT 0
|
|
|
|
#endif
|
2004-10-12 14:19:23 +08:00
|
|
|
#if HAVE_GETTEXT
|
1996-02-27 08:55:03 +08:00
|
|
|
#include "locale.h" /* for setlocale */
|
|
|
|
#include "libintl.h"
|
2004-10-12 14:19:23 +08:00
|
|
|
#endif /* HAVE_GETTEXT */
|
1996-02-27 08:55:03 +08:00
|
|
|
|
2012-11-10 05:20:57 +08:00
|
|
|
#if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
|
|
|
|
# define ATTRIBUTE_PURE __attribute__ ((__pure__))
|
|
|
|
#else
|
|
|
|
# define ATTRIBUTE_PURE /* empty */
|
|
|
|
#endif
|
|
|
|
|
Wed Feb 21 02:25:07 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
* Rules (others): Depend on $(extra-objs), $(install-lib),
$(install-bin), $(install-sbin), in object directory.
* Makeconfig (link-libc): Don't pass -rpath option with default
path, since ld.so should use the same default.
* Makerules (build-shlib): Likewise.
* Makerules (make-target-directory): Use mkinstalldirs.
* sysdeps/mach/i386/sysdep.h: Include unix/i386/sysdep.h to
redefine ENTRY et al.
* sysdeps/unix/sysv/linux/i386/sysdep.h (NO_UNDERSCORES, ENTRY):
Macros removed. The code in unix/i386/sysdep.h should now suffice.
* sysdeps/unix/i386/sysdep.h (ALIGNARG): New macro, defns for
[HAVE_ELF] and not.
(ENTRY): Use it in .align directive.
(CALL_MCOUNT): New macro, empty unless [PROF].
(ENTRY): Do CALL_MCOUNT just after the label.
* Makeconfig (CPPFLAGS-.po): New variable, use -DPROF so assembly
code can call mcount.
Tue Feb 20 23:05:16 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
* time/private.h, time/zdump.c, time/zic.c: Updated verbatim from ADO;
translation markings are now integrated in his distribution.
* stdlib/strtod.c (round_and_return): Add missing (mp_limb) 1 cast.
* po: New directory where `LANG.po' files for message translations
will reside.
* po/SYS_libc.pot: New file, template of message strings as in source.
* MakeTAGS: New rules to build po/SYS_libc.pot automatically from
the source using xgettext.
* Makerules ($(..)po/%.pot): New rule.
* manual/Makefile (../po/manual.pot): New target, make it empty.
* sysdeps/unix/bsd/alarm.c: Moved to sysdeps/unix/alarm.c.
* sysdeps/unix/bsd/nice.c: Moved to sysdeps/unix/nice.c.
* sysdeps/unix/bsd/stime.c: Moved to sysdeps/unix/stime.c.
* sysdeps/unix/bsd/time.c: Moved to sysdeps/unix/time.c.
* sysdeps/unix/bsd/utime.c: Moved to sysdeps/unix/utime.c.
1996-02-22 23:52:20 +08:00
|
|
|
/*
|
|
|
|
** For the benefit of GNU folk...
|
2016-01-07 19:45:07 +08:00
|
|
|
** '_(MSGID)' uses the current locale's message library string for MSGID.
|
1996-02-27 08:55:03 +08:00
|
|
|
** The default is to use gettext if available, and use MSGID otherwise.
|
Wed Feb 21 02:25:07 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
* Rules (others): Depend on $(extra-objs), $(install-lib),
$(install-bin), $(install-sbin), in object directory.
* Makeconfig (link-libc): Don't pass -rpath option with default
path, since ld.so should use the same default.
* Makerules (build-shlib): Likewise.
* Makerules (make-target-directory): Use mkinstalldirs.
* sysdeps/mach/i386/sysdep.h: Include unix/i386/sysdep.h to
redefine ENTRY et al.
* sysdeps/unix/sysv/linux/i386/sysdep.h (NO_UNDERSCORES, ENTRY):
Macros removed. The code in unix/i386/sysdep.h should now suffice.
* sysdeps/unix/i386/sysdep.h (ALIGNARG): New macro, defns for
[HAVE_ELF] and not.
(ENTRY): Use it in .align directive.
(CALL_MCOUNT): New macro, empty unless [PROF].
(ENTRY): Do CALL_MCOUNT just after the label.
* Makeconfig (CPPFLAGS-.po): New variable, use -DPROF so assembly
code can call mcount.
Tue Feb 20 23:05:16 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
* time/private.h, time/zdump.c, time/zic.c: Updated verbatim from ADO;
translation markings are now integrated in his distribution.
* stdlib/strtod.c (round_and_return): Add missing (mp_limb) 1 cast.
* po: New directory where `LANG.po' files for message translations
will reside.
* po/SYS_libc.pot: New file, template of message strings as in source.
* MakeTAGS: New rules to build po/SYS_libc.pot automatically from
the source using xgettext.
* Makerules ($(..)po/%.pot): New rule.
* manual/Makefile (../po/manual.pot): New target, make it empty.
* sysdeps/unix/bsd/alarm.c: Moved to sysdeps/unix/alarm.c.
* sysdeps/unix/bsd/nice.c: Moved to sysdeps/unix/nice.c.
* sysdeps/unix/bsd/stime.c: Moved to sysdeps/unix/stime.c.
* sysdeps/unix/bsd/time.c: Moved to sysdeps/unix/time.c.
* sysdeps/unix/bsd/utime.c: Moved to sysdeps/unix/utime.c.
1996-02-22 23:52:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _
|
2004-10-12 14:19:23 +08:00
|
|
|
#if HAVE_GETTEXT
|
1996-02-27 08:55:03 +08:00
|
|
|
#define _(msgid) gettext(msgid)
|
2004-10-12 14:19:23 +08:00
|
|
|
#else /* !HAVE_GETTEXT */
|
1996-02-27 08:55:03 +08:00
|
|
|
#define _(msgid) msgid
|
2004-10-12 14:19:23 +08:00
|
|
|
#endif /* !HAVE_GETTEXT */
|
Wed Feb 21 02:25:07 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
* Rules (others): Depend on $(extra-objs), $(install-lib),
$(install-bin), $(install-sbin), in object directory.
* Makeconfig (link-libc): Don't pass -rpath option with default
path, since ld.so should use the same default.
* Makerules (build-shlib): Likewise.
* Makerules (make-target-directory): Use mkinstalldirs.
* sysdeps/mach/i386/sysdep.h: Include unix/i386/sysdep.h to
redefine ENTRY et al.
* sysdeps/unix/sysv/linux/i386/sysdep.h (NO_UNDERSCORES, ENTRY):
Macros removed. The code in unix/i386/sysdep.h should now suffice.
* sysdeps/unix/i386/sysdep.h (ALIGNARG): New macro, defns for
[HAVE_ELF] and not.
(ENTRY): Use it in .align directive.
(CALL_MCOUNT): New macro, empty unless [PROF].
(ENTRY): Do CALL_MCOUNT just after the label.
* Makeconfig (CPPFLAGS-.po): New variable, use -DPROF so assembly
code can call mcount.
Tue Feb 20 23:05:16 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
* time/private.h, time/zdump.c, time/zic.c: Updated verbatim from ADO;
translation markings are now integrated in his distribution.
* stdlib/strtod.c (round_and_return): Add missing (mp_limb) 1 cast.
* po: New directory where `LANG.po' files for message translations
will reside.
* po/SYS_libc.pot: New file, template of message strings as in source.
* MakeTAGS: New rules to build po/SYS_libc.pot automatically from
the source using xgettext.
* Makerules ($(..)po/%.pot): New rule.
* manual/Makefile (../po/manual.pot): New target, make it empty.
* sysdeps/unix/bsd/alarm.c: Moved to sysdeps/unix/alarm.c.
* sysdeps/unix/bsd/nice.c: Moved to sysdeps/unix/nice.c.
* sysdeps/unix/bsd/stime.c: Moved to sysdeps/unix/stime.c.
* sysdeps/unix/bsd/time.c: Moved to sysdeps/unix/time.c.
* sysdeps/unix/bsd/utime.c: Moved to sysdeps/unix/utime.c.
1996-02-22 23:52:20 +08:00
|
|
|
#endif /* !defined _ */
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
#if !defined TZ_DOMAIN && defined HAVE_GETTEXT
|
|
|
|
# define TZ_DOMAIN "tz"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ! HAVE_LOCALTIME_RZ
|
|
|
|
# undef timezone_t
|
|
|
|
# define timezone_t char **
|
|
|
|
#endif
|
1996-02-27 08:55:03 +08:00
|
|
|
|
1995-02-18 09:27:10 +08:00
|
|
|
extern char ** environ;
|
2008-04-11 15:01:57 +08:00
|
|
|
extern int getopt(int argc, char * const argv[],
|
|
|
|
const char * options);
|
1995-02-18 09:27:10 +08:00
|
|
|
extern char * optarg;
|
|
|
|
extern int optind;
|
|
|
|
extern char * tzname[2];
|
|
|
|
|
2013-12-20 21:10:07 +08:00
|
|
|
/* The minimum and maximum finite time values. */
|
2016-01-07 19:45:07 +08:00
|
|
|
enum { atime_shift = CHAR_BIT * sizeof (time_t) - 2 };
|
2012-11-10 05:20:57 +08:00
|
|
|
static time_t const absolute_min_time =
|
2013-12-20 21:10:07 +08:00
|
|
|
((time_t) -1 < 0
|
2016-01-07 19:45:07 +08:00
|
|
|
? (- ((time_t) ~ (time_t) 0 < 0)
|
|
|
|
- (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift)))
|
2012-11-10 05:20:57 +08:00
|
|
|
: 0);
|
|
|
|
static time_t const absolute_max_time =
|
2013-12-20 21:10:07 +08:00
|
|
|
((time_t) -1 < 0
|
2016-01-07 19:45:07 +08:00
|
|
|
? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))
|
2013-12-20 21:10:07 +08:00
|
|
|
: -1);
|
2016-01-07 19:45:07 +08:00
|
|
|
static int longest;
|
2005-01-26 08:01:20 +08:00
|
|
|
static char * progname;
|
2016-01-07 19:45:07 +08:00
|
|
|
static bool warned;
|
|
|
|
static bool errout;
|
|
|
|
|
|
|
|
static char const *abbr(struct tm const *);
|
|
|
|
static intmax_t delta(struct tm *, struct tm *) ATTRIBUTE_PURE;
|
|
|
|
static void dumptime(struct tm const *);
|
|
|
|
static time_t hunt(timezone_t, char *, time_t, time_t);
|
|
|
|
static void show(timezone_t, char *, time_t, bool);
|
|
|
|
static const char *tformat(void);
|
|
|
|
static time_t yeartot(intmax_t) ATTRIBUTE_PURE;
|
|
|
|
|
|
|
|
/* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
|
|
|
|
#define is_digit(c) ((unsigned)(c) - '0' <= 9)
|
|
|
|
|
|
|
|
/* Is A an alphabetic character in the C locale? */
|
|
|
|
static bool
|
|
|
|
is_alpha(char a)
|
|
|
|
{
|
|
|
|
switch (a) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
|
|
|
|
case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
|
|
|
|
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
|
|
|
|
case 'V': case 'W': case 'X': case 'Y': case 'Z':
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
|
|
|
|
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
|
|
|
|
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
|
|
|
|
case 'v': case 'w': case 'x': case 'y': case 'z':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return A + B, exiting if the result would overflow. */
|
|
|
|
static size_t
|
|
|
|
sumsize(size_t a, size_t b)
|
|
|
|
{
|
|
|
|
size_t sum = a + b;
|
|
|
|
if (sum < a) {
|
|
|
|
fprintf(stderr, "%s: size overflow\n", progname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ! HAVE_TZSET
|
|
|
|
# undef tzset
|
|
|
|
# define tzset zdump_tzset
|
|
|
|
static void tzset(void) { }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Assume gmtime_r works if localtime_r does.
|
|
|
|
A replacement localtime_r is defined below if needed. */
|
|
|
|
#if ! HAVE_LOCALTIME_R
|
|
|
|
|
|
|
|
# undef gmtime_r
|
|
|
|
# define gmtime_r zdump_gmtime_r
|
|
|
|
|
|
|
|
static struct tm *
|
|
|
|
gmtime_r(time_t *tp, struct tm *tmp)
|
|
|
|
{
|
|
|
|
struct tm *r = gmtime(tp);
|
|
|
|
if (r) {
|
|
|
|
*tmp = *r;
|
|
|
|
r = tmp;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Platforms with TM_ZONE don't need tzname, so they can use the
|
|
|
|
faster localtime_rz or localtime_r if available. */
|
|
|
|
|
|
|
|
#if defined TM_ZONE && HAVE_LOCALTIME_RZ
|
|
|
|
# define USE_LOCALTIME_RZ true
|
|
|
|
#else
|
|
|
|
# define USE_LOCALTIME_RZ false
|
|
|
|
#endif
|
2005-01-26 08:01:20 +08:00
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
#if ! USE_LOCALTIME_RZ
|
|
|
|
|
|
|
|
# if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET
|
|
|
|
# undef localtime_r
|
|
|
|
# define localtime_r zdump_localtime_r
|
|
|
|
static struct tm *
|
|
|
|
localtime_r(time_t *tp, struct tm *tmp)
|
|
|
|
{
|
|
|
|
struct tm *r = localtime(tp);
|
|
|
|
if (r) {
|
|
|
|
*tmp = *r;
|
|
|
|
r = tmp;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# undef localtime_rz
|
|
|
|
# define localtime_rz zdump_localtime_rz
|
|
|
|
static struct tm *
|
|
|
|
localtime_rz(timezone_t rz, time_t *tp, struct tm *tmp)
|
|
|
|
{
|
|
|
|
return localtime_r(tp, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef TYPECHECK
|
|
|
|
# undef mktime_z
|
|
|
|
# define mktime_z zdump_mktime_z
|
|
|
|
static time_t
|
|
|
|
mktime_z(timezone_t tz, struct tm *tmp)
|
|
|
|
{
|
|
|
|
return mktime(tmp);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# undef tzalloc
|
|
|
|
# undef tzfree
|
|
|
|
# define tzalloc zdump_tzalloc
|
|
|
|
# define tzfree zdump_tzfree
|
|
|
|
|
|
|
|
static timezone_t
|
|
|
|
tzalloc(char const *val)
|
|
|
|
{
|
|
|
|
static char **fakeenv;
|
|
|
|
char **env = fakeenv;
|
|
|
|
char *env0;
|
|
|
|
if (! env) {
|
|
|
|
char **e = environ;
|
|
|
|
int to;
|
|
|
|
|
|
|
|
while (*e++)
|
|
|
|
continue;
|
|
|
|
env = malloc(sumsize(sizeof *environ,
|
|
|
|
(e - environ) * sizeof *environ));
|
|
|
|
if (! env) {
|
|
|
|
perror(progname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
to = 1;
|
|
|
|
for (e = environ; (env[to] = *e); e++)
|
|
|
|
to += strncmp(*e, "TZ=", 3) != 0;
|
|
|
|
}
|
|
|
|
env0 = malloc(sumsize(sizeof "TZ=", strlen(val)));
|
|
|
|
if (! env0) {
|
|
|
|
perror(progname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
env[0] = strcat(strcpy(env0, "TZ="), val);
|
|
|
|
environ = fakeenv = env;
|
|
|
|
tzset();
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tzfree(timezone_t env)
|
|
|
|
{
|
|
|
|
environ = env + 1;
|
|
|
|
free(env[0]);
|
|
|
|
}
|
|
|
|
#endif /* ! USE_LOCALTIME_RZ */
|
|
|
|
|
|
|
|
/* A UTC time zone, and its initializer. */
|
|
|
|
static timezone_t gmtz;
|
|
|
|
static void
|
|
|
|
gmtzinit(void)
|
|
|
|
{
|
|
|
|
if (USE_LOCALTIME_RZ) {
|
|
|
|
static char const utc[] = "UTC0";
|
|
|
|
gmtz = tzalloc(utc);
|
|
|
|
if (!gmtz) {
|
|
|
|
perror(utc);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert *TP to UTC, storing the broken-down time into *TMP.
|
|
|
|
Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP),
|
|
|
|
except typically faster if USE_LOCALTIME_RZ. */
|
|
|
|
static struct tm *
|
|
|
|
my_gmtime_r(time_t *tp, struct tm *tmp)
|
|
|
|
{
|
|
|
|
return USE_LOCALTIME_RZ ? localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp);
|
|
|
|
}
|
2005-01-26 08:01:20 +08:00
|
|
|
|
|
|
|
#ifndef TYPECHECK
|
2016-01-07 19:45:07 +08:00
|
|
|
# define my_localtime_rz localtime_rz
|
2005-01-26 08:01:20 +08:00
|
|
|
#else /* !defined TYPECHECK */
|
2016-01-07 19:45:07 +08:00
|
|
|
|
2005-01-26 08:01:20 +08:00
|
|
|
static struct tm *
|
2016-01-07 19:45:07 +08:00
|
|
|
my_localtime_rz(timezone_t tz, time_t *tp, struct tm *tmp)
|
2005-01-26 08:01:20 +08:00
|
|
|
{
|
2016-01-07 19:45:07 +08:00
|
|
|
tmp = localtime_rz(tz, tp, tmp);
|
|
|
|
if (tmp) {
|
2005-01-26 08:01:20 +08:00
|
|
|
struct tm tm;
|
|
|
|
register time_t t;
|
|
|
|
|
|
|
|
tm = *tmp;
|
2016-01-07 19:45:07 +08:00
|
|
|
t = mktime_z(tz, &tm);
|
2013-12-20 21:10:07 +08:00
|
|
|
if (t != *tp) {
|
2016-01-07 19:45:07 +08:00
|
|
|
fflush(stdout);
|
|
|
|
fprintf(stderr, "\n%s: ", progname);
|
|
|
|
fprintf(stderr, tformat(), *tp);
|
|
|
|
fprintf(stderr, " ->");
|
|
|
|
fprintf(stderr, " year=%d", tmp->tm_year);
|
|
|
|
fprintf(stderr, " mon=%d", tmp->tm_mon);
|
|
|
|
fprintf(stderr, " mday=%d", tmp->tm_mday);
|
|
|
|
fprintf(stderr, " hour=%d", tmp->tm_hour);
|
|
|
|
fprintf(stderr, " min=%d", tmp->tm_min);
|
|
|
|
fprintf(stderr, " sec=%d", tmp->tm_sec);
|
|
|
|
fprintf(stderr, " isdst=%d", tmp->tm_isdst);
|
|
|
|
fprintf(stderr, " -> ");
|
|
|
|
fprintf(stderr, tformat(), t);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
errout = true;
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
#endif /* !defined TYPECHECK */
|
1995-02-18 09:27:10 +08:00
|
|
|
|
2005-08-06 13:40:57 +08:00
|
|
|
static void
|
2012-11-10 05:20:57 +08:00
|
|
|
abbrok(const char *const abbrp, const char *const zone)
|
2005-08-06 13:40:57 +08:00
|
|
|
{
|
|
|
|
register const char * cp;
|
2012-11-10 05:20:57 +08:00
|
|
|
register const char * wp;
|
2005-08-06 13:40:57 +08:00
|
|
|
|
|
|
|
if (warned)
|
|
|
|
return;
|
2006-01-10 15:55:46 +08:00
|
|
|
cp = abbrp;
|
2016-01-07 19:45:07 +08:00
|
|
|
while (is_alpha(*cp) || is_digit(*cp) || *cp == '-' || *cp == '+')
|
2005-08-06 13:40:57 +08:00
|
|
|
++cp;
|
2016-01-07 19:45:07 +08:00
|
|
|
if (cp - abbrp < 3)
|
|
|
|
wp = _("has fewer than 3 characters");
|
2006-01-10 15:55:46 +08:00
|
|
|
else if (cp - abbrp > 6)
|
2016-01-07 19:45:07 +08:00
|
|
|
wp = _("has more than 6 characters");
|
|
|
|
else if (*cp)
|
|
|
|
wp = _("has characters other than ASCII alphanumerics, '-' or '+'");
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
fflush(stdout);
|
|
|
|
fprintf(stderr,
|
2006-01-10 15:55:46 +08:00
|
|
|
_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
|
|
|
|
progname, zone, abbrp, wp);
|
2016-01-07 19:45:07 +08:00
|
|
|
warned = errout = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a time zone abbreviation. If the abbreviation needs to be
|
|
|
|
saved, use *BUF (of size *BUFALLOC) to save it, and return the
|
|
|
|
abbreviation in the possibly-reallocated *BUF. Otherwise, just
|
|
|
|
return the abbreviation. Get the abbreviation from TMP.
|
|
|
|
Exit on memory allocation failure. */
|
|
|
|
static char const *
|
|
|
|
saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp)
|
|
|
|
{
|
|
|
|
char const *ab = abbr(tmp);
|
|
|
|
if (HAVE_LOCALTIME_RZ)
|
|
|
|
return ab;
|
|
|
|
else {
|
|
|
|
size_t ablen = strlen(ab);
|
|
|
|
if (*bufalloc <= ablen) {
|
|
|
|
free(*buf);
|
|
|
|
|
|
|
|
/* Make the new buffer at least twice as long as the old,
|
|
|
|
to avoid O(N**2) behavior on repeated calls. */
|
|
|
|
*bufalloc = sumsize(*bufalloc, ablen + 1);
|
|
|
|
|
|
|
|
*buf = malloc(*bufalloc);
|
|
|
|
if (! *buf) {
|
|
|
|
perror(progname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strcpy(*buf, ab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_file(FILE *stream)
|
|
|
|
{
|
|
|
|
char const *e = (ferror(stream) ? _("I/O error")
|
|
|
|
: fclose(stream) != 0 ? strerror(errno) : NULL);
|
|
|
|
if (e) {
|
|
|
|
fprintf(stderr, "%s: %s\n", progname, e);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2005-08-06 13:40:57 +08:00
|
|
|
}
|
|
|
|
|
2009-06-15 23:49:18 +08:00
|
|
|
static void
|
2012-11-10 05:20:57 +08:00
|
|
|
usage(FILE * const stream, const int status)
|
2009-06-15 23:49:18 +08:00
|
|
|
{
|
2016-01-07 19:45:07 +08:00
|
|
|
fprintf(stream,
|
2013-12-20 21:10:07 +08:00
|
|
|
_("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
|
|
|
|
"\n"
|
|
|
|
"Report bugs to %s.\n"),
|
2012-11-14 06:25:45 +08:00
|
|
|
progname, progname, REPORT_BUGS_TO);
|
2016-01-07 19:45:07 +08:00
|
|
|
if (status == EXIT_SUCCESS)
|
|
|
|
close_file(stream);
|
2009-06-15 23:49:18 +08:00
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
1995-02-18 09:27:10 +08:00
|
|
|
int
|
2012-11-10 05:20:57 +08:00
|
|
|
main(int argc, char *argv[])
|
1995-02-18 09:27:10 +08:00
|
|
|
{
|
2016-01-07 19:45:07 +08:00
|
|
|
/* These are static so that they're initially zero. */
|
|
|
|
static char * abbrev;
|
|
|
|
static size_t abbrevsize;
|
|
|
|
static struct tm newtm;
|
|
|
|
|
Wed Mar 8 13:38:13 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
* posix/glob/configure.bat: Fixes from DJ.
* time/backward, time/europe, time/northamerica, time/pacificnew,
time/zdump.c, time/zic.c, time/tzfile.h, time/private.h,
time/ialloc.c: Code and data updated from ADO's 95b.
* time/emkdir.c: File removed.
* time/Makefile (distribute, extra-objs, zic): Omit it.
* time/localtime.c: Deansideclized. Never #define __tzname et al
to non-__ names.
* locale/lc-ctype.c (__ctype_tolower, __ctype_toupper): Use int *
instead of short int *.
* ctype/ctype-info.c: Likewise.
* ctype/ctype.h: Likewise.
* locale/langinfo.h (_NL_CTYPE_CLASS): Use this (just one) instead
of EB and EL versions.
1995-03-09 18:00:12 +08:00
|
|
|
register int i;
|
2016-01-07 19:45:07 +08:00
|
|
|
register bool vflag;
|
|
|
|
register bool Vflag;
|
2005-01-26 08:01:20 +08:00
|
|
|
register char * cutarg;
|
2013-12-20 21:10:07 +08:00
|
|
|
register char * cuttimes;
|
2005-01-26 08:01:20 +08:00
|
|
|
register time_t cutlotime;
|
|
|
|
register time_t cuthitime;
|
1995-02-18 09:27:10 +08:00
|
|
|
time_t now;
|
Wed Mar 8 13:38:13 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
* posix/glob/configure.bat: Fixes from DJ.
* time/backward, time/europe, time/northamerica, time/pacificnew,
time/zdump.c, time/zic.c, time/tzfile.h, time/private.h,
time/ialloc.c: Code and data updated from ADO's 95b.
* time/emkdir.c: File removed.
* time/Makefile (distribute, extra-objs, zic): Omit it.
* time/localtime.c: Deansideclized. Never #define __tzname et al
to non-__ names.
* locale/lc-ctype.c (__ctype_tolower, __ctype_toupper): Use int *
instead of short int *.
* ctype/ctype-info.c: Likewise.
* ctype/ctype.h: Likewise.
* locale/langinfo.h (_NL_CTYPE_CLASS): Use this (just one) instead
of EB and EL versions.
1995-03-09 18:00:12 +08:00
|
|
|
time_t t;
|
|
|
|
time_t newt;
|
|
|
|
struct tm tm;
|
2005-01-26 08:01:20 +08:00
|
|
|
register struct tm * tmp;
|
|
|
|
register struct tm * newtmp;
|
1995-02-18 09:27:10 +08:00
|
|
|
|
2013-12-20 21:10:07 +08:00
|
|
|
cutlotime = absolute_min_time;
|
|
|
|
cuthitime = absolute_max_time;
|
2004-10-12 14:19:23 +08:00
|
|
|
#if HAVE_GETTEXT
|
2016-01-07 19:45:07 +08:00
|
|
|
setlocale(LC_ALL, "");
|
1996-02-27 08:55:03 +08:00
|
|
|
#ifdef TZ_DOMAINDIR
|
2016-01-07 19:45:07 +08:00
|
|
|
bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
|
2004-10-12 14:19:23 +08:00
|
|
|
#endif /* defined TEXTDOMAINDIR */
|
2016-01-07 19:45:07 +08:00
|
|
|
textdomain(TZ_DOMAIN);
|
2004-10-12 14:19:23 +08:00
|
|
|
#endif /* HAVE_GETTEXT */
|
1995-02-18 09:27:10 +08:00
|
|
|
progname = argv[0];
|
2003-09-23 03:51:38 +08:00
|
|
|
for (i = 1; i < argc; ++i)
|
|
|
|
if (strcmp(argv[i], "--version") == 0) {
|
2016-01-07 19:45:07 +08:00
|
|
|
printf("zdump %s%s\n", PKGVERSION, TZVERSION);
|
|
|
|
return EXIT_SUCCESS;
|
2009-06-15 23:49:18 +08:00
|
|
|
} else if (strcmp(argv[i], "--help") == 0) {
|
2012-04-26 02:50:39 +08:00
|
|
|
usage(stdout, EXIT_SUCCESS);
|
2003-09-23 03:51:38 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
vflag = Vflag = false;
|
2013-12-20 21:10:07 +08:00
|
|
|
cutarg = cuttimes = NULL;
|
|
|
|
for (;;)
|
|
|
|
switch (getopt(argc, argv, "c:t:vV")) {
|
|
|
|
case 'c': cutarg = optarg; break;
|
|
|
|
case 't': cuttimes = optarg; break;
|
2016-01-07 19:45:07 +08:00
|
|
|
case 'v': vflag = true; break;
|
|
|
|
case 'V': Vflag = true; break;
|
2013-12-20 21:10:07 +08:00
|
|
|
case -1:
|
|
|
|
if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
|
|
|
|
goto arg_processing_done;
|
|
|
|
/* Fall through. */
|
|
|
|
default:
|
|
|
|
usage(stderr, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
arg_processing_done:;
|
|
|
|
|
|
|
|
if (vflag | Vflag) {
|
|
|
|
intmax_t lo;
|
|
|
|
intmax_t hi;
|
|
|
|
char *loend, *hiend;
|
|
|
|
register intmax_t cutloyear = ZDUMP_LO_YEAR;
|
|
|
|
register intmax_t cuthiyear = ZDUMP_HI_YEAR;
|
2005-01-26 08:01:20 +08:00
|
|
|
if (cutarg != NULL) {
|
2013-12-20 21:10:07 +08:00
|
|
|
lo = strtoimax(cutarg, &loend, 10);
|
|
|
|
if (cutarg != loend && !*loend) {
|
|
|
|
hi = lo;
|
|
|
|
cuthiyear = hi;
|
|
|
|
} else if (cutarg != loend && *loend == ','
|
|
|
|
&& (hi = strtoimax(loend + 1, &hiend, 10),
|
|
|
|
loend + 1 != hiend && !*hiend)) {
|
|
|
|
cutloyear = lo;
|
2005-01-26 08:01:20 +08:00
|
|
|
cuthiyear = hi;
|
|
|
|
} else {
|
2016-01-07 19:45:07 +08:00
|
|
|
fprintf(stderr, _("%s: wild -c argument %s\n"),
|
2005-01-26 08:01:20 +08:00
|
|
|
progname, cutarg);
|
2016-01-07 19:45:07 +08:00
|
|
|
return EXIT_FAILURE;
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 21:10:07 +08:00
|
|
|
if (cutarg != NULL || cuttimes == NULL) {
|
|
|
|
cutlotime = yeartot(cutloyear);
|
|
|
|
cuthitime = yeartot(cuthiyear);
|
|
|
|
}
|
|
|
|
if (cuttimes != NULL) {
|
|
|
|
lo = strtoimax(cuttimes, &loend, 10);
|
|
|
|
if (cuttimes != loend && !*loend) {
|
|
|
|
hi = lo;
|
|
|
|
if (hi < cuthitime) {
|
|
|
|
if (hi < absolute_min_time)
|
|
|
|
hi = absolute_min_time;
|
|
|
|
cuthitime = hi;
|
|
|
|
}
|
|
|
|
} else if (cuttimes != loend && *loend == ','
|
|
|
|
&& (hi = strtoimax(loend + 1, &hiend, 10),
|
|
|
|
loend + 1 != hiend && !*hiend)) {
|
|
|
|
if (cutlotime < lo) {
|
|
|
|
if (absolute_max_time < lo)
|
|
|
|
lo = absolute_max_time;
|
|
|
|
cutlotime = lo;
|
|
|
|
}
|
|
|
|
if (hi < cuthitime) {
|
|
|
|
if (hi < absolute_min_time)
|
|
|
|
hi = absolute_min_time;
|
|
|
|
cuthitime = hi;
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-07 19:45:07 +08:00
|
|
|
fprintf(stderr,
|
2013-12-20 21:10:07 +08:00
|
|
|
_("%s: wild -t argument %s\n"),
|
|
|
|
progname, cuttimes);
|
2016-01-07 19:45:07 +08:00
|
|
|
return EXIT_FAILURE;
|
2013-12-20 21:10:07 +08:00
|
|
|
}
|
|
|
|
}
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
gmtzinit();
|
|
|
|
now = time(NULL);
|
1995-02-18 09:27:10 +08:00
|
|
|
longest = 0;
|
2016-01-07 19:45:07 +08:00
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
size_t arglen = strlen(argv[i]);
|
|
|
|
if (longest < arglen)
|
|
|
|
longest = arglen < INT_MAX ? arglen : INT_MAX;
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
for (i = optind; i < argc; ++i) {
|
|
|
|
timezone_t tz = tzalloc(argv[i]);
|
|
|
|
char const *ab;
|
|
|
|
if (!tz) {
|
|
|
|
perror(argv[i]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-12-20 21:10:07 +08:00
|
|
|
if (! (vflag | Vflag)) {
|
2016-01-07 19:45:07 +08:00
|
|
|
show(tz, argv[i], now, false);
|
|
|
|
tzfree(tz);
|
1995-02-18 09:27:10 +08:00
|
|
|
continue;
|
* time/tzfile.h, time/private.h, time/zdump.c, time/zic.c,
time/africa, time/asia, time/australasia, time/europe,
time/northamerica, time/southamerica: Updated from ADO 96h.
* Make-dist (all-headers): Filter $(sysdep_headers) out of $(headers).
(try-sysdeps, +sysdep-names): Check sysdeps/libm-ieee754 too.
* Makerules (distinfo-vars): Add sysdep_headers.
* sysdeps/unix/sysv/linux/Makefile (sysdep_headers): Append to
this instead of $(headers).
* sysdeps/unix/sysv/linux/alpha/Makefile: Likewise.
* Make-dist [subdirs-dirs]: Don't prefix $(subdir-dirs) with
$(subdir); we are already there.
* sysdeps/unix/sysv/sysv4/solaris2/sparc/Dist: Removed.
1996-06-06 07:25:04 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
warned = false;
|
2005-01-26 08:01:20 +08:00
|
|
|
t = absolute_min_time;
|
2013-12-20 21:10:07 +08:00
|
|
|
if (!Vflag) {
|
2016-01-07 19:45:07 +08:00
|
|
|
show(tz, argv[i], t, true);
|
2013-12-20 21:10:07 +08:00
|
|
|
t += SECSPERDAY;
|
2016-01-07 19:45:07 +08:00
|
|
|
show(tz, argv[i], t, true);
|
2013-12-20 21:10:07 +08:00
|
|
|
}
|
2005-01-26 08:01:20 +08:00
|
|
|
if (t < cutlotime)
|
|
|
|
t = cutlotime;
|
2016-01-07 19:45:07 +08:00
|
|
|
tmp = my_localtime_rz(tz, &t, &tm);
|
|
|
|
if (tmp)
|
|
|
|
ab = saveabbr(&abbrev, &abbrevsize, &tm);
|
|
|
|
while (t < cuthitime) {
|
|
|
|
newt = ((t < absolute_max_time - SECSPERDAY / 2
|
|
|
|
&& t + SECSPERDAY / 2 < cuthitime)
|
2013-12-20 21:10:07 +08:00
|
|
|
? t + SECSPERDAY / 2
|
2016-01-07 19:45:07 +08:00
|
|
|
: cuthitime);
|
|
|
|
newtmp = localtime_rz(tz, &newt, &newtm);
|
2005-04-05 04:47:29 +08:00
|
|
|
if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
|
2005-01-26 08:01:20 +08:00
|
|
|
(delta(&newtm, &tm) != (newt - t) ||
|
1995-02-18 09:27:10 +08:00
|
|
|
newtm.tm_isdst != tm.tm_isdst ||
|
2016-01-07 19:45:07 +08:00
|
|
|
strcmp(abbr(&newtm), ab) != 0)) {
|
|
|
|
newt = hunt(tz, argv[i], t, newt);
|
|
|
|
newtmp = localtime_rz(tz, &newt, &newtm);
|
|
|
|
if (newtmp)
|
|
|
|
ab = saveabbr(&abbrev, &abbrevsize,
|
|
|
|
&newtm);
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
|
|
|
t = newt;
|
|
|
|
tm = newtm;
|
2005-01-26 08:01:20 +08:00
|
|
|
tmp = newtmp;
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
2013-12-20 21:10:07 +08:00
|
|
|
if (!Vflag) {
|
|
|
|
t = absolute_max_time;
|
|
|
|
t -= SECSPERDAY;
|
2016-01-07 19:45:07 +08:00
|
|
|
show(tz, argv[i], t, true);
|
2013-12-20 21:10:07 +08:00
|
|
|
t += SECSPERDAY;
|
2016-01-07 19:45:07 +08:00
|
|
|
show(tz, argv[i], t, true);
|
2013-12-20 21:10:07 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
tzfree(tz);
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
close_file(stdout);
|
|
|
|
if (errout && (ferror(stderr) || fclose(stderr) != 0))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
return EXIT_SUCCESS;
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static time_t
|
2016-01-07 19:45:07 +08:00
|
|
|
yeartot(intmax_t y)
|
2005-01-26 08:01:20 +08:00
|
|
|
{
|
2013-12-20 21:10:07 +08:00
|
|
|
register intmax_t myy, seconds, years;
|
|
|
|
register time_t t;
|
2005-01-26 08:01:20 +08:00
|
|
|
|
|
|
|
myy = EPOCH_YEAR;
|
|
|
|
t = 0;
|
2013-12-20 21:10:07 +08:00
|
|
|
while (myy < y) {
|
|
|
|
if (SECSPER400YEARS_FITS && 400 <= y - myy) {
|
|
|
|
intmax_t diff400 = (y - myy) / 400;
|
|
|
|
if (INTMAX_MAX / SECSPER400YEARS < diff400)
|
|
|
|
return absolute_max_time;
|
|
|
|
seconds = diff400 * SECSPER400YEARS;
|
|
|
|
years = diff400 * 400;
|
|
|
|
} else {
|
2005-01-26 08:01:20 +08:00
|
|
|
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
|
2013-12-20 21:10:07 +08:00
|
|
|
years = 1;
|
|
|
|
}
|
|
|
|
myy += years;
|
|
|
|
if (t > absolute_max_time - seconds)
|
|
|
|
return absolute_max_time;
|
|
|
|
t += seconds;
|
|
|
|
}
|
|
|
|
while (y < myy) {
|
|
|
|
if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
|
|
|
|
intmax_t diff400 = (myy - y) / 400;
|
|
|
|
if (INTMAX_MAX / SECSPER400YEARS < diff400)
|
|
|
|
return absolute_min_time;
|
|
|
|
seconds = diff400 * SECSPER400YEARS;
|
|
|
|
years = diff400 * 400;
|
2005-01-26 08:01:20 +08:00
|
|
|
} else {
|
2013-12-20 21:10:07 +08:00
|
|
|
seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
|
|
|
|
years = 1;
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
2013-12-20 21:10:07 +08:00
|
|
|
myy -= years;
|
|
|
|
if (t < absolute_min_time + seconds)
|
|
|
|
return absolute_min_time;
|
|
|
|
t -= seconds;
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
|
|
|
return t;
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static time_t
|
2016-01-07 19:45:07 +08:00
|
|
|
hunt(timezone_t tz, char *name, time_t lot, time_t hit)
|
1995-02-18 09:27:10 +08:00
|
|
|
{
|
2016-01-07 19:45:07 +08:00
|
|
|
static char * loab;
|
|
|
|
static size_t loabsize;
|
|
|
|
char const * ab;
|
2005-01-26 08:01:20 +08:00
|
|
|
time_t t;
|
|
|
|
struct tm lotm;
|
|
|
|
register struct tm * lotmp;
|
|
|
|
struct tm tm;
|
|
|
|
register struct tm * tmp;
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
lotmp = my_localtime_rz(tz, &lot, &lotm);
|
|
|
|
if (lotmp)
|
|
|
|
ab = saveabbr(&loab, &loabsize, &lotm);
|
2005-01-26 08:01:20 +08:00
|
|
|
for ( ; ; ) {
|
2013-12-20 21:10:07 +08:00
|
|
|
time_t diff = hit - lot;
|
2005-01-26 08:01:20 +08:00
|
|
|
if (diff < 2)
|
|
|
|
break;
|
|
|
|
t = lot;
|
|
|
|
t += diff / 2;
|
1995-02-18 09:27:10 +08:00
|
|
|
if (t <= lot)
|
|
|
|
++t;
|
|
|
|
else if (t >= hit)
|
|
|
|
--t;
|
2016-01-07 19:45:07 +08:00
|
|
|
tmp = my_localtime_rz(tz, &t, &tm);
|
2005-01-26 08:01:20 +08:00
|
|
|
if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
|
|
|
|
(delta(&tm, &lotm) == (t - lot) &&
|
1995-02-18 09:27:10 +08:00
|
|
|
tm.tm_isdst == lotm.tm_isdst &&
|
2016-01-07 19:45:07 +08:00
|
|
|
strcmp(abbr(&tm), ab) == 0)) {
|
1995-02-18 09:27:10 +08:00
|
|
|
lot = t;
|
|
|
|
lotm = tm;
|
2005-01-26 08:01:20 +08:00
|
|
|
lotmp = tmp;
|
1995-02-18 09:27:10 +08:00
|
|
|
} else hit = t;
|
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
show(tz, name, lot, true);
|
|
|
|
show(tz, name, hit, true);
|
1995-02-18 09:27:10 +08:00
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-01-10 15:55:46 +08:00
|
|
|
** Thanks to Paul Eggert for logic used in delta.
|
1995-02-18 09:27:10 +08:00
|
|
|
*/
|
|
|
|
|
2013-12-20 21:10:07 +08:00
|
|
|
static intmax_t
|
2012-11-10 05:20:57 +08:00
|
|
|
delta(struct tm * newp, struct tm *oldp)
|
1995-02-18 09:27:10 +08:00
|
|
|
{
|
2013-12-20 21:10:07 +08:00
|
|
|
register intmax_t result;
|
|
|
|
register int tmy;
|
1995-02-18 09:27:10 +08:00
|
|
|
|
|
|
|
if (newp->tm_year < oldp->tm_year)
|
|
|
|
return -delta(oldp, newp);
|
|
|
|
result = 0;
|
|
|
|
for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
|
2005-01-26 08:01:20 +08:00
|
|
|
result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
|
1995-02-18 09:27:10 +08:00
|
|
|
result += newp->tm_yday - oldp->tm_yday;
|
|
|
|
result *= HOURSPERDAY;
|
|
|
|
result += newp->tm_hour - oldp->tm_hour;
|
|
|
|
result *= MINSPERHOUR;
|
|
|
|
result += newp->tm_min - oldp->tm_min;
|
|
|
|
result *= SECSPERMIN;
|
|
|
|
result += newp->tm_sec - oldp->tm_sec;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
#ifndef TM_GMTOFF
|
|
|
|
/* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday.
|
|
|
|
Assume A and B differ by at most one year. */
|
|
|
|
static int
|
|
|
|
adjusted_yday(struct tm const *a, struct tm const *b)
|
|
|
|
{
|
|
|
|
int yday = a->tm_yday;
|
|
|
|
if (b->tm_year < a->tm_year)
|
|
|
|
yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
|
|
|
|
return yday;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If A is the broken-down local time and B the broken-down UTC for
|
|
|
|
the same instant, return A's UTC offset in seconds, where positive
|
|
|
|
offsets are east of Greenwich. On failure, return LONG_MIN. */
|
|
|
|
static long
|
|
|
|
gmtoff(struct tm const *a, struct tm const *b)
|
|
|
|
{
|
|
|
|
#ifdef TM_GMTOFF
|
|
|
|
return a->TM_GMTOFF;
|
|
|
|
#else
|
|
|
|
if (! b)
|
|
|
|
return LONG_MIN;
|
|
|
|
else {
|
|
|
|
int ayday = adjusted_yday(a, b);
|
|
|
|
int byday = adjusted_yday(b, a);
|
|
|
|
int days = ayday - byday;
|
|
|
|
long hours = a->tm_hour - b->tm_hour + 24 * days;
|
|
|
|
long minutes = a->tm_min - b->tm_min + 60 * hours;
|
|
|
|
long seconds = a->tm_sec - b->tm_sec + 60 * minutes;
|
|
|
|
return seconds;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1995-02-18 09:27:10 +08:00
|
|
|
static void
|
2016-01-07 19:45:07 +08:00
|
|
|
show(timezone_t tz, char *zone, time_t t, bool v)
|
1995-02-18 09:27:10 +08:00
|
|
|
{
|
2005-01-26 08:01:20 +08:00
|
|
|
register struct tm * tmp;
|
2016-01-07 19:45:07 +08:00
|
|
|
register struct tm * gmtmp;
|
|
|
|
struct tm tm, gmtm;
|
1995-02-18 09:27:10 +08:00
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
printf("%-*s ", longest, zone);
|
2004-10-12 14:19:23 +08:00
|
|
|
if (v) {
|
2016-01-07 19:45:07 +08:00
|
|
|
gmtmp = my_gmtime_r(&t, &gmtm);
|
|
|
|
if (gmtmp == NULL) {
|
|
|
|
printf(tformat(), t);
|
2005-01-26 08:01:20 +08:00
|
|
|
} else {
|
2016-01-07 19:45:07 +08:00
|
|
|
dumptime(gmtmp);
|
|
|
|
printf(" UT");
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
printf(" = ");
|
2004-10-12 14:19:23 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
tmp = my_localtime_rz(tz, &t, &tm);
|
2004-10-12 14:19:23 +08:00
|
|
|
dumptime(tmp);
|
2005-01-26 08:01:20 +08:00
|
|
|
if (tmp != NULL) {
|
|
|
|
if (*abbr(tmp) != '\0')
|
2016-01-07 19:45:07 +08:00
|
|
|
printf(" %s", abbr(tmp));
|
2005-01-26 08:01:20 +08:00
|
|
|
if (v) {
|
2016-01-07 19:45:07 +08:00
|
|
|
long off = gmtoff(tmp, gmtmp);
|
|
|
|
printf(" isdst=%d", tmp->tm_isdst);
|
|
|
|
if (off != LONG_MIN)
|
|
|
|
printf(" gmtoff=%ld", off);
|
2005-01-26 08:01:20 +08:00
|
|
|
}
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
2016-01-07 19:45:07 +08:00
|
|
|
printf("\n");
|
2005-08-06 13:40:57 +08:00
|
|
|
if (tmp != NULL && *abbr(tmp) != '\0')
|
|
|
|
abbrok(abbr(tmp), zone);
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-07 19:45:07 +08:00
|
|
|
static char const *
|
|
|
|
abbr(struct tm const *tmp)
|
1995-02-18 09:27:10 +08:00
|
|
|
{
|
2016-01-07 19:45:07 +08:00
|
|
|
#ifdef TM_ZONE
|
|
|
|
return tmp->TM_ZONE;
|
|
|
|
#else
|
|
|
|
return (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst]
|
|
|
|
? tzname[0 < tmp->tm_isdst]
|
|
|
|
: "");
|
|
|
|
#endif
|
1995-02-18 09:27:10 +08:00
|
|
|
}
|
2004-10-12 14:19:23 +08:00
|
|
|
|
2005-01-26 08:01:20 +08:00
|
|
|
/*
|
|
|
|
** The code below can fail on certain theoretical systems;
|
|
|
|
** it works on all known real-world systems as of 2004-12-30.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
2008-04-11 15:01:57 +08:00
|
|
|
tformat(void)
|
2005-01-26 08:01:20 +08:00
|
|
|
{
|
|
|
|
if (0 > (time_t) -1) { /* signed */
|
2013-12-20 21:10:07 +08:00
|
|
|
if (sizeof (time_t) == sizeof (intmax_t))
|
|
|
|
return "%"PRIdMAX;
|
2005-01-26 08:01:20 +08:00
|
|
|
if (sizeof (time_t) > sizeof (long))
|
|
|
|
return "%lld";
|
|
|
|
if (sizeof (time_t) > sizeof (int))
|
|
|
|
return "%ld";
|
|
|
|
return "%d";
|
|
|
|
}
|
2013-12-20 21:10:07 +08:00
|
|
|
#ifdef PRIuMAX
|
|
|
|
if (sizeof (time_t) == sizeof (uintmax_t))
|
|
|
|
return "%"PRIuMAX;
|
|
|
|
#endif
|
2005-01-26 08:01:20 +08:00
|
|
|
if (sizeof (time_t) > sizeof (unsigned long))
|
|
|
|
return "%llu";
|
|
|
|
if (sizeof (time_t) > sizeof (unsigned int))
|
|
|
|
return "%lu";
|
|
|
|
return "%u";
|
|
|
|
}
|
|
|
|
|
2004-10-12 14:19:23 +08:00
|
|
|
static void
|
2012-11-10 05:20:57 +08:00
|
|
|
dumptime(register const struct tm *timeptr)
|
2004-10-12 14:19:23 +08:00
|
|
|
{
|
|
|
|
static const char wday_name[][3] = {
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
};
|
|
|
|
static const char mon_name[][3] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
|
|
|
register const char * wn;
|
|
|
|
register const char * mn;
|
2005-01-26 08:01:20 +08:00
|
|
|
register int lead;
|
|
|
|
register int trail;
|
2004-10-12 14:19:23 +08:00
|
|
|
|
2005-01-26 08:01:20 +08:00
|
|
|
if (timeptr == NULL) {
|
2016-01-07 19:45:07 +08:00
|
|
|
printf("NULL");
|
2005-01-26 08:01:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2004-10-12 14:19:23 +08:00
|
|
|
/*
|
2016-01-07 19:45:07 +08:00
|
|
|
** The packaged localtime_rz and gmtime_r never put out-of-range
|
2004-10-12 14:19:23 +08:00
|
|
|
** values in tm_wday or tm_mon, but since this code might be compiled
|
|
|
|
** with other (perhaps experimental) versions, paranoia is in order.
|
|
|
|
*/
|
|
|
|
if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
|
|
|
|
(int) (sizeof wday_name / sizeof wday_name[0]))
|
|
|
|
wn = "???";
|
|
|
|
else wn = wday_name[timeptr->tm_wday];
|
|
|
|
if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
|
|
|
|
(int) (sizeof mon_name / sizeof mon_name[0]))
|
|
|
|
mn = "???";
|
|
|
|
else mn = mon_name[timeptr->tm_mon];
|
2016-01-07 19:45:07 +08:00
|
|
|
printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
|
2004-10-12 14:19:23 +08:00
|
|
|
wn, mn,
|
|
|
|
timeptr->tm_mday, timeptr->tm_hour,
|
2005-01-26 08:01:20 +08:00
|
|
|
timeptr->tm_min, timeptr->tm_sec);
|
|
|
|
#define DIVISOR 10
|
|
|
|
trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
|
|
|
|
lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
|
|
|
|
trail / DIVISOR;
|
|
|
|
trail %= DIVISOR;
|
|
|
|
if (trail < 0 && lead > 0) {
|
|
|
|
trail += DIVISOR;
|
|
|
|
--lead;
|
|
|
|
} else if (lead < 0 && trail > 0) {
|
|
|
|
trail -= DIVISOR;
|
|
|
|
++lead;
|
|
|
|
}
|
|
|
|
if (lead == 0)
|
2016-01-07 19:45:07 +08:00
|
|
|
printf("%d", trail);
|
|
|
|
else printf("%d%d", lead, ((trail < 0) ? -trail : trail));
|
2004-10-12 14:19:23 +08:00
|
|
|
}
|