w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rtime.cpp
Go to the documentation of this file.
1// $Id: Rtime.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2017-02-20 854 1.0 Initial version
8// ---------------------------------------------------------------------------
9
13// Note on double conversion precision:
14// double has 52 bits mantissa --> 52*log10(2) = 15.6 dig or ~4.e15 res
15// --> up to ~4.e6sec or ~ 46days we have 1nsec resolution
16// for realclock time stamps: t = ~1.5e9 --> 1/2600000 res --> better 1usec
17
18#include <errno.h>
19
20#include <sstream>
21
22#include "RosFill.hpp"
23#include "RosPrintf.hpp"
24#include "Rexception.hpp"
25
26#include "Rtime.hpp"
27
28using namespace std;
29
35// all method definitions in namespace Retro
36namespace Retro {
37
38//------------------------------------------+-----------------------------------
40
41void Rtime::GetClock(clockid_t clkid)
42{
43 if (::clock_gettime(clkid, &fTime) != 0) {
44 throw Rexception("Rtime::GetClock()", "clock_gettime() failed: ", errno);
45 }
46 return;
47}
48
49//------------------------------------------+-----------------------------------
51
52void Rtime::SetNSec(long nsec)
53{
54 if (nsec < 0 || nsec > 999999999)
55 throw Rexception("Rtime::SetNSec()", "bad args: <0 or >999999999 ");
56 fTime.tv_nsec = nsec;
57}
58
59//------------------------------------------+-----------------------------------
61
62std::string Rtime::ToString() const
63{
64 ostringstream sos;
65 Print(sos);
66 return sos.str();
67}
68
69//------------------------------------------+-----------------------------------
71
72double Rtime::Age(clockid_t clkid) const
73{
74 if (IsZero()) return 0.;
75 Rtime now(clkid);
76 return double(now - *this);
77}
78
79//------------------------------------------+-----------------------------------
81
82void Rtime::Print(std::ostream& os) const
83{
84 if (fTime.tv_sec < 365*24*3600) { // looks like dt (<1year)
85 os << RosPrintf(ToDouble(),"f",18,9);
86 } else {
87 struct tm tymd;
88 ::localtime_r(&fTime.tv_sec, &tymd);
89 os << RosPrintf(tymd.tm_year+1900,"d",4) << "-"
90 << RosPrintf(tymd.tm_mon+1,"d0",2) << "-"
91 << RosPrintf(tymd.tm_mday,"d0",2) << " "
92 << RosPrintf(tymd.tm_hour,"d0",2) << ":"
93 << RosPrintf(tymd.tm_min,"d0",2) << ":"
94 << RosPrintf(tymd.tm_sec,"d0",2) << "."
95 << RosPrintf(fTime.tv_nsec,"d0",9);
96 }
97 return;
98}
99
100//------------------------------------------+-----------------------------------
102
103void Rtime::Dump(std::ostream& os, int ind, const char* text) const
104{
105 RosFill bl(ind);
106 os << bl << (text?text:"--") << "Rtime @ " << this << endl;
107 os << bl << " fTime: " << RosPrintf(fTime.tv_sec,"d",10)
108 << "," << RosPrintf(fTime.tv_nsec,"d",9)
109 << " : " << ToString() << endl;
110 return;
111}
112
113
114} // end namespace Retro
FIXME_docs.
Definition: Rexception.hpp:29
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
FIXME_docs.
Definition: Rtime.hpp:25
bool IsZero() const
FIXME_docs.
Definition: Rtime.ipp:85
std::string ToString() const
FIXME_docs.
Definition: Rtime.cpp:62
void Print(std::ostream &os) const
FIXME_docs.
Definition: Rtime.cpp:82
void SetNSec(long nsec)
FIXME_docs.
Definition: Rtime.cpp:52
void Dump(std::ostream &os, int ind=0, const char *text=0) const
FIXME_docs.
Definition: Rtime.cpp:103
struct timespec fTime
time
Definition: Rtime.hpp:69
void GetClock(clockid_t clkid)
FIXME_docs.
Definition: Rtime.cpp:41
double ToDouble() const
FIXME_docs.
Definition: Rtime.ipp:142
double Age(clockid_t clkid) const
FIXME_docs.
Definition: Rtime.cpp:72
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Definition: RosPrintf.ipp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47