TimeStamp.cc
Go to the documentation of this file.
1 #include <boost/format.hpp>
2 #include <sys/time.h>
3 #include <utl/TimeStamp.h>
4 #include <utl/ErrorLogger.h>
5 #include <utl/AugerException.h>
6 #include <utl/UTCDateTime.h>
7 
8 using namespace std;
9 using namespace boost;
10 
11 
12 namespace utl {
13 
14  void
15  TimeStamp::SetNormalized(long sec, double nsec)
16  {
17  // normalize
18  const long sperns = long(second/nanosecond);
19  if (nsec < 0) {
20  --sec;
21  nsec += sperns;
22  } else if (nsec >= sperns) {
23  ++sec;
24  nsec -= sperns;
25  }
26 
27  // must be within the GPS epoch
28 
29  if (sec >= 0) {
30  fGPSSecond = sec;
31  fGPSNanoSecond = nsec;
32 
33  } else {
34  ostringstream err;
35  err << "operation for "
36  "sec = " << sec << " nsec = " << nsec << " "
37  "results in time before GPS Epoch";
38  ERROR(err);
39  fGPSSecond = fGPSNanoSecond = 0;
40  throw OutOfBoundException(err.str());
41  }
42  }
43 
44 
45  TimeStamp
47  {
48  timeval tv;
49  gettimeofday(&tv, nullptr);
50  const tm* const time = gmtime(&tv.tv_sec);
51  return
53  time->tm_year + 1900,
54  time->tm_mon + 1,
55  time->tm_mday,
56  time->tm_hour,
57  time->tm_min,
58  time->tm_sec,
59  1000 * tv.tv_usec
60  ).GetTimeStamp();
61  }
62 
63 
64  ostream&
65  operator<<(ostream& os, const TimeStamp& ts)
66  {
67  os << UTCDateTime(ts) << " (UTC), ";
68  const double ns = ts.GetGPSNanoSecond();
69  if (ns)
70  return os << format("%1%:%2$012.2f (GPS)") % ts.GetGPSSecond() % ns;
71  else
72  return os << format("%1% (GPS)") % ts.GetGPSSecond();
73  }
74 
75 }
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
Exception for reporting variable out of valid range.
ostream & operator<<(ostream &os, const Separator< T > &s)
Definition: Util.h:55
const double ns
constexpr double nanosecond
Definition: AugerUnits.h:143
const double second
Definition: GalacticUnits.h:32
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
double GetGPSNanoSecond() const
GPS nanosecond.
Definition: TimeStamp.h:127
TimeStamp GetCurrentSystemTime()
get current time as reported by system
Definition: TimeStamp.cc:46
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
TimeStamp GetTimeStamp() const
Definition: UTCDateTime.cc:115

, generated on Tue Sep 26 2023.