Jpp
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 <ostream>
7 
8 /**
9  * \file
10  * Date and time functions.
11  * \author mdejong
12  */
13 namespace JSYSTEM {}
14 namespace JPP { using namespace JSYSTEM; }
15 
16 namespace JSYSTEM {
17 
18  /**
19  * Get ASCII formatted date.
20  *
21  * \return date
22  */
23  inline const char* getDate()
24  {
25  static time_t ts;
26  static const int MAX_SIZE = 256;
27  static char buffer[MAX_SIZE];
28 
29  time(&ts);
30 
31  strftime(buffer, MAX_SIZE, "%x", localtime(&ts));
32 
33  return buffer;
34  }
35 
36 
37  /**
38  * Get ASCII formatted time.
39  *
40  * \return time
41  */
42  inline const char* getTime()
43  {
44  static time_t ts;
45  static const int MAX_SIZE = 256;
46  static char buffer[MAX_SIZE];
47 
48  time(&ts);
49 
50  strftime(buffer, MAX_SIZE, "%X", localtime(&ts));
51 
52  return buffer;
53  }
54 
55 
56  /**
57  * Auxililary class to get date and time.
58  */
59  struct JDateAndTime {
60  /**
61  * Default constructor.
62  */
64  {
65  set();
66  }
67 
68 
69  /**
70  * Constructor.
71  *
72  * \param t1 time
73  */
74  JDateAndTime(const time_t t1)
75  {
76  set(t1);
77  }
78 
79 
80  /**
81  * Smart pointer.
82  *
83  * \return pointer to time structure
84  */
85  const tm* operator->() const
86  {
87  return tp;
88  }
89 
90 
91  int getSeconds() const { return tp->tm_sec; } //!< seconds after the minute [0-59]
92  int getMinutes() const { return tp->tm_min; } //!< minutes after the hour [0-59]
93  int getHour() const { return tp->tm_hour; } //!< hours after midnight [0-23]
94  int getDay() const { return tp->tm_mday; } //!< day of the month [1-31]
95  int getMonth() const { return tp->tm_mon + 1; } //!< month of the year [1-12]
96  int getYear() const { return tp->tm_year + 1900; } //!< year a.d.
97 
98 
99  /**
100  * Type conversion operator.
101  *
102  * \return ASCII formatted date and time
103  */
104  operator std::string() const
105  {
106  return toString();
107  }
108 
109 
110  /**
111  * Get ASCII formatted date and time.
112  *
113  * \return ASCII formatted date and time
114  */
115  inline std::string toString() const
116  {
117  const std::string buffer = ctime(&ts);
118 
119  // remove the last character (carriage return) from the date string
120 
121  return buffer.substr(0, buffer.length() - 1);
122  }
123 
124 
125  /**
126  * Set date and time.
127  *
128  * \return date and time
129  */
130  inline const JDateAndTime& operator()() const
131  {
132  set();
133 
134  return *this;
135  }
136 
137 
138  /**
139  * Set to actual time.
140  */
141  void set() const
142  {
143  time(&ts);
144 
145  tp = localtime(&ts);
146  }
147 
148 
149  /**
150  * Set to given time.
151  *
152  * \param t1 time
153  */
154  void set(const time_t t1) const
155  {
156  ts = t1;
157  tp = localtime(&t1);
158  }
159 
160 
161  /**
162  * Get elapsed time since given date and time.
163  *
164  * \param object date and time
165  * \return time [s]
166  */
167  double getElapsedTime(const JDateAndTime& object) const
168  {
169  return difftime(this->ts, object.ts);
170  }
171 
172 
173  /**
174  * Write date and time to output.
175  *
176  * \param out output stream
177  * \param object date and time
178  * \return output stream
179  */
180  friend inline std::ostream& operator<<(std::ostream& out, const JDateAndTime& object)
181  {
182  return out << object.toString();
183  }
184 
185 
186  private:
187  mutable time_t ts;
188  mutable tm* tp;
189  };
190 
191 
192  /**
193  * Function object to get ASCII formatted date and time.
194  */
196 }
197 
198 #endif
JSYSTEM::JDateAndTime::getSeconds
int getSeconds() const
seconds after the minute [0-59]
Definition: JDate.hh:91
JSYSTEM::getTime
const char * getTime()
Get ASCII formatted time.
Definition: JDate.hh:42
JSYSTEM::JDateAndTime::tp
tm * tp
Definition: JDate.hh:188
JSYSTEM::JDateAndTime::toString
std::string toString() const
Get ASCII formatted date and time.
Definition: JDate.hh:115
JSYSTEM::JDateAndTime::getDay
int getDay() const
day of the month [1-31]
Definition: JDate.hh:94
JSYSTEM::getDateAndTime
static JDateAndTime getDateAndTime
Function object to get ASCII formatted date and time.
Definition: JDate.hh:195
JSYSTEM::JDateAndTime::ts
time_t ts
Definition: JDate.hh:187
JSYSTEM::JDateAndTime::JDateAndTime
JDateAndTime(const time_t t1)
Constructor.
Definition: JDate.hh:74
JSYSTEM::JDateAndTime::operator<<
friend std::ostream & operator<<(std::ostream &out, const JDateAndTime &object)
Write date and time to output.
Definition: JDate.hh:180
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JSYSTEM::JDateAndTime::JDateAndTime
JDateAndTime()
Default constructor.
Definition: JDate.hh:63
JSYSTEM
Auxiliary classes and methods for operating system calls.
Definition: JDate.hh:13
JSYSTEM::JDateAndTime::getYear
int getYear() const
year a.d.
Definition: JDate.hh:96
JSYSTEM::JDateAndTime::getMonth
int getMonth() const
month of the year [1-12]
Definition: JDate.hh:95
JSYSTEM::JDateAndTime::getElapsedTime
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time since given date and time.
Definition: JDate.hh:167
JSYSTEM::JDateAndTime
Auxililary class to get date and time.
Definition: JDate.hh:59
JSYSTEM::JDateAndTime::operator->
const tm * operator->() const
Smart pointer.
Definition: JDate.hh:85
JSYSTEM::JDateAndTime::operator()
const JDateAndTime & operator()() const
Set date and time.
Definition: JDate.hh:130
JSYSTEM::JDateAndTime::set
void set(const time_t t1) const
Set to given time.
Definition: JDate.hh:154
JSYSTEM::getDate
const char * getDate()
Get ASCII formatted date.
Definition: JDate.hh:23
JSYSTEM::JDateAndTime::getHour
int getHour() const
hours after midnight [0-23]
Definition: JDate.hh:93
JSYSTEM::JDateAndTime::getMinutes
int getMinutes() const
minutes after the hour [0-59]
Definition: JDate.hh:92
JSYSTEM::JDateAndTime::set
void set() const
Set to actual time.
Definition: JDate.hh:141