FXTime.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                         P r o c e s s   S u p p o r t                         *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2005 by Niall Douglas.   All Rights Reserved.                   *
00007 *       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL      *
00008 *********************************************************************************
00009 * This code is free software; you can redistribute it and/or modify it under    *
00010 * the terms of the GNU Library General Public License v2.1 as published by the  *
00011 * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not  *
00012 * "upgrade" this code to the GPL without my prior written permission.           *
00013 * Please consult the file "License_Addendum2.txt" accompanying this file.       *
00014 *                                                                               *
00015 * This code is distributed in the hope that it will be useful,                  *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
00018 *********************************************************************************
00019 * $Id:                                                                          *
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     // NOTE: Defined in FXProcess.cxx
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 } // namespace
00130 
00131 #endif
00132 
00133 

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:24 2009 for TnFOX by doxygen v1.4.7