PhoenixClock
2.6.0
Library wrapping the usage of system's clock, allowing to use clock mock-ups
Toggle main menu visibility
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
15
std::string
phoenix_dateNs
(std::time_t currentDateNs,
const
char
* format){
16
std::time_t currentTimeSecond = currentDateNs / 1000000000l;
17
std::tm* now_tm = std::gmtime(¤tTimeSecond);
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
30
std::string
phoenix_dateNs
(std::time_t currentDateNs){
31
return
phoenix_dateNs
(currentDateNs,
"%Y/%m/%d-%X"
);
32
}
33
35
38
std::string
phoenix_dateCompactNs
(std::time_t currentDateNs){
39
return
phoenix_dateNs
(currentDateNs,
"%Y/%m/%d : %X"
);
40
}
41
43
PClockNs::PClockNs
(){
44
initialisationPClockNs
();
45
}
46
48
PClockNs::~PClockNs
(){
49
50
}
51
53
55
void
PClockNs::setOffsetTimeNs
(std::time_t offsetTimeNs){
56
p_offsetTimeNs
= offsetTimeNs;
57
}
58
60
62
std::time_t
PClockNs::getOffsetTimeNs
()
const
{
63
return
p_offsetTimeNs
;
64
}
65
67
69
std::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
80
std::string
PClockNs::getDateNs
()
const
{
81
return
phoenix_dateNs
(
getFullTimeNs
());
82
}
83
85
87
std::string
PClockNs::getDateCompactNs
()
const
{
88
return
phoenix_dateCompactNs
(
getFullTimeNs
());
89
}
90
92
94
std::time_t
PClockNs::now
()
const
{
95
return
getFullTimeNs
();
96
}
97
99
101
void
PClockNs::sleep
(
EllapsedTime
ellapsedTime)
const
{
102
std::this_thread::sleep_for(std::chrono::nanoseconds(ellapsedTime));
103
}
104
106
void
PClockNs::initialisationPClockNs
(){
107
p_baseSystemClockSecond
= std::time(0);
108
p_baseSteadyClockNs
= PClockNs::SteadyClock::now();
109
p_offsetTimeNs
= 0l;
110
}
111
112
113
114
115
phoenix_dateNs
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
phoenix_dateCompactNs
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
PClockNs.h
phoenix_dateNs
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
phoenix_dateCompactNs
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
PClockNs::getDateCompactNs
std::string getDateCompactNs() const
Get the date at nanosecond precision (even if it is not precise at the nanosecond scale,...
Definition
PClockNs.cpp:87
PClockNs::now
std::time_t now() const
Get the full time in nanoseconds.
Definition
PClockNs.cpp:94
PClockNs::initialisationPClockNs
void initialisationPClockNs()
Initialisation function of the class PClockNs.
Definition
PClockNs.cpp:106
PClockNs::sleep
void sleep(EllapsedTime ellapsedTime) const
Sleep for the given ellapsed time.
Definition
PClockNs.cpp:101
PClockNs::getOffsetTimeNs
std::time_t getOffsetTimeNs() const
Get the offset in nanoseconds of the current clock.
Definition
PClockNs.cpp:62
PClockNs::getFullTimeNs
std::time_t getFullTimeNs() const
Get the full time in nanoseconds.
Definition
PClockNs.cpp:69
PClockNs::~PClockNs
virtual ~PClockNs()
Destructor of PClockNs.
Definition
PClockNs.cpp:48
PClockNs::setOffsetTimeNs
void setOffsetTimeNs(std::time_t offsetTimeNs)
Set the offset in nanoseconds of the current clock.
Definition
PClockNs.cpp:55
PClockNs::getDateNs
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::PClockNs
PClockNs()
Default constructor of PClockNs.
Definition
PClockNs.cpp:43
PClockNs::p_baseSystemClockSecond
std::time_t p_baseSystemClockSecond
Base of the system clock in second.
Definition
PClockNs.h:39
PClockNs::p_offsetTimeNs
std::time_t p_offsetTimeNs
Offset of time with date and nanoseconds.
Definition
PClockNs.h:43
PClockNs::p_baseSteadyClockNs
TimeNs p_baseSteadyClockNs
Base clock time with a nanosecond precision but since the starting of the CPU.
Definition
PClockNs.h:41
EllapsedTime
time_t EllapsedTime
Definition
ellapsedTime.h:12
src
PClockNs.cpp
Generated by
1.17.0