Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
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.
 
 JDateAndTime (const time_t t1, const bool utc=false)
 Constructor.
 
 JDateAndTime (const time_t t1, const float f1, const bool utc=false)
 Constructor.
 
 JDateAndTime (const std::string &buffer)
 Constructor.
 
 JDateAndTime (const char *const buffer)
 Constructor.
 
bool less (const JDateAndTime &object) const
 Less than method.
 
const tm * operator-> () const
 Smart pointer.
 
bool isUTC () const
 Check if UTC time.
 
time_t getTime () const
 time
 
int getSeconds () const
 seconds after the minute [0-59]
 
int getMinutes () const
 minutes after the hour [0-59]
 
int getHour () const
 hours after midnight [0-23]
 
int getDay () const
 day of the month [1-31]
 
int getMonth () const
 month of the year [1-12]
 
int getYear () const
 year a.d.
 
int getDST () const
 daylight saving time
 
float getFS () const
 fraction of second
 
 operator std::string () const
 Type conversion operator.
 
std::string toString () const
 Get ASCII formatted date and time.
 
const JDateAndTimeoperator() (const bool utc=false)
 Set date and time.
 
void set (const bool utc=false)
 Set to current local time.
 
void set (const time_t t1, const bool utc=false)
 Set to given time.
 
void set (const time_t t1, const float f1, const bool utc=false)
 Set to given time.
 
void add (const time_t t1)
 Add given time.
 
void sub (const time_t t1)
 Subtract given time.
 
double getElapsedTime (const JDateAndTime &object) const
 Get elapsed time to given date and time.
 

Static Public Member Functions

static JDateAndTime min (const bool utc=false)
 Get minimum date and time.
 
static JDateAndTime max (const bool utc=false)
 Get maximum date and time.
 
static bool isISO8601 (const std::string &buffer)
 Function to check ISO-8601 conformity.
 

Private Member Functions

tm * get () const
 Get representation.
 

Private Attributes

time_t ts
 value
 
float fs = 0.0
 fraction of second
 
tm * tp
 representation
 
bool utc = false
 UTC.
 

Friends

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

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

Constructor & Destructor Documentation

◆ JDateAndTime() [1/5]

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

Constructor.

Parameters
utcUTC

Definition at line 87 of file JDateAndTime.hh.

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

◆ JDateAndTime() [2/5]

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

Constructor.

Parameters
t1time [s]
utcUTC

Definition at line 99 of file JDateAndTime.hh.

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

◆ JDateAndTime() [3/5]

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

113 {
114 set(t1, f1, utc);
115 }

◆ JDateAndTime() [4/5]

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

Constructor.

Parameters
bufferdate and time

Definition at line 123 of file JDateAndTime.hh.

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

◆ JDateAndTime() [5/5]

JSYSTEM::JDateAndTime::JDateAndTime ( const char *const buffer)
inline

Constructor.

Parameters
bufferdate and time

Definition at line 136 of file JDateAndTime.hh.

137 {
138 std::istringstream is(buffer);
139
140 is >> *this;
141 }

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 date and time; else false

Definition at line 150 of file JDateAndTime.hh.

151 {
152 return getElapsedTime(object) > 0.0;
153 }
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 161 of file JDateAndTime.hh.

162 {
163 return get();
164 }
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 172 of file JDateAndTime.hh.

173 {
174 return utc;
175 }

◆ getTime()

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

time

Definition at line 178 of file JDateAndTime.hh.

◆ getSeconds()

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

seconds after the minute [0-59]

Definition at line 180 of file JDateAndTime.hh.

◆ getMinutes()

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

minutes after the hour [0-59]

Definition at line 181 of file JDateAndTime.hh.

◆ getHour()

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

hours after midnight [0-23]

Definition at line 182 of file JDateAndTime.hh.

◆ getDay()

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

day of the month [1-31]

Definition at line 183 of file JDateAndTime.hh.

◆ getMonth()

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

month of the year [1-12]

Definition at line 184 of file JDateAndTime.hh.

◆ getYear()

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

year a.d.

Definition at line 185 of file JDateAndTime.hh.

◆ getDST()

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

daylight saving time

Definition at line 186 of file JDateAndTime.hh.

◆ getFS()

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

fraction of second

Definition at line 188 of file JDateAndTime.hh.

◆ min()

static JDateAndTime JSYSTEM::JDateAndTime::min ( const bool utc = false)
inlinestatic

Get minimum date and time.

Parameters
utcUTC
Returns
value

Definition at line 197 of file JDateAndTime.hh.

198 {
199 return JDateAndTime(0, utc);
200 }
JDateAndTime(const bool utc=false)
Constructor.

◆ max()

static JDateAndTime JSYSTEM::JDateAndTime::max ( const bool utc = false)
inlinestatic

Get maximum date and time.

Parameters
utcUTC
Returns
value

Definition at line 209 of file JDateAndTime.hh.

210 {
211 return JDateAndTime(std::numeric_limits<time_t>::max(), utc);
212 }

◆ operator std::string()

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

Type conversion operator.

Returns
ASCII formatted date and time

Definition at line 220 of file JDateAndTime.hh.

221 {
222 return this->toString();
223 }
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 232 of file JDateAndTime.hh.

233 {
234 using namespace std;
235
236 try {
237
238 JDateAndTime t1(buffer);
239
240 return true;
241 }
242 catch(const std::exception& error) {
243 return false;
244 }
245 }

◆ toString()

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

Get ASCII formatted date and time.

Returns
ASCII formatted date and time

Definition at line 253 of file JDateAndTime.hh.

