Jpp in_tag_pdf_generation
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.
 
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/4]

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/4]

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

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

◆ JDateAndTime() [4/4]

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 }

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

138 {
139 return getElapsedTime(object) > 0.0;
140 }
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 148 of file JDateAndTime.hh.

149 {
150 return get();
151 }
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 159 of file JDateAndTime.hh.

160 {
161 return utc;
162 }

◆ getTime()

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

time

Definition at line 165 of file JDateAndTime.hh.

◆ getSeconds()

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

seconds after the minute [0-59]

Definition at line 167 of file JDateAndTime.hh.

◆ getMinutes()

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

minutes after the hour [0-59]

Definition at line 168 of file JDateAndTime.hh.

◆ getHour()

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

hours after midnight [0-23]

Definition at line 169 of file JDateAndTime.hh.

◆ getDay()

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

day of the month [1-31]

Definition at line 170 of file JDateAndTime.hh.

◆ getMonth()

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

month of the year [1-12]

Definition at line 171 of file JDateAndTime.hh.

◆ getYear()

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

year a.d.

Definition at line 172 of file JDateAndTime.hh.

◆ getDST()

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

daylight saving time

Definition at line 173 of file JDateAndTime.hh.

◆ getFS()

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

fraction of second

Definition at line 175 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 184 of file JDateAndTime.hh.

185 {
186 return JDateAndTime(0, utc);
187 }
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 196 of file JDateAndTime.hh.

197 {
198 return JDateAndTime(std::numeric_limits<time_t>::max(), utc);
199 }

◆ operator std::string()

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

Type conversion operator.

Returns
ASCII formatted date and time

Definition at line 207 of file JDateAndTime.hh.

208 {
209 return this->toString();
210 }
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 219 of file JDateAndTime.hh.

220 {
221 using namespace std;
222
223 try {
224
225 JDateAndTime t1(buffer);
226
227 return true;
228 }
229 catch(const std::exception& error) {
230 return false;
231 }
232 }

◆ toString()

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

Get ASCII formatted date and time.

Returns
ASCII formatted date and time

Definition at line 240 of file JDateAndTime.hh.

241 {
242 using namespace std;
243
244 static const size_t MAX_SIZE = 256;
245 static char buffer[MAX_SIZE];
246
247 const size_t pos = strftime(buffer, MAX_SIZE, (isUTC() ? "%FT%TZ" : "%FT%T%z"), this->get());
248
249 return string(buffer, pos);
250 }
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 259 of file JDateAndTime.hh.

260 {
261 set(utc);
262
263 return *this;
264 }

◆ set() [1/3]

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

Set to current local time.

Parameters
utcUTC

Definition at line 272 of file JDateAndTime.hh.

273 {
274 time_t tx;
275
276 time(&tx);
277
278 set(tx, utc);
279 }

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

289 {
290 this->ts = t1;
291 this->utc = utc;
292 }

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

303 {
304 this->ts = t1;
305 this->fs = f1;
306 this->utc = utc;
307 }
const JPolynome f1(1.0, 2.0, 3.0)
Function.
float fs
fraction of second

◆ add()

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

Add given time.

Parameters
t1time [s]

Definition at line 315 of file JDateAndTime.hh.

316 {
317 set(this->ts + t1, this->utc);
318 }

◆ sub()

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

Subtract given time.

Parameters
t1time [s]

Definition at line 326 of file JDateAndTime.hh.

327 {
328 set(this->ts - t1, this->utc);
329 }

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

339 {
340 return difftime(object.ts, this->ts) + (object.fs - this->fs);
341 }

◆ get()

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

Get representation.

Definition at line 479 of file JDateAndTime.hh.

480 {
481 return (this->tp = (utc ? gmtime(&ts) : localtime(&ts)));
482 }
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 351 of file JDateAndTime.hh.

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

469 {
470 return out << object.toString();
471 }

Member Data Documentation

◆ ts

time_t JSYSTEM::JDateAndTime::ts
private

value

Definition at line 484 of file JDateAndTime.hh.

◆ fs

float JSYSTEM::JDateAndTime::fs = 0.0
private

fraction of second

Definition at line 485 of file JDateAndTime.hh.

◆ tp

tm* JSYSTEM::JDateAndTime::tp
mutableprivate

representation

Definition at line 486 of file JDateAndTime.hh.

◆ utc

bool JSYSTEM::JDateAndTime::utc = false
private

UTC.

Definition at line 487 of file JDateAndTime.hh.


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