UTCDate.h
Go to the documentation of this file.
1 #ifndef _utl_UTCDate_h_
2 #define _utl_UTCDate_h_
3 
4 #include <iostream>
5 #include <ctime>
6 
7 
8 namespace utl {
9 
10 
19  class UTCDate {
20  public:
21  enum Month {
22  eJan = 1,
23  eFeb = 2,
24  eMar = 3,
25  eApr = 4,
26  eMay = 5,
27  eJun = 6,
28  eJul = 7,
29  eAug = 8,
30  eSep = 9,
31  eOct = 10,
32  eNov = 11,
33  eDec = 12
34  };
35 
36  UTCDate() : fYear(0), fMonth(0), fDay(0) { }
37 
39  UTCDate(const int year, const int month, const int day)
40  { Set(year, month, day); }
41 
42  void Set(const int year, const int month, const int day);
43 
44  int GetYear() const { return fYear; }
45 
46  int GetMonth() const { return fMonth; }
47 
48  int GetDay() const { return fDay; }
49 
50  std::string GetInAugerFormat() const;
51 
52  std::string GetInXMLFormat() const
53  { return GetInXMLFormatZone("Z"); }
54 
55  explicit operator bool() const { return fYear && fMonth && fDay; }
56 
57  bool operator==(const UTCDate& d) const
58  { return fYear == d.fYear && fMonth == d.fMonth && fDay == d.fDay; }
59 
60  bool operator!=(const UTCDate& d) const
61  { return !operator==(d); }
62 
63  static UTCDate GetGPSEpoch() { return UTCDate(1980, eJan, 6); }
64 
65  static UTCDate GetUnixEpoch() { return UTCDate(1970, eJan, 1); }
66 
68  std::time_t GetUnixSecond() const
69  { return GetUnixSecond(fYear, fMonth, fDay, 0, 0, 0); }
70 
71  protected:
72  static const char* const fgMonthNames[];
73 
74  static std::time_t GetUnixSecond(const int year, const int month, const int day,
75  const int hour, const int minute, const int second);
76 
77  std::string GetInXMLFormatZone(const char* const zone) const;
78 
79  std::istream& Parse(std::istream& is, const bool zone = true);
80 
81  private:
82  static bool IsLeapYear(const int year)
83  { return (!(year%4) && (year%100)) || !(year%400); }
84 
85  static
86  int
87  NumberOfDaysInMonth(const int year, const int month)
88  {
89  switch (month) {
90  case eJan:
91  case eMar:
92  case eMay:
93  case eJul:
94  case eAug:
95  case eOct:
96  case eDec:
97  return 31;
98  case eFeb:
99  return IsLeapYear(year) ? 29 : 28;
100  case eApr:
101  case eJun:
102  case eSep:
103  case eNov:
104  return 30;
105  default:
106  return 0;
107  }
108  }
109 
110  int fYear;
111  int fMonth;
112  int fDay;
113 
114  friend std::istream& operator>>(std::istream&, UTCDate&);
115  };
116 
117 
119  inline std::ostream& operator<<(std::ostream& os, const UTCDate& date)
120  { return os << date.GetInXMLFormat(); }
121 
122 
124  inline std::istream& operator>>(std::istream& is, UTCDate& date)
125  { return date.Parse(is); }
126 
127 
128 }
129 
130 
131 #endif
std::time_t GetUnixSecond() const
Relative to Unix epoch (1 Jan 1970 00:00:00 UTC) without leap corrections.
Definition: UTCDate.h:68
constexpr double second
Definition: AugerUnits.h:145
UTCDate(const int year, const int month, const int day)
Jan = 1, Dec = 12.
Definition: UTCDate.h:39
std::istream & operator>>(std::istream &is, SVector< n, T > &v)
Definition: SVector.h:346
static bool IsLeapYear(const int year)
Definition: UTCDate.h:82
static const char *const fgMonthNames[]
Definition: UTCDate.h:72
std::string GetInAugerFormat() const
Definition: UTCDate.cc:70
void Set(const int year, const int month, const int day)
Definition: UTCDate.cc:19
bool is(const double a, const double b)
Definition: testlib.cc:113
int fMonth
Definition: UTCDate.h:111
std::istream & Parse(std::istream &is, const bool zone=true)
Definition: UTCDate.cc:97
int GetYear() const
Definition: UTCDate.h:44
Stream & operator<<(Stream &s, MessageLoggerConfig &mlc)
Applies the configuration to the given stream.
static UTCDate GetUnixEpoch()
Definition: UTCDate.h:65
bool operator==(const UTCDate &d) const
Definition: UTCDate.h:57
static int NumberOfDaysInMonth(const int year, const int month)
Definition: UTCDate.h:87
static UTCDate GetGPSEpoch()
Definition: UTCDate.h:63
bool operator!=(const UTCDate &d) const
Definition: UTCDate.h:60
std::string GetInXMLFormat() const
Definition: UTCDate.h:52
constexpr double minute
Definition: AugerUnits.h:149
constexpr double hour
Definition: AugerUnits.h:150
int GetMonth() const
Definition: UTCDate.h:46
friend std::istream & operator>>(std::istream &, UTCDate &)
read from XML schema type &quot;Date&quot; (any timezone) compliant format
Definition: UTCDate.h:124
int GetDay() const
Definition: UTCDate.h:48
std::string GetInXMLFormatZone(const char *const zone) const
Definition: UTCDate.cc:83
constexpr double day
Definition: AugerUnits.h:151
const double year
Definition: GalacticUnits.h:22

, generated on Tue Sep 26 2023.