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);
138 std::istringstream is(buffer);
220 operator std::string()
const
242 catch(
const std::exception& error) {
257 static const size_t MAX_SIZE = 256;
258 static char buffer[MAX_SIZE];
260 const size_t pos = strftime(buffer, MAX_SIZE, (
isUTC() ?
"%FT%TZ" :
"%FT%T%z"), this->
get());
262 return string(buffer, pos);
301 void set(
const time_t t1,
const bool utc =
false)
315 void set(
const time_t t1,
const float f1,
const bool utc =
false)
353 return difftime(
object.
ts, this->ts) + (
object.fs - this->
fs);
368 const size_t LENGTH = 10;
372 for (
int c; (c = in.peek()) != EOF && isspace((
char) in.peek()); ) {
376 for (
int c; (c = in.get()) != EOF && (c !=
' ' || buffer.length() == LENGTH); ) {
377 buffer.push_back((
char) c);
386 size_t pos = string::npos;
388 if (pos == string::npos) { pos = buffer.find(
'Z', LENGTH); }
389 if (pos == string::npos) { pos = buffer.find(
'+', LENGTH); }
390 if (pos == string::npos) { pos = buffer.find(
'-', LENGTH); }
396 const string td = buffer.substr(0, pos);
397 const char* pd = NULL;
399 switch (td.length()) {
410 pd = strptime(td.c_str(),
"%Y-%m-%d %H:%M:%S", &tx); sscanf(pd,
"%f%n", &
object.
fs, &nd); pd += nd;
414 pd = strptime(td.c_str(),
"%Y-%m-%dT%H:%M:%S", &tx);
418 pd = strptime(td.c_str(),
"%Y%m%dT%H%M%s", &tx);
422 pd = strptime(td.c_str(),
"%Y-%m-%d", &tx);
426 if (pd == NULL || *pd !=
'\0') {
435 if (pos != string::npos && (buffer[pos] ==
'+' ||
436 buffer[pos] ==
'-')) {
438 const string tz = buffer.substr(pos + 1);
439 const char* pz = tz.c_str();
441 switch (tz.length()) {
444 pz = strptime(tz.c_str(),
"%H", &ty);
448 pz = strptime(tz.c_str(),
"%H%M", &ty);
452 pz = strptime(tz.c_str(),
"%H:%M", &ty);
456 if (pz == NULL || *pz !=
'\0') {
463 time_t t1 = mktime(&tx);
465 t1 -= (ty.tm_hour * 60 + ty.tm_min) * 60;
466 t1 += mktime(localtime(&t1)) - mktime(gmtime(&t1));
468 object.set(t1, buffer[pos] ==
'Z');
483 return out <<
object.toString();
494 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]
JDateAndTime(const char *const buffer)
Constructor.
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.