Jpp  15.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
JSYSTEM::JDateAndTime Struct Reference

Auxililary class to get date and time. More...

#include <JDate.hh>

Public Member Functions

 JDateAndTime ()
 Default constructor. More...
 
 JDateAndTime (const time_t t1)
 Constructor. More...
 
tm * operator-> ()
 Smart pointer. More...
 
const tm * operator-> () const
 Smart pointer. 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...
 
 operator std::string () const
 Type conversion operator. More...
 
std::string toString (const JDateAndTimeFormat option=ISO8601) const
 Get ASCII formatted date and time. More...
 
const JDateAndTimeoperator() () const
 Set date and time. More...
 
void set () const
 Set to actual time. More...
 
void set (const time_t t1) const
 Set to given time. More...
 
double getElapsedTime (const JDateAndTime &object) const
 Get elapsed time since given date and time. More...
 

Static Public Member Functions

static bool isISO8601 (const std::string &datestr)
 Function to check ISO-8601 conformity of string-formatted date and time. More...
 

Private Attributes

time_t ts
 
tm * tp
 

Friends

std::ostream & operator<< (std::ostream &out, const JDateAndTime &object)
 Write date and time to output. More...
 

Detailed Description

Auxililary class to get date and time.

Definition at line 96 of file JSystem/JDate.hh.

Constructor & Destructor Documentation

JSYSTEM::JDateAndTime::JDateAndTime ( )
inline

Default constructor.

Definition at line 100 of file JSystem/JDate.hh.

101  {
102  set();
103  }
void set() const
Set to actual time.
JSYSTEM::JDateAndTime::JDateAndTime ( const time_t  t1)
inline

Constructor.

Parameters
t1time

Definition at line 111 of file JSystem/JDate.hh.

112  {
113  set(t1);
114  }
void set() const
Set to actual time.

Member Function Documentation

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

Smart pointer.

Returns
pointer to time structure

Definition at line 122 of file JSystem/JDate.hh.

123  {
124  return tp;
125  }
const tm* JSYSTEM::JDateAndTime::operator-> ( ) const
inline

Smart pointer.

Returns
pointer to time structure

Definition at line 133 of file JSystem/JDate.hh.

134  {
135  return tp;
136  }
int JSYSTEM::JDateAndTime::getSeconds ( ) const
inline

seconds after the minute [0-59]

Definition at line 138 of file JSystem/JDate.hh.

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

minutes after the hour [0-59]

Definition at line 139 of file JSystem/JDate.hh.

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

hours after midnight [0-23]

Definition at line 140 of file JSystem/JDate.hh.

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

day of the month [1-31]

Definition at line 141 of file JSystem/JDate.hh.

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

month of the year [1-12]

Definition at line 142 of file JSystem/JDate.hh.

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

year a.d.

Definition at line 143 of file JSystem/JDate.hh.

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

Type conversion operator.

Returns
ASCII formatted date and time

Definition at line 151 of file JSystem/JDate.hh.

152  {
153  return toString();
154  }
std::string toString(const JDateAndTimeFormat option=ISO8601) const
Get ASCII formatted date and time.
static bool JSYSTEM::JDateAndTime::isISO8601 ( const std::string &  datestr)
inlinestatic

Function to check ISO-8601 conformity of string-formatted date and time.

Parameters
datestrstring-formatted date and time
Returns
true if date and time are ISO-8601-conform; else false

Definition at line 163 of file JSystem/JDate.hh.

164  {
165  using namespace std;
166 
167  const size_t pos = ( datestr.find('Z') != string::npos ? datestr.find('Z') :
168  (datestr.find('+') != string::npos ? datestr.find('+') : datestr.find('-')) );
169 
170  if (pos != string::npos) {
171 
172  static tm t;
173 
174  const string td = datestr.substr(0, pos);
175  const string tz = datestr.substr(pos+1);
176 
177  const char* p0 = strptime(td.c_str(), "%FT%T", &t);
178  const char* p1 = ( tz.size() < 3 ? strptime(tz.c_str(), "%H", &t) :
179  (tz.size() < 5 ? strptime(tz.c_str(), "%H%M", &t) : strptime(tz.c_str(), "%H:%M", &t)) );
180 
181  return ( (p0 != NULL && string(p0).empty()) && ((p1 != NULL && string(p1).empty()) || tz == "Z") );
182  }
183 
184  return false;
185  }
TPaveText * p1
std::string JSYSTEM::JDateAndTime::toString ( const JDateAndTimeFormat  option = ISO8601) const
inline

Get ASCII formatted date and time.

Parameters
optionformatting option
Returns
ASCII formatted date and time

Definition at line 194 of file JSystem/JDate.hh.

195  {
196  using namespace std;
197 
198  mktime(tp);
199 
200  static const int MAX_SIZE = 256;
201  static char buffer[MAX_SIZE];
202 
203  switch (option) {
204 
205  case ISO8601:
206  strftime(buffer, MAX_SIZE, "%FT%T%z", tp);
207  break;
208 
209  case HUMAN_READABLE:
210  strftime(buffer, MAX_SIZE, "%a %b %d %X %Z %Y", tp);
211  break;
212 
213  default:
214  THROW(JLANG::JValueOutOfRange, "JDateAndTime::toString(): Invalid formatting option.");
215  }
216 
217  // remove the last character (carriage return) from the date string
218 
219  buffer[MAX_SIZE-1] = '\0';
220 
221  return string(buffer);
222  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Human readable format (Www Mmm dd hh:mm:ss yyyy)
ISO-8601 standard.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
const JDateAndTime& JSYSTEM::JDateAndTime::operator() ( ) const
inline

Set date and time.

Returns
date and time

Definition at line 230 of file JSystem/JDate.hh.

231  {
232  set();
233 
234  return *this;
235  }
void set() const
Set to actual time.
void JSYSTEM::JDateAndTime::set ( ) const
inline

Set to actual time.

Definition at line 241 of file JSystem/JDate.hh.

242  {
243  time(&ts);
244 
245  tp = localtime(&ts);
246  }
void JSYSTEM::JDateAndTime::set ( const time_t  t1) const
inline

Set to given time.

Parameters
t1time

Definition at line 254 of file JSystem/JDate.hh.

255  {
256  ts = t1;
257  tp = localtime(&t1);
258  }
double JSYSTEM::JDateAndTime::getElapsedTime ( const JDateAndTime object) const
inline

Get elapsed time since given date and time.

Parameters
objectdate and time
Returns
time [s]

Definition at line 267 of file JSystem/JDate.hh.

268  {
269  return difftime(this->ts, object.ts);
270  }

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const JDateAndTime object 
)
friend

Write date and time to output.

Parameters
outoutput stream
objectdate and time
Returns
output stream

Definition at line 280 of file JSystem/JDate.hh.

281  {
282  return out << object.toString();
283  }

Member Data Documentation

time_t JSYSTEM::JDateAndTime::ts
mutableprivate

Definition at line 287 of file JSystem/JDate.hh.

tm* JSYSTEM::JDateAndTime::tp
mutableprivate

Definition at line 288 of file JSystem/JDate.hh.


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