Commit 5bd33a75 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

reintegrated feature branch: new classes to conveniently specify times

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 4577
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 29ac950d
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
* 02/02/2004 V1.4 moved conversion code double2time() * 02/02/2004 V1.4 moved conversion code double2time()
* 17/12/2007 V1.5 replace long int by typedef timeint * 17/12/2007 V1.5 replace long int by typedef timeint
* 11/11/2009 V1.6 header cstdlib is required for abort * 11/11/2009 V1.6 header cstdlib is required for abort
* 20/02/2012 V1.7 use const char*
* *
* ============================================================================ * ============================================================================
*/ */
...@@ -45,21 +46,21 @@ ...@@ -45,21 +46,21 @@
namespace libtime { namespace libtime {
void TBaseClassTime::char_read(char *timestring) void TBaseClassTime::string_read(const std::string &timestring)
{ {
if (time_kernel::time_read(&Mtime_Ts, timestring)!=EXIT_SUCCESS) if (time_kernel::time_read(&Mtime_Ts,
timestring.c_str())
!=EXIT_SUCCESS)
{ {
std::cerr << "TBaseClassTime could not initialize time structure " std::cerr << "TBaseClassTime could not initialize time structure "
<< "from string:\n" << std::string(timestring) << "\n"; << "from string:\n" << timestring << "\n";
std::abort(); std::abort();
} }
} }
void TBaseClassTime::string_read(const std::string &timestring) void TBaseClassTime::char_read(const char *timestring)
{ {
char charstring[TIME_SLEN+2]; string_read(timestring);
std::strncpy(charstring, timestring.c_str(), TIME_SLEN+1);
char_read(charstring);
} }
}; // namespace libtime }; // namespace libtime
......
...@@ -36,10 +36,10 @@ ...@@ -36,10 +36,10 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
int time_read(time_Ts *Date, char *String) int time_read(time_Ts *Date, const char *String)
{ {
double sec; double sec;
char *ptr; const char *ptr;
char cmilmicsec[7]; char cmilmicsec[7];
integer day, month, milmicsec; integer day, month, milmicsec;
int i; int i;
......
...@@ -46,6 +46,9 @@ ...@@ -46,6 +46,9 @@
* 17/12/2007 V1.12 replace long int by typedef timeint * 17/12/2007 V1.12 replace long int by typedef timeint
* 25/11/2008 V1.13 added function utc() * 25/11/2008 V1.13 added function utc()
* 12/12/2008 V1.14 added hierarchical time strings * 12/12/2008 V1.14 added hierarchical time strings
* 20/02/2012 V1.15
* - implement new classes for shorter time intervals
* - use const char*
* *
* ============================================================================ * ============================================================================
*/ */
...@@ -64,8 +67,8 @@ namespace libtime { ...@@ -64,8 +67,8 @@ namespace libtime {
/*! \mainpage /*! \mainpage
* *
* \author Thomas Forbriger * \author Thomas Forbriger
* \date $Date: 2008/12/12 09:25:46 $ * \date $Date$
* \since 1997, 2000 * \since 1997, 2012
* \version $Id$ * \version $Id$
* *
* This library supports numerical operations for dates and times with integer * This library supports numerical operations for dates and times with integer
...@@ -125,14 +128,14 @@ class TBaseClassTime { ...@@ -125,14 +128,14 @@ class TBaseClassTime {
// make constructors and member protected // make constructors and member protected
protected: protected:
TBaseClassTime(const std::string &Itimestring); TBaseClassTime(const std::string &Itimestring);
TBaseClassTime(char *Itimestring); TBaseClassTime(const char *Itimestring);
TBaseClassTime(const time_kernel::time_Ts &Itime_Ts); TBaseClassTime(const time_kernel::time_Ts &Itime_Ts);
TBaseClassTime(const timeint &year, const timeint &doy, TBaseClassTime(const timeint &year, const timeint &doy,
const timeint &hour=0, const timeint &hour=0,
const timeint &minute=0, const timeint &second=0, const timeint &minute=0, const timeint &second=0,
const timeint &milsec=0, const timeint &micsec=0); const timeint &milsec=0, const timeint &micsec=0);
void string_read(const std::string &timestring); void string_read(const std::string &timestring);
void char_read(char *timestring); void char_read(const char *timestring);
// well, there is no need to make norm accessible to public, as we will // well, there is no need to make norm accessible to public, as we will
// always norm the structure ourselfs // always norm the structure ourselfs
void norm(); void norm();
...@@ -150,7 +153,7 @@ class TAbsoluteTime: public TBaseClassTime { ...@@ -150,7 +153,7 @@ class TAbsoluteTime: public TBaseClassTime {
friend class TRelativeTime; friend class TRelativeTime;
public: public:
TAbsoluteTime(const std::string &Itimestring); TAbsoluteTime(const std::string &Itimestring);
TAbsoluteTime(char *Itimestring); TAbsoluteTime(const char *Itimestring);
TAbsoluteTime(const time_kernel::time_Ts &Itime_Ts); TAbsoluteTime(const time_kernel::time_Ts &Itime_Ts);
TAbsoluteTime(const timeint &year=2000, const timeint &month=1, TAbsoluteTime(const timeint &year=2000, const timeint &month=1,
const timeint &day=1, const timeint &hour=0, const timeint &day=1, const timeint &hour=0,
...@@ -184,7 +187,7 @@ class TAbsoluteTime: public TBaseClassTime { ...@@ -184,7 +187,7 @@ class TAbsoluteTime: public TBaseClassTime {
TAbsoluteTime &operator= (const time_kernel::time_Ts &A); TAbsoluteTime &operator= (const time_kernel::time_Ts &A);
TAbsoluteTime &operator= (const std::string &timestring); TAbsoluteTime &operator= (const std::string &timestring);
TAbsoluteTime &operator= (char *timestring); TAbsoluteTime &operator= (const char *timestring);
}; };
/*! \brief class to contain relative times /*! \brief class to contain relative times
...@@ -199,7 +202,7 @@ class TRelativeTime: public TBaseClassTime { ...@@ -199,7 +202,7 @@ class TRelativeTime: public TBaseClassTime {
friend class TAbsoluteTime; friend class TAbsoluteTime;
public: public:
TRelativeTime(const std::string &Itimestring); TRelativeTime(const std::string &Itimestring);
TRelativeTime(char *Itimestring); TRelativeTime(const char *Itimestring);
TRelativeTime(const time_kernel::time_Ts &Itime_Ts); TRelativeTime(const time_kernel::time_Ts &Itime_Ts);
TRelativeTime(const int &days=0, const int &hour=0, TRelativeTime(const int &days=0, const int &hour=0,
const int &minute=0, const int &second=0, const int &minute=0, const int &second=0,
...@@ -240,7 +243,7 @@ class TRelativeTime: public TBaseClassTime { ...@@ -240,7 +243,7 @@ class TRelativeTime: public TBaseClassTime {
TRelativeTime &operator= (const time_kernel::time_Ts &A); TRelativeTime &operator= (const time_kernel::time_Ts &A);
TRelativeTime &operator= (const std::string &timestring); TRelativeTime &operator= (const std::string &timestring);
TRelativeTime &operator= (char *timestring); TRelativeTime &operator= (const char *timestring);
}; };
TRelativeTime operator* (const time_kernel::timeint &n, const TRelativeTime &A); TRelativeTime operator* (const time_kernel::timeint &n, const TRelativeTime &A);
...@@ -258,7 +261,7 @@ TRelativeTime operator* (const time_kernel::timeint &n, const TRelativeTime &A); ...@@ -258,7 +261,7 @@ TRelativeTime operator* (const time_kernel::timeint &n, const TRelativeTime &A);
*/ */
inline TBaseClassTime::TBaseClassTime(const std::string &Itimestring) inline TBaseClassTime::TBaseClassTime(const std::string &Itimestring)
{ string_read(Itimestring); } { string_read(Itimestring); }
inline TBaseClassTime::TBaseClassTime(char *Itimestring) inline TBaseClassTime::TBaseClassTime(const char *Itimestring)
{ char_read(Itimestring); } { char_read(Itimestring); }
inline std::string TBaseClassTime::timestring() const inline std::string TBaseClassTime::timestring() const
...@@ -302,7 +305,7 @@ inline TBaseClassTime::operator std::string() const { return(timestring()); } ...@@ -302,7 +305,7 @@ inline TBaseClassTime::operator std::string() const { return(timestring()); }
*/ */
inline TAbsoluteTime::TAbsoluteTime(const std::string &Itimestring): inline TAbsoluteTime::TAbsoluteTime(const std::string &Itimestring):
TBaseClassTime(Itimestring) { time_kernel::time_finish(&Mtime_Ts); } TBaseClassTime(Itimestring) { time_kernel::time_finish(&Mtime_Ts); }
inline TAbsoluteTime::TAbsoluteTime(char *Itimestring): inline TAbsoluteTime::TAbsoluteTime(const char *Itimestring):
TBaseClassTime(Itimestring) { time_kernel::time_finish(&Mtime_Ts); } TBaseClassTime(Itimestring) { time_kernel::time_finish(&Mtime_Ts); }
inline TAbsoluteTime::TAbsoluteTime(const time_kernel::time_Ts &Itime_Ts): inline TAbsoluteTime::TAbsoluteTime(const time_kernel::time_Ts &Itime_Ts):
...@@ -379,7 +382,7 @@ inline TAbsoluteTime &TAbsoluteTime::operator= (const time_kernel::time_Ts &A) ...@@ -379,7 +382,7 @@ inline TAbsoluteTime &TAbsoluteTime::operator= (const time_kernel::time_Ts &A)
{ Mtime_Ts=A; time_kernel::time_finish(&Mtime_Ts); return(*this); } { Mtime_Ts=A; time_kernel::time_finish(&Mtime_Ts); return(*this); }
inline TAbsoluteTime &TAbsoluteTime::operator= (const std::string &timestring) inline TAbsoluteTime &TAbsoluteTime::operator= (const std::string &timestring)
{ string_read(timestring); return(*this); } { string_read(timestring); return(*this); }
inline TAbsoluteTime &TAbsoluteTime::operator= (char *timestring) inline TAbsoluteTime &TAbsoluteTime::operator= (const char *timestring)
{ char_read(timestring); return(*this); } { char_read(timestring); return(*this); }
/* /*
...@@ -388,7 +391,7 @@ inline TAbsoluteTime &TAbsoluteTime::operator= (char *timestring) ...@@ -388,7 +391,7 @@ inline TAbsoluteTime &TAbsoluteTime::operator= (char *timestring)
*/ */
inline TRelativeTime::TRelativeTime(const string &Itimestring): inline TRelativeTime::TRelativeTime(const string &Itimestring):
TBaseClassTime("0/0/" + Itimestring) { } TBaseClassTime("0/0/" + Itimestring) { }
inline TRelativeTime::TRelativeTime(char *Itimestring): inline TRelativeTime::TRelativeTime(const char *Itimestring):
TBaseClassTime("0/0/" + std::string(Itimestring)) { } TBaseClassTime("0/0/" + std::string(Itimestring)) { }
inline TRelativeTime::TRelativeTime(const time_kernel::time_Ts &Itime_Ts): inline TRelativeTime::TRelativeTime(const time_kernel::time_Ts &Itime_Ts):
...@@ -503,9 +506,9 @@ inline TRelativeTime &TRelativeTime::operator= (const time_kernel::time_Ts &A) ...@@ -503,9 +506,9 @@ inline TRelativeTime &TRelativeTime::operator= (const time_kernel::time_Ts &A)
{ Mtime_Ts=A; Mtime_Ts.year=0; norm(); return(*this); } { Mtime_Ts=A; Mtime_Ts.year=0; norm(); return(*this); }
inline TRelativeTime &TRelativeTime::operator= (const std::string &timestring) inline TRelativeTime &TRelativeTime::operator= (const std::string &timestring)
{ string_read("0/0/" + timestring); return(*this); } { string_read("0/0/" + timestring); return(*this); }
inline TRelativeTime &TRelativeTime::operator= (char *timestring) inline TRelativeTime &TRelativeTime::operator= (const char *timestring)
{ string_read("0/0/" + std::string(timestring)); return(*this); } { string_read("0/0/" + std::string(timestring)); return(*this); }
/*======================================================================*/ /*======================================================================*/
/*S*/ /*S*/
...@@ -522,6 +525,83 @@ double time2double(const TRelativeTime&); ...@@ -522,6 +525,83 @@ double time2double(const TRelativeTime&);
//! convert seconds to relative time //! convert seconds to relative time
TRelativeTime double2time(const double& seconds); TRelativeTime double2time(const double& seconds);
/*E*/ /*E*/
/*======================================================================*/
/*S*/
/*! \brief provide a convenient way to specify time intervals in the order of
* one day
*/
class Days: public TRelativeTime {
public:
Days(const timeint &day=1, const timeint &hour=0,
const timeint &minute=0, const timeint &second=0,
const timeint &milsec=0, const timeint &micsec=0)
: TRelativeTime(day, hour, minute, second, milsec, micsec) { }
Days(const double &days)
: TRelativeTime(double2time(60.*60.*24.*days)) { }
}; // class Days
/*! \brief provide a convenient way to specify time intervals in the order of
* one hour
*/
class Hours: public TRelativeTime {
public:
Hours(const timeint &hour=1,
const timeint &minute=0, const timeint &second=0,
const timeint &milsec=0, const timeint &micsec=0)
: TRelativeTime(0, hour, minute, second, milsec, micsec) { }
Hours(const double &hours)
: TRelativeTime(double2time(60.*60.*hours)) { }
}; // class Hours
/*! \brief provide a convenient way to specify time intervals in the order of
* one minute
*/
class Minutes: public TRelativeTime {
public:
Minutes(const timeint &minute=1, const timeint &second=0,
const timeint &milsec=0, const timeint &micsec=0)
: TRelativeTime(0, 0, minute, second, milsec, micsec) { }
Minutes(const double &minutes)
: TRelativeTime(double2time(60.*minutes)) { }
}; // class Minutes
/*! \brief provide a convenient way to specify time intervals in the order of
* one second
*/
class Seconds: public TRelativeTime {
public:
Seconds(const timeint &second=1,
const timeint &milsec=0, const timeint &micsec=0)
: TRelativeTime(0, 0, 0, second, milsec, micsec) { }
Seconds(const double &seconds)
: TRelativeTime(double2time(seconds)) { }
}; // class Seconds
/*! \brief provide a convenient way to specify time intervals in the order of
* one millisecond
*/
class Milliseconds: public TRelativeTime {
public:
Milliseconds(const timeint &milsec=1, const timeint &micsec=0)
: TRelativeTime(0, 0, 0, 0, milsec, micsec) { }
Milliseconds(const double &milliseconds)
: TRelativeTime(double2time(1.e-3*milliseconds)) { }
}; // class Milliseconds
/*! \brief provide a convenient way to specify time intervals in the order of
* one microsecond
*/
class Microseconds: public TRelativeTime {
public:
Microseconds(const timeint &micsec=1)
: TRelativeTime(0, 0, 0, 0, 0, micsec) { }
Microseconds(const double &microseconds)
: TRelativeTime(double2time(1.e-6*microseconds)) { }
}; // class Microseconds
/*E*/
/*======================================================================*/ /*======================================================================*/
/*S*/ /*S*/
......
...@@ -272,7 +272,7 @@ void time_sub(time_Ts, time_Ts, time_Ts *); ...@@ -272,7 +272,7 @@ void time_sub(time_Ts, time_Ts, time_Ts *);
* ============================== * ==============================
*/ */
/* int time_read(time_Ts *date, char *string) /* int time_read(time_Ts *date, const char *string)
* *
* string: input: character representation of a time with the fields in the * string: input: character representation of a time with the fields in the
* following order: * following order:
...@@ -288,7 +288,7 @@ void time_sub(time_Ts, time_Ts, time_Ts *); ...@@ -288,7 +288,7 @@ void time_sub(time_Ts, time_Ts, time_Ts *);
* *
* NOTICE: time_read is not the direct inverse operation of time_sprint * NOTICE: time_read is not the direct inverse operation of time_sprint
*/ */
int time_read(time_Ts *, char *); int time_read(time_Ts *, const char *);
/* char *time_sprint(time_Ts date) /* char *time_sprint(time_Ts date)
* *
......
...@@ -35,6 +35,11 @@ ...@@ -35,6 +35,11 @@
using std::cout; using std::cout;
using std::endl; using std::endl;
using namespace libtime;
#define PRINTVALUE( V ) \
cout << #V << ":\n " << string(V) << endl;
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
void example1() void example1()
...@@ -254,7 +259,7 @@ void example6() ...@@ -254,7 +259,7 @@ void example6()
libtime::TAbsoluteTime exampletime; libtime::TAbsoluteTime exampletime;
cout << "\n" cout << "\n"
<< "Example 5: How about year value abbreviation?\n" << "Example 6: How about year value abbreviation?\n"
<< "----------\n" << "----------\n"
<< "\n"; << "\n";
...@@ -280,6 +285,27 @@ void example6() ...@@ -280,6 +285,27 @@ void example6()
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
void example7()
{
cout << "\n"
<< "Example 7: Convenient ways to specify times\n"
<< "----------\n"
<< "\n";
PRINTVALUE( Hours(6) );
PRINTVALUE( 118*Days()+4*Hours()+10*Minutes() );
PRINTVALUE( Minutes(34.123456) );
PRINTVALUE( Minutes(34,123,456) );
PRINTVALUE( Seconds(34.123456) );
PRINTVALUE( Seconds(34,123,456) );
PRINTVALUE( Seconds(90072) );
PRINTVALUE( 2.5*Days() );
PRINTVALUE( Days(2.5) );
PRINTVALUE( Seconds(2.5*time2double(Days())) );
}
/*----------------------------------------------------------------------*/
int main() int main()
{ {
cout << "Example++ ($Id$)\n\n" cout << "Example++ ($Id$)\n\n"
...@@ -298,6 +324,7 @@ int main() ...@@ -298,6 +324,7 @@ int main()
example4(); example4();
example5(); example5();
example6(); example6();
example7();
cout << "\n" cout << "\n"
<< "That's it... any questions?\n"; << "That's it... any questions?\n";
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <timeutil.h> #include <timeutil.h>
#define TU_EXIT_FAILURE 1 #define TU_EXIT_FAILURE 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment