| 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 0 (3→4) taken 103 times.
|
103 | std::stringstream dateNs; |
| 22 |
4/4✓ Branch 0 (4→5) taken 103 times.
✓ Branch 2 (5→6) taken 103 times.
✓ Branch 4 (7→8) taken 103 times.
✓ Branch 6 (10→11) taken 103 times.
|
103 | dateNs << buf << "." << std::setfill('0') << std::setw(9) << timeNanoSecond; |
| 23 |
1/1✓ Branch 0 (11→12) 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 | 4 | PClockNs::PClockNs(){ | |
| 44 | 4 | initialisationPClockNs(); | |
| 45 | 4 | } | |
| 46 | |||
| 47 | ///Destructor of PClockNs | ||
| 48 | 5 | PClockNs::~PClockNs(){ | |
| 49 | |||
| 50 | 5 | } | |
| 51 | |||
| 52 | ///Set the offset in nanoseconds of the current clock | ||
| 53 | /** @param offsetTimeNs : offset in nanoseconds of the current clock | ||
| 54 | */ | ||
| 55 | 1 | void PClockNs::setOffsetTimeNs(std::time_t offsetTimeNs){ | |
| 56 | 1 | p_offsetTimeNs = offsetTimeNs; | |
| 57 | 1 | } | |
| 58 | |||
| 59 | ///Get the offset in nanoseconds of the current clock | ||
| 60 | /** @return offset in nanoseconds of the current clock | ||
| 61 | */ | ||
| 62 | 3 | std::time_t PClockNs::getOffsetTimeNs() const{ | |
| 63 | 3 | return p_offsetTimeNs; | |
| 64 | } | ||
| 65 | |||
| 66 | ///Get the full time in nanoseconds | ||
| 67 | /** @return full time in nanoseconds | ||
| 68 | */ | ||
| 69 | 107 | std::time_t PClockNs::getFullTimeNs() const{ | |
| 70 | //Let's get the time in nanoseconds since the start of the PClockNs | ||
| 71 |
1/1✓ Branch 0 (3→4) taken 107 times.
|
107 | std::time_t ellapsedTimeNsSinceStart = (PClockNs::SteadyClock::now() - p_baseSteadyClockNs).count(); |
| 72 | |||
| 73 | 107 | std::time_t fullTimeNs = p_baseSystemClockSecond*1000000000l + ellapsedTimeNsSinceStart; | |
| 74 | 107 | return fullTimeNs + p_offsetTimeNs; | |
| 75 | } | ||
| 76 | |||
| 77 | ///Get the date at nanosecond precision (even if it is not precise at the nanosecond scale, it is normally coherent at this scale) | ||
| 78 | /** @return current date | ||
| 79 | */ | ||
| 80 | 2 | std::string PClockNs::getDateNs() const{ | |
| 81 | 2 | return phoenix_dateNs(getFullTimeNs()); | |
| 82 | } | ||
| 83 | |||
| 84 | ///Get the date at nanosecond precision (even if it is not precise at the nanosecond scale, it is normally coherent at this scale) | ||
| 85 | /** @return current date in printed in a more compact way | ||
| 86 | */ | ||
| 87 | 1 | std::string PClockNs::getDateCompactNs() const{ | |
| 88 | 1 | return phoenix_dateCompactNs(getFullTimeNs()); | |
| 89 | } | ||
| 90 | |||
| 91 | ///Get the full time in nanoseconds | ||
| 92 | /** @return full time in nanoseconds | ||
| 93 | */ | ||
| 94 | 2 | std::time_t PClockNs::now() const{ | |
| 95 | 2 | return getFullTimeNs(); | |
| 96 | } | ||
| 97 | |||
| 98 | ///Sleep for the given ellapsed time | ||
| 99 | /** @param ellapsedTime : time to sleep in nanoseconds | ||
| 100 | */ | ||
| 101 | 1 | void PClockNs::sleep(EllapsedTime ellapsedTime) const{ | |
| 102 |
1/1✓ Branch 0 (3→4) taken 1 times.
|
1 | std::this_thread::sleep_for(std::chrono::nanoseconds(ellapsedTime)); |
| 103 | 1 | } | |
| 104 | |||
| 105 | ///Initialisation function of the class PClockNs | ||
| 106 | 4 | void PClockNs::initialisationPClockNs(){ | |
| 107 | 4 | p_baseSystemClockSecond = std::time(0); | |
| 108 | 4 | p_baseSteadyClockNs = PClockNs::SteadyClock::now(); | |
| 109 | 4 | p_offsetTimeNs = 0l; | |
| 110 | 4 | } | |
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 |