PhoenixClock  2.2.0
Library wrapping the usage of system's clock, allowing to use clock mock-ups
Loading...
Searching...
No Matches
PClockNs.cpp
Go to the documentation of this file.
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
11
15std::string phoenix_dateNs(std::time_t currentDateNs, const char* format){
16 std::time_t currentTimeSecond = currentDateNs / 1000000000l;
17 std::tm* now_tm = std::gmtime(&currentTimeSecond);
18 char buf[42];
19 std::strftime(buf, 42, "%Y/%m/%d : %X", now_tm);
20 std::time_t timeNanoSecond = currentDateNs - currentTimeSecond*1000000000l;
21 std::stringstream dateNs;
22 dateNs << buf << "." << std::setfill('0') << std::setw(9) << timeNanoSecond;
23 return dateNs.str();
24}
25
27
30std::string phoenix_dateNs(std::time_t currentDateNs){
31 return phoenix_dateNs(currentDateNs, "%Y/%m/%d-%X");
32}
33
35
38std::string phoenix_dateCompactNs(std::time_t currentDateNs){
39 return phoenix_dateNs(currentDateNs, "%Y/%m/%d : %X");
40}
41
46
51
53
55void PClockNs::setOffsetTimeNs(std::time_t offsetTimeNs){
56 p_offsetTimeNs = offsetTimeNs;
57}
58
60
62std::time_t PClockNs::getOffsetTimeNs() const{
63 return p_offsetTimeNs;
64}
65
67
69std::time_t PClockNs::getFullTimeNs() const{
70 //Let's get the time in nanoseconds since the start of the PClockNs
71 std::time_t ellapsedTimeNsSinceStart = (PClockNs::SteadyClock::now() - p_baseSteadyClockNs).count();
72
73 std::time_t fullTimeNs = p_baseSystemClockSecond*1000000000l + ellapsedTimeNsSinceStart;
74 return fullTimeNs + p_offsetTimeNs;
75}
76
78
80std::string PClockNs::getDateNs() const{
82}
83
85
87std::string PClockNs::getDateCompactNs() const{
89}
90
92
94std::time_t PClockNs::now() const{
95 return getFullTimeNs();
96}
97
99
101void PClockNs::sleep(EllapsedTime ellapsedTime) const{
102 std::this_thread::sleep_for(std::chrono::nanoseconds(ellapsedTime));
103}
104
107 p_baseSystemClockSecond = std::time(0);
108 p_baseSteadyClockNs = PClockNs::SteadyClock::now();
109 p_offsetTimeNs = 0l;
110}
111
112
113
114
115
std::string phoenix_dateNs(std::time_t currentDateNs, const char *format)
Convert a given date in nanosecond into a text date.
Definition PClockNs.cpp:15
std::string phoenix_dateCompactNs(std::time_t currentDateNs)
Convert a given date in nanosecond into a text date in a more compact way.
Definition PClockNs.cpp:38
std::string phoenix_dateNs(std::time_t currentDateNs, const char *format)
Convert a given date in nanosecond into a text date.
Definition PClockNs.cpp:15
std::string phoenix_dateCompactNs(std::time_t currentDateNs)
Convert a given date in nanosecond into a text date in a more compact way.
Definition PClockNs.cpp:38
std::string getDateCompactNs() const
Get the date at nanosecond precision (even if it is not precise at the nanosecond scale,...
Definition PClockNs.cpp:87
std::time_t now() const
Get the full time in nanoseconds.
Definition PClockNs.cpp:94
void initialisationPClockNs()
Initialisation function of the class PClockNs.
Definition PClockNs.cpp:106
void sleep(EllapsedTime ellapsedTime) const
Sleep for the given ellapsed time.
Definition PClockNs.cpp:101
std::time_t getOffsetTimeNs() const
Get the offset in nanoseconds of the current clock.
Definition PClockNs.cpp:62
std::time_t getFullTimeNs() const
Get the full time in nanoseconds.
Definition PClockNs.cpp:69
virtual ~PClockNs()
Destructor of PClockNs.
Definition PClockNs.cpp:48
void setOffsetTimeNs(std::time_t offsetTimeNs)
Set the offset in nanoseconds of the current clock.
Definition PClockNs.cpp:55
std::string getDateNs() const
Get the date at nanosecond precision (even if it is not precise at the nanosecond scale,...
Definition PClockNs.cpp:80
PClockNs()
Default constructor of PClockNs.
Definition PClockNs.cpp:43
std::time_t p_baseSystemClockSecond
Base of the system clock in second.
Definition PClockNs.h:39
std::time_t p_offsetTimeNs
Offset of time with date and nanoseconds.
Definition PClockNs.h:43
TimeNs p_baseSteadyClockNs
Base clock time with a nanosecond precision but since the starting of the CPU.
Definition PClockNs.h:41
time_t EllapsedTime