254 {
255 using namespace std;
256
257 static const size_t MAX_SIZE = 256;
258 static char buffer[MAX_SIZE];
259
260 const size_t pos = strftime(buffer, MAX_SIZE, (isUTC() ? "%FT%TZ" : "%FT%T%z"), this->get());
261
262 return string(buffer, pos);
263 }
bool isUTC() const
Check if UTC time.
operator std::string() const
Type conversion operator.

◆ operator()()

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

Set date and time.

Parameters
utcUTC
Returns
date and time

Definition at line 272 of file JDateAndTime.hh.

273 {
274 set(utc);
275
276 return *this;
277 }

◆ set() [1/3]

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

Set to current local time.

Parameters
utcUTC

Definition at line 285 of file JDateAndTime.hh.

286 {
287 time_t tx;
288
289 time(&tx);
290
291 set(tx, utc);
292 }

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

302 {
303 this->ts = t1;
304 this->utc = utc;
305 }

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

316 {
317 this->ts = t1;
318 this->fs = f1;
319 this->utc = utc;
320 }
float fs
fraction of second

◆ add()

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

Add given time.

Parameters
t1time [s]

Definition at line 328 of file JDateAndTime.hh.

329 {
330 set(this->ts + t1, this->utc);
331 }

◆ sub()

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

Subtract given time.

Parameters
t1time [s]

Definition at line 339 of file JDateAndTime.hh.

340 {
341 set(this->ts - t1, this->utc);
342 }

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

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

◆ get()

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

Get representation.

Definition at line 492 of file JDateAndTime.hh.

493 {
494 return (this->tp = (utc ? gmtime(&ts) : localtime(&ts)));
495 }
tm * tp
representation

Friends And Related Symbol 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 364 of file JDateAndTime.hh.

365 {
366 using namespace std;
367
368 const size_t LENGTH = 10; // date may contain '-' and/or be followed by ' '
369
370 string buffer;
371
372 for (int c; (c = in.peek()) != EOF && isspace((char) in.peek()); ) {
373 in.ignore(1);
374 }
375
376 for (int c; (c = in.get()) != EOF && (c != ' ' || buffer.length() == LENGTH); ) {
377 buffer.push_back((char) c);
378 }
379
380 if (in.bad()) {
381 THROW(JIOException, "");
382 }
383
384 // separation of date and time from optional time zone
385
386 size_t pos = string::npos;
387
388 if (pos == string::npos) { pos = buffer.find('Z', LENGTH); } // UTC
389 if (pos == string::npos) { pos = buffer.find('+', LENGTH); } // optional time zone
390 if (pos == string::npos) { pos = buffer.find('-', LENGTH); } // optional time zone
391
392 tm tx;
393 object.fs = 0; // fraction of seconds
394 int nd = 0;
395
396 const string td = buffer.substr(0, pos);
397 const char* pd = NULL;
398
399 switch (td.length()) {
400
401 case 29:
402 case 28:
403 case 27:
404 case 26:
405 case 25:
406 case 24:
407 case 23:
408 case 22:
409 case 21:
410 pd = strptime(td.c_str(), "%Y-%m-%d %H:%M:%S", &tx); sscanf(pd, "%f%n", &object.fs, &nd); pd += nd;
411 break;
412
413 case 19:
414 pd = strptime(td.c_str(), "%Y-%m-%dT%H:%M:%S", &tx);
415 break;
416
417 case 15:
418 pd = strptime(td.c_str(), "%Y%m%dT%H%M%s", &tx);
419 break;
420
421 case 10:
422 pd = strptime(td.c_str(), "%Y-%m-%d", &tx);
423 break;
424 }
425
426 if (pd == NULL || *pd != '\0') {
427 THROW(JParseError, "invalid input <" << buffer << ">");
428 }
429
430 tm ty;
431
432 ty.tm_hour = 0;
433 ty.tm_min = 0;
434
435 if (pos != string::npos && (buffer[pos] == '+' ||
436 buffer[pos] == '-')) {
437
438 const string tz = buffer.substr(pos + 1);
439 const char* pz = tz.c_str();
440
441 switch (tz.length()) {
442
443 case 2:
444 pz = strptime(tz.c_str(), "%H", &ty);
445 break;
446
447 case 4:
448 pz = strptime(tz.c_str(), "%H%M", &ty);
449 break;
450
451 case 5:
452 pz = strptime(tz.c_str(), "%H:%M", &ty);
453 break;
454 }
455
456 if (pz == NULL || *pz != '\0') {
457 THROW(JParseError, "invalid input <" << buffer << ">");
458 }
459 }
460
461 tx.tm_isdst = 0; // switch off day light saving time
462
463 time_t t1 = mktime(&tx);
464
465 t1 -= (ty.tm_hour * 60 + ty.tm_min) * 60; // correct time zone
466 t1 += mktime(localtime(&t1)) - mktime(gmtime(&t1)); //
467
468 object.set(t1, buffer[pos] == 'Z');
469
470 return in;
471 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.

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

482 {
483 return out << object.toString();
484 }

Member Data Documentation

◆ ts

time_t JSYSTEM::JDateAndTime::ts
private

value

Definition at line 497 of file JDateAndTime.hh.

◆ fs

float JSYSTEM::JDateAndTime::fs = 0.0
private

fraction of second

Definition at line 498 of file JDateAndTime.hh.

◆ tp

tm* JSYSTEM::JDateAndTime::tp
mutableprivate

representation

Definition at line 499 of file JDateAndTime.hh.

◆ utc

bool JSYSTEM::JDateAndTime::utc = false
private

UTC.

Definition at line 500 of file JDateAndTime.hh.


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