1#ifndef __JSYSTEM__JDATEANDTIME__
2#define __JSYSTEM__JDATEANDTIME__
40 static const size_t MAX_SIZE = 256;
41 static char buffer[MAX_SIZE];
45 strftime(buffer, MAX_SIZE,
"%F", localtime(&ts));
59 static const size_t MAX_SIZE = 256;
60 static char buffer[MAX_SIZE];
64 strftime(buffer, MAX_SIZE,
"%T%z", localtime(&ts));
125 std::istringstream is(buffer);
207 operator std::string()
const
229 catch(
const std::exception& error) {
244 static const size_t MAX_SIZE = 256;
245 static char buffer[MAX_SIZE];
247 const size_t pos = strftime(buffer, MAX_SIZE, (
isUTC() ?
"%FT%TZ" :
"%FT%T%z"), this->
get());
249 return string(buffer, pos);
288 void set(
const time_t t1,
const bool utc =
false)
302 void set(
const time_t t1,
const float f1,
const bool utc =
false)
340 return difftime(
object.
ts, this->ts) + (
object.fs - this->
fs);
355 const size_t LENGTH = 10;
359 for (
int c; (c = in.peek()) != EOF && isspace((
char) in.peek()); ) {
363 for (
int c; (c = in.get()) != EOF && (c !=
' ' || buffer.length() == LENGTH); ) {
364 buffer.push_back((
char) c);
373 size_t pos = string::npos;
375 if (pos == string::npos) { pos = buffer.find(
'Z', LENGTH); }
376 if (pos == string::npos) { pos = buffer.find(
'+', LENGTH); }
377 if (pos == string::npos) { pos = buffer.find(
'-', LENGTH); }
383 const string td = buffer.substr(0, pos);
384 const char* pd = NULL;
386 switch (td.length()) {
397 pd = strptime(td.c_str(),
"%Y-%m-%d %H:%M:%S", &tx); sscanf(pd,
"%f%n", &
object.
fs, &nd); pd += nd;
401 pd = strptime(td.c_str(),
"%Y-%m-%dT%H:%M:%S", &tx);
405 pd = strptime(td.c_str(),
"%Y%m%dT%H%M%s", &tx);
409 pd = strptime(td.c_str(),
"%Y-%m-%d", &tx);
413 if (pd == NULL || *pd !=
'\0') {
422 if (pos != string::npos && (buffer[pos] ==
'+' ||
423 buffer[pos] ==
'-')) {
425 const string tz = buffer.substr(pos + 1);
426 const char* pz = tz.c_str();
428 switch (tz.length()) {
431 pz = strptime(tz.c_str(),
"%H", &ty);
435 pz = strptime(tz.c_str(),
"%H%M", &ty);
439 pz = strptime(tz.c_str(),
"%H:%M", &ty);
443 if (pz == NULL || *pz !=
'\0') {
450 time_t t1 = mktime(&tx);
452 t1 -= (ty.tm_hour * 60 + ty.tm_min) * 60;
453 t1 += mktime(localtime(&t1)) - mktime(gmtime(&t1));
455 object.set(t1, buffer[pos] ==
'Z');
470 return out <<
object.toString();
481 return (this->
tp = (
utc ? gmtime(&
ts) : localtime(&
ts)));
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for parsing value.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.
const char * getTime()
Get current local time conform ISO-8601 standard.
const char * getDate()
Get current local date conform ISO-8601 standard.
static JDateAndTime getDateAndTime
Function object to get current date and time.
Template definition of auxiliary base class for comparison of data structures.
Auxiliary class for date and time.
JDateAndTime(const bool utc=false)
Constructor.
bool isUTC() const
Check if UTC time.
friend std::ostream & operator<<(std::ostream &out, const JDateAndTime &object)
Write date and time to output stream.
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time to given date and time.
JDateAndTime(const std::string &buffer)
Constructor.
std::string toString() const
Get ASCII formatted date and time.
JDateAndTime(const time_t t1, const float f1, const bool utc=false)
Constructor.
int getMinutes() const
minutes after the hour [0-59]
int getYear() const
year a.d.
time_t getTime() const
time
int getHour() const
hours after midnight [0-23]
int getDST() const
daylight saving time
void set(const bool utc=false)
Set to current local time.
bool less(const JDateAndTime &object) const
Less than method.
int getDay() const
day of the month [1-31]
float getFS() const
fraction of second
const tm * operator->() const
Smart pointer.
int getMonth() const
month of the year [1-12]
JDateAndTime(const time_t t1, const bool utc=false)
Constructor.
static bool isISO8601(const std::string &buffer)
Function to check ISO-8601 conformity.
int getSeconds() const
seconds after the minute [0-59]
void set(const time_t t1, const bool utc=false)
Set to given time.
tm * get() const
Get representation.
friend std::istream & operator>>(std::istream &in, JDateAndTime &object)
Read date and time from input stream.
void sub(const time_t t1)
Subtract given time.
void add(const time_t t1)
Add given time.
static JDateAndTime min(const bool utc=false)
Get minimum date and time.
float fs
fraction of second
const JDateAndTime & operator()(const bool utc=false)
Set date and time.
operator std::string() const
Type conversion operator.
void set(const time_t t1, const float f1, const bool utc=false)
Set to given time.
static JDateAndTime max(const bool utc=false)
Get maximum date and time.