cel_time.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00004 //
00005 // (C) 2003-2006 Celartem Technology Inc. All rights reserved.
00006 //----------------------------------------------------------------------------
00007 
00008 #ifndef _cel_time_h_
00009 #define _cel_time_h_
00010 
00011 #include "cel_types.h"
00012 #include "cel_memory.h"
00013 #include "cel_string.h"
00014 #include "cel_exception.h"
00015 #include "cel_datatraits.h"
00016 #include <ctime>
00017 
00018 #if CEL_ENV_WINDOWS
00019 struct _FILETIME;
00020 struct _SYSTEMTIME;
00021 #endif
00022 
00023 namespace Celartem
00024 {
00025     class Stream;
00026 
00031     struct Time
00032     {
00037         std::time_t t;
00038 
00045         Time(std::time_t _t = 0) : t(_t) {}
00046 
00052         Time(const Time& _time) : t(_time.t) {}
00053 
00060         Time(const String& time) {parseDateString(time);}
00061 
00068         Time(const std::tm *_tm) {*this = mktime(_tm);}
00069 
00089         Time(
00090             size_t year, size_t month, size_t day,
00091             size_t hour = 0, size_t minute = 0, size_t seconds = 0,
00092             bool isUTC = false)
00093         {
00094             *this = createTime(year, month, day, hour, minute, seconds, isUTC);
00095         }
00096 
00104         Time& operator=(std::time_t _t)
00105         {
00106             t = _t;
00107             return *this;
00108         }
00109 
00117         Time& operator=(const Time& _time)
00118         {
00119             t = _time.t;
00120             return *this;
00121         }
00122 
00131         Time& operator=(const std::tm *_tm)
00132         {
00133             *this = mktime(_tm);
00134             return *this;
00135         }
00136 
00147         Time& operator=(const String& time)
00148         {
00149             parseDateString(time);
00150             return *this;
00151         }
00152 
00158         void gmtime(std::tm *out_tm) const;
00159 
00165         void localtime(std::tm *out_tm) const;
00166 
00175         String strftime(const String& format) const;
00176 
00183         bool operator<(const Time& r) const {return (t < r.t);}
00184 
00191         bool operator<=(const Time& r) const {return (t <= r.t);}
00192 
00199         bool operator>(const Time& r) const {return (t > r.t);}
00200 
00207         bool operator>=(const Time& r) const {return (t >= r.t);}
00208 
00215         bool operator==(const Time& r) const {return (t == r.t);}
00216 
00223         bool operator!=(const Time& r) const {return (t != r.t);}
00224 
00231         ssize_t operator-(const Time& r) const;
00232 
00239         Time operator+(ssize_t diff) const {return Time(t + diff);}
00240 
00247         String ctime() const;
00248 
00257         String getRFC822timeString(bool isLocalTime = true) const;
00258 
00264         static Time getCurrentTime();
00265 
00273         static Time mktime(const std::tm *_tm);
00274 
00282         static Time timelocal(const std::tm *_tm) {return mktime(_tm);}
00283 
00291         static Time timegm(const std::tm *_tm);
00292 
00312         static Time createTime(
00313             size_t year, size_t month, size_t day,
00314             size_t hour = 0, size_t minute = 0, size_t seconds = 0,
00315             bool isUTC = false);
00316         
00325         void parseDateString(const String& inDateString);
00326 
00333         void serialize(
00334             Stream *inStream, size_t inLevel, Endian inEndian) const;
00335 
00342         void deserialize(Stream *inStream, size_t inLevel, Endian inEndian);
00343         
00350         String toGMTStr() const;
00351         
00356         static Time fromGMTStr(const String& inGMTStr);
00357 
00358 #if CEL_ENV_WINDOWS
00359 
00366         Time(const _FILETIME& inFileTime);
00367 
00375         Time(const _SYSTEMTIME& inSystemTime);
00376         
00384         Time& operator=(const _FILETIME& inFileTime);
00385         
00393         Time& operator=(const _SYSTEMTIME& inSystemTime);
00394         
00401         operator _FILETIME() const;
00402         
00411         operator _SYSTEMTIME() const;
00412 #endif
00413 
00414 
00415     };
00416 
00420     struct TimeSpan
00421     {
00422         Time from; //<! The time something starts from.
00423         Time to;   //<! The time something continues to.
00424 
00431         ssize_t span() const {return from - to;}
00432 
00442         bool intersectsWith(const Time& t) const
00443         {
00444             if(from <= t && t < to)
00445                 return true;
00446             return false;
00447         }
00448 
00458         bool intersectsWith(const TimeSpan& ts) const
00459         {
00460             if(from <= ts.from && ts.from < to)
00461                 return true;
00462             if(from <= ts.to && ts.to < to)
00463                 return true;
00464             return false;
00465         }
00466 
00473         void serialize(
00474             Stream *inStream, size_t inLevel, Endian inEndian) const;
00475 
00482         void deserialize(
00483             Stream *inStream, size_t inLevel, Endian inEndian);
00484     };
00485 
00486     CEL_DEFINE_DATATRAITS(Time, byConstructor, byClassMethods);
00487     CEL_DEFINE_DATATRAITS(TimeSpan, byConstructor, byClassMethods);
00488     
00492     struct TimeUtils
00493     {
00503         static u64 getTick();
00504 
00511         static u64 getTicksPerSecond();
00512     };
00513 
00517     class DebugTimer
00518     {
00519     public:
00526         DebugTimer(const String& label)
00527         {
00528             startMilestone(label);
00529         }
00530         
00536         void milestone(const String& label)
00537         {
00538             finishMilestone();
00539             startMilestone(label);
00540         }
00541         
00546         ~DebugTimer()
00547         {
00548             finishMilestone();
00549         }
00550     
00551     private:
00552         String m_label;
00553         u64 m_tick;
00554         
00555         void startMilestone(const String& label);
00556         void finishMilestone();
00557     };
00558     
00559 }  // namespace Celartem
00560 
00561 #endif // _cel_time_h_

This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:21:55 2008.