00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _FXTime_h_
00023 #define _FXTime_h_
00024
00025 #include "fxdefs.h"
00026 #include "FXString.h"
00027 #include <time.h>
00028
00029 namespace FX {
00030
00047 struct FXAPI FXTime
00048 {
00050 static const FXulong micsPerSecond=1000000;
00052 static const FXulong micsPerMinute=micsPerSecond*60;
00054 static const FXulong micsPerHour=micsPerMinute*60;
00056 static const FXulong micsPerDay=micsPerHour*24;
00058 static const FXulong micsPerYear=31556839024390LL;
00060 static const FXulong mics1stJan1601=50522499278048780LL;
00062 static const FXulong mics1stJan1970=62166972878048780LL;
00063
00065 bool isLocalTime;
00067 FXulong value;
00068
00070 explicit FXTime(FXulong _value=0, bool _isLocalTime=false) : isLocalTime(_isLocalTime), value(_value) { }
00072 explicit FXTime(time_t ctime) : isLocalTime(false), value(0) { set_time_t(ctime); }
00073 explicit FXTime(int ctime) : isLocalTime(false), value(0) { set_time_t((time_t) ctime); }
00074 FXTime &operator=(FXulong _value) { value=_value; return *this; }
00075 FXTime &operator=(time_t ctime) { set_time_t(ctime); return *this; }
00076 FXTime &operator=(int ctime) { return *this=(time_t) ctime; }
00077 bool operator!() const throw() { return !value; }
00078 bool operator==(const FXTime &o) const throw() { return isLocalTime==o.isLocalTime && value==o.value; }
00079 bool operator!=(const FXTime &o) const throw() { return isLocalTime!=o.isLocalTime || value!=o.value; }
00080 bool operator<(const FXTime &o) const throw() { return isLocalTime<o.isLocalTime || value<o.value; }
00081 bool operator>(const FXTime &o) const throw() { return isLocalTime>o.isLocalTime || value>o.value; }
00082
00083 friend FXTime operator+(const FXTime &a, const FXTime &b) throw() { return FXTime(a.value+b.value); }
00084 friend FXTime operator+(const FXTime &a, FXulong v) throw() { return FXTime(a.value+v); }
00085 friend FXTime operator-(const FXTime &a, const FXTime &b) throw() { return FXTime(a.value-b.value); }
00086 friend FXTime operator-(const FXTime &a, FXulong v) throw() { return FXTime(a.value-v); }
00087 FXTime &operator+=(const FXTime &b) throw() { value+=b.value; return *this; }
00088 FXTime &operator+=(FXulong v) throw() { value+=v; return *this; }
00089 FXTime &operator-=(const FXTime &b) throw() { value-=b.value; return *this; }
00090 FXTime &operator-=(FXulong v) throw() { value-=v; return *this; }
00091
00093 time_t as_time_t() const throw()
00094 {
00095 return (time_t)(value ? (value-mics1stJan1970)/micsPerSecond : 0);
00096 }
00098 FXTime &set_time_t(time_t v) throw()
00099 {
00100 value=v ? mics1stJan1970+v*micsPerSecond : 0;
00101 return *this;
00102 }
00104 struct tm *as_tm(struct tm *buf, bool inLocalTime=false) const;
00106 FXTime &set_tm(struct tm *buf, bool isInLocalTime=false);
00107
00112 FXString asString(const FXString &fmt="%Y/%m/%d %H:%M:%S.%F %Z") const;
00114 FXTime &toLocalTime() { if(!isLocalTime) { if(value) value+=localTimeDiff(); isLocalTime=true; } return *this; }
00116 FXTime &toUTC() { if(isLocalTime) { if(value) value-=localTimeDiff(); isLocalTime=false; } return *this; }
00117
00119 static FXTime now(bool inLocalTime=false);
00121 static FXlong localTimeDiff();
00122
00123 friend FXAPI FXStream &operator<<(FXStream &s, const FXTime &i);
00124 friend FXAPI FXStream &operator>>(FXStream &s, FXTime &i);
00125 };
00126 #define FXTIMETOFILETIME(filetime, time) { FXulong temp=((time).value-FXTime::mics1stJan1601)*10; (filetime).dwHighDateTime=(DWORD)(temp>>32); (filetime).dwLowDateTime=(DWORD) temp; }
00127 #define FXTIMEFROMFILETIME(time, filetime) { (time).value=(((FXulong) (filetime).dwHighDateTime<<32)|(FXulong) (filetime).dwLowDateTime)/10; (time).value+=FXTime::mics1stJan1601; }
00128
00129 }
00130
00131 #endif
00132
00133