Jpp  17.3.1
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 <istream>
5 #include <ostream>
6 #include <string>
7 #include <string.h>
8 #include <time.h>
9 
10 #include "JLang/JException.hh"
11 
12 
13 /**
14  * \file
15  * Date and time functions.
16  *
17  * \author mdejong, bjung
18  */
19 namespace JSYSTEM {}
20 namespace JPP { using namespace JSYSTEM; }
21 
22 namespace JSYSTEM {
23 
25  using JLANG::JParseError;
26 
27 
28  /**
29  * Get current date conform ISO-8601 standard.
30  *
31  * \return date
32  */
33  inline const char* getDate()
34  {
35  static time_t ts;
36  static const size_t MAX_SIZE = 256;
37  static char buffer[MAX_SIZE];
38 
39  time(&ts);
40 
41  strftime(buffer, MAX_SIZE, "%F", localtime(&ts));
42 
43  return buffer;
44  }
45 
46 
47  /**
48  * Get current time conform ISO-8601 standard.
49  *
50  * \return time
51  */
52  inline const char* getTime()
53  {
54  static time_t ts;
55  static const size_t MAX_SIZE = 256;
56  static char buffer[MAX_SIZE];
57 
58  time(&ts);
59 
60  strftime(buffer, MAX_SIZE, "%T%z", localtime(&ts));
61 
62  return buffer;
63  }
64 
65 
66  /**
67  * Auxiliary class for date and time.
68  *
69  * The data structure <tt>time_t</tt> is used for the internal value and
70  * the data structure <tt>tm</tt> for the representation.
71  *
72  * The I/O format conforms with ISO-8601 standard
73  * but it is also possible to parse <tt>ROOT TTimeStamp::AsString("c")</tt>.
74  */
75  struct JDateAndTime {
76  /**
77  * Default constructor.
78  */
80  {
81  set();
82  }
83 
84 
85  /**
86  * Constructor.
87  *
88  * \param t1 time
89  * \param utc UTC
90  */
91  JDateAndTime(const time_t t1, const bool utc = false)
92  {
93  set(t1, utc);
94  }
95 
96 
97  /**
98  * Constructor.
99  *
100  * \param buffer date and time
101  */
102  JDateAndTime(const std::string& buffer)
103  {
104  using namespace std;
105 
106  // separation of date and time from optional time zone
107 
108  const size_t pos = (buffer.find('Z') != string::npos ? buffer.find('Z') :
109  buffer.find('+') != string::npos ? buffer.find('+') : buffer.rfind('-'));
110 
111  if (pos == string::npos) {
112  THROW(JParseError, "invalid input <" << buffer << ">");
113  }
114 
115  tm tx;
116  long ns = 0;
117 
118  const string td = buffer.substr(0, pos);
119  const char* pd = NULL;
120 
121  switch (td.length()) {
122 
123  case 29:
124  pd = strptime(td.c_str(), "%Y-%m-%d %H:%M:%S.", &tx); sscanf(pd, "%9ld", &ns); pd += 9;
125  break;
126 
127  case 19:
128  pd = strptime(td.c_str(), "%Y-%m-%dT%H:%M:%S", &tx);
129  break;
130 
131  case 15:
132  pd = strptime(td.c_str(), "%Y%m%dT%H%M%s", &tx);
133  break;
134 
135  case 10:
136  pd = strptime(td.c_str(), "%Y-%m-%d", &tx);
137  break;
138  }
139 
140  if (pd == NULL || *pd != '\0') {
141  THROW(JParseError, "invalid input <" << buffer << ">");
142  }
143 
144  tm ty;
145 
146  const string tz = buffer.substr(pos + 1);
147  const char* pz = tz.c_str();
148 
149  ty.tm_hour = 0;
150  ty.tm_min = 0;
151 
152  switch (tz.length()) {
153 
154  case 0:
155  break;
156 
157  case 2:
158  pz = strptime(tz.c_str(), "%H", &ty);
159  break;
160 
161  case 4:
162  pz = strptime(tz.c_str(), "%H%M", &ty);
163  break;
164 
165  case 5:
166  pz = strptime(tz.c_str(), "%H:%M", &ty);
167  break;
168  }
169 
170  if (pz == NULL || *pz != '\0') {
171  THROW(JParseError, "invalid input <" << buffer << ">");
172  }
173 
174  tx.tm_isdst = 0;
175 
176  time_t t1 = mktime(&tx);
177 
178  t1 -= (ty.tm_hour * 60 + ty.tm_min) * 60; // apply read time zone
179  t1 += mktime(localtime(&t1)) - mktime(gmtime(&t1)); //
180 
181  set(t1, buffer[pos] == 'Z');
182  }
183 
184 
185  /**
186  * Smart pointer.
187  *
188  * \return pointer to time structure
189  */
190  const tm* operator->() const
191  {
192  return this->tp;
193  }
194 
195 
196  /**
197  * Smart pointer.
198  *
199  * \return pointer to time structure
200  */
202  {
203  return this->tp;
204  }
205 
206 
207  /**
208  * Check if UTC time.
209  *
210  * \return true if UTC time; else false
211  */
212  bool isUTC() const
213  {
214  return utc;
215  }
216 
217 
218  time_t getTime() const { return ts; } //!< time
219 
220  int getSeconds() const { return tp->tm_sec; } //!< seconds after the minute [0-59]
221  int getMinutes() const { return tp->tm_min; } //!< minutes after the hour [0-59]
222  int getHour() const { return tp->tm_hour; } //!< hours after midnight [0-23]
223  int getDay() const { return tp->tm_mday; } //!< day of the month [1-31]
224  int getMonth() const { return tp->tm_mon + 1; } //!< month of the year [1-12]
225  int getYear() const { return tp->tm_year + 1900; } //!< year a.d.
226  int getDST() const { return tp->tm_isdst; } //!< daylight saving time
227 
228 
229  /**
230  * Type conversion operator.
231  *
232  * \return ASCII formatted date and time
233  */
234  operator std::string() const
235  {
236  return this->toString();
237  }
238 
239 
240  /**
241  * Function to check ISO-8601 conformity.
242  *
243  * \param buffer date and time
244  * \return true if date and time are ISO-8601 conform; else false
245  */
246  inline static bool isISO8601(const std::string& buffer)
247  {
248  using namespace std;
249 
250  try {
251 
252  JDateAndTime t1(buffer);
253 
254  return true;
255  }
256  catch(const std::exception& error) {
257  return false;
258  }
259  }
260 
261 
262  /**
263  * Get ASCII formatted date and time.
264  *
265  * \return ASCII formatted date and time
266  */
267  inline std::string toString() const
268  {
269  using namespace std;
270 
271  static const size_t MAX_SIZE = 256;
272  static char buffer[MAX_SIZE];
273 
274  const size_t pos = strftime(buffer, MAX_SIZE, (isUTC() ? "%FT%TZ" : "%FT%T%z"), this->tp);
275 
276  return string(buffer, pos);
277  }
278 
279 
280  /**
281  * Set date and time.
282  *
283  * \return date and time
284  */
285  inline const JDateAndTime& operator()()
286  {
287  set();
288 
289  return *this;
290  }
291 
292 
293  /**
294  * Set to current local time.
295  *
296  * \param utc UTC
297  */
298  void set(const bool utc = false)
299  {
300  time_t tx;
301 
302  time(&tx);
303 
304  set(tx, utc);
305  }
306 
307 
308  /**
309  * Set to given time.
310  *
311  * \param t1 time
312  * \param utc UTC
313  */
314  void set(const time_t t1, const bool utc = false)
315  {
316  this->ts = t1;
317  this->tp = (utc ? gmtime(&t1) : localtime(&t1));
318  this->utc = utc;
319  }
320 
321 
322  /**
323  * Add given time.
324  *
325  * \param t1 time
326  */
327  void add(const time_t t1)
328  {
329  set(this->ts + t1, this->utc);
330  }
331 
332 
333  /**
334  * Subtract given time.
335  *
336  * \param t1 time
337  */
338  void sub(const time_t t1)
339  {
340  set(this->ts - t1, this->utc);
341  }
342 
343 
344  /**
345  * Get elapsed time to given date and time.
346  *
347  * \param object date and time
348  * \return time [s]
349  */
350  double getElapsedTime(const JDateAndTime& object) const
351  {
352  return difftime(object.ts, this->ts);
353  }
354 
355 
356  /**
357  * Read date and time from input stream.
358  *
359  * \param in input stream
360  * \param object date and time
361  * \return input stream
362  */
363  friend inline std::istream& operator>>(std::istream& in, JDateAndTime& object)
364  {
365  std::string buffer;
366 
367  if (in >> buffer) {
368  object = JDateAndTime(buffer);
369  }
370 
371  return in;
372  }
373 
374 
375  /**
376  * Write date and time to output stream.
377  *
378  * \param out output stream
379  * \param object date and time
380  * \return output stream
381  */
382  friend inline std::ostream& operator<<(std::ostream& out, const JDateAndTime& object)
383  {
384  return out << object.toString();
385  }
386 
387 
388  private:
389  time_t ts; //!< value
390  tm* tp; //!< representation
391  bool utc = false; //!< UTC
392  };
393 
394 
395  /**
396  * Function object to get current date and time.
397  */
399 }
400 
401 #endif
void set(const time_t t1, const bool utc=false)
Set to given time.
Exceptions.
const JDateAndTime & operator()()
Set date and time.
const tm * operator->() const
Smart pointer.
time_t getTime() const
time
friend std::ostream & operator<<(std::ostream &out, const JDateAndTime &object)
Write date and time to output stream.
std::string toString() const
Get ASCII formatted date and time.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::vector< size_t > ns
JDateAndTime(const time_t t1, const bool utc=false)
Constructor.
void set(const bool utc=false)
Set to current local time.
int getSeconds() const
seconds after the minute [0-59]
double getTime(const Hit &hit)
Get true time of hit.
void sub(const time_t t1)
Subtract given time.
JDateAndTime()
Default constructor.
static JDateAndTime getDateAndTime
Function object to get current date and time.
bool isUTC() const
Check if UTC time.
friend std::istream & operator>>(std::istream &in, JDateAndTime &object)
Read date and time from input stream.
static bool isISO8601(const std::string &buffer)
Function to check ISO-8601 conformity.
JDateAndTime(const std::string &buffer)
Constructor.
int getMonth() const
month of the year [1-12]
void add(const time_t t1)
Add given time.
then awk string
int getDay() const
day of the month [1-31]
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time to given date and time.
tm * tp
representation
const char * getDate()
Get current date conform ISO-8601 standard.
Auxiliary class for date and time.
Exception for parsing value.
Definition: JException.hh:180
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 getDST() const
daylight saving time
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
int getYear() const
year a.d.
tm * operator->()
Smart pointer.