UTCDate.cc
Go to the documentation of this file.
1 #include <sstream>
2 #include <boost/format.hpp>
3 #include <utl/AugerException.h>
4 #include <utl/ErrorLogger.h>
5 #include <utl/UTCDate.h>
6 
7 using namespace std;
8 using namespace utl;
9 using namespace boost;
10 
11 
12 const char* const UTCDate::fgMonthNames[] = {
13  "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
14  "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
15 };
16 
17 
18 void
19 UTCDate::Set(const int year, const int month, const int day)
20 {
21  if (year < 1980 || (year == 1980 && month == 1 && day < 6)) {
22  const char* const err = "Date before GPS epoch";
23  ERROR(err);
24  throw OutOfBoundException(err);
25  }
26  if (month < 1 || month > 12) {
27  ostringstream err;
28  err << "month " << month << " not in range 1-12";
29  ERROR(err);
30  throw OutOfBoundException(err.str());
31  }
32  if (day < 1 || day > NumberOfDaysInMonth(year, month)) {
33  ostringstream err;
34  err << "day " << day << " not in range 1-"
35  << NumberOfDaysInMonth(year, month);
36  ERROR(err);
37  throw OutOfBoundException(err.str());
38  }
39  fYear = year;
40  fMonth = month;
41  fDay = day;
42 }
43 
44 
45 time_t
46 UTCDate::GetUnixSecond(const int year, const int month, const int day,
47  const int hour, const int minute, const int second)
48 {
49  tm breakdown = {
50  second, // tm_sec
51  minute, // tm_min
52  hour, // tm_hour
53  day, // tm_mday
54  month-1, // tm_mon
55  year-1900, // tm_year
56  0, // tm_wday
57  0, // tm_yday
58  0, // tm_isdst
59  0, 0
60  };
61  const time_t t1 = mktime(&breakdown);
62  const time_t dgm = mktime(gmtime(&t1));
63  //const time_t dlt = mktime(localtime(&t1));
64  //const time_t t2 = mktime(&breakdown);
65  return t1 - (dgm - t1);
66 }
67 
68 
69 string
70 UTCDate::GetInAugerFormat()
71  const
72 {
73  return
74  (format("%|1$02| %2% %|3$4|")
75  % fDay
76  % fgMonthNames[fMonth - eJan]
77  % fYear
78  ).str();
79 }
80 
81 
82 string
83 UTCDate::GetInXMLFormatZone(const char* const zone)
84  const
85 {
86  return
87  (format("%1%-%|2$02|-%|3$02|%4%")
88  % GetYear()
89  % GetMonth()
90  % GetDay()
91  % zone
92  ).str();
93 }
94 
95 
96 istream&
97 UTCDate::Parse(istream& is, const bool zone)
98 {
99  int year = 0;
100  int month = 0;
101  int day = 0;
102  char minus = '?';
103  char minus2 = '?';
104  char tz = '?';
105  if ((is >> year >> minus >> month >> minus2 >> day) &&
106  minus == '-' && minus2 == '-' &&
107  (!zone || is.eof() || ((is >> tz) && tz == 'Z'))) {
108  Set(year, month, day);
109  return is;
110  }
111  ostringstream err;
112  err << "Initializing UTCDate variable failed:"
113  " year=" << year << " month=" << month << " day=" << day
114  << " minus=" << minus << " minus2=" << minus2 << " tz=" << tz;
115  ERROR(err);
116  throw OutOfBoundException(err.str());
117 }
bool is(const double a, const double b)
Definition: testlib.cc:113
Exception for reporting variable out of valid range.
const double second
Definition: GalacticUnits.h:32
constexpr double minute
Definition: AugerUnits.h:149
constexpr double hour
Definition: AugerUnits.h:150
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
constexpr double day
Definition: AugerUnits.h:151
const double year
Definition: GalacticUnits.h:22

, generated on Tue Sep 26 2023.