Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
JSYSTEM::JDateAndTime Struct Reference

Auxiliary class for date and time. More...

#include <JDateAndTime.hh>

Inheritance diagram for JSYSTEM::JDateAndTime:
JLANG::JComparable< JFirst_t, JSecond_t >

Public Member Functions

 JDateAndTime (const bool utc=false)
 Constructor. More...
 
 JDateAndTime (const time_t t1, const bool utc=false)
 Constructor. More...
 
 JDateAndTime (const time_t t1, const float f1, const bool utc=false)
 Constructor. More...
 
 JDateAndTime (const std::string &buffer)
 Constructor. More...
 
bool less (const JDateAndTime &object) const
 Less than method. More...
 
const tm * operator-> () const
 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...
 
float getFS () const
 fraction of second 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 set (const time_t t1, const float f1, 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 Member Functions

tm * get () const
 Get representation. More...
 

Private Attributes

time_t ts
 value More...
 
float fs = 0.0
 fraction of second 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 78 of file JDateAndTime.hh.

Constructor & Destructor Documentation

◆ JDateAndTime() [1/4]

JSYSTEM::JDateAndTime::JDateAndTime ( const bool  utc = false)
inline

Constructor.

Parameters
utcUTC

Definition at line 86 of file JDateAndTime.hh.

87  {
88  set(utc);
89  }
void set(const bool utc=false)
Set to current local time.

◆ JDateAndTime() [2/4]

JSYSTEM::JDateAndTime::JDateAndTime ( const time_t  t1,
const bool  utc = false 
)
inline

Constructor.

Parameters
t1time [s]
utcUTC

Definition at line 98 of file JDateAndTime.hh.

99  {
100  set(t1, utc);
101  }

◆ JDateAndTime() [3/4]

JSYSTEM::JDateAndTime::JDateAndTime ( const time_t  t1,
const float  f1,
const bool  utc = false 
)
inline

Constructor.

Parameters
t1time [s]
f1fraction of seconds
utcUTC

Definition at line 111 of file JDateAndTime.hh.

112  {
113  set(t1, f1, utc);
114  }
const JPolynome f1(1.0, 2.0, 3.0)
Function.

◆ JDateAndTime() [4/4]

JSYSTEM::JDateAndTime::JDateAndTime ( const std::string &  buffer)
inline

Constructor.

Parameters
bufferdate and time

Definition at line 122 of file JDateAndTime.hh.

123  {
124  std::istringstream is(buffer);
125 
126  is >> *this;
127  }

Member Function Documentation

◆ less()

bool JSYSTEM::JDateAndTime::less ( const JDateAndTime object) const
inline

Less than method.

Parameters
objectdate and time
Returns
true if this date and time is less than given data and time; else false

Definition at line 136 of file JDateAndTime.hh.

137  {
138  return getElapsedTime(object) > 0.0;
139  }
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time to given date and time.

◆ operator->()

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

Smart pointer.

Returns
pointer to time structure

Definition at line 147 of file JDateAndTime.hh.

148  {
149  return get();
150  }
tm * get() const
Get representation.

◆ isUTC()

bool JSYSTEM::JDateAndTime::isUTC ( ) const
inline

Check if UTC time.

Returns
true if UTC time; else false

Definition at line 158 of file JDateAndTime.hh.

159  {
160  return utc;
161  }

◆ getTime()

time_t JSYSTEM::JDateAndTime::getTime ( ) const
inline

time

Definition at line 164 of file JDateAndTime.hh.

◆ getSeconds()

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

seconds after the minute [0-59]

Definition at line 166 of file JDateAndTime.hh.

◆ getMinutes()

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

minutes after the hour [0-59]

Definition at line 167 of file JDateAndTime.hh.

◆ getHour()

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

hours after midnight [0-23]

Definition at line 168 of file JDateAndTime.hh.

◆ getDay()

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

day of the month [1-31]

Definition at line 169 of file JDateAndTime.hh.

◆ getMonth()

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

month of the year [1-12]

Definition at line 170 of file JDateAndTime.hh.

◆ getYear()

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

year a.d.

Definition at line 171 of file JDateAndTime.hh.

◆ getDST()

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

daylight saving time

Definition at line 172 of file JDateAndTime.hh.

◆ getFS()

float JSYSTEM::JDateAndTime::getFS ( ) const
inline

fraction of second

Definition at line 174 of file JDateAndTime.hh.

◆ operator std::string()

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

Type conversion operator.

Returns
ASCII formatted date and time

Definition at line 182 of file JDateAndTime.hh.

183  {
184  return this->toString();
185  }
std::string toString() const
Get ASCII formatted date and time.

◆ isISO8601()

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 194 of file JDateAndTime.hh.

195  {
196  using namespace std;
197 
198  try {
199 
200  JDateAndTime t1(buffer);
201 
202  return true;
203  }
204  catch(const std::exception& error) {
205  return false;
206  }
207  }
Definition: JSTDTypes.hh:14
Auxiliary class for date and time.
Definition: JDateAndTime.hh:80

◆ toString()

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

Get ASCII formatted date and time.

Returns
ASCII formatted date and time

Definition at line 215 of file JDateAndTime.hh.

216  {
217  using namespace std;
218 
219  static const size_t MAX_SIZE = 256;
220  static char buffer[MAX_SIZE];
221 
222  const size_t pos = strftime(buffer, MAX_SIZE, (isUTC() ? "%FT%TZ" : "%FT%T%z"), this->get());
223 
224  return string(buffer, pos);
225  }
bool isUTC() const
Check if UTC time.

◆ operator()()

const JDateAndTime& JSYSTEM::JDateAndTime::operator() ( )
inline

Set date and time.

Returns
date and time

Definition at line 233 of file JDateAndTime.hh.

234  {
235  set();
236 
237  return *this;
238  }

◆ set() [1/3]

void JSYSTEM::JDateAndTime::set ( const bool  utc = false)
inline

Set to current local time.

Parameters
utcUTC

Definition at line 246 of file JDateAndTime.hh.

247  {
248  time_t tx;
249 
250  time(&tx);
251 
252  set(tx, utc);
253  }

◆ set() [2/3]

void JSYSTEM::JDateAndTime::set ( const time_t  t1,
const bool  utc = false 
)
inline

Set to given time.

Parameters
t1time [s]
utcUTC

Definition at line 262 of file JDateAndTime.hh.

263  {
264  this->ts = t1;
265  this->utc = utc;
266  }

◆ set() [3/3]

void JSYSTEM::JDateAndTime::set ( const time_t  t1,
const float  f1,
const bool  utc = false 
)
inline

Set to given time.

Parameters
t1time [s]
f1fraction of seconds
utcUTC

Definition at line 276 of file JDateAndTime.hh.

277  {
278  this->ts = t1;
279  this->fs = f1;
280  this->utc = utc;
281  }
float fs
fraction of second

◆ add()

void JSYSTEM::JDateAndTime::add ( const time_t  t1)
inline

Add given time.

Parameters
t1time [s]

Definition at line 289 of file JDateAndTime.hh.

290  {
291  set(this->ts + t1, this->utc);
292  }

◆ sub()

void JSYSTEM::JDateAndTime::sub ( const time_t  t1)
inline

Subtract given time.

Parameters
t1time [s]

Definition at line 300 of file JDateAndTime.hh.

301  {
302  set(this->ts - t1, this->utc);
303  }

◆ getElapsedTime()

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 312 of file JDateAndTime.hh.

313  {
314  return difftime(object.ts, this->ts) + (object.fs - this->fs);
315  }

◆ get()

tm* JSYSTEM::JDateAndTime::get ( ) const
inlineprivate

Get representation.

Definition at line 453 of file JDateAndTime.hh.

454  {
455  return (this->tp = (utc ? gmtime(&ts) : localtime(&ts)));
456  }
tm * tp
representation

Friends And Related Function Documentation

◆ operator>>

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 325 of file JDateAndTime.hh.

326  {
327  using namespace std;
328 
329  const size_t LENGTH = 10; // date may contain '-' and/or be followed by ' '
330 
331  string buffer;
332 
333  for (int c; (c = in.peek()) != EOF && isspace((char) in.peek()); ) {
334  in.ignore(1);
335  }
336 
337  for (int c; (c = in.get()) != EOF && (c != ' ' || buffer.length() == LENGTH); ) {
338  buffer.push_back((char) c);
339  }
340 
341  if (in.bad()) {
342  THROW(JIOException, "");
343  }
344 
345  // separation of date and time from optional time zone
346 
347  size_t pos = string::npos;
348 
349  if (pos == string::npos) { pos = buffer.find('Z', LENGTH); } // UTC
350  if (pos == string::npos) { pos = buffer.find('+', LENGTH); } // optional time zone
351  if (pos == string::npos) { pos = buffer.find('-', LENGTH); } // optional time zone
352 
353  tm tx;
354  object.fs = 0; // fraction of seconds
355  int nd = 0;
356 
357  const string td = buffer.substr(0, pos);
358  const char* pd = NULL;
359 
360  switch (td.length()) {
361 
362  case 29:
363  case 28:
364  case 27:
365  case 26:
366  case 25:
367  case 24:
368  case 23:
369  case 22:
370  case 21:
371  pd = strptime(td.c_str(), "%Y-%m-%d %H:%M:%S", &tx); sscanf(pd, "%f%n", &object.fs, &nd); pd += nd;
372  break;
373 
374  case 19:
375  pd = strptime(td.c_str(), "%Y-%m-%dT%H:%M:%S", &tx);
376  break;
377 
378  case 15:
379  pd = strptime(td.c_str(), "%Y%m%dT%H%M%s", &tx);
380  break;
381 
382  case 10:
383  pd = strptime(td.c_str(), "%Y-%m-%d", &tx);
384  break;
385  }
386 
387  if (pd == NULL || *pd != '\0') {
388  THROW(JParseError, "invalid input <" << buffer << ">");
389  }
390 
391  tm ty;
392 
393  ty.tm_hour = 0;
394  ty.tm_min = 0;
395 
396  if (pos != string::npos && (buffer[pos] == '+' ||
397  buffer[pos] == '-')) {
398 
399  const string tz = buffer.substr(pos + 1);
400  const char* pz = tz.c_str();
401 
402  switch (tz.length()) {
403 
404  case 2:
405  pz = strptime(tz.c_str(), "%H", &ty);
406  break;
407 
408  case 4:
409  pz = strptime(tz.c_str(), "%H%M", &ty);
410  break;
411 
412  case 5:
413  pz = strptime(tz.c_str(), "%H:%M", &ty);
414  break;
415  }
416 
417  if (pz == NULL || *pz != '\0') {
418  THROW(JParseError, "invalid input <" << buffer << ">");
419  }
420  }
421 
422  tx.tm_isdst = 0; // switch off day light saving time
423 
424  time_t t1 = mktime(&tx);
425 
426  t1 -= (ty.tm_hour * 60 + ty.tm_min) * 60; // correct time zone
427  t1 += mktime(localtime(&t1)) - mktime(gmtime(&t1)); //
428 
429  object.set(t1, buffer[pos] == 'Z');
430 
431  return in;
432  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712

◆ operator<<

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 442 of file JDateAndTime.hh.

443  {
444  return out << object.toString();
445  }

Member Data Documentation

◆ ts

time_t JSYSTEM::JDateAndTime::ts
private

value

Definition at line 458 of file JDateAndTime.hh.

◆ fs

float JSYSTEM::JDateAndTime::fs = 0.0
private

fraction of second

Definition at line 459 of file JDateAndTime.hh.

◆ tp

tm* JSYSTEM::JDateAndTime::tp
mutableprivate

representation

Definition at line 460 of file JDateAndTime.hh.

◆ utc

bool JSYSTEM::JDateAndTime::utc = false
private

UTC.

Definition at line 461 of file JDateAndTime.hh.


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