Scopira 20080306

time.h

00001 
00002 /*
00003  *  Copyright (c) 2004    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED__SCOPIRA_TOOL_TIME_H__
00015 #define __INCLUDED__SCOPIRA_TOOL_TIME_H__
00016 
00017 #include <scopira/tool/platform.h>
00018 
00019 #include <string>
00020 #include <time.h>
00021 #include <assert.h>
00022 
00023 #ifdef PLATFORM_UNIX
00024 #include <sys/time.h>
00025 #endif
00026 
00027 #include <scopira/tool/platform.h>
00028 #include <scopira/tool/export.h>
00029 
00030 namespace scopira
00031 {
00032   namespace tool
00033   {
00034     class timespan;
00035     class timestamp;
00036     class chrono;
00037   }
00038 }
00039 
00046 class scopira::tool::timespan
00047 {
00048   friend class timestamp;
00049   public:
00051     timespan(void) : dm_time(-1) {}
00053     timespan(time_t t) : dm_time(t) {}
00055     SCOPIRA_EXPORT timespan(int days, int hours, int minutes, int seconds);
00056 
00058     bool is_null(void) const { return dm_time == -1; }
00059 
00061     int get_days(void) const          { return static_cast<int>(dm_time / (24*60*60)); }
00062 
00064     int get_hours(void) const         { return get_total_hours() % 24; }
00066     int get_total_hours(void) const   { return static_cast<int>(dm_time / (60*60)); }
00067 
00069     int get_minutes(void) const       { return get_total_minutes() % 60; }
00071     int get_total_minutes(void) const { return static_cast<int>(dm_time / 60); }
00072 
00074     int get_seconds(void) const       { return static_cast<int>(dm_time % 60); }
00076     int get_total_seconds(void) const { return static_cast<int>(dm_time); }
00077 
00079     bool operator< (timespan rhs) const { return dm_time <  rhs.dm_time; }
00080     bool operator<=(timespan rhs) const { return dm_time <= rhs.dm_time; }
00081     bool operator> (timespan rhs) const { return dm_time >  rhs.dm_time; }
00082     bool operator>=(timespan rhs) const { return dm_time >= rhs.dm_time; }
00083     bool operator==(timespan rhs) const { return dm_time == rhs.dm_time; }
00084     bool operator!=(timespan rhs) const { return dm_time != rhs.dm_time; }
00085 
00087     timespan  operator+ (timespan rhs) const { return dm_time +  rhs.dm_time; }
00088     timespan& operator+=(timespan rhs)       { dm_time += rhs.dm_time; return *this; }
00089     timespan  operator- (timespan rhs) const { return dm_time -  rhs.dm_time; }
00090     timespan& operator-=(timespan rhs)       { dm_time -= rhs.dm_time; return *this; }
00091 
00093     timespan& operator= (timespan rhs)       { dm_time = rhs.dm_time; return *this; }
00094 
00095   private:
00096     time_t dm_time;  
00097 };
00098 
00104 class scopira::tool::timestamp
00105 {
00106   public:
00108     timestamp(void) : dm_time(-1) {}
00109     timestamp(time_t t) : dm_time(t) {}
00110 
00112     bool is_null(void) const { return dm_time == -1; }
00113 
00128     SCOPIRA_EXPORT timestamp(int year, int month, int day, int hour, int minute, int second, int dst = -1);
00129 
00138     SCOPIRA_EXPORT void format(const std::string& format, std::string& out, bool gmt=false) const;
00139 
00141     SCOPIRA_EXPORT static timestamp current_time(void);
00142 
00144     int get_day(void) const { return get_tm()->tm_mday; }
00146     int get_day_of_week(void) const { return get_tm()->tm_wday; }
00148     int get_hour(void) const { return get_tm()->tm_hour; }
00150     int get_minute(void) const { return get_tm()->tm_min; }
00152     int get_month(void) const { return get_tm()->tm_mon; }
00154     int get_second(void) const { return get_tm()->tm_sec; }
00156     int get_year(void) const { return get_tm()->tm_year; }
00157 
00159     time_t get_time(void) const { return dm_time; }
00160 
00161 #ifdef PLATFORM_win32
00162 // disable depreacted warnings
00163 #pragma warning(push)
00164 #pragma warning(disable:4996)
00165 #endif
00166 
00174     tm* get_tm(void) const { return localtime(&dm_time); }
00175 #ifdef PLATFORM_win32
00176 // disable depreacted warnings
00177 #pragma warning(pop)
00178 #endif
00179 
00181     bool operator< (timestamp rhs) const { return dm_time <  rhs.dm_time; }
00182     bool operator<=(timestamp rhs) const { return dm_time <= rhs.dm_time; }
00183     bool operator> (timestamp rhs) const { return dm_time >  rhs.dm_time; }
00184     bool operator>=(timestamp rhs) const { return dm_time >= rhs.dm_time; }
00185     bool operator==(timestamp rhs) const { return dm_time == rhs.dm_time; }
00186     bool operator!=(timestamp rhs) const { return dm_time != rhs.dm_time; }
00187 
00190     timestamp operator+ (timespan rhs) const { return dm_time + rhs.dm_time; }
00191     timestamp operator+=(timespan rhs)       { dm_time += rhs.dm_time; return *this; }
00192     timestamp operator- (timespan rhs) const { return dm_time - rhs.dm_time; }
00193     timestamp operator-=(timespan rhs)       { dm_time -= rhs.dm_time; return *this; }
00194 
00196     timestamp& operator=(timestamp rhs)   { dm_time = rhs.dm_time; return *this; }
00197     timestamp& operator=(time_t rhs) { dm_time = rhs; return *this; }
00198 
00200     timespan operator-(timestamp rhs) const { return dm_time - rhs.dm_time; }
00201 
00203     static int string_to_month(const std::string& month);
00204 
00205   private:
00206     void assign(int year, int month, int mday, int hour, int minute, int second, int isdst = -1, bool isgmt = false);
00207 
00208     time_t dm_time;   
00209 };
00210 
00241 class scopira::tool::chrono
00242 {
00243   public:
00254     SCOPIRA_EXPORT chrono(void);
00255 
00261     SCOPIRA_EXPORT void reset(void);
00262 
00268     SCOPIRA_EXPORT void start(void);
00269 
00279     SCOPIRA_EXPORT void stop(void);
00280 
00290     SCOPIRA_EXPORT double get_total_time(void) const;
00299     SCOPIRA_EXPORT double get_lap_time(void) const;
00309     SCOPIRA_EXPORT double get_running_time(void) const;
00310 
00311   private:
00312     struct timeval dm_total, dm_delta; //current total, and current start-delta
00313 };
00314 
00315 #endif
00316