Mercurial > octave
view liboctave/system/oct-time.h @ 32628:ae4e19c0a2b1
maint: Place class name and class keyword on one line.
* libinterp/corefcn/Cell.h, libinterp/corefcn/__magick_read__.cc,
libinterp/corefcn/auto-shlib.h, libinterp/corefcn/base-text-renderer.h,
libinterp/corefcn/c-file-ptr-stream.h, libinterp/corefcn/call-stack.h,
libinterp/corefcn/dynamic-ld.h, libinterp/corefcn/event-manager.h,
libinterp/corefcn/event-queue.h, libinterp/corefcn/ft-text-renderer.cc,
libinterp/corefcn/gl-render.cc, libinterp/corefcn/gl-render.h,
libinterp/corefcn/gl2ps-print.cc, libinterp/corefcn/graphics.cc,
libinterp/corefcn/graphics.in.h, libinterp/corefcn/input.cc,
libinterp/corefcn/latex-text-renderer.cc, libinterp/corefcn/load-path.h,
libinterp/corefcn/mx-type-traits.h, libinterp/corefcn/oct-errno.h,
libinterp/corefcn/oct-fstrm.h, libinterp/corefcn/oct-iostrm.h,
libinterp/corefcn/oct-prcstrm.h, libinterp/corefcn/oct-procbuf.h,
libinterp/corefcn/oct-process.h, libinterp/corefcn/oct-stdstrm.h,
libinterp/corefcn/oct-stream.cc, libinterp/corefcn/oct-stream.h,
libinterp/corefcn/oct-strstrm.h, libinterp/corefcn/pager.h,
libinterp/corefcn/pr-flt-fmt.h, libinterp/corefcn/pr-output.cc,
libinterp/corefcn/pr-output.h, libinterp/corefcn/procstream.h,
libinterp/corefcn/syminfo.h, libinterp/corefcn/text-engine.h,
libinterp/corefcn/text-renderer.h, libinterp/corefcn/utils.h,
libinterp/octave-value/cdef-class.h, libinterp/octave-value/cdef-manager.h,
libinterp/octave-value/cdef-method.h, libinterp/octave-value/cdef-object.h,
libinterp/octave-value/cdef-package.h, libinterp/octave-value/cdef-property.h,
libinterp/octave-value/ov-base-diag.h, libinterp/octave-value/ov-base-int.h,
libinterp/octave-value/ov-base-mat.h, libinterp/octave-value/ov-base-scalar.h,
libinterp/octave-value/ov-base-sparse.h, libinterp/octave-value/ov-base.h,
libinterp/octave-value/ov-bool-mat.h, libinterp/octave-value/ov-bool-sparse.h,
libinterp/octave-value/ov-bool.h, libinterp/octave-value/ov-builtin.h,
libinterp/octave-value/ov-cell.h, libinterp/octave-value/ov-ch-mat.h,
libinterp/octave-value/ov-class.cc, libinterp/octave-value/ov-class.h,
libinterp/octave-value/ov-classdef.h, libinterp/octave-value/ov-colon.h,
libinterp/octave-value/ov-complex.h, libinterp/octave-value/ov-cs-list.h,
libinterp/octave-value/ov-cx-diag.h, libinterp/octave-value/ov-cx-mat.h,
libinterp/octave-value/ov-cx-sparse.h, libinterp/octave-value/ov-dld-fcn.h,
libinterp/octave-value/ov-fcn-handle.h, libinterp/octave-value/ov-fcn.h,
libinterp/octave-value/ov-float.h, libinterp/octave-value/ov-flt-complex.h,
libinterp/octave-value/ov-flt-cx-diag.h,
libinterp/octave-value/ov-flt-cx-mat.h,
libinterp/octave-value/ov-flt-re-diag.h,
libinterp/octave-value/ov-flt-re-mat.h, libinterp/octave-value/ov-int-traits.h,
libinterp/octave-value/ov-intx.h, libinterp/octave-value/ov-lazy-idx.h,
libinterp/octave-value/ov-legacy-range.cc,
libinterp/octave-value/ov-legacy-range.h,
libinterp/octave-value/ov-magic-int.h, libinterp/octave-value/ov-mex-fcn.h,
libinterp/octave-value/ov-null-mat.h, libinterp/octave-value/ov-perm.h,
libinterp/octave-value/ov-range-traits.h, libinterp/octave-value/ov-range.h,
libinterp/octave-value/ov-re-diag.h, libinterp/octave-value/ov-re-mat.h,
libinterp/octave-value/ov-re-sparse.h, libinterp/octave-value/ov-scalar.h,
libinterp/octave-value/ov-str-mat.h, libinterp/octave-value/ov-struct.h,
libinterp/octave-value/ov-typeinfo.h, libinterp/octave-value/ov-usr-fcn.h,
libinterp/octave-value/ov.h, libinterp/octave-value/ovl.h,
libinterp/parse-tree/comment-list.h, libinterp/parse-tree/lex.h,
libinterp/parse-tree/profiler.h, libinterp/parse-tree/pt-anon-scopes.h,
liboctave/array/Array.h, liboctave/array/CColVector.h,
liboctave/array/CDiagMatrix.h, liboctave/array/CMatrix.h,
liboctave/array/CNDArray.h, liboctave/array/CRowVector.h,
liboctave/array/CSparse.h, liboctave/array/DiagArray2.h,
liboctave/array/MArray.h, liboctave/array/MDiagArray2.h,
liboctave/array/MSparse.h, liboctave/array/MatrixType.h,
liboctave/array/Range.h, liboctave/array/Sparse.h,
liboctave/array/boolMatrix.h, liboctave/array/boolNDArray.h,
liboctave/array/boolSparse.h, liboctave/array/chMatrix.h,
liboctave/array/chNDArray.h, liboctave/array/dColVector.h,
liboctave/array/dDiagMatrix.h, liboctave/array/dMatrix.h,
liboctave/array/dNDArray.h, liboctave/array/dRowVector.h,
liboctave/array/dSparse.h, liboctave/array/dim-vector.h,
liboctave/array/fCColVector.h, liboctave/array/fCDiagMatrix.h,
liboctave/array/fCMatrix.h, liboctave/array/fCNDArray.h,
liboctave/array/fCRowVector.h, liboctave/array/fColVector.h,
liboctave/array/fDiagMatrix.h, liboctave/array/fMatrix.h,
liboctave/array/fNDArray.h, liboctave/array/fRowVector.h,
liboctave/array/idx-vector.h, liboctave/array/intNDArray.h,
liboctave/numeric/DAE.h, liboctave/numeric/DAEFunc.h,
liboctave/numeric/DAERT.h, liboctave/numeric/DAERTFunc.h,
liboctave/numeric/DASPK.h, liboctave/numeric/DASRT.h,
liboctave/numeric/DASSL.h, liboctave/numeric/DET.h, liboctave/numeric/EIG.h,
liboctave/numeric/LSODE.h, liboctave/numeric/ODE.h,
liboctave/numeric/ODEFunc.h, liboctave/numeric/ODES.h,
liboctave/numeric/ODESFunc.h, liboctave/numeric/Quad.h,
liboctave/numeric/aepbalance.h, liboctave/numeric/base-dae.h,
liboctave/numeric/base-de.h, liboctave/numeric/chol.h,
liboctave/numeric/fEIG.h, liboctave/numeric/gepbalance.h,
liboctave/numeric/gsvd.h, liboctave/numeric/hess.h, liboctave/numeric/lu.h,
liboctave/numeric/oct-fftw.h, liboctave/numeric/qr.h, liboctave/numeric/qrp.h,
liboctave/numeric/schur.h, liboctave/numeric/sparse-chol.h,
liboctave/numeric/sparse-lu.h, liboctave/numeric/sparse-qr.cc,
liboctave/numeric/sparse-qr.h, liboctave/numeric/svd.cc,
liboctave/numeric/svd.h, liboctave/system/dir-ops.h,
liboctave/system/file-stat.h, liboctave/system/oct-env.h,
liboctave/system/oct-group.h, liboctave/system/oct-password.h,
liboctave/system/oct-time.h, liboctave/system/oct-uname.h,
liboctave/util/action-container.h, liboctave/util/base-list.h,
liboctave/util/cmd-edit.cc, liboctave/util/cmd-edit.h,
liboctave/util/cmd-hist.cc, liboctave/util/cmd-hist.h,
liboctave/util/data-conv.h, liboctave/util/glob-match.h,
liboctave/util/lo-regexp.h, liboctave/util/oct-inttypes.h,
liboctave/util/oct-mutex.cc, liboctave/util/oct-mutex.h,
liboctave/util/oct-shlib.cc, liboctave/util/oct-shlib.h,
liboctave/util/oct-sort.h, liboctave/util/oct-string.h,
liboctave/util/octave-preserve-stream-state.h, liboctave/util/pathsearch.h,
liboctave/util/singleton-cleanup.h, liboctave/util/str-vec.h,
liboctave/util/unwind-prot.h, liboctave/util/url-transfer.h:
Place class name and class keyword on one line.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 21 Dec 2023 11:22:26 -0800 |
parents | 7d663f770c5a |
children | 4b601ca024d5 |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// // // Copyright (C) 1999-2023 The Octave Project Developers // // See the file COPYRIGHT.md in the top-level directory of this // distribution or <https://octave.org/copyright/>. // // 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 3 of the License, 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, see // <https://www.gnu.org/licenses/>. // //////////////////////////////////////////////////////////////////////// #if ! defined (octave_oct_time_h) #define octave_oct_time_h 1 #include "octave-config.h" #include <iosfwd> #include <string> #if defined (OCTAVE_USE_WINDOWS_API) // Some Windows headers must be included in a certain order. // Don't include "windows.h" here to avoid potential issues due to that. // Instead just define the one type we need for the interface of one function. struct OCTAVE_WIN_FILETIME { uint32_t dwLowDateTime; uint32_t dwHighDateTime; }; #endif static inline double as_double (OCTAVE_TIME_T sec, long usec) { // Unix time will be exactly representable as a double for more than // 100 million years, so no worry there, and microseconds has a // range of 0-1e6, so we are safe there as well. return (static_cast<double> (sec) + static_cast<double> (usec) / 1.0e6); } OCTAVE_BEGIN_NAMESPACE(octave) OCTAVE_BEGIN_NAMESPACE(sys) class base_tm; class time { public: time () : m_ot_unix_time (0), m_ot_usec (0) { stamp (); } time (OCTAVE_TIME_T t) : m_ot_unix_time (t), m_ot_usec (0) { } time (OCTAVE_TIME_T t, long us) : m_ot_unix_time (t), m_ot_usec () { long rem, extra; if (us >= 0) { rem = us % 1000000; extra = (us - rem) / 1000000; } else { us = -us; rem = us % 1000000; extra = - (1 + (us - rem) / 1000000); rem = 1000000 - us % 1000000; } m_ot_usec = rem; m_ot_unix_time += extra; } OCTAVE_API time (double d); OCTAVE_API time (const base_tm& tm); time (const time& ot) : m_ot_unix_time (ot.m_ot_unix_time), m_ot_usec (ot.m_ot_usec) { } time& operator = (const time& ot) { if (this != &ot) { m_ot_unix_time = ot.m_ot_unix_time; m_ot_usec = ot.m_ot_usec; } return *this; } ~time () = default; OCTAVE_API void stamp (); double double_value () const { return as_double (m_ot_unix_time, m_ot_usec); } OCTAVE_TIME_T unix_time () const { return m_ot_unix_time; } long usec () const { return m_ot_usec; } OCTAVE_API std::string ctime () const; friend OCTAVE_API std::ostream& operator << (std::ostream& os, const time& ot); private: // Seconds since the epoch. OCTAVE_TIME_T m_ot_unix_time; // Additional microseconds. long m_ot_usec; }; inline bool operator == (const time& t1, const time& t2) { return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ()); } inline bool operator != (const time& t1, const time& t2) { return ! (t1 == t2); } inline bool operator < (const time& t1, const time& t2) { if (t1.unix_time () < t2.unix_time ()) return true; else if (t1.unix_time () > t2.unix_time ()) return false; else if (t1.usec () < t2.usec ()) return true; else return false; } inline bool operator <= (const time& t1, const time& t2) { return (t1 < t2 || t1 == t2); } inline bool operator > (const time& t1, const time& t2) { if (t1.unix_time () > t2.unix_time ()) return true; else if (t1.unix_time () < t2.unix_time ()) return false; else if (t1.usec () > t2.usec ()) return true; else return false; } inline bool operator >= (const time& t1, const time& t2) { return (t1 > t2 || t1 == t2); } inline time operator + (const time& t1, const time& t2) { return time (t1.unix_time () + t2.unix_time (), t1.usec () + t2.usec ()); } class base_tm { public: base_tm () : m_usec (0), m_sec (0), m_min (0), m_hour (0), m_mday (0), m_mon (0), m_year (0), m_wday (0), m_yday (0), m_isdst (0), m_gmtoff (0), m_zone ("unknown") { } base_tm (const base_tm& tm) : m_usec (tm.m_usec), m_sec (tm.m_sec), m_min (tm.m_min), m_hour (tm.m_hour), m_mday (tm.m_mday), m_mon (tm.m_mon), m_year (tm.m_year), m_wday (tm.m_wday), m_yday (tm.m_yday), m_isdst (tm.m_isdst), m_gmtoff (tm.m_gmtoff), m_zone (tm.m_zone) { } base_tm& operator = (const base_tm& tm) { if (this != &tm) { m_usec = tm.m_usec; m_sec = tm.m_sec; m_min = tm.m_min; m_hour = tm.m_hour; m_mday = tm.m_mday; m_mon = tm.m_mon; m_year = tm.m_year; m_wday = tm.m_wday; m_yday = tm.m_yday; m_isdst = tm.m_isdst; m_gmtoff = tm.m_gmtoff; m_zone = tm.m_zone; } return *this; } virtual ~base_tm () = default; int usec () const { return m_usec; } int sec () const { return m_sec; } int min () const { return m_min; } int hour () const { return m_hour; } int mday () const { return m_mday; } int mon () const { return m_mon; } int year () const { return m_year; } int wday () const { return m_wday; } int yday () const { return m_yday; } int isdst () const { return m_isdst; } long gmtoff () const { return m_gmtoff; } std::string zone () const { return m_zone; } OCTAVE_API base_tm& usec (int v); OCTAVE_API base_tm& sec (int v); OCTAVE_API base_tm& min (int v); OCTAVE_API base_tm& hour (int v); OCTAVE_API base_tm& mday (int v); OCTAVE_API base_tm& mon (int v); OCTAVE_API base_tm& year (int v); OCTAVE_API base_tm& wday (int v); OCTAVE_API base_tm& yday (int v); OCTAVE_API base_tm& isdst (int v); OCTAVE_API base_tm& gmtoff (long v); OCTAVE_API base_tm& zone (const std::string& s); OCTAVE_API std::string strftime (const std::string& fmt) const; std::string asctime () const { return strftime ("%a %b %d %H:%M:%S %Y\n"); } protected: // Microseconds after the second (0, 999999). int m_usec; // Seconds after the minute (0, 61). int m_sec; // Minutes after the hour (0, 59). int m_min; // Hours since midnight (0, 23). int m_hour; // Day of the month (1, 31). int m_mday; // Months since January (0, 11). int m_mon; // Years since 1900. int m_year; // Days since Sunday (0, 6). int m_wday; // Days since January 1 (0, 365). int m_yday; // Daylight saving time flag. int m_isdst; // Time zone. long m_gmtoff; // Time zone. std::string m_zone; OCTAVE_API void init (void *p); }; class localtime : public base_tm { public: localtime () : base_tm () { init (time ()); } localtime (const time& ot) : base_tm () { init (ot); } localtime (const localtime& t) : base_tm (t) { } localtime& operator = (const localtime& t) { base_tm::operator = (t); return *this; } ~localtime () = default; private: OCTAVE_API void init (const time& ot); }; class gmtime : public base_tm { public: gmtime () : base_tm () { init (time ()); } gmtime (const time& ot) : base_tm () { init (ot); } OCTAVE_DEFAULT_COPY_MOVE_DELETE (gmtime) private: OCTAVE_API void init (const time& ot); }; class strptime : public base_tm { public: strptime () = delete; strptime (const std::string& str, const std::string& fmt) : base_tm (), m_nchars (0) { init (str, fmt); } OCTAVE_DEFAULT_COPY_MOVE_DELETE (strptime) int characters_converted () const { return m_nchars; } private: int m_nchars; OCTAVE_API void init (const std::string& str, const std::string& fmt); }; class cpu_time { public: friend class resource_usage; cpu_time () : m_usr_sec (0), m_sys_sec (0), m_usr_usec (0), m_sys_usec (0) { stamp (); } OCTAVE_DEFAULT_COPY_MOVE_DELETE (cpu_time) OCTAVE_API void stamp (); double user () const { return as_double (m_usr_sec, m_usr_usec); } double system () const { return as_double (m_sys_sec, m_sys_usec); } OCTAVE_TIME_T user_sec () const { return m_usr_sec; } long user_usec () const { return m_usr_usec; } OCTAVE_TIME_T system_sec () const { return m_sys_sec; } long system_usec () const { return m_sys_usec; } private: OCTAVE_TIME_T m_usr_sec; OCTAVE_TIME_T m_sys_sec; long m_usr_usec; long m_sys_usec; cpu_time (OCTAVE_TIME_T usr_sec, OCTAVE_TIME_T sys_sec, long usr_usec, long sys_usec) : m_usr_sec (usr_sec), m_sys_sec (sys_sec), m_usr_usec (usr_usec), m_sys_usec (sys_usec) { } }; class resource_usage { public: resource_usage () : m_cpu (), m_maxrss (0), m_ixrss (0), m_idrss (0), m_isrss (0), m_minflt (0), m_majflt (0), m_nswap (0), m_inblock (0), m_oublock (0), m_msgsnd (0), m_msgrcv (0), m_nsignals (0), m_nvcsw (0), m_nivcsw (0) { stamp (); } OCTAVE_DEFAULT_COPY_MOVE_DELETE (resource_usage) OCTAVE_API void stamp (); cpu_time cpu () const { return m_cpu; } long maxrss () const { return m_maxrss; } long ixrss () const { return m_ixrss; } long idrss () const { return m_idrss; } long isrss () const { return m_isrss; } long minflt () const { return m_minflt; } long majflt () const { return m_majflt; } long nswap () const { return m_nswap; } long inblock () const { return m_inblock; } long oublock () const { return m_oublock; } long msgsnd () const { return m_msgsnd; } long msgrcv () const { return m_msgrcv; } long nsignals () const { return m_nsignals; } long nvcsw () const { return m_nvcsw; } long nivcsw () const { return m_nivcsw; } private: cpu_time m_cpu; long m_maxrss; long m_ixrss; long m_idrss; long m_isrss; long m_minflt; long m_majflt; long m_nswap; long m_inblock; long m_oublock; long m_msgsnd; long m_msgrcv; long m_nsignals; long m_nvcsw; long m_nivcsw; }; // class to handle file time efficiently on different platforms class OCTAVE_API file_time { public: file_time (); file_time (OCTAVE_TIME_T t) : m_time (t) { } #if defined (OCTAVE_USE_WINDOWS_API) file_time (OCTAVE_WIN_FILETIME& t) { m_time = (static_cast<OCTAVE_TIME_T> (t.dwHighDateTime)) >> 32 | t.dwLowDateTime; } #endif file_time (const std::string& filename); file_time (const file_time& ot) { m_time = ot.time (); } file_time& operator = (const file_time& ot) { if (this != &ot) m_time = ot.time (); return *this; } ~file_time () = default; inline static file_time time_resolution () { #if defined (OCTAVE_USE_WINDOWS_API) // FAT file systems have 2 seconds resolution for the modification time. static OCTAVE_TIME_T time_resolution = 20000; #else // Assume 1 second (see file_stat) static OCTAVE_TIME_T time_resolution = 1; #endif return time_resolution; } inline bool operator == (const file_time& t2) const { return time () == t2.time (); } inline bool operator != (const file_time& t2) const { return ! (*this == t2); } inline bool operator < (const file_time& t2) const { return time () < t2.time (); } inline bool operator <= (const file_time& t2) const { return (*this < t2 || *this == t2); } inline bool operator > (const file_time& t2) const { return time () > t2.time (); } inline bool operator >= (const file_time& t2) const { return (*this > t2 || *this == t2); } inline file_time operator + (const file_time& t2) const { return file_time (time () + t2.time ()); } inline file_time operator + (const OCTAVE_TIME_T t2) const { return file_time (time () + t2); } OCTAVE_TIME_T time () const { return m_time; } private: // The native file time type differs per platform. // On POSIX, this is the number of 1 second intervals since the epoch. // On Windows, this is the number of 0.1 ms intervals since a different epoch. OCTAVE_TIME_T m_time; }; OCTAVE_END_NAMESPACE(sys) OCTAVE_END_NAMESPACE(octave) #endif