view liboctave/system/oct-time.cc @ 25438:cb1606f78f6b

prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible Using <iostream> brings with it a static initializer for the std::cin, std::cout, and std::cerr streams. In most cases they are not needed and should be avoided if possible. Files affected: build-aux/mk-opts.pl libgui/qterminal/libqterminal/win32/QWinTerminalImpl.cpp libinterp/corefcn/__dsearchn__.cc libinterp/corefcn/c-file-ptr-stream.cc libinterp/corefcn/c-file-ptr-stream.h libinterp/corefcn/daspk.cc libinterp/corefcn/dasrt.cc libinterp/corefcn/dassl.cc libinterp/corefcn/defaults.cc libinterp/corefcn/defun.cc libinterp/corefcn/file-io.cc libinterp/corefcn/ft-text-renderer.cc libinterp/corefcn/gl-render.cc libinterp/corefcn/help.cc libinterp/corefcn/ls-ascii-helper.cc libinterp/corefcn/ls-hdf5.cc libinterp/corefcn/ls-hdf5.h libinterp/corefcn/ls-mat-ascii.cc libinterp/corefcn/ls-mat4.cc libinterp/corefcn/ls-mat5.cc libinterp/corefcn/ls-oct-binary.cc libinterp/corefcn/ls-oct-text.cc libinterp/corefcn/lsode.cc libinterp/corefcn/oct-iostrm.cc libinterp/corefcn/oct-procbuf.cc libinterp/corefcn/oct-stdstrm.h libinterp/corefcn/procstream.cc libinterp/corefcn/procstream.h libinterp/corefcn/quad.cc libinterp/corefcn/symscope.h libinterp/corefcn/symtab.h libinterp/corefcn/toplev.cc libinterp/corefcn/urlwrite.cc libinterp/corefcn/utils.cc libinterp/corefcn/zfstream.cc libinterp/dldfcn/__ode15__.cc libinterp/dldfcn/convhulln.cc libinterp/octave-value/ov-base-diag.cc libinterp/octave-value/ov-base-int.cc libinterp/octave-value/ov-base-mat.cc libinterp/octave-value/ov-base-scalar.cc libinterp/octave-value/ov-base-sparse.cc libinterp/octave-value/ov-base.cc libinterp/octave-value/ov-bool-mat.cc libinterp/octave-value/ov-bool-sparse.cc libinterp/octave-value/ov-bool.cc libinterp/octave-value/ov-cell.cc libinterp/octave-value/ov-ch-mat.cc libinterp/octave-value/ov-class.cc libinterp/octave-value/ov-colon.cc libinterp/octave-value/ov-complex.cc libinterp/octave-value/ov-cs-list.cc libinterp/octave-value/ov-cx-mat.cc libinterp/octave-value/ov-cx-sparse.cc libinterp/octave-value/ov-fcn-handle.cc libinterp/octave-value/ov-fcn-inline.cc libinterp/octave-value/ov-float.cc libinterp/octave-value/ov-flt-complex.cc libinterp/octave-value/ov-flt-cx-mat.cc libinterp/octave-value/ov-flt-re-mat.cc libinterp/octave-value/ov-int16.cc libinterp/octave-value/ov-int32.cc libinterp/octave-value/ov-int64.cc libinterp/octave-value/ov-int8.cc libinterp/octave-value/ov-java.cc libinterp/octave-value/ov-range.cc libinterp/octave-value/ov-re-mat.cc libinterp/octave-value/ov-re-sparse.cc libinterp/octave-value/ov-scalar.cc libinterp/octave-value/ov-str-mat.cc libinterp/octave-value/ov-struct.cc libinterp/octave-value/ov-typeinfo.cc libinterp/octave-value/ov-uint16.cc libinterp/octave-value/ov-uint32.cc libinterp/octave-value/ov-uint64.cc libinterp/octave-value/ov-uint8.cc libinterp/octave.cc libinterp/parse-tree/bp-table.cc libinterp/parse-tree/lex.h libinterp/parse-tree/profiler.cc libinterp/parse-tree/pt-arg-list.cc libinterp/parse-tree/pt-array-list.cc libinterp/parse-tree/pt-assign.cc libinterp/parse-tree/pt-cell.cc libinterp/parse-tree/pt-const.cc libinterp/parse-tree/pt-eval.cc libinterp/parse-tree/pt-exp.cc libinterp/parse-tree/pt-fcn-handle.cc libinterp/parse-tree/pt-jit.cc libinterp/parse-tree/pt-pr-code.cc libinterp/parse-tree/pt-tm-const.cc libinterp/parse-tree/pt.cc liboctave/array/Array.cc liboctave/array/CColVector.cc liboctave/array/CDiagMatrix.cc liboctave/array/CMatrix.cc liboctave/array/CNDArray.cc liboctave/array/CRowVector.cc liboctave/array/CSparse.cc liboctave/array/DiagArray2.cc liboctave/array/MArray.cc liboctave/array/Range.cc liboctave/array/Sparse.cc liboctave/array/boolMatrix.cc liboctave/array/boolSparse.cc liboctave/array/chMatrix.cc liboctave/array/dColVector.cc liboctave/array/dDiagMatrix.cc liboctave/array/dMatrix.cc liboctave/array/dNDArray.cc liboctave/array/dRowVector.cc liboctave/array/dSparse.cc liboctave/array/fCColVector.cc liboctave/array/fCDiagMatrix.cc liboctave/array/fCMatrix.cc liboctave/array/fCNDArray.cc liboctave/array/fCRowVector.cc liboctave/array/fColVector.cc liboctave/array/fDiagMatrix.cc liboctave/array/fMatrix.cc liboctave/array/fNDArray.cc liboctave/array/fRowVector.cc liboctave/array/idx-vector.cc liboctave/numeric/CollocWt.cc liboctave/numeric/eigs-base.cc liboctave/system/file-ops.cc liboctave/system/oct-time.cc liboctave/util/cmd-hist.cc liboctave/util/data-conv.cc liboctave/util/data-conv.h liboctave/util/file-info.cc liboctave/util/lo-utils.cc liboctave/util/lo-utils.h liboctave/util/quit.cc liboctave/util/str-vec.cc liboctave/util/url-transfer.cc liboctave/util/url-transfer.h
author John W. Eaton <jwe@octave.org>
date Thu, 07 Jun 2018 10:11:54 -0400
parents 6652d3823428
children 00f796120a6d
line wrap: on
line source

