Go to the documentation of this file.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 <ctime>
00027 #include <string>
00028
00029 #include "lo-math.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 (time_t t, int us)
00046 : ot_unix_time (t), ot_usec ()
00047 {
00048 int rem, extra;
00049
00050 if (us >= 0)
00051 {
00052 rem = us % 1000000;
00053 extra = (us - rem) / 1000000;
00054 }
00055 else
00056 {
00057 us = -us;
00058 rem = us % 1000000;
00059 extra = - (1 + (us - rem) / 1000000);
00060 rem = 1000000 - us % 1000000;
00061 }
00062
00063 ot_usec = rem;
00064 ot_unix_time += extra;
00065 }
00066
00067 octave_time (double d)
00068 : ot_unix_time (static_cast<time_t> (d)), ot_usec (0)
00069 {
00070 double ip;
00071 ot_usec = static_cast<int> (std::modf (d, &ip) * 1e6);
00072 }
00073
00074 octave_time (const octave_base_tm& tm);
00075
00076 octave_time (const octave_time& ot)
00077 : ot_unix_time (ot.ot_unix_time), ot_usec (ot.ot_usec) { }
00078
00079 octave_time& operator = (const octave_time& ot)
00080 {
00081 if (this != &ot)
00082 {
00083 ot_unix_time = ot.ot_unix_time;
00084 ot_usec = ot.ot_usec;
00085 }
00086
00087 return *this;
00088 }
00089
00090 ~octave_time (void) { }
00091
00092 void stamp (void);
00093
00094 double double_value (void) const { return ot_unix_time + ot_usec / 1e6; }
00095
00096 time_t unix_time (void) const { return ot_unix_time; }
00097
00098 int usec (void) const { return ot_usec; }
00099
00100 std::string ctime (void) const;
00101
00102 private:
00103
00104
00105 time_t ot_unix_time;
00106
00107
00108 int ot_usec;
00109 };
00110
00111 inline bool
00112 operator == (const octave_time& t1, const octave_time& t2)
00113 {
00114 return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
00115 }
00116
00117 inline bool
00118 operator != (const octave_time& t1, const octave_time& t2)
00119 {
00120 return ! (t1 == t2);
00121 }
00122
00123 inline bool
00124 operator < (const octave_time& t1, const octave_time& t2)
00125 {
00126 if (t1.unix_time () < t2.unix_time ())
00127 return true;
00128 else if (t1.unix_time () > t2.unix_time ())
00129 return false;
00130 else if (t1.usec () < t2.usec ())
00131 return true;
00132 else
00133 return false;
00134 }
00135
00136 inline bool
00137 operator <= (const octave_time& t1, const octave_time& t2)
00138 {
00139 return (t1 < t2 || t1 == t2);
00140 }
00141
00142 inline bool
00143 operator > (const octave_time& t1, const octave_time& t2)
00144 {
00145 if (t1.unix_time () > t2.unix_time ())
00146 return true;
00147 else if (t1.unix_time () < t2.unix_time ())
00148 return false;
00149 else if (t1.usec () > t2.usec ())
00150 return true;
00151 else
00152 return false;
00153 }
00154
00155 inline bool
00156 operator >= (const octave_time& t1, const octave_time& t2)
00157 {
00158 return (t1 > t2 || t1 == t2);
00159 }
00160
00161 inline octave_time
00162 operator + (const octave_time& t1, const octave_time& t2)
00163 {
00164 return octave_time (t1.unix_time () + t2.unix_time (),
00165 t1.usec () + t2.usec ());
00166 }
00167
00168 class
00169 OCTAVE_API
00170 octave_base_tm
00171 {
00172 public:
00173
00174 octave_base_tm (void)
00175 : tm_usec (0), tm_sec (0), tm_min (0), tm_hour (0),
00176 tm_mday (0), tm_mon (0), tm_year (0), tm_wday (0),
00177 tm_yday (0), tm_isdst (0), tm_gmtoff (0), tm_zone ("unknown")
00178 { }
00179
00180 octave_base_tm (const octave_base_tm& tm)
00181 : tm_usec (tm.tm_usec), tm_sec (tm.tm_sec), tm_min (tm.tm_min),
00182 tm_hour (tm.tm_hour), tm_mday (tm.tm_mday), tm_mon (tm.tm_mon),
00183 tm_year (tm.tm_year), tm_wday (tm.tm_wday), tm_yday (tm.tm_yday),
00184 tm_isdst (tm.tm_isdst), tm_gmtoff (tm.tm_gmtoff), tm_zone (tm.tm_zone)
00185 { }
00186
00187 octave_base_tm& operator = (const octave_base_tm& tm)
00188 {
00189 if (this != &tm)
00190 {
00191 tm_usec = tm.tm_usec;
00192 tm_sec = tm.tm_sec;
00193 tm_min = tm.tm_min;
00194 tm_hour = tm.tm_hour;
00195 tm_mday = tm.tm_mday;
00196 tm_mon = tm.tm_mon;
00197 tm_year = tm.tm_year;
00198 tm_wday = tm.tm_wday;
00199 tm_yday = tm.tm_yday;
00200 tm_isdst = tm.tm_isdst;
00201 tm_gmtoff = tm.tm_gmtoff;
00202 tm_zone = tm.tm_zone;
00203 }
00204
00205 return *this;
00206 }
00207
00208 virtual ~octave_base_tm (void) { }
00209
00210 int usec (void) const { return tm_usec; }
00211 int sec (void) const { return tm_sec; }
00212 int min (void) const { return tm_min; }
00213 int hour (void) const { return tm_hour; }
00214 int mday (void) const { return tm_mday; }
00215 int mon (void) const { return tm_mon; }
00216 int year (void) const { return tm_year; }
00217 int wday (void) const { return tm_wday; }
00218 int yday (void) const { return tm_yday; }
00219 int isdst (void) const { return tm_isdst; }
00220 long gmtoff (void) const { return tm_gmtoff; }
00221 std::string zone (void) const { return tm_zone; }
00222
00223 octave_base_tm& usec (int v);
00224 octave_base_tm& sec (int v);
00225 octave_base_tm& min (int v);
00226 octave_base_tm& hour (int v);
00227 octave_base_tm& mday (int v);
00228 octave_base_tm& mon (int v);
00229 octave_base_tm& year (int v);
00230 octave_base_tm& wday (int v);
00231 octave_base_tm& yday (int v);
00232 octave_base_tm& isdst (int v);
00233 octave_base_tm& gmtoff (long v);
00234 octave_base_tm& zone (const std::string& s);
00235
00236 std::string strftime (const std::string& fmt) const;
00237
00238 std::string asctime (void) const
00239 { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
00240
00241 protected:
00242
00243
00244 int tm_usec;
00245
00246
00247 int tm_sec;
00248
00249
00250 int tm_min;
00251
00252
00253 int tm_hour;
00254
00255
00256 int tm_mday;
00257
00258
00259 int tm_mon;
00260
00261
00262 int tm_year;
00263
00264
00265 int tm_wday;
00266
00267
00268 int tm_yday;
00269
00270
00271 int tm_isdst;
00272
00273
00274 long tm_gmtoff;
00275
00276
00277 std::string tm_zone;
00278
00279 void init (void *p);
00280 };
00281
00282 class
00283 OCTAVE_API
00284 octave_localtime : public octave_base_tm
00285 {
00286 public:
00287
00288 octave_localtime (void)
00289 : octave_base_tm () { init (octave_time ()); }
00290
00291 octave_localtime (const octave_time& ot)
00292 : octave_base_tm () { init (ot); }
00293
00294 octave_localtime (const octave_localtime& t)
00295 : octave_base_tm (t) { }
00296
00297 octave_localtime& operator = (const octave_localtime& t)
00298 {
00299 octave_base_tm::operator = (t);
00300 return *this;
00301 }
00302
00303 ~octave_localtime (void) { }
00304
00305 private:
00306
00307 void init (const octave_time& ot);
00308 };
00309
00310 class
00311 OCTAVE_API
00312 octave_gmtime : public octave_base_tm
00313 {
00314 public:
00315
00316 octave_gmtime (void)
00317 : octave_base_tm () { init (octave_time ()); }
00318
00319 octave_gmtime (const octave_time& ot)
00320 : octave_base_tm () { init (ot); }
00321
00322 octave_gmtime& operator = (const octave_gmtime& t)
00323 {
00324 octave_base_tm::operator = (t);
00325 return *this;
00326 }
00327
00328 ~octave_gmtime (void) { }
00329
00330 private:
00331
00332 void init (const octave_time& ot);
00333 };
00334
00335 class
00336 OCTAVE_API
00337 octave_strptime : public octave_base_tm
00338 {
00339 public:
00340
00341 octave_strptime (const std::string& str, const std::string& fmt)
00342 : octave_base_tm (), nchars (0)
00343 {
00344 init (str, fmt);
00345 }
00346
00347 octave_strptime (const octave_strptime& s)
00348 : octave_base_tm (s), nchars (s.nchars) { }
00349
00350 octave_strptime& operator = (const octave_strptime& s)
00351 {
00352 octave_base_tm::operator = (s);
00353 nchars = s.nchars;
00354 return *this;
00355 }
00356
00357 int characters_converted (void) const { return nchars; }
00358
00359 ~octave_strptime (void) { }
00360
00361 private:
00362
00363 int nchars;
00364
00365 void init (const std::string& str, const std::string& fmt);
00366 };
00367
00368 #endif