(root)/
util-linux-2.39/
include/
timeutils.h
       1  /***
       2    SPDX-License-Identifier: LGPL-2.1-or-later
       3  
       4  
       5    First set of functions in this file are part of systemd, and were
       6    copied to util-linux at August 2013.
       7  
       8    Copyright 2010 Lennart Poettering
       9    Copyright (C) 2014 Karel Zak <kzak@redhat.com>
      10  
      11    systemd is free software; you can redistribute it and/or modify it
      12    under the terms of the GNU Lesser General Public License as published by
      13    the Free Software Foundation; either version 2.1 of the License, or
      14    (at your option) any later version.
      15  
      16    systemd is distributed in the hope that it will be useful, but
      17    WITHOUT ANY WARRANTY; without even the implied warranty of
      18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      19    Lesser General Public License for more details.
      20  
      21    You should have received a copy of the GNU Lesser General Public License
      22    along with systemd; If not, see <http://www.gnu.org/licenses/>.
      23  ***/
      24  #ifndef UTIL_LINUX_TIME_UTIL_H
      25  #define UTIL_LINUX_TIME_UTIL_H
      26  
      27  #include <stdio.h>
      28  #include <inttypes.h>
      29  #include <sys/time.h>
      30  
      31  typedef uint64_t usec_t;
      32  typedef uint64_t nsec_t;
      33  
      34  #define MSEC_PER_SEC  1000ULL
      35  #define USEC_PER_SEC  1000000ULL
      36  #define USEC_PER_MSEC 1000ULL
      37  #define NSEC_PER_SEC  1000000000ULL
      38  #define NSEC_PER_MSEC 1000000ULL
      39  #define NSEC_PER_USEC 1000ULL
      40  
      41  #define USEC_PER_MINUTE	(60ULL*USEC_PER_SEC)
      42  #define NSEC_PER_MINUTE	(60ULL*NSEC_PER_SEC)
      43  #define USEC_PER_HOUR	(60ULL*USEC_PER_MINUTE)
      44  #define NSEC_PER_HOUR	(60ULL*NSEC_PER_MINUTE)
      45  #define USEC_PER_DAY	(24ULL*USEC_PER_HOUR)
      46  #define NSEC_PER_DAY	(24ULL*NSEC_PER_HOUR)
      47  #define USEC_PER_WEEK	(7ULL*USEC_PER_DAY)
      48  #define NSEC_PER_WEEK	(7ULL*NSEC_PER_DAY)
      49  #define USEC_PER_MONTH	(2629800ULL*USEC_PER_SEC)
      50  #define NSEC_PER_MONTH	(2629800ULL*NSEC_PER_SEC)
      51  #define USEC_PER_YEAR	(31557600ULL*USEC_PER_SEC)
      52  #define NSEC_PER_YEAR	(31557600ULL*NSEC_PER_SEC)
      53  
      54  #define FORMAT_TIMESTAMP_MAX ((4*4+1)+11+9+4+1)	/* weekdays can be unicode */
      55  #define FORMAT_TIMESTAMP_RELATIVE_MAX 256
      56  #define FORMAT_TIMESPAN_MAX 64
      57  
      58  int parse_timestamp(const char *t, usec_t *usec);
      59  int get_gmtoff(const struct tm *tp);
      60  
      61  /* flags and masks for strxxx_iso() functions */
      62  enum {
      63  	ISO_DATE		= (1 << 0),
      64  	ISO_TIME		= (1 << 1),
      65  	ISO_TIMEZONE		= (1 << 2),
      66  	ISO_DOTUSEC		= (1 << 3),
      67  	ISO_COMMAUSEC		= (1 << 4),
      68  	ISO_T			= (1 << 5),
      69  	ISO_GMTIME		= (1 << 6),
      70  	ISO_TIMESTAMP		= ISO_DATE | ISO_TIME | ISO_TIMEZONE,
      71  	ISO_TIMESTAMP_T		= ISO_TIMESTAMP | ISO_T,
      72  	ISO_TIMESTAMP_DOT	= ISO_TIMESTAMP | ISO_DOTUSEC,
      73  	ISO_TIMESTAMP_DOT_T	= ISO_TIMESTAMP_DOT | ISO_T,
      74  	ISO_TIMESTAMP_COMMA	= ISO_TIMESTAMP | ISO_COMMAUSEC,
      75  	ISO_TIMESTAMP_COMMA_T	= ISO_TIMESTAMP_COMMA | ISO_T,
      76  	ISO_TIMESTAMP_COMMA_G	= ISO_TIMESTAMP_COMMA | ISO_GMTIME,
      77  	ISO_TIMESTAMP_COMMA_GT  = ISO_TIMESTAMP_COMMA_G | ISO_T
      78  };
      79  
      80  #define CTIME_BUFSIZ	26
      81  #define ISO_BUFSIZ	42
      82  
      83  int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz);
      84  int strtm_iso(struct tm *tm, int flags, char *buf, size_t bufsz);
      85  int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz);
      86  
      87  #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1)
      88  
      89  int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz);
      90  
      91  #ifndef HAVE_TIMEGM
      92  extern time_t timegm(struct tm *tm);
      93  #endif
      94  
      95  static inline usec_t timeval_to_usec(const struct timeval *t)
      96  {
      97  	return t->tv_sec * USEC_PER_SEC + t->tv_usec;
      98  }
      99  
     100  static inline usec_t timespec_to_usec(const struct timespec *t)
     101  {
     102  	return t->tv_sec * USEC_PER_SEC + t->tv_nsec / NSEC_PER_USEC;
     103  }
     104  
     105  static inline struct timeval usec_to_timeval(usec_t t)
     106  {
     107  	struct timeval r = {
     108  		.tv_sec = t / USEC_PER_SEC,
     109  		.tv_usec = t % USEC_PER_SEC,
     110  	};
     111  	return r;
     112  }
     113  
     114  #endif /* UTIL_LINUX_TIME_UTIL_H */