/*

Copyright (C) 1999-2018 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 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 (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <cmath>
#include <ctime>

#include <iomanip>
#include <limits>
#include <ostream>

#include "lo-error.h"
#include "lo-utils.h"
#include "oct-locbuf.h"
#include "oct-time.h"
#include "octave-preserve-stream-state.h"
#include "strftime-wrapper.h"
#include "strptime-wrapper.h"
#include "time-wrappers.h"

namespace octave
{
  namespace sys
  {
    time::time (double d)
      : ot_unix_time (static_cast<time_t> (d)), ot_usec (0)
    {
      double ip;
      ot_usec = static_cast<int> (std::modf (d, &ip) * 1e6);
    }

    time::time (const base_tm& tm)
      : ot_unix_time (), ot_usec ()
    {
      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_GMTOFF)
      t.tm_gmtoff = tm.gmtoff ();
#endif

#if defined (HAVE_STRUCT_TM_TM_ZONE)
      std::string s = tm.zone ();
      char *ps = strsave (s.c_str ());
      t.tm_zone = ps;
#endif

      ot_unix_time = octave_mktime_wrapper (&t);

#if defined (HAVE_STRUCT_TM_TM_ZONE)
      delete [] ps;
#endif

      ot_usec = tm.usec ();
    }

    std::string
    time::ctime (void) const
    {
      return localtime (*this).asctime ();
    }

    std::ostream&
    operator << (std::ostream& os, const time& ot)
    {
      preserve_stream_state stream_state (os);

      os << ot.ot_unix_time << '.'
         << std::setw (6) << std::setfill ('0') << ot.ot_usec;

      return os;
    }

    void
    time::stamp (void)
    {
      octave_gettimeofday_wrapper (&ot_unix_time, &ot_usec);
    }

    // From the mktime() manual page:
    //
    //     The mktime() function converts a broken-down time structure,
    //     expressed as local time, to calendar time representation.
    //
    //     <snip>
    //
    //     If structure members are outside their legal interval, they
    //     will be normalized (so that, e.g., 40 October is changed into
    //     9 November).
    //
    // So, we no longer check limits here.

#define DEFINE_SET_FIELD_FCN(type, f, lo, hi)   \
    base_tm&                                    \
    base_tm::f (type v)                         \
    {                                           \
      m_ ## f = v;                              \
                                                \
      return *this;                             \
    }

#define DEFINE_SET_INT_FIELD_FCN(f, lo, hi)     \
    DEFINE_SET_FIELD_FCN (int, f, lo, hi)

    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, std::numeric_limits<int>::min (),
                              std::numeric_limitd<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)
    DEFINE_SET_FIELD_FCN (long, gmtoff, -86400, 0)

    base_tm&
    base_tm::zone (const std::string& s)
    {
      m_zone = s;
      return *this;
    }

#if ! defined STRFTIME_BUF_INITIAL_SIZE
#  define STRFTIME_BUF_INITIAL_SIZE 128
#endif

    std::string
    base_tm::strftime (const std::string& fmt) const
    {
      std::string retval;

      if (! fmt.empty ())
        {
          struct ::tm t;

          t.tm_sec = m_sec;
          t.tm_min = m_min;
          t.tm_hour = m_hour;
          t.tm_mday = m_mday;
          t.tm_mon = m_mon;
          t.tm_year = m_year;
          t.tm_wday = m_wday;
          t.tm_yday = m_yday;
          t.tm_isdst = m_isdst;

#if defined (HAVE_TM_GMTOFF)
          t.tm_gmtoff = m_gmtoff;
#endif

#if defined (HAVE_STRUCT_TM_TM_ZONE)
          char *ps = strsave (m_zone.c_str ());
          t.tm_zone = ps;
#endif

          const char *fmt_str = fmt.c_str ();

          char *buf = nullptr;
          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
                = octave_strftime_wrapper (buf, bufsize, fmt_str, &t);

              bufsize *= 2;
            }

#if defined (HAVE_STRUCT_TM_TM_ZONE)
          delete [] ps;
#endif

          retval = buf;

          delete [] buf;
        }

      return retval;
    }

    void
    base_tm::init (void *p)
    {
      if (! p)
        return;

      struct ::tm *t = static_cast<struct ::tm *> (p);

      m_sec = t->tm_sec;
      m_min = t->tm_min;
      m_hour = t->tm_hour;
      m_mday = t->tm_mday;
      m_mon = t->tm_mon;
      m_year = t->tm_year;
      m_wday = t->tm_wday;
      m_yday = t->tm_yday;
      m_isdst = t->tm_isdst;

#if defined (HAVE_TM_GMTOFF)
      m_gmtoff = t->tm_gmtoff;
#endif

#if defined (HAVE_STRUCT_TM_TM_ZONE)
      if (t->tm_zone)
        m_zone = t->tm_zone;
#elif defined (HAVE_TZNAME)
      if (t->tm_isdst == 0 || t->tm_isdst == 1)
        m_zone = tzname[t->tm_isdst];
#endif
    }

    void
    localtime::init (const time& ot)
    {
      m_usec = ot.usec ();

      time_t t = ot.unix_time ();

      base_tm::init (std::localtime (&t));
    }

    void
    gmtime::init (const time& ot)
    {
      m_usec = ot.usec ();

      time_t t = ot.unix_time ();

      base_tm::init (std::gmtime (&t));
    }

    void
    strptime::init (const std::string& str, const std::string& fmt)
    {
      struct ::tm t;

      t.tm_sec = 0;
      t.tm_min = 0;
      t.tm_hour = 0;
      t.tm_mday = 0;
      t.tm_mon = -1;
      t.tm_year = std::numeric_limits<int>::min ();
      t.tm_wday = 0;
      t.tm_yday = 0;
      t.tm_isdst = 0;

#if defined (HAVE_TM_GMTOFF)
      t.tm_gmtoff = 0;
#endif

#if defined (HAVE_STRUCT_TM_TM_ZONE)
      char *ps = strsave ("");
      t.tm_zone = ps;
#endif

      const char *p = str.c_str ();

      char *q = octave_strptime_wrapper (p, fmt.c_str (), &t);

      // Fill in wday and yday, but only if mday is valid and the mon and year
      // are filled in, avoiding issues with mktime and invalid dates.
      if (t.tm_mday != 0 && t.tm_mon >= 0
          && t.tm_year != std::numeric_limits<int>::min ())
        {
          t.tm_isdst = -1;
          octave_mktime_wrapper (&t);
        }

      if (t.tm_mon < 0)
        t.tm_mon = 0;

      if (t.tm_year == std::numeric_limits<int>::min ())
        t.tm_year = 0;

      if (q)
        nchars = q - p + 1;
      else
        nchars = 0;

      base_tm::init (&t);

#if defined (HAVE_STRUCT_TM_TM_ZONE)
      delete [] ps;
#endif
    }

    void
    cpu_time::stamp (void)
    {
      octave_cpu_time (&m_usr_sec, &m_sys_sec, &m_usr_usec, &m_sys_usec);
    }

    void
    resource_usage::stamp (void)
    {
      time_t usr_sec, sys_sec;
      long usr_usec, sys_usec;

      octave_getrusage_wrapper (&usr_sec, &sys_sec, &usr_usec,
                                &sys_usec, &m_maxrss, &m_ixrss,
                                &m_idrss, &m_isrss, &m_minflt,
                                &m_majflt, &m_nswap, &m_inblock,
                                &m_oublock, &m_msgsnd, &m_msgrcv,
                                &m_nsignals, &m_nvcsw, &m_nivcsw);

      m_cpu = cpu_time (usr_sec, sys_sec, usr_usec, sys_usec);
    }
  }
}