Jpp  17.1.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSystem/JDate.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEM__JDATE__
2 #define __JSYSTEM__JDATE__
3 
4 #include <time.h>
5 #include <string>
6 #include <istream>
7 
8 #include "JLang/JException.hh"
9 
10 /**
11  * \file
12  * Date and time functions.
13  * \author mdejong, bjung
14  */
15 namespace JSYSTEM {}
16 namespace JPP { using namespace JSYSTEM; }
17 
18 namespace JSYSTEM {
19 
20  /**
21  * Date and time formats.
22  */
24  HUMAN_READABLE = 0, //!< Human readable format (Www Mmm dd hh:mm:ss yyyy)
25  ISO8601 = 1 //!< ISO-8601 standard
26  };
27 
28 
29  /**
30  * Get ASCII formatted date.
31  *
32  * \param option formatting option
33  * \return date
34  */
35  inline const char* getDate(const JDateAndTimeFormat option = ISO8601)
36  {
37  static time_t ts;
38  static const int MAX_SIZE = 256;
39  static char buffer[MAX_SIZE];
40 
41  time(&ts);
42 
43  switch (option) {
44 
45  case HUMAN_READABLE:
46  strftime(buffer, MAX_SIZE, "%x", localtime(&ts));
47  break;
48 
49  case ISO8601:
50  strftime(buffer, MAX_SIZE, "%F", localtime(&ts));
51  break;
52 
53  default:
54  THROW(JLANG::JValueOutOfRange, "JDate::getDate(): Invalid formatting option.");
55  }
56 
57  return buffer;
58  }
59 
60 
61  /**
62  * Get ASCII formatted time.
63  *
64  * \param option formatting option
65  * \return time
66  */
67  inline const char* getTime(const JDateAndTimeFormat option = ISO8601)
68  {
69  static time_t ts;
70  static const int MAX_SIZE = 256;
71  static char buffer[MAX_SIZE];
72 
73  time(&ts);
74 
75  switch (option) {
76 
77  case HUMAN_READABLE:
78  strftime(buffer, MAX_SIZE, "%X %Z", localtime(&ts));
79  break;
80 
81  case ISO8601:
82  strftime(buffer, MAX_SIZE, "%T%z", localtime(&ts));
83  break;
84 
85  default:
86  THROW(JLANG::JValueOutOfRange, "JDate::getTime(): Invalid formatting option.");
87  }
88 
89  return buffer;
90  }
91 
92 
93  /**
94  * Auxililary class to get date and time.
95  */
96  struct JDateAndTime {
97  /**
98  * Default constructor.
99  */
101  {
102  set();
103  }
104 
105 
106  /**
107  * Constructor.
108  *
109  * \param t1 time
110  */
111  JDateAndTime(const time_t t1)
112  {
113  set(t1);
114  }
115 
116 
117  /**
118  * Smart pointer.
119  *
120  * \return pointer to time structure
121  */
123  {
124  return tp;
125  }
126 
127 
128  /**
129  * Smart pointer.
130  *
131  * \return pointer to time structure
132  */
133  const tm* operator->() const
134  {
135  return tp;
136  }
137 
138  int getSeconds() const { return tp->tm_sec; } //!< seconds after the minute [0-59]
139  int getMinutes() const { return tp->tm_min; } //!< minutes after the hour [0-59]
140  int getHour() const { return tp->tm_hour; } //!< hours after midnight [0-23]
141  int getDay() const { return tp->tm_mday; } //!< day of the month [1-31]
142  int getMonth() const { return tp->tm_mon + 1; } //!< month of the year [1-12]
143  int getYear() const { return tp->tm_year + 1900; } //!< year a.d.
144 
145 
146  /**
147  * Type conversion operator.
148  *
149  * \return ASCII formatted date and time
150  */
151  operator std::string() const
152  {
153  return toString();
154  }
155 
156 
157  /**
158  * Function to check ISO-8601 conformity of string-formatted date and time.
159  *
160  * \param datestr string-formatted date and time
161  * \return true if date and time are ISO-8601-conform; else false
162  */
163  inline static bool isISO8601(const std::string& datestr)
164  {
165  using namespace std;
166 
167  const size_t pos = ( datestr.find('Z') != string::npos ? datestr.find('Z') :
168  (datestr.find('+') != string::npos ? datestr.find('+') : datestr.find('-')) );
169 
170  if (pos != string::npos) {
171 
172  static tm t;
173 
174  const string td = datestr.substr(0, pos);
175  const string tz = datestr.substr(pos+1);
176 
177  const char* p0 = strptime(td.c_str(), "%FT%T", &t);
178  const char* p1 = ( tz.size() < 3 ? strptime(tz.c_str(), "%H", &t) :
179  (tz.size() < 5 ? strptime(tz.c_str(), "%H%M", &t) : strptime(tz.c_str(), "%H:%M", &t)) );
180 
181  return ( (p0 != NULL && string(p0).empty()) && ((p1 != NULL && string(p1).empty()) || tz == "Z") );
182  }
183 
184  return false;
185  }
186 
187 
188  /**
189  * Get ASCII formatted date and time.
190  *
191  * \param option formatting option
192  * \return ASCII formatted date and time
193  */
194  inline std::string toString(const JDateAndTimeFormat option = ISO8601) const
195  {
196  using namespace std;
197 
198  mktime(tp);
199 
200  static const int MAX_SIZE = 256;
201  static char buffer[MAX_SIZE];
202 
203  switch (option) {
204 
205  case ISO8601:
206  strftime(buffer, MAX_SIZE, "%FT%T%z", tp);
207  break;
208 
209  case HUMAN_READABLE:
210  strftime(buffer, MAX_SIZE, "%a %b %d %X %Z %Y", tp);
211  break;
212 
213  default:
214  THROW(JLANG::JValueOutOfRange, "JDateAndTime::toString(): Invalid formatting option.");
215  }
216 
217  // remove the last character (carriage return) from the date string
218 
219  buffer[MAX_SIZE-1] = '\0';
220 
221  return string(buffer);
222  }
223 
224 
225  /**
226  * Set date and time.
227  *
228  * \return date and time
229  */
230  inline const JDateAndTime& operator()() const
231  {
232  set();
233 
234  return *this;
235  }
236 
237 
238  /**
239  * Set to actual time.
240  */
241  void set() const
242  {
243  time(&ts);
244 
245  tp = localtime(&ts);
246  }
247 
248 
249  /**
250  * Set to given time.
251  *
252  * \param t1 time
253  */
254  void set(const time_t t1) const
255  {
256  ts = t1;
257  tp = localtime(&t1);
258  }
259 
260 
261  /**
262  * Get elapsed time since given date and time.
263  *
264  * \param object date and time
265  * \return time [s]
266  */
267  double getElapsedTime(const JDateAndTime& object) const
268  {
269  return difftime(this->ts, object.ts);
270  }
271 
272 
273  /**
274  * Write date and time to output.
275  *
276  * \param out output stream
277  * \param object date and time
278  * \return output stream
279  */
280  friend inline std::ostream& operator<<(std::ostream& out, const JDateAndTime& object)
281  {
282  return out << object.toString();
283  }
284 
285 
286  private:
287  mutable time_t ts;
288  mutable tm* tp;
289  };
290 
291 
292  /**
293  * Function object to get ASCII formatted date and time.
294  */
296 }
297 
298 #endif
Exceptions.
void set() const
Set to actual time.
TPaveText * p1
const tm * operator->() const
Smart pointer.
friend std::ostream & operator<<(std::ostream &out, const JDateAndTime &object)
Write date and time to output.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
JDateAndTimeFormat
Date and time formats.
int getSeconds() const
seconds after the minute [0-59]
double getTime(const Hit &hit)
Get true time of hit.
std::string toString(const JDateAndTimeFormat option=ISO8601) const
Get ASCII formatted date and time.
const JDateAndTime & operator()() const
Set date and time.
JDateAndTime()
Default constructor.
static JDateAndTime getDateAndTime
Function object to get ASCII formatted date and time.
Human readable format (Www Mmm dd hh:mm:ss yyyy)
int getMonth() const
month of the year [1-12]
static bool isISO8601(const std::string &datestr)
Function to check ISO-8601 conformity of string-formatted date and time.
ISO-8601 standard.
const char * getDate(const JDateAndTimeFormat option=ISO8601)
Get ASCII formatted date.
int getDay() const
day of the month [1-31]
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time since given date and time.
void set(const time_t t1) const
Set to given time.
Auxililary class to get date and time.
int getHour() const
hours after midnight [0-23]
int getMinutes() const
minutes after the hour [0-59]
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
int getYear() const
year a.d.
tm * operator->()
Smart pointer.
JDateAndTime(const time_t t1)
Constructor.