Jpp  17.3.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
JSYSTEM::JDateAndTime Struct Reference

Auxiliary class for date and time. More...

#include <JDate.hh>

Public Member Functions

 JDateAndTime ()
 Default constructor. More...
 
 JDateAndTime (const time_t t1, const bool utc=false)
 Constructor. More...
 
 JDateAndTime (const std::string &buffer)
 Constructor. More...
 
const tm * operator-> () const
 Smart pointer. More...
 
tm * operator-> ()
 Smart pointer. More...
 
bool isUTC () const
 Check if UTC time. More...
 
time_t getTime () const
 time More...
 
int getSeconds () const
 seconds after the minute [0-59] More...
 
int getMinutes () const
 minutes after the hour [0-59] More...
 
int getHour () const
 hours after midnight [0-23] More...
 
int getDay () const
 day of the month [1-31] More...
 
int getMonth () const
 month of the year [1-12] More...
 
int getYear () const
 year a.d. More...
 
int getDST () const
 daylight saving time More...
 
 operator std::string () const
 Type conversion operator. More...
 
std::string toString () const
 Get ASCII formatted date and time. More...
 
const JDateAndTimeoperator() ()
 Set date and time. More...
 
void set (const bool utc=false)
 Set to current local time. More...
 
void set (const time_t t1, const bool utc=false)
 Set to given time. More...
 
void add (const time_t t1)
 Add given time. More...
 
void sub (const time_t t1)
 Subtract given time. More...
 
double getElapsedTime (const JDateAndTime &object) const
 Get elapsed time to given date and time. More...
 

Static Public Member Functions

static bool isISO8601 (const std::string &buffer)
 Function to check ISO-8601 conformity. More...
 

Private Attributes

time_t ts
 value More...
 
tm * tp
 representation More...
 
bool utc = false
 UTC. More...
 

Friends

std::istream & operator>> (std::istream &in, JDateAndTime &object)
 Read date and time from input stream. More...
 
std::ostream & operator<< (std::ostream &out, const JDateAndTime &object)
 Write date and time to output stream. More...
 

Detailed Description

Auxiliary class for date and time.

The data structure time_t is used for the internal value and the data structure tm for the representation.

The I/O format conforms with ISO-8601 standard but it is also possible to parse ROOT TTimeStamp::AsString("c").

Definition at line 75 of file JSystem/JDate.hh.

Constructor & Destructor Documentation

JSYSTEM::JDateAndTime::JDateAndTime ( )
inline

Default constructor.

Definition at line 79 of file JSystem/JDate.hh.

80  {
81  set();
82  }
void set(const bool utc=false)
Set to current local time.
JSYSTEM::JDateAndTime::JDateAndTime ( const time_t  t1,
const bool  utc = false 
)
inline

Constructor.

Parameters
t1time
utcUTC

Definition at line 91 of file JSystem/JDate.hh.

92  {
93  set(t1, utc);
94  }
void set(const bool utc=false)
Set to current local time.
JSYSTEM::JDateAndTime::JDateAndTime ( const std::string buffer)
inline

Constructor.

Parameters
bufferdate and time

Definition at line 102 of file JSystem/JDate.hh.

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  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::vector< size_t > ns
void set(const bool utc=false)
Set to current local time.

Member Function Documentation

const tm* JSYSTEM::JDateAndTime::operator-> ( ) const
inline

Smart pointer.

Returns
pointer to time structure

Definition at line 190 of file JSystem/JDate.hh.

191  {
192  return this->tp;
193  }
tm * tp
representation
tm* JSYSTEM::JDateAndTime::operator-> ( )
inline

Smart pointer.

Returns
pointer to time structure

Definition at line 201 of file JSystem/JDate.hh.

202  {
203  return this->tp;
204  }
tm * tp
representation
bool JSYSTEM::JDateAndTime::isUTC ( ) const
inline

Check if UTC time.

Returns
true if UTC time; else false

Definition at line 212 of file JSystem/JDate.hh.

213  {
214  return utc;
215  }
time_t JSYSTEM::JDateAndTime::getTime ( ) const
inline

time

Definition at line 218 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getSeconds ( ) const
inline

seconds after the minute [0-59]

Definition at line 220 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getMinutes ( ) const
inline

minutes after the hour [0-59]

Definition at line 221 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getHour ( ) const
inline

hours after midnight [0-23]

Definition at line 222 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getDay ( ) const
inline

day of the month [1-31]

