GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-time.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <limits>
28 
29 #include <ctime>
30 
31 #include "lo-error.h"
32 #include "lo-math.h"
33 #include "lo-utils.h"
34 #include "oct-locbuf.h"
35 #include "oct-time.h"
36 #include "strftime-wrapper.h"
37 #include "strptime-wrapper.h"
38 #include "time-wrappers.h"
39 
40 namespace octave
41 {
42  namespace sys
43  {
44  time::time (double d)
45  : ot_unix_time (static_cast<time_t> (d)), ot_usec (0)
46  {
47  double ip;
48  ot_usec = static_cast<int> (std::modf (d, &ip) * 1e6);
49  }
50 
51  time::time (const base_tm& tm)
52  : ot_unix_time (), ot_usec ()
53  {
54  struct ::tm t;
55 
56  t.tm_sec = tm.sec ();
57  t.tm_min = tm.min ();
58  t.tm_hour = tm.hour ();
59  t.tm_mday = tm.mday ();
60  t.tm_mon = tm.mon ();
61  t.tm_year = tm.year ();
62  t.tm_wday = tm.wday ();
63  t.tm_yday = tm.yday ();
64  t.tm_isdst = tm.isdst ();
65 
66 #if defined (HAVE_TM_GMTOFF)
67  t.tm_gmtoff = tm.gmtoff ();
68 #endif
69 
70 #if defined (HAVE_STRUCT_TM_TM_ZONE)
71  std::string s = tm.zone ();
72  char *ps = strsave (s.c_str ());
73  t.tm_zone = ps;
74 #endif
75 
77 
78 #if defined (HAVE_STRUCT_TM_TM_ZONE)
79  delete [] ps;
80 #endif
81 
82  ot_usec = tm.usec ();
83  }
84 
86  time::ctime (void) const
87  {
88  return localtime (*this).asctime ();
89  }
90 
91  void
92  time::stamp (void)
93  {
95  }
96 
97  // From the mktime() manual page:
98  //
99  // The mktime() function converts a broken-down time structure,
100  // expressed as local time, to calendar time representation.
101  //
102  // <snip>
103  //
104  // If structure members are outside their legal interval, they
105  // will be normalized (so that, e.g., 40 October is changed into
106  // 9 November).
107  //
108  // So, we no longer check limits here.
109 
110 #define DEFINE_SET_FIELD_FCN(type, f, lo, hi) \
111  base_tm& \
112  base_tm::f (type v) \
113  { \
114  m_ ## f = v; \
115  \
116  return *this; \
117  }
118 
119 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \
120  DEFINE_SET_FIELD_FCN (int, f, lo, hi)
121 
122  DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000)
128  DEFINE_SET_INT_FIELD_FCN (year, std::numeric_limits<int>::min (),
129  std::numeric_limitd<int>::max ())
133  DEFINE_SET_FIELD_FCN (long, gmtoff, -86400, 0)
134 
135  base_tm&
136  base_tm::zone (const std::string& s)
137  {
138  m_zone = s;
139  return *this;
140  }
141 
142 #if ! defined STRFTIME_BUF_INITIAL_SIZE
143 # define STRFTIME_BUF_INITIAL_SIZE 128
144 #endif
145 
147  base_tm::strftime (const std::string& fmt) const
148  {
150 
151  if (! fmt.empty ())
152  {
153  struct ::tm t;
154 
155  t.tm_sec = m_sec;
156  t.tm_min = m_min;
157  t.tm_hour = m_hour;
158  t.tm_mday = m_mday;
159  t.tm_mon = m_mon;
160  t.tm_year = m_year;
161  t.tm_wday = m_wday;
162  t.tm_yday = m_yday;
163  t.tm_isdst = m_isdst;
164 
165 #if defined (HAVE_TM_GMTOFF)
166  t.tm_gmtoff = m_gmtoff;
167 #endif
168 
169 #if defined (HAVE_STRUCT_TM_TM_ZONE)
170  char *ps = strsave (m_zone.c_str ());
171  t.tm_zone = ps;
172 #endif
173 
174  const char *fmt_str = fmt.c_str ();
175 
176  char *buf = 0;
177  size_t bufsize = STRFTIME_BUF_INITIAL_SIZE;
178  size_t chars_written = 0;
179 
180  while (chars_written == 0)
181  {
182  delete [] buf;
183  buf = new char [bufsize];
184  buf[0] = '\0';
185 
186  chars_written
187  = octave_strftime_wrapper (buf, bufsize, fmt_str, &t, 0, 0);
188 
189  bufsize *= 2;
190  }
191 
192 #if defined (HAVE_STRUCT_TM_TM_ZONE)
193  delete [] ps;
194 #endif
195 
196  retval = buf;
197 
198  delete [] buf;
199  }
200 
201  return retval;
202  }
203 
204  void
205  base_tm::init (void *p)
206  {
207  if (! p)
208  return;
209 
210  struct ::tm *t = static_cast<struct ::tm*> (p);
211 
212  m_sec = t->tm_sec;
213  m_min = t->tm_min;
214  m_hour = t->tm_hour;
215  m_mday = t->tm_mday;
216  m_mon = t->tm_mon;
217  m_year = t->tm_year;
218  m_wday = t->tm_wday;
219  m_yday = t->tm_yday;
220  m_isdst = t->tm_isdst;
221 
222 #if defined (HAVE_TM_GMTOFF)
223  m_gmtoff = t->tm_gmtoff;
224 #endif
225 
226 #if defined (HAVE_STRUCT_TM_TM_ZONE)
227  if (t->tm_zone)
228  m_zone = t->tm_zone;
229 #elif defined (HAVE_TZNAME)
230  if (t->tm_isdst == 0 || t->tm_isdst == 1)
231  m_zone = tzname[t->tm_isdst];
232 #endif
233  }
234 
235  void
236  localtime::init (const time& ot)
237  {
238  m_usec = ot.usec ();
239 
240  time_t t = ot.unix_time ();
241 
242  base_tm::init (std::localtime (&t));
243  }
244 
245  void
246  gmtime::init (const time& ot)
247  {
248  m_usec = ot.usec ();
249 
250  time_t t = ot.unix_time ();
251 
252  base_tm::init (std::gmtime (&t));
253  }
254 
255  void
257  {
258  struct ::tm t;
259 
260  t.tm_sec = 0;
261  t.tm_min = 0;
262  t.tm_hour = 0;
263  t.tm_mday = 0;
264  t.tm_mon = -1;
265  t.tm_year = std::numeric_limits<int>::min ();
266  t.tm_wday = 0;
267  t.tm_yday = 0;
268  t.tm_isdst = 0;
269 
270 #if defined (HAVE_TM_GMTOFF)
271  t.tm_gmtoff = 0;
272 #endif
273 
274 #if defined (HAVE_STRUCT_TM_TM_ZONE)
275  char *ps = strsave ("");
276  t.tm_zone = ps;
277 #endif
278 
279  const char *p = str.c_str ();
280 
281  char *q = octave_strptime_wrapper (p, fmt.c_str (), &t);
282 
283  // Fill in wday and yday, but only if mday is valid and the mon and year
284  // are filled in, avoiding issues with mktime and invalid dates.
285  if (t.tm_mday != 0 && t.tm_mon >= 0
286  && t.tm_year != std::numeric_limits<int>::min ())
287  {
288  t.tm_isdst = -1;
290  }
291 
292  if (t.tm_mon < 0)
293  t.tm_mon = 0;
294 
295  if (t.tm_year == std::numeric_limits<int>::min ())
296  t.tm_year = 0;
297 
298  if (q)
299  nchars = q - p + 1;
300  else
301  nchars = 0;
302 
303  base_tm::init (&t);
304 
305 #if defined (HAVE_STRUCT_TM_TM_ZONE)
306  delete [] ps;
307 #endif
308  }
309 
310  void
312  {
314  }
315 
316  void
318  {
319  time_t usr_sec, sys_sec;
320  long usr_usec, sys_usec;
321 
322  octave_getrusage_wrapper (&usr_sec, &sys_sec, &usr_usec,
323  &sys_usec, &m_maxrss, &m_ixrss,
324  &m_idrss, &m_isrss, &m_minflt,
328 
329  m_cpu = cpu_time (usr_sec, sys_sec, usr_usec, sys_usec);
330  }
331  }
332 }
for fields that display a single padding can be changed or inhibited by following the hour(hh:mm:ss[AP]M).tem%R Time
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
time_t unix_time(void) const
Definition: oct-time.h:95
#define DEFINE_SET_INT_FIELD_FCN(f, lo, hi)
Definition: oct-time.cc:119
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:1688
int mday(void) const
Definition: oct-time.h:213
std::string asctime(void) const
Definition: oct-time.h:237
void init(const time &ot)
Definition: oct-time.cc:236
int min(void) const
Definition: oct-time.h:211
int sec(void) const
Definition: oct-time.h:210
static char * strsave(const char *s)
Definition: main.cc:183
int isdst(void) const
Definition: oct-time.h:218
time_t ot_unix_time
Definition: oct-time.h:104
void init(void *p)
Definition: oct-time.cc:205
STL namespace.
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:935
int yday(void) const
Definition: oct-time.h:217
s
Definition: file-io.cc:2682
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 octave_gettimeofday_wrapper(time_t *sec, long *usec)
Definition: time-wrappers.c:39
size_t octave_strftime_wrapper(char *buf, size_t len, const char *fmt, const struct tm *t, struct tm_zone *tz, int ns)
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
time_t octave_mktime_wrapper(struct tm *tp)
int octave_cpu_time(time_t *usr_sec, time_t *sys_sec, long *usr_usec, long *sys_usec)
Definition: time-wrappers.c:60
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:256
std::string str
Definition: hash.cc:118
std::string zone(void) const
Definition: oct-time.h:220
int wday(void) const
Definition: oct-time.h:216
octave_value retval
Definition: data.cc:6294
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
int year(void) const
Definition: oct-time.h:215
#define DEFINE_SET_FIELD_FCN(type, f, lo, hi)
Definition: oct-time.cc:110
int mon(void) const
Definition: oct-time.h:214
p
Definition: lu.cc:138
long gmtoff(void) const
Definition: oct-time.h:219
#define STRFTIME_BUF_INITIAL_SIZE
Definition: oct-time.cc:143
std::string m_zone
Definition: oct-time.h:276
int hour(void) const
Definition: oct-time.h:212
void stamp(void)
Definition: oct-time.cc:92
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:854
void init(const time &ot)
Definition: oct-time.cc:246
int usec(void) const
Definition: oct-time.h:209
std::string ctime(void) const
Definition: oct-time.cc:86
std::string strftime(const std::string &fmt) const
Definition: oct-time.cc:147
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:205
long usec(void) const
Definition: oct-time.h:97