LCOV - code coverage report
Current view: top level - basic - time-util.h (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 24 24 100.0 %
Date: 2019-08-23 13:36:53 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 19 24 79.2 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : #pragma once
       3                 :            : 
       4                 :            : #include <inttypes.h>
       5                 :            : #include <stdbool.h>
       6                 :            : #include <stddef.h>
       7                 :            : #include <stdint.h>
       8                 :            : #include <stdio.h>
       9                 :            : #include <time.h>
      10                 :            : 
      11                 :            : typedef uint64_t usec_t;
      12                 :            : typedef uint64_t nsec_t;
      13                 :            : 
      14                 :            : #define PRI_NSEC PRIu64
      15                 :            : #define PRI_USEC PRIu64
      16                 :            : #define NSEC_FMT "%" PRI_NSEC
      17                 :            : #define USEC_FMT "%" PRI_USEC
      18                 :            : 
      19                 :            : #include "macro.h"
      20                 :            : 
      21                 :            : typedef struct dual_timestamp {
      22                 :            :         usec_t realtime;
      23                 :            :         usec_t monotonic;
      24                 :            : } dual_timestamp;
      25                 :            : 
      26                 :            : typedef struct triple_timestamp {
      27                 :            :         usec_t realtime;
      28                 :            :         usec_t monotonic;
      29                 :            :         usec_t boottime;
      30                 :            : } triple_timestamp;
      31                 :            : 
      32                 :            : #define USEC_INFINITY ((usec_t) -1)
      33                 :            : #define NSEC_INFINITY ((nsec_t) -1)
      34                 :            : 
      35                 :            : #define MSEC_PER_SEC  1000ULL
      36                 :            : #define USEC_PER_SEC  ((usec_t) 1000000ULL)
      37                 :            : #define USEC_PER_MSEC ((usec_t) 1000ULL)
      38                 :            : #define NSEC_PER_SEC  ((nsec_t) 1000000000ULL)
      39                 :            : #define NSEC_PER_MSEC ((nsec_t) 1000000ULL)
      40                 :            : #define NSEC_PER_USEC ((nsec_t) 1000ULL)
      41                 :            : 
      42                 :            : #define USEC_PER_MINUTE ((usec_t) (60ULL*USEC_PER_SEC))
      43                 :            : #define NSEC_PER_MINUTE ((nsec_t) (60ULL*NSEC_PER_SEC))
      44                 :            : #define USEC_PER_HOUR ((usec_t) (60ULL*USEC_PER_MINUTE))
      45                 :            : #define NSEC_PER_HOUR ((nsec_t) (60ULL*NSEC_PER_MINUTE))
      46                 :            : #define USEC_PER_DAY ((usec_t) (24ULL*USEC_PER_HOUR))
      47                 :            : #define NSEC_PER_DAY ((nsec_t) (24ULL*NSEC_PER_HOUR))
      48                 :            : #define USEC_PER_WEEK ((usec_t) (7ULL*USEC_PER_DAY))
      49                 :            : #define NSEC_PER_WEEK ((nsec_t) (7ULL*NSEC_PER_DAY))
      50                 :            : #define USEC_PER_MONTH ((usec_t) (2629800ULL*USEC_PER_SEC))
      51                 :            : #define NSEC_PER_MONTH ((nsec_t) (2629800ULL*NSEC_PER_SEC))
      52                 :            : #define USEC_PER_YEAR ((usec_t) (31557600ULL*USEC_PER_SEC))
      53                 :            : #define NSEC_PER_YEAR ((nsec_t) (31557600ULL*NSEC_PER_SEC))
      54                 :            : 
      55                 :            : /* We assume a maximum timezone length of 6. TZNAME_MAX is not defined on Linux, but glibc internally initializes this
      56                 :            :  * to 6. Let's rely on that. */
      57                 :            : #define FORMAT_TIMESTAMP_MAX (3+1+10+1+8+1+6+1+6+1)
      58                 :            : #define FORMAT_TIMESTAMP_WIDTH 28 /* when outputting, assume this width */
      59                 :            : #define FORMAT_TIMESTAMP_RELATIVE_MAX 256
      60                 :            : #define FORMAT_TIMESPAN_MAX 64
      61                 :            : 
      62                 :            : #define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1)
      63                 :            : 
      64                 :            : #define DUAL_TIMESTAMP_NULL ((struct dual_timestamp) {})
      65                 :            : #define TRIPLE_TIMESTAMP_NULL ((struct triple_timestamp) {})
      66                 :            : 
      67                 :            : usec_t now(clockid_t clock);
      68                 :            : nsec_t now_nsec(clockid_t clock);
      69                 :            : 
      70                 :            : dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
      71                 :            : dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
      72                 :            : dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u);
      73                 :            : dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u);
      74                 :            : 
      75                 :            : triple_timestamp* triple_timestamp_get(triple_timestamp *ts);
      76                 :            : triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u);
      77                 :            : 
      78                 :            : #define DUAL_TIMESTAMP_HAS_CLOCK(clock)                               \
      79                 :            :         IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC)
      80                 :            : 
      81                 :            : #define TRIPLE_TIMESTAMP_HAS_CLOCK(clock)                               \
      82                 :            :         IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM)
      83                 :            : 
      84                 :      20390 : static inline bool timestamp_is_set(usec_t timestamp) {
      85   [ +  +  +  - ]:      20390 :         return timestamp > 0 && timestamp != USEC_INFINITY;
      86                 :            : }
      87                 :            : 
      88                 :      10100 : static inline bool dual_timestamp_is_set(const dual_timestamp *ts) {
      89   [ +  +  -  + ]:      20046 :         return timestamp_is_set(ts->realtime) ||
      90                 :       9946 :                timestamp_is_set(ts->monotonic);
      91                 :            : }
      92                 :            : 
      93                 :        256 : static inline bool triple_timestamp_is_set(const triple_timestamp *ts) {
      94         [ +  - ]:        300 :         return timestamp_is_set(ts->realtime) ||
      95   [ +  +  -  + ]:        300 :                timestamp_is_set(ts->monotonic) ||
      96                 :         44 :                timestamp_is_set(ts->boottime);
      97                 :            : }
      98                 :            : 
      99                 :            : usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock);
     100                 :            : 
     101                 :            : usec_t timespec_load(const struct timespec *ts) _pure_;
     102                 :            : nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
     103                 :            : struct timespec *timespec_store(struct timespec *ts, usec_t u);
     104                 :            : 
     105                 :            : usec_t timeval_load(const struct timeval *tv) _pure_;
     106                 :            : struct timeval *timeval_store(struct timeval *tv, usec_t u);
     107                 :            : 
     108                 :            : char *format_timestamp(char *buf, size_t l, usec_t t);
     109                 :            : char *format_timestamp_utc(char *buf, size_t l, usec_t t);
     110                 :            : char *format_timestamp_us(char *buf, size_t l, usec_t t);
     111                 :            : char *format_timestamp_us_utc(char *buf, size_t l, usec_t t);
     112                 :            : char *format_timestamp_relative(char *buf, size_t l, usec_t t);
     113                 :            : char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy);
     114                 :            : 
     115                 :            : int parse_timestamp(const char *t, usec_t *usec);
     116                 :            : 
     117                 :            : int parse_sec(const char *t, usec_t *usec);
     118                 :            : int parse_sec_fix_0(const char *t, usec_t *usec);
     119                 :            : int parse_sec_def_infinity(const char *t, usec_t *usec);
     120                 :            : int parse_time(const char *t, usec_t *usec, usec_t default_unit);
     121                 :            : int parse_nsec(const char *t, nsec_t *nsec);
     122                 :            : 
     123                 :            : bool ntp_synced(void);
     124                 :            : 
     125                 :            : int get_timezones(char ***l);
     126                 :            : bool timezone_is_valid(const char *name, int log_level);
     127                 :            : 
     128                 :            : bool clock_boottime_supported(void);
     129                 :            : bool clock_supported(clockid_t clock);
     130                 :            : clockid_t clock_boottime_or_monotonic(void);
     131                 :            : 
     132                 :            : usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
     133                 :            : 
     134                 :            : int get_timezone(char **timezone);
     135                 :            : 
     136                 :            : time_t mktime_or_timegm(struct tm *tm, bool utc);
     137                 :            : struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
     138                 :            : 
     139                 :            : uint32_t usec_to_jiffies(usec_t usec);
     140                 :            : usec_t jiffies_to_usec(uint32_t jiffies);
     141                 :            : 
     142                 :            : bool in_utc_timezone(void);
     143                 :            : 
     144                 :      38896 : static inline usec_t usec_add(usec_t a, usec_t b) {
     145                 :            :         usec_t c;
     146                 :            : 
     147                 :            :         /* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't
     148                 :            :          * overflow. */
     149                 :            : 
     150                 :      38896 :         c = a + b;
     151   [ +  +  -  + ]:      38896 :         if (c < a || c < b) /* overflow check */
     152                 :         48 :                 return USEC_INFINITY;
     153                 :            : 
     154                 :      38848 :         return c;
     155                 :            : }
     156                 :            : 
     157                 :        276 : static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
     158                 :            : 
     159         [ +  + ]:        276 :         if (timestamp == USEC_INFINITY) /* Make sure infinity doesn't degrade */
     160                 :         20 :                 return USEC_INFINITY;
     161         [ +  + ]:        256 :         if (timestamp < delta)
     162                 :         28 :                 return 0;
     163                 :            : 
     164                 :        228 :         return timestamp - delta;
     165                 :            : }
     166                 :            : 
     167                 :         36 : static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
     168         [ +  + ]:         36 :         if (delta < 0)
     169                 :         12 :                 return usec_add(timestamp, (usec_t) (-delta));
     170                 :            :         else
     171                 :         24 :                 return usec_sub_unsigned(timestamp, (usec_t) delta);
     172                 :            : }
     173                 :            : 
     174                 :            : #if SIZEOF_TIME_T == 8
     175                 :            : /* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit year
     176                 :            :  * territory. However, since we want to stay away from this in all timezones we take one day off. */
     177                 :            : #define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 253402214399000000)
     178                 :            : #elif SIZEOF_TIME_T == 4
     179                 :            : /* With a 32bit time_t we can't go beyond 2038... */
     180                 :            : #define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 2147483647000000)
     181                 :            : #else
     182                 :            : #error "Yuck, time_t is neither 4 nor 8 bytes wide?"
     183                 :            : #endif
     184                 :            : 
     185                 :            : int time_change_fd(void);

Generated by: LCOV version 1.14