Definition at line 223 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getMonth ( ) const
inline

month of the year [1-12]

Definition at line 224 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getYear ( ) const
inline

year a.d.

Definition at line 225 of file JSystem/JDate.hh.

int JSYSTEM::JDateAndTime::getDST ( ) const
inline

daylight saving time

Definition at line 226 of file JSystem/JDate.hh.

JSYSTEM::JDateAndTime::operator std::string ( ) const
inline

Type conversion operator.

Returns
ASCII formatted date and time

Definition at line 234 of file JSystem/JDate.hh.

235  {
236  return this->toString();
237  }
std::string toString() const
Get ASCII formatted date and time.
static bool JSYSTEM::JDateAndTime::isISO8601 ( const std::string buffer)
inlinestatic

Function to check ISO-8601 conformity.

Parameters
bufferdate and time
Returns
true if date and time are ISO-8601 conform; else false

Definition at line 246 of file JSystem/JDate.hh.

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  }
Auxiliary class for date and time.
std::string JSYSTEM::JDateAndTime::toString ( ) const
inline

Get ASCII formatted date and time.

Returns
ASCII formatted date and time

Definition at line 267 of file JSystem/JDate.hh.

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  }
bool isUTC() const
Check if UTC time.
then awk string
tm * tp
representation
const JDateAndTime& JSYSTEM::JDateAndTime::operator() ( )
inline

Set date and time.

Returns
date and time

Definition at line 285 of file JSystem/JDate.hh.

286  {
287  set();
288 
289  return *this;
290  }
void set(const bool utc=false)
Set to current local time.
void JSYSTEM::JDateAndTime::set ( const bool  utc = false)
inline

Set to current local time.

Parameters
utcUTC

Definition at line 298 of file JSystem/JDate.hh.

299  {
300  time_t tx;
301 
302  time(&tx);
303 
304  set(tx, utc);
305  }
void set(const bool utc=false)
Set to current local time.
void JSYSTEM::JDateAndTime::set ( const time_t  t1,
const bool  utc = false 
)
inline

Set to given time.

Parameters
t1time
utcUTC

Definition at line 314 of file JSystem/JDate.hh.

315  {
316  this->ts = t1;
317  this->tp = (utc ? gmtime(&t1) : localtime(&t1));
318  this->utc = utc;
319  }
tm * tp
representation
void JSYSTEM::JDateAndTime::add ( const time_t  t1)
inline

Add given time.

Parameters
t1time

Definition at line 327 of file JSystem/JDate.hh.

328  {
329  set(this->ts + t1, this->utc);
330  }
void set(const bool utc=false)
Set to current local time.
void JSYSTEM::JDateAndTime::sub ( const time_t  t1)
inline

Subtract given time.

Parameters
t1time

Definition at line 338 of file JSystem/JDate.hh.

339  {
340  set(this->ts - t1, this->utc);
341  }
void set(const bool utc=false)
Set to current local time.
double JSYSTEM::JDateAndTime::getElapsedTime ( const JDateAndTime object) const
inline

Get elapsed time to given date and time.

Parameters
objectdate and time
Returns
time [s]

Definition at line 350 of file JSystem/JDate.hh.

351  {
352  return difftime(object.ts, this->ts);
353  }

Friends And Related Function Documentation

std::istream& operator>> ( std::istream &  in,
JDateAndTime object 
)
friend

Read date and time from input stream.

Parameters
ininput stream
objectdate and time
Returns
input stream

Definition at line 363 of file JSystem/JDate.hh.

364  {
365  std::string buffer;
366 
367  if (in >> buffer) {
368  object = JDateAndTime(buffer);
369  }
370 
371  return in;
372  }
JDateAndTime()
Default constructor.
then awk string
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
std::ostream& operator<< ( std::ostream &  out,
const JDateAndTime object 
)
friend

Write date and time to output stream.

Parameters
outoutput stream
objectdate and time
Returns
output stream

Definition at line 382 of file JSystem/JDate.hh.

383  {
384  return out << object.toString();
385  }

Member Data Documentation

time_t JSYSTEM::JDateAndTime::ts
private

value

Definition at line 389 of file JSystem/JDate.hh.

tm* JSYSTEM::JDateAndTime::tp
private

representation

Definition at line 390 of file JSystem/JDate.hh.

bool JSYSTEM::JDateAndTime::utc = false
private

UTC.

Definition at line 391 of file JSystem/JDate.hh.


The documentation for this struct was generated from the following file: