Switch time() calls to monotonic clock calls (#7597)
[libt2n] / src / monotonic_clock.cpp
CommitLineData
0a531de6
GB
1/*
2Copyright (C) 2024 by Intra2net AG
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on this file might be covered by the GNU General Public License.
21*/
22
23#include <time.h>
24
25#include "monotonic_clock.hxx"
26
27/**
28 * @brief fetches the value from the monotonic clock source.
29 * @param[out] seconds the seconds.
30 * @param[out] nano_seconds the nano seconds.
31 * @return @a true if the clock was successfully read.
32 */
33bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
34{
35 struct timespec tp[1];
36 int res= clock_gettime (CLOCK_MONOTONIC, tp);
37 if (0 == res)
38 {
39 seconds= tp->tv_sec;
40 nano_seconds= tp->tv_nsec;
41 }
42 return (res==0);
43}
44
45/**
46 * @brief fetches the value from the monotonic clock source.
47 * @return the time since system start in seconds, 0 if read was unsuccessful
48 */
49int monotonic_clock_gettime_sec()
50{
51 long int seconds=0;
52 long int nano_seconds;
53
54 monotonic_clock_gettime(seconds,nano_seconds);
55 return seconds;
56}