メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

oct-time.h

解説を見る。
00001 /*
00002 
00003 Copyright (C) 1999 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 2, or (at your option) any
00010 later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, write to the Free
00019 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 */
00022 
00023 #if !defined (octave_time_h)
00024 #define octave_time_h 1
00025 
00026 #include <cmath>
00027 
00028 #include <string>
00029 
00030 #include "systime.h"
00031 
00032 class octave_base_tm;
00033 
00034 class
00035 octave_time
00036 {
00037 public:
00038 
00039   octave_time (void)
00040     : ot_unix_time (0), ot_usec (0) { stamp (); }
00041 
00042   octave_time (time_t t)
00043     : ot_unix_time (t), ot_usec (0) { }
00044 
00045   octave_time (double d)
00046     : ot_unix_time (static_cast<time_t> (d)), ot_usec (0)
00047   {
00048     double ip;
00049     ot_usec = static_cast<int> (std::modf (d, &ip) * 1e6);
00050   }
00051 
00052   octave_time (const octave_base_tm& tm);
00053 
00054   octave_time (const octave_time& ot)
00055     : ot_unix_time (ot.ot_unix_time), ot_usec (ot.ot_usec) { }
00056 
00057   octave_time& operator = (const octave_time& ot)
00058   {
00059     if (this != &ot)
00060       {
00061         ot_unix_time = ot.ot_unix_time;
00062         ot_usec = ot.ot_usec;
00063       }
00064 
00065     return *this;
00066   }
00067 
00068   ~octave_time (void) { }
00069 
00070   void stamp (void);
00071 
00072   operator double () const { return ot_unix_time + ot_usec / 1e6; }
00073 
00074   operator time_t () const { return ot_unix_time; }
00075 
00076   time_t unix_time (void) const { return ot_unix_time; }
00077 
00078   int usec (void) const { return ot_usec; }
00079 
00080   std::string ctime (void) const;
00081 
00082 private:
00083 
00084   // Seconds since the epoch.
00085   time_t ot_unix_time;
00086 
00087   // Additional microseconds.
00088   int ot_usec;
00089 };
00090 
00091 inline bool
00092 operator == (const octave_time& t1, const octave_time& t2)
00093 {
00094   return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
00095 }
00096 
00097 inline bool
00098 operator != (const octave_time& t1, const octave_time& t2)
00099 {
00100   return ! (t1 == t2);
00101 }
00102 
00103 inline bool
00104 operator < (const octave_time& t1, const octave_time& t2)
00105 {
00106   if (t1.unix_time () < t2.unix_time ())
00107     return true;
00108   else if (t1.unix_time () > t2.unix_time ())
00109     return false;
00110   else if (t1.usec () < t2.usec ())
00111     return true;
00112   else
00113     return false;
00114 }
00115 
00116 inline bool
00117 operator <= (const octave_time& t1, const octave_time& t2)
00118 {
00119   return (t1 < t2 || t1 == t2);
00120 }
00121 
00122 inline bool
00123 operator > (const octave_time& t1, const octave_time& t2)
00124 {
00125   if (t1.unix_time () > t2.unix_time ())
00126     return true;
00127   else if (t1.unix_time () < t2.unix_time ())
00128     return false;
00129   else if (t1.usec () > t2.usec ())
00130     return true;
00131   else
00132     return false;
00133 }
00134 
00135 inline bool
00136 operator >= (const octave_time& t1, const octave_time& t2)
00137 {
00138   return (t1 > t2 || t1 == t2);
00139 }
00140 
00141 class
00142 octave_base_tm
00143 {
00144 public:
00145 
00146   octave_base_tm (void)
00147     : tm_usec (0), tm_sec (0), tm_min (0), tm_hour (0),
00148       tm_mday (0), tm_mon (0), tm_year (0), tm_wday (0),
00149       tm_yday (0), tm_isdst (0), tm_zone ("unknown")
00150   { }
00151 
00152   octave_base_tm (const octave_base_tm& tm)
00153     : tm_usec (tm.tm_usec), tm_sec (tm.tm_sec), tm_min (tm.tm_min),
00154       tm_hour (tm.tm_hour), tm_mday (tm.tm_mday), tm_mon (tm.tm_mon),
00155       tm_year (tm.tm_year), tm_wday (tm.tm_wday), tm_yday (tm.tm_yday),
00156       tm_isdst (tm.tm_isdst), tm_zone (tm.tm_zone)
00157   { }
00158 
00159   octave_base_tm& operator = (const octave_base_tm& tm)
00160   {
00161     if (this != &tm)
00162       {
00163         tm_usec = tm.tm_usec;
00164         tm_sec = tm.tm_sec;
00165         tm_min = tm.tm_min;
00166         tm_hour = tm.tm_hour;
00167         tm_mday = tm.tm_mday;
00168         tm_mon = tm.tm_mon;
00169         tm_year = tm.tm_year;
00170         tm_wday = tm.tm_wday;
00171         tm_yday = tm.tm_yday;
00172         tm_isdst = tm.tm_isdst;
00173         tm_zone = tm.tm_zone;
00174       }
00175 
00176     return *this;
00177   }
00178 
00179   virtual ~octave_base_tm (void) { }
00180 
00181   int usec (void) const { return tm_usec; }
00182   int sec (void) const { return tm_sec; }
00183   int min (void) const { return tm_min; }
00184   int hour (void) const { return tm_hour; }
00185   int mday (void) const { return tm_mday; }
00186   int mon (void) const { return tm_mon; }
00187   int year (void) const { return tm_year; }
00188   int wday (void) const { return tm_wday; }
00189   int yday (void) const { return tm_yday; }
00190   int isdst (void) const { return tm_isdst; }
00191   std::string zone (void) const { return tm_zone; }
00192 
00193   octave_base_tm& usec (int v);
00194   octave_base_tm& sec (int v);
00195   octave_base_tm& min (int v);
00196   octave_base_tm& hour (int v);
00197   octave_base_tm& mday (int v);
00198   octave_base_tm& mon (int v);
00199   octave_base_tm& year (int v);
00200   octave_base_tm& wday (int v);
00201   octave_base_tm& yday (int v);
00202   octave_base_tm& isdst (int v);
00203   octave_base_tm& zone (const std::string& s);
00204 
00205   std::string strftime (const std::string& fmt) const;
00206 
00207   std::string asctime (void) const
00208     { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
00209 
00210 protected:
00211 
00212   // Microseconds after the second (0, 999999).
00213   int tm_usec;
00214 
00215   // Seconds after the minute (0, 61).
00216   int tm_sec;
00217 
00218   // Minutes after the hour (0, 59).
00219   int tm_min;
00220 
00221   // Hours since midnight (0, 23).
00222   int tm_hour;
00223 
00224   // Day of the month (1, 31).
00225   int tm_mday;
00226 
00227   // Months since January (0, 11).
00228   int tm_mon;
00229 
00230   // Years since 1900.
00231   int tm_year;
00232 
00233   // Days since Sunday (0, 6).
00234   int tm_wday;
00235 
00236   // Days since January 1 (0, 365).
00237   int tm_yday;
00238 
00239   // Daylight Savings Time flag.
00240   int tm_isdst;
00241 
00242   // Time zone.
00243   std::string tm_zone;
00244 
00245   void init (void *p);
00246 };
00247 
00248 class
00249 octave_localtime : public octave_base_tm
00250 {
00251 public:
00252 
00253   octave_localtime (void)
00254     : octave_base_tm () { init (octave_time ()); }
00255 
00256   octave_localtime (const octave_time& ot)
00257     : octave_base_tm () { init (ot); }
00258 
00259   octave_localtime (const octave_localtime& t)
00260     : octave_base_tm (t) { }
00261 
00262   octave_localtime& operator = (const octave_localtime& t)
00263   {
00264     octave_base_tm::operator = (t);
00265     return *this;
00266   }
00267 
00268   ~octave_localtime (void) { }
00269 
00270 private:
00271 
00272   void init (const octave_time& ot);
00273 };
00274 
00275 class
00276 octave_gmtime : public octave_base_tm
00277 {
00278 public:
00279 
00280   octave_gmtime (void)
00281     : octave_base_tm () { init (octave_time ()); }
00282 
00283   octave_gmtime (const octave_time& ot)
00284     : octave_base_tm () { init (ot); }
00285 
00286   octave_gmtime& operator = (const octave_gmtime& t)
00287   {
00288     octave_base_tm::operator = (t);
00289     return *this;
00290   }
00291 
00292   ~octave_gmtime (void) { }
00293 
00294 private:
00295 
00296   void init (const octave_time& ot);
00297 };
00298 
00299 class
00300 octave_strptime : public octave_base_tm
00301 {
00302 public:
00303 
00304   octave_strptime (const std::string& str, const std::string& fmt)
00305     : octave_base_tm () { init (str, fmt); }
00306 
00307   octave_strptime (const octave_strptime& s)
00308     : octave_base_tm (s) { nchars = s.nchars; }
00309 
00310   octave_strptime& operator = (const octave_strptime& s)
00311   {
00312     octave_base_tm::operator = (s);
00313     nchars = s.nchars;
00314     return *this;
00315   }
00316 
00317   int characters_converted (void) const { return nchars; }
00318 
00319   ~octave_strptime (void) { }
00320 
00321 private:
00322 
00323   int nchars;
00324 
00325   void init (const std::string& str, const std::string& fmt);
00326 };
00327 
00328 #endif
00329 
00330 /*
00331 ;;; Local Variables: ***
00332 ;;; mode: C++ ***
00333 ;;; End: ***
00334 */

Wed Dec 29 11:52:13 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送