Scopira  20080306
time.h
1 
2 /*
3  * Copyright (c) 2004 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED__SCOPIRA_TOOL_TIME_H__
15 #define __INCLUDED__SCOPIRA_TOOL_TIME_H__
16 
17 #include <scopira/tool/platform.h>
18 
19 #include <string>
20 #include <time.h>
21 #include <assert.h>
22 
23 #ifdef PLATFORM_UNIX
24 #include <sys/time.h>
25 #endif
26 
27 #include <scopira/tool/platform.h>
28 #include <scopira/tool/export.h>
29 
30 namespace scopira
31 {
32  namespace tool
33  {
34  class timespan;
35  class timestamp;
36  class chrono;
37  }
38 }
39 
47 {
48  friend class timestamp;
49  public:
51  timespan(void) : dm_time(-1) {}
53  timespan(time_t t) : dm_time(t) {}
55  SCOPIRA_EXPORT timespan(int days, int hours, int minutes, int seconds);
56 
58  bool is_null(void) const { return dm_time == -1; }
59 
61  int get_days(void) const { return static_cast<int>(dm_time / (24*60*60)); }
62 
64  int get_hours(void) const { return get_total_hours() % 24; }
66  int get_total_hours(void) const { return static_cast<int>(dm_time / (60*60)); }
67 
69  int get_minutes(void) const { return get_total_minutes() % 60; }
71  int get_total_minutes(void) const { return static_cast<int>(dm_time / 60); }
72 
74  int get_seconds(void) const { return static_cast<int>(dm_time % 60); }
76  int get_total_seconds(void) const { return static_cast<int>(dm_time); }
77 
79  bool operator< (timespan rhs) const { return dm_time < rhs.dm_time; }
80  bool operator<=(timespan rhs) const { return dm_time <= rhs.dm_time; }
81  bool operator> (timespan rhs) const { return dm_time > rhs.dm_time; }
82  bool operator>=(timespan rhs) const { return dm_time >= rhs.dm_time; }
83  bool operator==(timespan rhs) const { return dm_time == rhs.dm_time; }
84  bool operator!=(timespan rhs) const { return dm_time != rhs.dm_time; }
85 
87  timespan operator+ (timespan rhs) const { return dm_time + rhs.dm_time; }
88  timespan& operator+=(timespan rhs) { dm_time += rhs.dm_time; return *this; }
89  timespan operator- (timespan rhs) const { return dm_time - rhs.dm_time; }
90  timespan& operator-=(timespan rhs) { dm_time -= rhs.dm_time; return *this; }
91 
93  timespan& operator= (timespan rhs) { dm_time = rhs.dm_time; return *this; }
94 
95  private:
96  time_t dm_time;
97 };
98 
105 {
106  public:
108  timestamp(void) : dm_time(-1) {}
109  timestamp(time_t t) : dm_time(t) {}
110 
112  bool is_null(void) const { return dm_time == -1; }
113 
128  SCOPIRA_EXPORT timestamp(int year, int month, int day, int hour, int minute, int second, int dst = -1);
129 
138  SCOPIRA_EXPORT void format(const std::string& format, std::string& out, bool gmt=false) const;
139 
141  SCOPIRA_EXPORT static timestamp current_time(void);
142 
144  int get_day(void) const { return get_tm()->tm_mday; }
146  int get_day_of_week(void) const { return get_tm()->tm_wday; }
148  int get_hour(void) const { return get_tm()->tm_hour; }
150  int get_minute(void) const { return get_tm()->tm_min; }
152  int get_month(void) const { return get_tm()->tm_mon; }
154  int get_second(void) const { return get_tm()->tm_sec; }
156  int get_year(void) const { return get_tm()->tm_year; }
157 
159  time_t get_time(void) const { return dm_time; }
160 
161 #ifdef PLATFORM_win32
162 // disable depreacted warnings
163 #pragma warning(push)
164 #pragma warning(disable:4996)
165 #endif
166 
174  tm* get_tm(void) const { return localtime(&dm_time); }
175 #ifdef PLATFORM_win32
176 // disable depreacted warnings
177 #pragma warning(pop)
178 #endif
179 
181  bool operator< (timestamp rhs) const { return dm_time < rhs.dm_time; }
182  bool operator<=(timestamp rhs) const { return dm_time <= rhs.dm_time; }
183  bool operator> (timestamp rhs) const { return dm_time > rhs.dm_time; }
184  bool operator>=(timestamp rhs) const { return dm_time >= rhs.dm_time; }
185  bool operator==(timestamp rhs) const { return dm_time == rhs.dm_time; }
186  bool operator!=(timestamp rhs) const { return dm_time != rhs.dm_time; }
187 
190  timestamp operator+ (timespan rhs) const { return dm_time + rhs.dm_time; }
191  timestamp operator+=(timespan rhs) { dm_time += rhs.dm_time; return *this; }
192  timestamp operator- (timespan rhs) const { return dm_time - rhs.dm_time; }
193  timestamp operator-=(timespan rhs) { dm_time -= rhs.dm_time; return *this; }
194 
196  timestamp& operator=(timestamp rhs) { dm_time = rhs.dm_time; return *this; }
197  timestamp& operator=(time_t rhs) { dm_time = rhs; return *this; }
198 
200  timespan operator-(timestamp rhs) const { return dm_time - rhs.dm_time; }
201 
203  static int string_to_month(const std::string& month);
204 
205  private:
206  void assign(int year, int month, int mday, int hour, int minute, int second, int isdst = -1, bool isgmt = false);
207 
208  time_t dm_time;
209 };
210 
242 {
243  public:
254  SCOPIRA_EXPORT chrono(void);
255 
261  SCOPIRA_EXPORT void reset(void);
262 
268  SCOPIRA_EXPORT void start(void);
269 
279  SCOPIRA_EXPORT void stop(void);
280 
290  SCOPIRA_EXPORT double get_total_time(void) const;
299  SCOPIRA_EXPORT double get_lap_time(void) const;
309  SCOPIRA_EXPORT double get_running_time(void) const;
310 
311  private:
312  struct timeval dm_total, dm_delta; //current total, and current start-delta
313 };
314 
315 #endif
316 
timespan & operator=(timespan rhs)
assignment operator
Definition: time.h:93
timespan(time_t t)
initing ctor
Definition: time.h:53
tm * get_tm(void) const
Definition: time.h:174
int get_total_hours(void) const
gets the total number of hours in the timespan
Definition: time.h:66
Definition: archiveflow.h:20
timespan operator+(timespan rhs) const
addition and subtraction operators
Definition: time.h:87
bool is_null(void) const
checks for null time
Definition: time.h:112
int get_second(void) const
gets the second of the time value (0-59)
Definition: time.h:154
bool is_null(void) const
checks for null time
Definition: time.h:58
timestamp(void)
constructors
Definition: time.h:108
int get_hours(void) const
gets the hours unit of the timespan
Definition: time.h:64
int get_days(void) const
gets the days unit of the timespan
Definition: time.h:61
timespan operator-(timestamp rhs) const
subtraction operator to retreive the timespan difference of two times
Definition: time.h:200
int get_total_minutes(void) const
gets the total number of minutes in the timespan
Definition: time.h:71
bool operator<(timespan rhs) const
comparison operators
Definition: time.h:79
timestamp & operator=(timestamp rhs)
assignment operators
Definition: time.h:196
int get_month(void) const
gets the month of the time value (0-11, 0=January)
Definition: time.h:152
timespan(void)
constructor - inits to null time
Definition: time.h:51
int get_minute(void) const
gets the minute of the time value (0-59)
Definition: time.h:150
int get_year(void) const
gets the year of the time value (current year minus 1900)
Definition: time.h:156
int get_hour(void) const
gets the hour of the local time value (0-23, 0=Sunday)
Definition: time.h:148
int get_total_seconds(void) const
gets the total number of seconds in the timespan
Definition: time.h:76
time_t get_time(void) const
gets the current time value (seconds since January 1, 1970)
Definition: time.h:159
int get_minutes(void) const
gets the miutes unit of the timespan
Definition: time.h:69
Definition: time.h:46
Definition: time.h:104
int get_seconds(void) const
gets the seconds unit of the timespan
Definition: time.h:74
Definition: time.h:241
int get_day_of_week(void) const
gets the day of the week of the time value (0-6, 0=Sunday)
Definition: time.h:146
int get_day(void) const
gets the day of the time value (1-31)
Definition: time.h:144