GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-time.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <cmath>
28 #include <ctime>
29 
30 #include <iomanip>
31 #include <iostream>
32 #include <limits>
33 
34 #include "lo-error.h"
35 #include "lo-utils.h"
36 #include "oct-locbuf.h"
37 #include "oct-time.h"
39 #include "strftime-wrapper.h"
40 #include "strptime-wrapper.h"
41 #include "time-wrappers.h"
42 
43 namespace octave
44 {
45  namespace sys
46  {
47  time::time (double d)
48  : ot_unix_time (static_cast<time_t> (d)), ot_usec (0)
49  {
50  double ip;
51  ot_usec = static_cast<int> (std::modf (d, &ip) * 1e6);
52  }
53 
54  time::time (const base_tm& tm)
55  : ot_unix_time (), ot_usec ()
56  {
57  struct ::tm t;
58 
59  t.tm_sec = tm.sec ();
60  t.tm_min = tm.min ();
61  t.tm_hour = tm.hour ();
62  t.tm_mday = tm.mday ();
63  t.tm_mon = tm.mon ();
64  t.tm_year = tm.year ();
65  t.tm_wday = tm.wday ();
66  t.tm_yday = tm.yday ();
67  t.tm_isdst = tm.isdst ();
68 
69 #if defined (HAVE_TM_GMTOFF)
70  t.tm_gmtoff = tm.gmtoff ();
71 #endif
72 
73 #if defined (HAVE_STRUCT_TM_TM_ZONE)
74  std::string s = tm.zone ();
75  char *ps = strsave (s.c_str ());
76  t.tm_zone = ps;
77 #endif
78 
80 
81 #if defined (HAVE_STRUCT_TM_TM_ZONE)
82  delete [] ps;
83 #endif
84 
85  ot_usec = tm.usec ();
86  }
87 
89  time::ctime (void) const
90  {
91  return localtime (*this).asctime ();
92  }
93 
94  std::ostream&
95  operator << (std::ostream& os, const time& ot)
96  {
97  preserve_stream_state stream_state (os);
98 
99  os << ot.ot_unix_time << '.'
100  << std::setw (6) << std::setfill ('0') << ot.ot_usec;
101 
102  return os;
103  }
104 
105  void
106  time::stamp (void)
107  {
109  }
110 
111  // From the mktime() manual page:
112  //
113  // The mktime() function converts a broken-down time structure,
114  // expressed as local time, to calendar time representation.
115  //
116  // <snip>
117  //
118  // If structure members are outside their legal interval, they
119  // will be normalized (so that, e.g., 40 October is changed into
120  // 9 November).
121  //
122  // So, we no longer check limits here.
123 
124 #define DEFINE_SET_FIELD_FCN(type, f, lo, hi) \
125  base_tm& \
126  base_tm::f (type v) \
127  { \
128  m_ ## f = v; \
129  \
130  return *this; \
131  }
132 
133 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \
134  DEFINE_SET_FIELD_FCN (int, f, lo, hi)
135 
136  DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000)
142  DEFINE_SET_INT_FIELD_FCN (year, std::numeric_limits<int>::min (),
143  std::numeric_limitd<int>::max ())
147  DEFINE_SET_FIELD_FCN (long, gmtoff, -86400, 0)
148 
149  base_tm&
150  base_tm::zone (const std::string& s)
151  {
152  m_zone = s;
153  return *this;
154  }
155 
156 #if ! defined STRFTIME_BUF_INITIAL_SIZE
157 # define STRFTIME_BUF_INITIAL_SIZE 128
158 #endif
159 
161  base_tm::strftime (const std::string& fmt) const
162  {
164 
165  if (! fmt.empty ())
166  {
167  struct ::tm t;
168 
169  t.tm_sec = m_sec;
170  t.tm_min = m_min;
171  t.tm_hour = m_hour;
172  t.tm_mday = m_mday;
173  t.tm_mon = m_mon;
174  t.tm_year = m_year;
175  t.tm_wday = m_wday;
176  t.tm_yday = m_yday;
177  t.tm_isdst = m_isdst;
178 
179 #if defined (HAVE_TM_GMTOFF)
180  t.tm_gmtoff = m_gmtoff;
181 #endif
182 
183 #if defined (HAVE_STRUCT_TM_TM_ZONE)
184  char *ps = strsave (m_zone.c_str ());
185  t.tm_zone = ps;
186 #endif
187 
188  const char *fmt_str = fmt.c_str ();
189 
190  char *buf = nullptr;
191  size_t bufsize = STRFTIME_BUF_INITIAL_SIZE;
192  size_t chars_written = 0;
193 
194  while (chars_written == 0)
195  {
196  delete [] buf;
197  buf = new char [bufsize];
198  buf[0] = '\0';
199 
200  chars_written
201  = octave_strftime_wrapper (buf, bufsize, fmt_str, &t);
202 
203  bufsize *= 2;
204  }
205 
206 #if defined (HAVE_STRUCT_TM_TM_ZONE)
207  delete [] ps;
208 #endif
209 
210  retval = buf;
211 
212  delete [] buf;
213  }
214 
215  return retval;
216  }
217 
218  void
219  base_tm::init (void *p)
220  {
221  if (! p)
222  return;
223 
224  struct ::tm *t = static_cast<struct ::tm *> (p);
225 
226  m_sec = t->tm_sec;
227  m_min = t->tm_min;
228  m_hour = t->tm_hour;
229  m_mday = t->tm_mday;
230  m_mon = t->tm_mon;
231  m_year = t->tm_year;
232  m_wday = t->tm_wday;
233  m_yday = t->tm_yday;
234  m_isdst = t->tm_isdst;
235 
236 #if defined (HAVE_TM_GMTOFF)
237  m_gmtoff = t->tm_gmtoff;
238 #endif
239 
240 #if defined (HAVE_STRUCT_TM_TM_ZONE)
241  if (t->tm_zone)
242  m_zone = t->tm_zone;
243 #elif defined (HAVE_TZNAME)
244  if (t->tm_isdst == 0 || t->tm_isdst == 1)
245  m_zone = tzname[t->tm_isdst];
246 #endif
247  }
248 
249  void
250  localtime::init (const time& ot)
251  {
252  m_usec = ot.usec ();
253 
254  time_t t = ot.unix_time ();
255 
256  base_tm::init (std::localtime (&t));
257  }
258 
259  void
260  gmtime::init (const time& ot)
261  {
262  m_usec = ot.usec ();
263 
264  time_t t = ot.unix_time ();
265 
266  base_tm::init (std::gmtime (&t));
267  }
268 
269  void
271  {
272  struct ::tm t;
273 
274  t.tm_sec = 0;
275  t.tm_min = 0;
276  t.tm_hour = 0;
277  t.tm_mday = 0;
278  t.tm_mon = -1;
279  t.tm_year = std::numeric_limits<int>::min ();
280  t.tm_wday = 0;
281  t.tm_yday = 0;
282  t.tm_isdst = 0;
283 
284 #if defined (HAVE_TM_GMTOFF)
285  t.tm_gmtoff = 0;
286 #endif
287 
288 #if defined (HAVE_STRUCT_TM_TM_ZONE)
289  char *ps = strsave ("");
290  t.tm_zone = ps;
291 #endif
292 
293  const char *p = str.c_str ();
294 
295  char *q = octave_strptime_wrapper (p, fmt.c_str (), &t);
296 
297  // Fill in wday and yday, but only if mday is valid and the mon and year
298  // are filled in, avoiding issues with mktime and invalid dates.
299  if (t.tm_mday != 0 && t.tm_mon >= 0
300  && t.tm_year != std::numeric_limits<int>::min ())
301  {
302  t.tm_isdst = -1;
304  }
305 
306  if (t.tm_mon < 0)
307  t.tm_mon = 0;
308 
309  if (t.tm_year == std::numeric_limits<int>::min ())
310  t.tm_year = 0;
311 
312  if (q)
313  nchars = q - p + 1;
314  else
315  nchars = 0;
316 
317  base_tm::init (&t);
318 
319 #if defined (HAVE_STRUCT_TM_TM_ZONE)
320  delete [] ps;
321 #endif
322  }
323 
324  void
326  {
328  }
329 
330  void
332  {
333  time_t usr_sec, sys_sec;
334  long usr_usec, sys_usec;
335 
336  octave_getrusage_wrapper (&usr_sec, &sys_sec, &usr_usec,
337  &sys_usec, &m_maxrss, &m_ixrss,
338  &m_idrss, &m_isrss, &m_minflt,
342 
343  m_cpu = cpu_time (usr_sec, sys_sec, usr_usec, sys_usec);
344  }
345  }
346 }
for fields that display a single padding can be changed or inhibited by following the hour(hh:mm:ss [AP]M). tem %R Time
std::ostream & operator<<(std::ostream &os, const time &ot)
Definition: oct-time.cc:95
std::string asctime(void) const
Definition: oct-time.h:254
int hour(void) const
Definition: oct-time.h:229
#define DEFINE_SET_INT_FIELD_FCN(f, lo, hi)
Definition: oct-time.cc:133
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are create an empty structure array with the specified field names If the argument is an return the underlying struct Observe that the syntax is optimized for struct trong struct("foo", 1) esult
Definition: ov-struct.cc:1736
void init(const time &ot)
Definition: oct-time.cc:250
time_t ot_unix_time
Definition: oct-time.h:121
void init(void *p)
Definition: oct-time.cc:219
STL namespace.
int year(void) const
Definition: oct-time.h:232
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
s
Definition: file-io.cc:2729
int octave_getrusage_wrapper(time_t *usr_sec, time_t *sys_sec, long *usr_usec, long *sys_usec, long *maxrss, long *ixrss, long *idrss, long *isrss, long *minflt, long *majflt, long *nswap, long *inblock, long *oublock, long *msgsnd, long *msgrcv, long *nsignals, long *nvcsw, long *nivcsw)
Definition: time-wrappers.c:88
int sec(void) const
Definition: oct-time.h:227
int octave_gettimeofday_wrapper(time_t *sec, long *usec)
Definition: time-wrappers.c:39
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
time_t octave_mktime_wrapper(struct tm *tp)
long usec(void) const
Definition: oct-time.h:112
std::string strftime(const std::string &fmt) const
Definition: oct-time.cc:161
int octave_cpu_time(time_t *usr_sec, time_t *sys_sec, long *usr_usec, long *sys_usec)
Definition: time-wrappers.c:60
std::string zone(void) const
Definition: oct-time.h:237
int yday(void) const
Definition: oct-time.h:234
char * octave_strptime_wrapper(const char *p, const char *fmt, struct tm *t)
void init(const std::string &str, const std::string &fmt)
Definition: oct-time.cc:270
int min(void) const
Definition: oct-time.h:228
std::string str
Definition: hash.cc:118
octave_value retval
Definition: data.cc:6246
int mon(void) const
Definition: oct-time.h:231
time_t unix_time(void) const
Definition: oct-time.h:110
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:227
static char * strsave(const char *s)
Definition: main.in.cc:190
#define DEFINE_SET_FIELD_FCN(type, f, lo, hi)
Definition: oct-time.cc:124
p
Definition: lu.cc:138
std::string ctime(void) const
Definition: oct-time.cc:89
int wday(void) const
Definition: oct-time.h:233
#define STRFTIME_BUF_INITIAL_SIZE
Definition: oct-time.cc:157
long gmtoff(void) const
Definition: oct-time.h:236
int usec(void) const
Definition: oct-time.h:226
std::string m_zone
Definition: oct-time.h:293
void stamp(void)
Definition: oct-time.cc:106
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
void init(const time &ot)
Definition: oct-time.cc:260
int mday(void) const
Definition: oct-time.h:230
octave::stream os
Definition: file-io.cc:627
int isdst(void) const
Definition: oct-time.h:235
size_t octave_strftime_wrapper(char *buf, size_t len, const char *fmt, const struct tm *t)
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:204