rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

clock_time.h (1865B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef TIME_H
     17 #define TIME_H
     18 
     19 #include "rsys.h"
     20 
     21 struct time {
     22   /* Internal data */
     23   int64_t sec;
     24   int64_t nsec;
     25 };
     26 
     27 /* time_dump flag constant used to auto-format the time_dump message */
     28 #define TIME_ALL (-1)
     29 
     30 enum time_unit {
     31   TIME_NSEC = BIT(0),
     32   TIME_USEC = BIT(1),
     33   TIME_MSEC = BIT(2),
     34   TIME_SEC = BIT(3),
     35   TIME_MIN = BIT(4),
     36   TIME_HOUR = BIT(5),
     37   TIME_DAY = BIT(6)
     38 };
     39 
     40 static FINLINE struct time*
     41 time_zero(struct time* time)
     42 {
     43   ASSERT(time);
     44   time->sec = time->nsec = 0;
     45   return time;
     46 }
     47 
     48 BEGIN_DECLS
     49 
     50 RSYS_API struct time*
     51 time_current
     52   (struct time* time);
     53 
     54 RSYS_API struct time*
     55 time_sub
     56   (struct time* res,
     57    const struct time* a,
     58    const struct time* b);
     59 
     60 RSYS_API struct time*
     61 time_add
     62   (struct time* res,
     63    const struct time* a,
     64    const struct time* b);
     65 
     66 RSYS_API int64_t
     67 time_val
     68   (const struct time* time,
     69    enum time_unit unit);
     70 
     71 RSYS_API void
     72 time_dump
     73   (const struct time* time,
     74    int flag, /* Combination of time_unit or TIME_ALL */
     75    size_t* real_dump_len, /* May be NULL (wo '\0') */
     76    char* dump, /* May be NULL */
     77    size_t max_dump_len); /* With '\0' */
     78 
     79 END_DECLS
     80 
     81 #endif /* TIME_H */