00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_time_h)
00024 #define octave_time_h 1
00025
00026 #include <string>
00027
00028 #include "lo-math.h"
00029 #include "systime.h"
00030
00031 class octave_base_tm;
00032
00033 class
00034 OCTAVE_API
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 double double_value (void) const { return ot_unix_time + ot_usec / 1e6; }
00073
00074 time_t unix_time (void) const { return ot_unix_time; }
00075
00076 int usec (void) const { return ot_usec; }
00077
00078 std::string ctime (void) const;
00079
00080 private:
00081
00082
00083 time_t ot_unix_time;
00084
00085
00086 int ot_usec;
00087 };
00088
00089 inline bool
00090 operator == (const octave_time& t1, const octave_time& t2)
00091 {
00092 return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
00093 }
00094
00095 inline bool
00096 operator != (const octave_time& t1, const octave_time& t2)
00097 {
00098 return ! (t1 == t2);
00099 }
00100
00101 inline bool
00102 operator < (const octave_time& t1, const octave_time& t2)
00103 {
00104 if (t1.unix_time () < t2.unix_time ())
00105 return true;
00106 else if (t1.unix_time () > t2.unix_time ())
00107 return false;
00108 else if (t1.usec () < t2.usec ())
00109 return true;
00110 else
00111 return false;
00112 }
00113
00114 inline bool
00115 operator <= (const octave_time& t1, const octave_time& t2)
00116 {
00117 return (t1 < t2 || t1 == t2);
00118 }
00119
00120 inline bool
00121 operator > (const octave_time& t1, const octave_time& t2)
00122 {
00123 if (t1.unix_time () > t2.unix_time ())
00124 return true;
00125 else if (t1.unix_time () < t2.unix_time ())
00126 return false;
00127 else if (t1.usec () > t2.usec ())
00128 return true;
00129 else
00130 return false;
00131 }
00132
00133 inline bool
00134 operator >= (const octave_time& t1, const octave_time& t2)
00135 {
00136 return (t1 > t2 || t1 == t2);
00137 }
00138
00139 class
00140 OCTAVE_API
00141 octave_base_tm
00142 {
00143 public:
00144
00145 octave_base_tm (void)
00146 : tm_usec (0), tm_sec (0), tm_min (0), tm_hour (0),
00147 tm_mday (0), tm_mon (0), tm_year (0), tm_wday (0),
00148 tm_yday (0), tm_isdst (0), tm_zone ("unknown")
00149 { }
00150
00151 octave_base_tm (const octave_base_tm& tm)
00152 : tm_usec (tm.tm_usec), tm_sec (tm.tm_sec), tm_min (tm.tm_min),
00153 tm_hour (tm.tm_hour), tm_mday (tm.tm_mday), tm_mon (tm.tm_mon),
00154 tm_year (tm.tm_year), tm_wday (tm.tm_wday), tm_yday (tm.tm_yday),
00155 tm_isdst (tm.tm_isdst), tm_zone (tm.tm_zone)
00156 { }
00157
00158 octave_base_tm& operator = (const octave_base_tm& tm)
00159 {
00160 if (this != &tm)
00161 {
00162 tm_usec = tm.tm_usec;
00163 tm_sec = tm.tm_sec;
00164 tm_min = tm.tm_min;
00165 tm_hour = tm.tm_hour;
00166 tm_mday = tm.tm_mday;
00167 tm_mon = tm.tm_mon;
00168 tm_year = tm.tm_year;
00169 tm_wday = tm.tm_wday;
00170 tm_yday = tm.tm_yday;
00171 tm_isdst = tm.tm_isdst;
00172 tm_zone = tm.tm_zone;
00173 }
00174
00175 return *this;
00176 }
00177
00178 virtual ~octave_base_tm (void) { }
00179
00180 int usec (void) const { return tm_usec; }
00181 int sec (void) const { return tm_sec; }
00182 int min (void) const { return tm_min; }
00183 int hour (void) const { return tm_hour; }
00184 int mday (void) const { return tm_mday; }
00185 int mon (void) const { return tm_mon; }
00186 int year (void) const { return tm_year; }
00187 int wday (void) const { return tm_wday; }
00188 int yday (void) const { return tm_yday; }
00189 int isdst (void) const { return tm_isdst; }
00190 std::string zone (void) const { return tm_zone; }
00191
00192 octave_base_tm& usec (int v);
00193 octave_base_tm& sec (int v);
00194 octave_base_tm& min (int v);
00195 octave_base_tm& hour (int v);
00196 octave_base_tm& mday (int v);
00197 octave_base_tm& mon (int v);
00198 octave_base_tm& year (int v);
00199 octave_base_tm& wday (int v);
00200 octave_base_tm& yday (int v);
00201 octave_base_tm& isdst (int v);
00202 octave_base_tm& zone (const std::string& s);
00203
00204 std::string strftime (const std::string& fmt) const;
00205
00206 std::string asctime (void) const
00207 { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
00208
00209 protected:
00210
00211
00212 int tm_usec;
00213
00214
00215 int tm_sec;
00216
00217
00218 int tm_min;
00219
00220
00221 int tm_hour;
00222
00223
00224 int tm_mday;
00225
00226
00227 int tm_mon;
00228
00229
00230 int tm_year;
00231
00232
00233 int tm_wday;
00234
00235
00236 int tm_yday;
00237
00238
00239 int tm_isdst;
00240
00241
00242 std::string tm_zone;
00243
00244 void init (void *p);
00245 };
00246
00247 class
00248 OCTAVE_API
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_API
00277 octave_gmtime : public octave_base_tm
00278 {
00279 public:
00280
00281 octave_gmtime (void)
00282 : octave_base_tm () { init (octave_time ()); }
00283
00284 octave_gmtime (const octave_time& ot)
00285 : octave_base_tm () { init (ot); }
00286
00287 octave_gmtime& operator = (const octave_gmtime& t)
00288 {
00289 octave_base_tm::operator = (t);
00290 return *this;
00291 }
00292
00293 ~octave_gmtime (void) { }
00294
00295 private:
00296
00297 void init (const octave_time& ot);
00298 };
00299
00300 class
00301 OCTAVE_API
00302 octave_strptime : public octave_base_tm
00303 {
00304 public:
00305
00306 octave_strptime (const std::string& str, const std::string& fmt)
00307 : octave_base_tm () { init (str, fmt); }
00308
00309 octave_strptime (const octave_strptime& s)
00310 : octave_base_tm (s) { nchars = s.nchars; }
00311
00312 octave_strptime& operator = (const octave_strptime& s)
00313 {
00314 octave_base_tm::operator = (s);
00315 nchars = s.nchars;
00316 return *this;
00317 }
00318
00319 int characters_converted (void) const { return nchars; }
00320
00321 ~octave_strptime (void) { }
00322
00323 private:
00324
00325 int nchars;
00326
00327 void init (const std::string& str, const std::string& fmt);
00328 };
00329
00330 #endif
00331
00332
00333
00334
00335
00336