# HG changeset patch # User jwe # Date 932054332 0 # Node ID 07d2d307c43e9bea729f014f3109ed5f17ac3359 # Parent 9c784bd188582032e1201bb91b3f9b6ec9b9a336 [project @ 1999-07-15 15:58:50 by jwe] diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,11 @@ +Wed Jul 14 17:38:07 1999 John W. Eaton + + * oct-time.h, oct-time.cc: New files. + * Makefile.in (INCLUDES, SOURCES): Add them to the lists. + + * systime.h: Move here from src directory. + * Makefile.in (INCLUDES): Add it to the list. + Mon Jul 12 22:34:34 1999 John W. Eaton * mx-defs.h (b_d_Mapper, b_c_Mapper): New typedefs. diff --git a/liboctave/Makefile.in b/liboctave/Makefile.in --- a/liboctave/Makefile.in +++ b/liboctave/Makefile.in @@ -47,9 +47,9 @@ dir-ops.h file-ops.h file-stat.h getopt.h glob-match.h \ idx-vector.h lo-ieee.h lo-mappers.h lo-specfun.h lo-sysdep.h \ lo-utils.h mach-info.h oct-alloc.h oct-cmplx.h oct-env.h \ - oct-group.h oct-passwd.h oct-syscalls.h pathlen.h \ + oct-group.h oct-passwd.h oct-syscalls.h oct-time.h pathlen.h \ pathsearch.h prog-args.h statdefs.h str-vec.h sun-utils.h \ - sysdir.h syswait.h \ + sysdir.h systime.h syswait.h \ $(MATRIX_INC) \ $(MX_OP_INC) @@ -81,8 +81,8 @@ idx-vector.cc lo-ieee.cc lo-mappers.cc lo-specfun.cc \ lo-sysdep.cc lo-utils.cc mach-info.cc mkdir.c oct-alloc.cc \ oct-env.cc oct-group.cc oct-passwd.cc oct-syscalls.cc \ - pathsearch.cc prog-args.cc rename.c rmdir.c str-vec.cc \ - tempname.c tempnam.c \ + oct-time.cc pathsearch.cc prog-args.cc rename.c rmdir.c \ + str-vec.cc tempname.c tempnam.c \ $(TEMPLATE_SRC) \ $(TI_SRC) \ $(MATRIX_SRC) \ diff --git a/liboctave/oct-time.cc b/liboctave/oct-time.cc new file mode 100644 --- /dev/null +++ b/liboctave/oct-time.cc @@ -0,0 +1,216 @@ +/* + +Copyright (C) 1999 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "lo-error.h" +#include "lo-utils.h" +#include "oct-time.h" + +octave_time::octave_time (const octave_base_tm& tm) +{ + struct tm t; + + t.tm_sec = tm.sec (); + t.tm_min = tm.min (); + t.tm_hour = tm.hour (); + t.tm_mday = tm.mday (); + t.tm_mon = tm.mon (); + t.tm_year = tm.year (); + t.tm_wday = tm.wday (); + t.tm_yday = tm.yday (); + t.tm_isdst = tm.isdst (); + +#if defined (HAVE_TM_ZONE) + string s = tm.zone (); + t.tm_zone = strsave (s.c_str ()); +#endif + + ot_unix_time = mktime (&t); + + ot_usec = tm.usec (); + +#if defined (HAVE_TM_ZONE) + delete [] t.tm_zone; +#endif +} + +void +octave_time::stamp (void) +{ +#if defined (HAVE_GETTIMEOFDAY) + + struct timeval tp; + +#if defined (GETTIMEOFDAY_NO_TZ) + gettimeofday (&tp); +#else + gettimeofday (&tp, 0); +#endif + + ot_unix_time = tp.tv_sec; + + ot_usec = tp.tv_usec; + +#else + + ot_unix_time = time (0); + +#endif +} + +#define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ + octave_base_tm& \ + octave_base_tm::f (int v) \ + { \ + if (v < lo || v > hi) \ + (*current_liboctave_error_handler) \ + ("invalid value specified for " #f); \ + \ + tm_ ## f = v; \ + \ + return *this; \ + } + +DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) +DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) +DEFINE_SET_INT_FIELD_FCN (min, 0, 59) +DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) +DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) +DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) +DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) +DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) +DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) +DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) + +octave_base_tm& +octave_base_tm::zone (const string& s) +{ + tm_zone = s; + return *this; +} + +#if !defined STRFTIME_BUF_INITIAL_SIZE +#define STRFTIME_BUF_INITIAL_SIZE 128 +#endif + +string +octave_base_tm::format_as_string (const string& fmt) const +{ + string retval; + + struct tm t; + + t.tm_sec = tm_sec; + t.tm_min = tm_min; + t.tm_hour = tm_hour; + t.tm_mday = tm_mday; + t.tm_mon = tm_mon; + t.tm_year = tm_year; + t.tm_wday = tm_wday; + t.tm_yday = tm_yday; + t.tm_isdst = tm_isdst; + +#if defined (HAVE_TM_ZONE) + t.tm_zone = strsave (tm_zone.c_str ()); +#endif + + const char *fmt_str = fmt.c_str (); + + char *buf = 0; + size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; + size_t chars_written = 0; + + while (chars_written == 0) + { + delete [] buf; + buf = new char[bufsize]; + buf[0] = '\0'; + + chars_written = strftime (buf, bufsize, fmt_str, &t); + + bufsize *= 2; + } + + retval = buf; + + delete [] buf; + +#if defined (HAVE_TM_ZONE) + delete [] t.tm_zone; +#endif + + return retval; +} + +void +octave_base_tm::init (void *p) +{ + struct tm *t = static_cast (p); + + tm_sec = t->tm_sec; + tm_min = t->tm_min; + tm_hour = t->tm_hour; + tm_mday = t->tm_mday; + tm_mon = t->tm_mon; + tm_year = t->tm_year; + tm_wday = t->tm_wday; + tm_yday = t->tm_yday; + tm_isdst = t->tm_isdst; + +#if defined (HAVE_TM_ZONE) + tm_zone = t->tm_zone; +#elif defined (HAVE_TZNAME) + if (t->tm_isdst == 0 || t->tm_isdst == 1) + tm_zone = tzname[t->tm_isdst]; +#endif +} + +void +octave_localtime::init (const octave_time& ot) +{ + tm_usec = ot.usec (); + + time_t t = ot.unix_time (); + + octave_base_tm::init (localtime (&t)); +} + +void +octave_gmtime::init (const octave_time& ot) +{ + tm_usec = ot.usec (); + + time_t t = ot.unix_time (); + + octave_base_tm::init (gmtime (&t)); +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/liboctave/oct-time.h b/liboctave/oct-time.h new file mode 100644 --- /dev/null +++ b/liboctave/oct-time.h @@ -0,0 +1,246 @@ +/* + +Copyright (C) 1999 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if !defined (octave_time_h) +#define octave_time_h 1 + +#include + +#include + +#include "systime.h" + +class octave_base_tm; + +class +octave_time +{ +public: + + octave_time (void) + : ot_unix_time (0), ot_usec (0) { stamp (); } + + octave_time (double d) + : ot_unix_time (static_cast (d)), + ot_usec (0) + { + double ip; + ot_usec = static_cast (modf (d, &ip) * 1e6); + } + + octave_time (const octave_base_tm& tm); + + octave_time (const octave_time& ot) + : ot_unix_time (ot.ot_unix_time), ot_usec (ot.ot_usec) { } + + octave_time& operator = (const octave_time& ot) + { + if (this != &ot) + { + ot_unix_time = ot.ot_unix_time; + ot_usec = ot.ot_usec; + } + + return *this; + } + + ~octave_time (void) { } + + void stamp (void); + + double as_double (void) const { return ot_unix_time + ot_usec / 1e6; } + + time_t unix_time (void) const { return ot_unix_time; } + + int usec (void) const { return ot_usec; } + +private: + + // Seconds since the epoch. + time_t ot_unix_time; + + // Additional microseconds. + int ot_usec; +}; + +class +octave_base_tm +{ +public: + + octave_base_tm (void) + : tm_usec (0), tm_sec (0), tm_min (0), tm_hour (0), + tm_mday (0), tm_mon (0), tm_year (0), tm_wday (0), + tm_yday (0), tm_isdst (0), tm_zone ("unknown") + { } + + octave_base_tm (const octave_base_tm& tm) + : tm_usec (tm.tm_usec), tm_sec (tm.tm_sec), tm_min (tm.tm_min), + tm_hour (tm.tm_hour), tm_mday (tm.tm_mday), tm_mon (tm.tm_mon), + tm_year (tm.tm_year), tm_wday (tm.tm_wday), tm_yday (tm.tm_yday), + tm_isdst (tm.tm_isdst), tm_zone (tm.tm_zone) + { } + + octave_base_tm& operator = (const octave_base_tm& tm) + { + if (this != &tm) + { + tm_usec = tm.tm_usec; + tm_sec = tm.tm_sec; + tm_min = tm.tm_min; + tm_hour = tm.tm_hour; + tm_mday = tm.tm_mday; + tm_mon = tm.tm_mon; + tm_year = tm.tm_year; + tm_wday = tm.tm_wday; + tm_yday = tm.tm_yday; + tm_isdst = tm.tm_isdst; + tm_zone = tm.tm_zone; + } + + return *this; + } + + virtual ~octave_base_tm (void) { } + + int usec (void) const { return tm_usec; } + int sec (void) const { return tm_sec; } + int min (void) const { return tm_min; } + int hour (void) const { return tm_hour; } + int mday (void) const { return tm_mday; } + int mon (void) const { return tm_mon; } + int year (void) const { return tm_year; } + int wday (void) const { return tm_wday; } + int yday (void) const { return tm_yday; } + int isdst (void) const { return tm_isdst; } + string zone (void) const { return tm_zone; } + + octave_base_tm& usec (int v); + octave_base_tm& sec (int v); + octave_base_tm& min (int v); + octave_base_tm& hour (int v); + octave_base_tm& mday (int v); + octave_base_tm& mon (int v); + octave_base_tm& year (int v); + octave_base_tm& wday (int v); + octave_base_tm& yday (int v); + octave_base_tm& isdst (int v); + octave_base_tm& zone (const string& s); + + string format_as_string (const string& fmt) const; + +protected: + + // Microseconds after the second (0, 999999). + int tm_usec; + + // Seconds after the minute (0, 61). + int tm_sec; + + // Minutes after the hour (0, 59). + int tm_min; + + // Hours since midnight (0, 23). + int tm_hour; + + // Day of the month (1, 31). + int tm_mday; + + // Months since January (0, 11). + int tm_mon; + + // Years since 1900. + int tm_year; + + // Days since Sunday (0, 6). + int tm_wday; + + // Days since January 1 (0, 365). + int tm_yday; + + // Daylight Savings Time flag. + int tm_isdst; + + // Time zone. + string tm_zone; + + void init (void *p); +}; + +class +octave_localtime : public octave_base_tm +{ +public: + + octave_localtime (void) + : octave_base_tm () { init (octave_time ()); } + + octave_localtime (const octave_time& ot) + : octave_base_tm () { init (ot); } + + octave_localtime (const octave_localtime& t) + : octave_base_tm (t) { } + + octave_localtime& operator = (const octave_localtime& t) + { + octave_base_tm::operator = (t); + return *this; + } + + ~octave_localtime (void) { } + +private: + + void init (const octave_time& ot); +}; + +class +octave_gmtime : public octave_base_tm +{ +public: + + octave_gmtime (void) + : octave_base_tm () { init (octave_time ()); } + + octave_gmtime (const octave_time& ot) + : octave_base_tm () { init (ot); } + + octave_gmtime& operator = (const octave_gmtime& t) + { + octave_base_tm::operator = (t); + return *this; + } + + ~octave_gmtime (void) { } + +private: + + void init (const octave_time& ot); +}; + +#endif + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/