Directory: | ./ |
---|---|
File: | tmp_project/PhoenixClock/src/PClockNs.cpp |
Date: | 2025-03-14 12:18:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 46 | 46 | 100.0% |
Branches: | 8 | 8 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <iomanip> | ||
8 | #include "PClockNs.h" | ||
9 | |||
10 | ///Convert a given date in nanosecond into a text date | ||
11 | /** @param currentDateNs : date in nanosecond to be converted | ||
12 | * @param format : format to print the date (refer to std::strftime for the detail) | ||
13 | * @return corresponding date in std::string | ||
14 | */ | ||
15 | 103 | std::string phoenix_dateNs(std::time_t currentDateNs, const char* format){ | |
16 | 103 | std::time_t currentTimeSecond = currentDateNs / 1000000000l; | |
17 | 103 | std::tm* now_tm = std::gmtime(¤tTimeSecond); | |
18 | char buf[42]; | ||
19 | 103 | std::strftime(buf, 42, "%Y/%m/%d : %X", now_tm); | |
20 | 103 | std::time_t timeNanoSecond = currentDateNs - currentTimeSecond*1000000000l; | |
21 |
1/1✓ Branch 1 taken 103 times.
|
103 | std::stringstream dateNs; |
22 |
5/5✓ Branch 1 taken 103 times.
✓ Branch 4 taken 103 times.
✓ Branch 8 taken 103 times.
✓ Branch 12 taken 103 times.
✓ Branch 15 taken 103 times.
|
103 | dateNs << buf << "." << std::setfill('0') << std::setw(9) << timeNanoSecond; |
23 |
1/1✓ Branch 1 taken 103 times.
|
206 | return dateNs.str(); |
24 | 103 | } | |
25 | |||
26 | ///Convert a given date in nanosecond into a text date | ||
27 | /** @param currentDateNs : date in nanosecond to be converted | ||
28 | * @return corresponding date in std::string | ||
29 | */ | ||
30 | 102 | std::string phoenix_dateNs(std::time_t currentDateNs){ | |
31 | 102 | return phoenix_dateNs(currentDateNs, "%Y/%m/%d-%X"); | |
32 | } | ||
33 | |||
34 | ///Convert a given date in nanosecond into a text date in a more compact way | ||
35 | /** @param currentDateNs : date in nanosecond to be converted | ||
36 | * @return corresponding date in std::string | ||
37 | */ | ||
38 | 1 | std::string phoenix_dateCompactNs(std::time_t currentDateNs){ | |
39 | 1 | return phoenix_dateNs(currentDateNs, "%Y/%m/%d : %X"); | |
40 | } | ||
41 | |||
42 | ///Default constructor of PClockNs | ||
43 | 10 | PClockNs::PClockNs(){ | |
44 | 10 | initialisationPClockNs(); | |
45 | 10 | } | |
46 | |||
47 | ///Copy constructor of PClockNs | ||
48 | /** @param other : class to copy | ||
49 | */ | ||
50 | 1 | PClockNs::PClockNs(const PClockNs & other){ | |
51 | 1 | copyPClockNs(other); | |
52 | 1 | } | |
53 | |||
54 | ///Destructor of PClockNs | ||
55 | 22 | PClockNs::~PClockNs(){ | |
56 | |||
57 | } | ||
58 | |||
59 | ///Definition of equal operator of PClockNs | ||
60 | /** @param other : class to copy | ||
61 | * @return copied class | ||
62 | */ | ||
63 | 1 | PClockNs & PClockNs::operator = (const PClockNs & other){ | |
64 | 1 | copyPClockNs(other); | |
65 | 1 | return *this; | |
66 | } | ||
67 | |||
68 | ///Set the offset in nanoseconds of the current clock | ||
69 | /** @param offsetTimeNs : offset in nanoseconds of the current clock | ||
70 | */ | ||
71 | 1 | void PClockNs::setOffsetTimeNs(std::time_t offsetTimeNs){ | |
72 | 1 | p_offsetTimeNs = offsetTimeNs; | |
73 | 1 | } | |
74 | |||
75 | ///Get the offset in nanoseconds of the current clock | ||
76 | /** @return offset in nanoseconds of the current clock | ||
77 | */ | ||
78 | 3 | std::time_t PClockNs::getOffsetTimeNs() const{ | |
79 | 3 | return p_offsetTimeNs; | |
80 | } | ||
81 | |||
82 | ///Get the full time in nanoseconds | ||
83 | /** @return full time in nanoseconds | ||
84 | */ | ||
85 | 107 | std::time_t PClockNs::getFullTimeNs() const{ | |
86 | //Let's get the time in nanoseconds since the start of the PClockNs | ||
87 |
1/1✓ Branch 2 taken 107 times.
|
107 | std::time_t ellapsedTimeNsSinceStart = (PClockNs::SteadyClock::now() - p_baseSteadyClockNs).count(); |
88 | |||
89 | 107 | std::time_t fullTimeNs = p_baseSystemClockSecond*1000000000l + ellapsedTimeNsSinceStart; | |
90 | 107 | return fullTimeNs + p_offsetTimeNs; | |
91 | } | ||
92 | |||
93 | ///Get the date at nanosecond precision (even if it is not precise at the nanosecond scale, it is normally coherent at this scale) | ||
94 | /** @return current date | ||
95 | */ | ||
96 | 2 | std::string PClockNs::getDateNs() const{ | |
97 | 2 | return phoenix_dateNs(getFullTimeNs()); | |
98 | } | ||
99 | |||
100 | ///Get the date at nanosecond precision (even if it is not precise at the nanosecond scale, it is normally coherent at this scale) | ||
101 | /** @return current date in printed in a more compact way | ||
102 | */ | ||
103 | 1 | std::string PClockNs::getDateCompactNs() const{ | |
104 | 1 | return phoenix_dateCompactNs(getFullTimeNs()); | |
105 | } | ||
106 | |||
107 | ///Copy function of PClockNs | ||
108 | /** @param other : class to copy | ||
109 | */ | ||
110 | 2 | void PClockNs::copyPClockNs(const PClockNs & other){ | |
111 | 2 | p_baseSystemClockSecond = other.p_baseSystemClockSecond; | |
112 | 2 | p_baseSteadyClockNs = other.p_baseSteadyClockNs; | |
113 | 2 | p_offsetTimeNs = other.p_offsetTimeNs; | |
114 | 2 | } | |
115 | |||
116 | ///Initialisation function of the class PClockNs | ||
117 | 10 | void PClockNs::initialisationPClockNs(){ | |
118 | 10 | p_baseSystemClockSecond = std::time(0); | |
119 | 10 | p_baseSteadyClockNs = PClockNs::SteadyClock::now(); | |
120 | 10 | p_offsetTimeNs = 0l; | |
121 | 10 | } | |
122 | |||
123 | |||
124 | |||
125 | |||
126 | |||
127 |