00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include <climits>
00028 #include <cmath>
00029
00030 #ifdef HAVE_UNISTD_H
00031 #ifdef HAVE_SYS_TYPES_H
00032 #include <sys/types.h>
00033 #endif
00034 #include <unistd.h>
00035 #endif
00036
00037 #if defined (OCTAVE_USE_WINDOWS_API)
00038 #include <windows.h>
00039 #endif
00040
00041 #include "lo-error.h"
00042 #include "lo-utils.h"
00043 #include "oct-time.h"
00044
00045 octave_time::octave_time (const octave_base_tm& tm)
00046 {
00047 struct tm t;
00048
00049 t.tm_sec = tm.sec ();
00050 t.tm_min = tm.min ();
00051 t.tm_hour = tm.hour ();
00052 t.tm_mday = tm.mday ();
00053 t.tm_mon = tm.mon ();
00054 t.tm_year = tm.year ();
00055 t.tm_wday = tm.wday ();
00056 t.tm_yday = tm.yday ();
00057 t.tm_isdst = tm.isdst ();
00058
00059 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00060 std::string s = tm.zone ();
00061 char *ps = strsave (s.c_str ());
00062 t.tm_zone = ps;
00063 #endif
00064
00065 ot_unix_time = mktime (&t);
00066
00067 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00068 delete [] ps;
00069 #endif
00070
00071 ot_usec = tm.usec ();
00072 }
00073
00074 std::string
00075 octave_time::ctime (void) const
00076 {
00077 return octave_localtime (*this) . asctime ();
00078 }
00079
00080 void
00081 octave_time::stamp (void)
00082 {
00083 #if defined (HAVE_GETTIMEOFDAY)
00084
00085 struct timeval tp;
00086
00087 #if defined (GETTIMEOFDAY_NO_TZ)
00088 gettimeofday (&tp);
00089 #else
00090 gettimeofday (&tp, 0);
00091 #endif
00092
00093 ot_unix_time = tp.tv_sec;
00094 ot_usec = tp.tv_usec;
00095
00096 #elif defined (OCTAVE_USE_WINDOWS_API)
00097
00098
00099
00100
00101
00102 const LONGLONG TIME_OFFSET = 0x19db1ded53e8000LL;
00103
00104 static int init = 1;
00105 static LARGE_INTEGER base;
00106 static LARGE_INTEGER t0;
00107 static double dt;
00108
00109 if (init)
00110 {
00111 LARGE_INTEGER ifreq;
00112
00113 if (QueryPerformanceFrequency (&ifreq))
00114 {
00115
00116 dt = (double) 1000000.0 / (double) ifreq.QuadPart;
00117
00118
00119 int priority = GetThreadPriority (GetCurrentThread ());
00120 SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
00121 if (QueryPerformanceCounter (&base))
00122 {
00123 FILETIME f;
00124
00125 GetSystemTimeAsFileTime (&f);
00126
00127 t0.HighPart = f.dwHighDateTime;
00128 t0.LowPart = f.dwLowDateTime;
00129 t0.QuadPart -= TIME_OFFSET;
00130 t0.QuadPart /= 10;
00131
00132 init = 0;
00133 }
00134
00135 SetThreadPriority (GetCurrentThread (), priority);
00136 }
00137
00138 if (! init)
00139 {
00140 ot_unix_time = time (0);
00141 ot_usec = 0;
00142
00143 return;
00144 }
00145 }
00146
00147 LARGE_INTEGER now;
00148
00149 if (QueryPerformanceCounter (&now))
00150 {
00151 now.QuadPart = (LONGLONG) (dt * (double)(now.QuadPart - base.QuadPart));
00152 now.QuadPart += t0.QuadPart;
00153
00154 ot_unix_time = now.QuadPart / 1000000LL;
00155 ot_usec = now.QuadPart % 1000000LL;
00156 }
00157 else
00158 {
00159 ot_unix_time = time (0);
00160 ot_usec = 0;
00161 }
00162
00163 #else
00164
00165 ot_unix_time = time (0);
00166
00167 #endif
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 #if 0
00184 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \
00185 octave_base_tm& \
00186 octave_base_tm::f (int v) \
00187 { \
00188 if (v < lo || v > hi) \
00189 (*current_liboctave_error_handler) \
00190 ("invalid value specified for " #f); \
00191 \
00192 tm_ ## f = v; \
00193 \
00194 return *this; \
00195 }
00196 #else
00197 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \
00198 octave_base_tm& \
00199 octave_base_tm::f (int v) \
00200 { \
00201 tm_ ## f = v; \
00202 \
00203 return *this; \
00204 }
00205 #endif
00206
00207 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000)
00208 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61)
00209 DEFINE_SET_INT_FIELD_FCN (min, 0, 59)
00210 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23)
00211 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31)
00212 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11)
00213 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX)
00214 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6)
00215 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365)
00216 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1)
00217
00218 octave_base_tm&
00219 octave_base_tm::zone (const std::string& s)
00220 {
00221 tm_zone = s;
00222 return *this;
00223 }
00224
00225 #if !defined STRFTIME_BUF_INITIAL_SIZE
00226 #define STRFTIME_BUF_INITIAL_SIZE 128
00227 #endif
00228
00229 std::string
00230 octave_base_tm::strftime (const std::string& fmt) const
00231 {
00232 std::string retval;
00233
00234 if (! fmt.empty ())
00235 {
00236 struct tm t;
00237
00238 t.tm_sec = tm_sec;
00239 t.tm_min = tm_min;
00240 t.tm_hour = tm_hour;
00241 t.tm_mday = tm_mday;
00242 t.tm_mon = tm_mon;
00243 t.tm_year = tm_year;
00244 t.tm_wday = tm_wday;
00245 t.tm_yday = tm_yday;
00246 t.tm_isdst = tm_isdst;
00247
00248 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00249 char *ps = strsave (tm_zone.c_str ());
00250 t.tm_zone = ps;
00251 #endif
00252
00253 const char *fmt_str = fmt.c_str ();
00254
00255 char *buf = 0;
00256 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE;
00257 size_t chars_written = 0;
00258
00259 while (chars_written == 0)
00260 {
00261 delete [] buf;
00262 buf = new char[bufsize];
00263 buf[0] = '\0';
00264
00265 chars_written = ::strftime (buf, bufsize, fmt_str, &t);
00266
00267 bufsize *= 2;
00268 }
00269
00270 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00271 delete [] ps;
00272 #endif
00273
00274 retval = buf;
00275
00276 delete [] buf;
00277 }
00278
00279 return retval;
00280 }
00281
00282 void
00283 octave_base_tm::init (void *p)
00284 {
00285 struct tm *t = static_cast<struct tm*> (p);
00286
00287 tm_sec = t->tm_sec;
00288 tm_min = t->tm_min;
00289 tm_hour = t->tm_hour;
00290 tm_mday = t->tm_mday;
00291 tm_mon = t->tm_mon;
00292 tm_year = t->tm_year;
00293 tm_wday = t->tm_wday;
00294 tm_yday = t->tm_yday;
00295 tm_isdst = t->tm_isdst;
00296
00297 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00298 tm_zone = t->tm_zone;
00299 #elif defined (HAVE_TZNAME)
00300 if (t->tm_isdst == 0 || t->tm_isdst == 1)
00301 tm_zone = tzname[t->tm_isdst];
00302 #endif
00303 }
00304
00305 void
00306 octave_localtime::init (const octave_time& ot)
00307 {
00308 tm_usec = ot.usec ();
00309
00310 time_t t = ot;
00311
00312 octave_base_tm::init (localtime (&t));
00313 }
00314
00315 void
00316 octave_gmtime::init (const octave_time& ot)
00317 {
00318 tm_usec = ot.usec ();
00319
00320 time_t t = ot;
00321
00322 octave_base_tm::init (gmtime (&t));
00323 }
00324
00325 void
00326 octave_strptime::init (const std::string& str, const std::string& fmt)
00327 {
00328 struct tm t;
00329
00330 t.tm_sec = 0;
00331 t.tm_min = 0;
00332 t.tm_hour = 0;
00333 t.tm_mday = 0;
00334 t.tm_mon = 0;
00335 t.tm_year = 0;
00336 t.tm_wday = 0;
00337 t.tm_yday = 0;
00338 t.tm_isdst = 0;
00339
00340 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00341 char *ps = strsave ("");
00342 t.tm_zone = ps;
00343 #endif
00344
00345 char *p = strsave (str.c_str ());
00346
00347 char *q = oct_strptime (p, fmt.c_str (), &t);
00348
00349 nchars = p - q;
00350
00351 delete [] p;
00352
00353 octave_base_tm::init (&t);
00354
00355 #if defined (HAVE_STRUCT_TM_TM_ZONE)
00356 delete ps;
00357 #endif
00358 }
00359
00360
00361
00362
00363
00364