Jpp - 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
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 = HUMAN_READABLE)
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 = HUMAN_READABLE)
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  * Get ASCII formatted date and time.
159  *
160  * \param option formatting option
161  * \return ASCII formatted date and time
162  */
163  inline std::string toString(const JDateAndTimeFormat option = HUMAN_READABLE) const
164  {
165  using namespace std;
166 
167  mktime(tp);
168 
169  static const int MAX_SIZE = 256;
170  static char buffer[MAX_SIZE];
171 
172  switch (option) {
173 
174  case ISO8601:
175  strftime(buffer, MAX_SIZE, "%FT%T%z", tp);
176  break;
177 
178  case HUMAN_READABLE:
179  strftime(buffer, MAX_SIZE, "%a %b %d %X %Z %Y", tp);
180  break;
181 
182  default:
183  THROW(JLANG::JValueOutOfRange, "JDateAndTime::toString(): Invalid formatting option.");
184  }
185 
186  // remove the last character (carriage return) from the date string
187 
188  buffer[MAX_SIZE-1] = '\0';
189 
190  return string(buffer);
191  }
192 
193 
194  /**
195  * Set date and time.
196  *
197  * \return date and time
198  */
199  inline const JDateAndTime& operator()() const
200  {
201  set();
202 
203  return *this;
204  }
205 
206 
207  /**
208  * Set to actual time.
209  */
210  void set() const
211  {
212  time(&ts);
213 
214  tp = localtime(&ts);
215  }
216 
217 
218  /**
219  * Set to given time.
220  *
221  * \param t1 time
222  */
223  void set(const time_t t1) const
224  {
225  ts = t1;
226  tp = localtime(&t1);
227  }
228 
229 
230  /**
231  * Get elapsed time since given date and time.
232  *
233  * \param object date and time
234  * \return time [s]
235  */
236  double getElapsedTime(const JDateAndTime& object) const
237  {
238  return difftime(this->ts, object.ts);
239  }
240 
241 
242  /**
243  * Write date and time to output.
244  *
245  * \param out output stream
246  * \param object date and time
247  * \return output stream
248  */
249  friend inline std::ostream& operator<<(std::ostream& out, const JDateAndTime& object)
250  {
251  return out << object.toString();
252  }
253 
254 
255  private:
256  mutable time_t ts;
257  mutable tm* tp;
258  };
259 
260 
261  /**
262  * Function object to get ASCII formatted date and time.
263  */
265 }
266 
267 #endif
Exceptions.
void set() const
Set to actual time.
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:670
JDateAndTimeFormat
Date and time formats.
int getSeconds() const
seconds after the minute [0-59]
double getTime(const Hit &hit)
Get true time of hit.
const JDateAndTime & operator()() const
Set date and time.
JDateAndTime()
Default constructor.
static JDateAndTime getDateAndTime
Function object to get ASCII formatted date and time.
std::string toString(const JDateAndTimeFormat option=HUMAN_READABLE) const
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]
ISO-8601 standard.
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.
const char * getDate(const JDateAndTimeFormat option=HUMAN_READABLE)
Get ASCII formatted date.
JDateAndTime(const time_t t1)
Constructor.