Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Time.h
Go to the documentation of this file.
1 #ifndef Protium_Time_h_
2 #define Protium_Time_h_
3 
4 #include <ctime>
5 #include <string>
6 #include <iomanip>
7 
8 namespace Protium{
9  namespace Time{
10 
15  protected:
17  time_t fTime;
18  public:
19  TimePrimitive(const time_t& t) : fTime(t) {}
22  fTime = other.fTime;
23  }
24  virtual ~ TimePrimitive(){}
25  inline time_t GetTime() const {return fTime;}
26  inline void SetTime(const time_t& t) {fTime=t;}
27  static TimePrimitive Now(){
28  return TimePrimitive( std::time(NULL) );
29  }
31  this->fTime-=other.fTime;
32  return *this;
33  }
34 
35  };
36  class TimeDate : public TimePrimitive{
37  tm fTM;
38  public:
39  TimeDate( const int& sec=0,
40  const int& min=0,
41  const int& hour =0,
42  const int& day=0,
43  const int& month=0,
44  const int& year=0,
45  const int& weekday=0,
46  const bool& dst=false ) : TimePrimitive( std::time(NULL) ) {
47  fTM.tm_sec = sec;
48  fTM.tm_min = min;
49  fTM.tm_hour = hour;
50  fTM.tm_mday = day;
51  fTM.tm_mon = month;
52  fTM.tm_year = year;
53  fTM.tm_wday = weekday;
54  fTM.tm_isdst = dst? 1 : 0;
55  fTime = mktime(&fTM);
56  }
57  TimeDate(const TimeDate& other) : TimePrimitive(std::time(NULL) ) {
58  this->fTM = other.fTM;
59  this->fTime = mktime(&fTM);
60  }
61 
62  TimeDate(const TimePrimitive& other) : TimePrimitive(other){
63  time_t t = other.GetTime();
64  this->fTM = *std::localtime( &t );
65  }
66 
67  inline time_t GetTimeLocal() const {return fTime ;}
68  inline time_t GetTimeGM() const {return mktime(std::gmtime(&fTime)) ;}
69 
70  std::string AsFormat(const std::string& frmt = "%d/%m/%Y %H:%M:%S"){
71  char buffer[80];
72 
73  const char* format = frmt.c_str();
74  strftime(buffer, 80, format, &fTM);
75 
76  return std::string(buffer);
77  }
78 
79  static TimeDate FromFormat(const std::string& t, const std::string& fmt="%d/%m/%Y %H:%M:%S"){
80  //put something
81  TimeDate tm;
82  const char* time_as_string = t.c_str();
83  const char* format = fmt.c_str();
84  strptime(time_as_string, format, &tm.fTM);
85  tm.fTime = mktime(&tm.fTM);
86  return tm;
87  }
88 
89  };
90  }
91 }
92 
93 
94 #endif //File Guardian
time_t GetTimeLocal() const
Definition: Time.h:67
TimePrimitive(const time_t &t)
Definition: Time.h:19
time_t GetTimeGM() const
Definition: Time.h:68
void SetTime(const time_t &t)
Definition: Time.h:26
time_t fTime
Data.
Definition: Time.h:17
time_t GetTime() const
Definition: Time.h:25
static TimePrimitive Now()
Definition: Time.h:27
TimePrimitive & operator-=(const TimePrimitive &other)
Definition: Time.h:30
Sequence< int, IntType< 0 >, IntType< 0 >, IntType< 1 >, IntType< 0 >, IntType< 0 >, IntType< 0 >, IntType< 0 > >::Type time
Definition: Dimensions.h:42
TimeDate(const TimePrimitive &other)
Definition: Time.h:62
TimePrimitive(const TimePrimitive &t)
Definition: Time.h:20
TimeDate(const int &sec=0, const int &min=0, const int &hour=0, const int &day=0, const int &month=0, const int &year=0, const int &weekday=0, const bool &dst=false)
Definition: Time.h:39
double Time
Definition: Quantities.h:85
std::string AsFormat(const std::string &frmt="%d/%m/%Y %H:%M:%S")
Definition: Time.h:70
TimeDate(const TimeDate &other)
Definition: Time.h:57
virtual ~TimePrimitive()
Definition: Time.h:24
TimePrimitive & operator=(const TimePrimitive &other)
Definition: Time.h:21
static TimeDate FromFormat(const std::string &t, const std::string &fmt="%d/%m/%Y %H:%M:%S")
Definition: Time.h:79