Home | History | Annotate | Line # | Download | only in import
time.in.h revision 1.1.1.1
      1  1.1  christos /* A more-standard <time.h>.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 2007-2020 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This program is free software; you can redistribute it and/or modify
      6  1.1  christos    it under the terms of the GNU General Public License as published by
      7  1.1  christos    the Free Software Foundation; either version 3, or (at your option)
      8  1.1  christos    any later version.
      9  1.1  christos 
     10  1.1  christos    This program is distributed in the hope that it will be useful,
     11  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  1.1  christos    GNU General Public License for more details.
     14  1.1  christos 
     15  1.1  christos    You should have received a copy of the GNU General Public License
     16  1.1  christos    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
     17  1.1  christos 
     18  1.1  christos #if __GNUC__ >= 3
     19  1.1  christos @PRAGMA_SYSTEM_HEADER@
     20  1.1  christos #endif
     21  1.1  christos @PRAGMA_COLUMNS@
     22  1.1  christos 
     23  1.1  christos /* Don't get in the way of glibc when it includes time.h merely to
     24  1.1  christos    declare a few standard symbols, rather than to declare all the
     25  1.1  christos    symbols.  (However, skip this for MinGW as it treats __need_time_t
     26  1.1  christos    incompatibly.)  Also, Solaris 8 <time.h> eventually includes itself
     27  1.1  christos    recursively; if that is happening, just include the system <time.h>
     28  1.1  christos    without adding our own declarations.  */
     29  1.1  christos #if (((defined __need_time_t || defined __need_clock_t \
     30  1.1  christos        || defined __need_timespec)                     \
     31  1.1  christos       && !defined __MINGW32__)                         \
     32  1.1  christos      || defined _@GUARD_PREFIX@_TIME_H)
     33  1.1  christos 
     34  1.1  christos # @INCLUDE_NEXT@ @NEXT_TIME_H@
     35  1.1  christos 
     36  1.1  christos #else
     37  1.1  christos 
     38  1.1  christos # define _@GUARD_PREFIX@_TIME_H
     39  1.1  christos 
     40  1.1  christos /* mingw's <time.h> provides the functions asctime_r, ctime_r, gmtime_r,
     41  1.1  christos    localtime_r only if <unistd.h> or <pthread.h> has been included before.  */
     42  1.1  christos # if defined __MINGW32__
     43  1.1  christos #  include <unistd.h>
     44  1.1  christos # endif
     45  1.1  christos 
     46  1.1  christos # @INCLUDE_NEXT@ @NEXT_TIME_H@
     47  1.1  christos 
     48  1.1  christos /* NetBSD 5.0 mis-defines NULL.  */
     49  1.1  christos # include <stddef.h>
     50  1.1  christos 
     51  1.1  christos /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
     52  1.1  christos 
     53  1.1  christos /* The definition of _GL_ARG_NONNULL is copied here.  */
     54  1.1  christos 
     55  1.1  christos /* The definition of _GL_WARN_ON_USE is copied here.  */
     56  1.1  christos 
     57  1.1  christos /* Some systems don't define struct timespec (e.g., AIX 4.1).
     58  1.1  christos    Or they define it with the wrong member names or define it in <sys/time.h>
     59  1.1  christos    (e.g., FreeBSD circa 1997).  Stock Mingw prior to 3.0 does not define it,
     60  1.1  christos    but the pthreads-win32 library defines it in <pthread.h>.  */
     61  1.1  christos # if ! @TIME_H_DEFINES_STRUCT_TIMESPEC@
     62  1.1  christos #  if @SYS_TIME_H_DEFINES_STRUCT_TIMESPEC@
     63  1.1  christos #   include <sys/time.h>
     64  1.1  christos #  elif @PTHREAD_H_DEFINES_STRUCT_TIMESPEC@
     65  1.1  christos #   include <pthread.h>
     66  1.1  christos #  elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@
     67  1.1  christos #   include <unistd.h>
     68  1.1  christos #  else
     69  1.1  christos 
     70  1.1  christos #   ifdef __cplusplus
     71  1.1  christos extern "C" {
     72  1.1  christos #   endif
     73  1.1  christos 
     74  1.1  christos #   if !GNULIB_defined_struct_timespec
     75  1.1  christos #    undef timespec
     76  1.1  christos #    define timespec rpl_timespec
     77  1.1  christos struct timespec
     78  1.1  christos {
     79  1.1  christos   time_t tv_sec;
     80  1.1  christos   long int tv_nsec;
     81  1.1  christos };
     82  1.1  christos #    define GNULIB_defined_struct_timespec 1
     83  1.1  christos #   endif
     84  1.1  christos 
     85  1.1  christos #   ifdef __cplusplus
     86  1.1  christos }
     87  1.1  christos #   endif
     88  1.1  christos 
     89  1.1  christos #  endif
     90  1.1  christos # endif
     91  1.1  christos 
     92  1.1  christos # if !GNULIB_defined_struct_time_t_must_be_integral
     93  1.1  christos /* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
     94  1.1  christos    requires time_t to be an integer type, even though C99 permits floating
     95  1.1  christos    point.  We don't know of any implementation that uses floating
     96  1.1  christos    point, and it is much easier to write code that doesn't have to
     97  1.1  christos    worry about that corner case, so we force the issue.  */
     98  1.1  christos struct __time_t_must_be_integral {
     99  1.1  christos   unsigned int __floating_time_t_unsupported : (time_t) 1;
    100  1.1  christos };
    101  1.1  christos #  define GNULIB_defined_struct_time_t_must_be_integral 1
    102  1.1  christos # endif
    103  1.1  christos 
    104  1.1  christos /* Sleep for at least RQTP seconds unless interrupted,  If interrupted,
    105  1.1  christos    return -1 and store the remaining time into RMTP.  See
    106  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>.  */
    107  1.1  christos # if @GNULIB_NANOSLEEP@
    108  1.1  christos #  if @REPLACE_NANOSLEEP@
    109  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    110  1.1  christos #    define nanosleep rpl_nanosleep
    111  1.1  christos #   endif
    112  1.1  christos _GL_FUNCDECL_RPL (nanosleep, int,
    113  1.1  christos                   (struct timespec const *__rqtp, struct timespec *__rmtp)
    114  1.1  christos                   _GL_ARG_NONNULL ((1)));
    115  1.1  christos _GL_CXXALIAS_RPL (nanosleep, int,
    116  1.1  christos                   (struct timespec const *__rqtp, struct timespec *__rmtp));
    117  1.1  christos #  else
    118  1.1  christos #   if ! @HAVE_NANOSLEEP@
    119  1.1  christos _GL_FUNCDECL_SYS (nanosleep, int,
    120  1.1  christos                   (struct timespec const *__rqtp, struct timespec *__rmtp)
    121  1.1  christos                   _GL_ARG_NONNULL ((1)));
    122  1.1  christos #   endif
    123  1.1  christos _GL_CXXALIAS_SYS (nanosleep, int,
    124  1.1  christos                   (struct timespec const *__rqtp, struct timespec *__rmtp));
    125  1.1  christos #  endif
    126  1.1  christos _GL_CXXALIASWARN (nanosleep);
    127  1.1  christos # endif
    128  1.1  christos 
    129  1.1  christos /* Initialize time conversion information.  */
    130  1.1  christos # if @GNULIB_TZSET@
    131  1.1  christos #  if @REPLACE_TZSET@
    132  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    133  1.1  christos #    undef tzset
    134  1.1  christos #    define tzset rpl_tzset
    135  1.1  christos #   endif
    136  1.1  christos _GL_FUNCDECL_RPL (tzset, void, (void));
    137  1.1  christos _GL_CXXALIAS_RPL (tzset, void, (void));
    138  1.1  christos #  else
    139  1.1  christos #   if ! @HAVE_TZSET@
    140  1.1  christos _GL_FUNCDECL_SYS (tzset, void, (void));
    141  1.1  christos #   endif
    142  1.1  christos _GL_CXXALIAS_SYS (tzset, void, (void));
    143  1.1  christos #  endif
    144  1.1  christos _GL_CXXALIASWARN (tzset);
    145  1.1  christos # endif
    146  1.1  christos 
    147  1.1  christos /* Return the 'time_t' representation of TP and normalize TP.  */
    148  1.1  christos # if @GNULIB_MKTIME@
    149  1.1  christos #  if @REPLACE_MKTIME@
    150  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    151  1.1  christos #    define mktime rpl_mktime
    152  1.1  christos #   endif
    153  1.1  christos _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
    154  1.1  christos _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
    155  1.1  christos #  else
    156  1.1  christos _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
    157  1.1  christos #  endif
    158  1.1  christos #  if __GLIBC__ >= 2
    159  1.1  christos _GL_CXXALIASWARN (mktime);
    160  1.1  christos #  endif
    161  1.1  christos # endif
    162  1.1  christos 
    163  1.1  christos /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
    164  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
    165  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>.  */
    166  1.1  christos # if @GNULIB_TIME_R@
    167  1.1  christos #  if @REPLACE_LOCALTIME_R@
    168  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    169  1.1  christos #    undef localtime_r
    170  1.1  christos #    define localtime_r rpl_localtime_r
    171  1.1  christos #   endif
    172  1.1  christos _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
    173  1.1  christos                                              struct tm *restrict __result)
    174  1.1  christos                                             _GL_ARG_NONNULL ((1, 2)));
    175  1.1  christos _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
    176  1.1  christos                                              struct tm *restrict __result));
    177  1.1  christos #  else
    178  1.1  christos #   if ! @HAVE_DECL_LOCALTIME_R@
    179  1.1  christos _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
    180  1.1  christos                                              struct tm *restrict __result)
    181  1.1  christos                                             _GL_ARG_NONNULL ((1, 2)));
    182  1.1  christos #   endif
    183  1.1  christos _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
    184  1.1  christos                                              struct tm *restrict __result));
    185  1.1  christos #  endif
    186  1.1  christos #  if @HAVE_DECL_LOCALTIME_R@
    187  1.1  christos _GL_CXXALIASWARN (localtime_r);
    188  1.1  christos #  endif
    189  1.1  christos #  if @REPLACE_LOCALTIME_R@
    190  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    191  1.1  christos #    undef gmtime_r
    192  1.1  christos #    define gmtime_r rpl_gmtime_r
    193  1.1  christos #   endif
    194  1.1  christos _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
    195  1.1  christos                                           struct tm *restrict __result)
    196  1.1  christos                                          _GL_ARG_NONNULL ((1, 2)));
    197  1.1  christos _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
    198  1.1  christos                                           struct tm *restrict __result));
    199  1.1  christos #  else
    200  1.1  christos #   if ! @HAVE_DECL_LOCALTIME_R@
    201  1.1  christos _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
    202  1.1  christos                                           struct tm *restrict __result)
    203  1.1  christos                                          _GL_ARG_NONNULL ((1, 2)));
    204  1.1  christos #   endif
    205  1.1  christos _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
    206  1.1  christos                                           struct tm *restrict __result));
    207  1.1  christos #  endif
    208  1.1  christos #  if @HAVE_DECL_LOCALTIME_R@
    209  1.1  christos _GL_CXXALIASWARN (gmtime_r);
    210  1.1  christos #  endif
    211  1.1  christos # endif
    212  1.1  christos 
    213  1.1  christos /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
    214  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
    215  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>.  */
    216  1.1  christos # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
    217  1.1  christos #  if @REPLACE_LOCALTIME@
    218  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    219  1.1  christos #    undef localtime
    220  1.1  christos #    define localtime rpl_localtime
    221  1.1  christos #   endif
    222  1.1  christos _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
    223  1.1  christos                                           _GL_ARG_NONNULL ((1)));
    224  1.1  christos _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
    225  1.1  christos #  else
    226  1.1  christos _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
    227  1.1  christos #  endif
    228  1.1  christos #  if __GLIBC__ >= 2
    229  1.1  christos _GL_CXXALIASWARN (localtime);
    230  1.1  christos #  endif
    231  1.1  christos # endif
    232  1.1  christos 
    233  1.1  christos # if 0 || @REPLACE_GMTIME@
    234  1.1  christos #  if @REPLACE_GMTIME@
    235  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    236  1.1  christos #    undef gmtime
    237  1.1  christos #    define gmtime rpl_gmtime
    238  1.1  christos #   endif
    239  1.1  christos _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
    240  1.1  christos                                        _GL_ARG_NONNULL ((1)));
    241  1.1  christos _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
    242  1.1  christos #  else
    243  1.1  christos _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
    244  1.1  christos #  endif
    245  1.1  christos _GL_CXXALIASWARN (gmtime);
    246  1.1  christos # endif
    247  1.1  christos 
    248  1.1  christos /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
    249  1.1  christos    the resulting broken-down time into TM.  See
    250  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>.  */
    251  1.1  christos # if @GNULIB_STRPTIME@
    252  1.1  christos #  if ! @HAVE_STRPTIME@
    253  1.1  christos _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
    254  1.1  christos                                      char const *restrict __format,
    255  1.1  christos                                      struct tm *restrict __tm)
    256  1.1  christos                                     _GL_ARG_NONNULL ((1, 2, 3)));
    257  1.1  christos #  endif
    258  1.1  christos _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
    259  1.1  christos                                      char const *restrict __format,
    260  1.1  christos                                      struct tm *restrict __tm));
    261  1.1  christos _GL_CXXALIASWARN (strptime);
    262  1.1  christos # endif
    263  1.1  christos 
    264  1.1  christos /* Convert *TP to a date and time string.  See
    265  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>.  */
    266  1.1  christos # if @GNULIB_CTIME@
    267  1.1  christos #  if @REPLACE_CTIME@
    268  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    269  1.1  christos #    define ctime rpl_ctime
    270  1.1  christos #   endif
    271  1.1  christos _GL_FUNCDECL_RPL (ctime, char *, (time_t const *__tp)
    272  1.1  christos                                  _GL_ARG_NONNULL ((1)));
    273  1.1  christos _GL_CXXALIAS_RPL (ctime, char *, (time_t const *__tp));
    274  1.1  christos #  else
    275  1.1  christos _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp));
    276  1.1  christos #  endif
    277  1.1  christos #  if __GLIBC__ >= 2
    278  1.1  christos _GL_CXXALIASWARN (ctime);
    279  1.1  christos #  endif
    280  1.1  christos # endif
    281  1.1  christos 
    282  1.1  christos /* Convert *TP to a date and time string.  See
    283  1.1  christos    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>.  */
    284  1.1  christos # if @GNULIB_STRFTIME@
    285  1.1  christos #  if @REPLACE_STRFTIME@
    286  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    287  1.1  christos #    define strftime rpl_strftime
    288  1.1  christos #   endif
    289  1.1  christos _GL_FUNCDECL_RPL (strftime, size_t,
    290  1.1  christos                   (char *restrict __buf, size_t __bufsize,
    291  1.1  christos                    const char *restrict __fmt, const struct tm *restrict __tp)
    292  1.1  christos                   _GL_ARG_NONNULL ((1, 3, 4)));
    293  1.1  christos _GL_CXXALIAS_RPL (strftime, size_t,
    294  1.1  christos                   (char *restrict __buf, size_t __bufsize,
    295  1.1  christos                    const char *restrict __fmt, const struct tm *restrict __tp));
    296  1.1  christos #  else
    297  1.1  christos _GL_CXXALIAS_SYS (strftime, size_t,
    298  1.1  christos                   (char *restrict __buf, size_t __bufsize,
    299  1.1  christos                    const char *restrict __fmt, const struct tm *restrict __tp));
    300  1.1  christos #  endif
    301  1.1  christos #  if __GLIBC__ >= 2
    302  1.1  christos _GL_CXXALIASWARN (strftime);
    303  1.1  christos #  endif
    304  1.1  christos # endif
    305  1.1  christos 
    306  1.1  christos # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
    307  1.1  christos typedef struct tm_zone *timezone_t;
    308  1.1  christos _GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name));
    309  1.1  christos _GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name));
    310  1.1  christos _GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz));
    311  1.1  christos _GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz));
    312  1.1  christos _GL_FUNCDECL_SYS (localtime_rz, struct tm *,
    313  1.1  christos                   (timezone_t __tz, time_t const *restrict __timer,
    314  1.1  christos                    struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3)));
    315  1.1  christos _GL_CXXALIAS_SYS (localtime_rz, struct tm *,
    316  1.1  christos                   (timezone_t __tz, time_t const *restrict __timer,
    317  1.1  christos                    struct tm *restrict __result));
    318  1.1  christos _GL_FUNCDECL_SYS (mktime_z, time_t,
    319  1.1  christos                   (timezone_t __tz, struct tm *restrict __result)
    320  1.1  christos                   _GL_ARG_NONNULL ((2)));
    321  1.1  christos _GL_CXXALIAS_SYS (mktime_z, time_t,
    322  1.1  christos                   (timezone_t __tz, struct tm *restrict __result));
    323  1.1  christos # endif
    324  1.1  christos 
    325  1.1  christos /* Convert TM to a time_t value, assuming UTC.  */
    326  1.1  christos # if @GNULIB_TIMEGM@
    327  1.1  christos #  if @REPLACE_TIMEGM@
    328  1.1  christos #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
    329  1.1  christos #    undef timegm
    330  1.1  christos #    define timegm rpl_timegm
    331  1.1  christos #   endif
    332  1.1  christos _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
    333  1.1  christos _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
    334  1.1  christos #  else
    335  1.1  christos #   if ! @HAVE_TIMEGM@
    336  1.1  christos _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
    337  1.1  christos #   endif
    338  1.1  christos _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
    339  1.1  christos #  endif
    340  1.1  christos _GL_CXXALIASWARN (timegm);
    341  1.1  christos # endif
    342  1.1  christos 
    343  1.1  christos /* Encourage applications to avoid unsafe functions that can overrun
    344  1.1  christos    buffers when given outlandish struct tm values.  Portable
    345  1.1  christos    applications should use strftime (or even sprintf) instead.  */
    346  1.1  christos # if defined GNULIB_POSIXCHECK
    347  1.1  christos #  undef asctime
    348  1.1  christos _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
    349  1.1  christos                  "better use strftime (or even sprintf) instead");
    350  1.1  christos # endif
    351  1.1  christos # if defined GNULIB_POSIXCHECK
    352  1.1  christos #  undef asctime_r
    353  1.1  christos _GL_WARN_ON_USE (asctime, "asctime_r can overrun buffers in some cases - "
    354  1.1  christos                  "better use strftime (or even sprintf) instead");
    355  1.1  christos # endif
    356  1.1  christos # if defined GNULIB_POSIXCHECK
    357  1.1  christos #  undef ctime
    358  1.1  christos _GL_WARN_ON_USE (asctime, "ctime can overrun buffers in some cases - "
    359  1.1  christos                  "better use strftime (or even sprintf) instead");
    360  1.1  christos # endif
    361  1.1  christos # if defined GNULIB_POSIXCHECK
    362  1.1  christos #  undef ctime_r
    363  1.1  christos _GL_WARN_ON_USE (asctime, "ctime_r can overrun buffers in some cases - "
    364  1.1  christos                  "better use strftime (or even sprintf) instead");
    365  1.1  christos # endif
    366  1.1  christos 
    367  1.1  christos #endif
    368