Mercurial > octave
annotate liboctave/system/oct-time.cc @ 21888:00f1249f2483
create wrappers for some gnulib functions
Begin moving toward a clean separation between Octave and gnulib
sources.
* liboctave/wrappers/canonicalize-file-name-wrapper.c,
liboctave/wrappers/canonicalize-file-name-wrapper.h,
liboctave/wrappers/gen-tempname-wrapper.c,
liboctave/wrappers/gen-tempname-wrapper.h,
liboctave/wrappers/mkostemp-wrapper.c,
liboctave/wrappers/mkostemp-wrapper.h,
liboctave/wrappers/nanosleep-wrapper.c,
liboctave/wrappers/nanosleep-wrapper.h,
liboctave/wrappers/nproc-wrapper.c,
liboctave/wrappers/nproc-wrapper.h,
liboctave/wrappers/putenv-wrapper.c,
liboctave/wrappers/putenv-wrapper.h,
liboctave/wrappers/strftime-wrapper.c,
liboctave/wrappers/strftime-wrapper.h,
liboctave/wrappers/strptime-wrapper.c,
liboctave/wrappers/strptime-wrapper.h,
liboctave/wrappers/unsetenv-wrapper.c,
liboctave/wrappers/unsetenv-wrapper.h,
liboctave/wrappers/vasprintf-wrapper.c,
liboctave/wrappers/vasprintf-wrapper.h,
liboctave/wrappers/module.mk: New files.
* liboctave/module.mk, changed libinterp/module.mk: Update.
* file-io.cc, load-save.cc, sysdep.cc, utils.cc, file-ops.cc,
oct-time.cc, lo-utils.cc: Include wrapper headers and use wrapper
functions instead of calling gnulib functions directly.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 13 Jun 2016 16:01:26 -0400 |
parents | 25493c55d976 |
children | f0c5dd1ea2b9 |
rev | line source |
---|---|
3253 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19375
diff
changeset
|
3 Copyright (C) 1999-2015 John W. Eaton |
3253 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
3253 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
3253 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21562
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21218
diff
changeset
|
24 # include "config.h" |
3253 | 25 #endif |
26 | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
27 #include <limits> |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
28 |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
29 #include <ctime> |
3253 | 30 |
10240
fa7b5751730c
use gnulib time, sys_time, and sys_times modules
John W. Eaton <jwe@octave.org>
parents:
10239
diff
changeset
|
31 #include <sys/time.h> |
3607 | 32 |
3253 | 33 #include "lo-error.h" |
7231 | 34 #include "lo-math.h" |
3253 | 35 #include "lo-utils.h" |
13975
16158606112d
avoid memory error in strptime
John W. Eaton <jwe@octave.org>
parents:
13974
diff
changeset
|
36 #include "oct-locbuf.h" |
3253 | 37 #include "oct-time.h" |
21888
00f1249f2483
create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents:
21852
diff
changeset
|
38 #include "strftime-wrapper.h" |
00f1249f2483
create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents:
21852
diff
changeset
|
39 #include "strptime-wrapper.h" |
3253 | 40 |
21734
11af9c03400c
write namespace keyword and name on same line in C++ files
John W. Eaton <jwe@octave.org>
parents:
21730
diff
changeset
|
41 namespace octave |
3253 | 42 { |
21734
11af9c03400c
write namespace keyword and name on same line in C++ files
John W. Eaton <jwe@octave.org>
parents:
21730
diff
changeset
|
43 namespace sys |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
44 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
45 time::time (double d) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
46 : ot_unix_time (static_cast<time_t> (d)), ot_usec (0) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
47 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
48 double ip; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
49 ot_usec = static_cast<int> (std::modf (d, &ip) * 1e6); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
50 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
52 time::time (const base_tm& tm) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
53 : ot_unix_time (), ot_usec () |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
54 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
55 struct ::tm t; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
56 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
57 t.tm_sec = tm.sec (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
58 t.tm_min = tm.min (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
59 t.tm_hour = tm.hour (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
60 t.tm_mday = tm.mday (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
61 t.tm_mon = tm.mon (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
62 t.tm_year = tm.year (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
63 t.tm_wday = tm.wday (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
64 t.tm_yday = tm.yday (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
65 t.tm_isdst = tm.isdst (); |
13984
1126c2907878
avoid accessing tm_gmtoff from struct tm unless it is present
John W. Eaton <jwe@octave.org>
parents:
13982
diff
changeset
|
66 |
20375
31f89b12aaf7
strftime: Add support for "%z" conversion specifier (bug #45592)
Mike Miller <mtmiller@octave.org>
parents:
19697
diff
changeset
|
67 #if defined (HAVE_TM_GMTOFF) |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
68 t.tm_gmtoff = tm.gmtoff (); |
13984
1126c2907878
avoid accessing tm_gmtoff from struct tm unless it is present
John W. Eaton <jwe@octave.org>
parents:
13982
diff
changeset
|
69 #endif |
3253 | 70 |
3887 | 71 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
72 std::string s = tm.zone (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
73 char *ps = strsave (s.c_str ()); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
74 t.tm_zone = ps; |
3253 | 75 #endif |
76 | |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
77 ot_unix_time = gnulib::mktime (&t); |
3253 | 78 |
3887 | 79 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
80 delete [] ps; |
3344 | 81 #endif |
82 | |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
83 ot_usec = tm.usec (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
84 } |
3253 | 85 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
86 std::string |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
87 time::ctime (void) const |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
88 { |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21734
diff
changeset
|
89 return localtime (*this).asctime (); |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
90 } |
3255 | 91 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
92 void |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
93 time::stamp (void) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
94 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
95 struct ::timeval tp; |
3253 | 96 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
97 gnulib::gettimeofday (&tp, 0); |
3253 | 98 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
99 ot_unix_time = tp.tv_sec; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
100 ot_usec = tp.tv_usec; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
101 } |
3253 | 102 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
103 // From the mktime() manual page: |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
104 // |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
105 // The mktime() function converts a broken-down time structure, |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
106 // expressed as local time, to calendar time representation. |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
107 // |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
108 // <snip> |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
109 // |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
110 // If structure members are outside their legal interval, they |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
111 // will be normalized (so that, e.g., 40 October is changed into |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
112 // 9 November). |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
113 // |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
114 // So, we no longer check limits here. |
3736 | 115 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
116 #define DEFINE_SET_FIELD_FCN(type, f, lo, hi) \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
117 base_tm& \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
118 base_tm::f (type v) \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
119 { \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
120 m_ ## f = v; \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
121 \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
122 return *this; \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
123 } |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
124 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
125 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
126 DEFINE_SET_FIELD_FCN (int, f, lo, hi) |
3253 | 127 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
128 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
129 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
130 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
131 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
132 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
133 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
134 DEFINE_SET_INT_FIELD_FCN (year, std::numeric_limits<int>::min (), |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
135 std::numeric_limitd<int>::max ()) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
136 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
137 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
138 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
139 DEFINE_SET_FIELD_FCN (long, gmtoff, -86400, 0) |
3253 | 140 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
141 base_tm& |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
142 base_tm::zone (const std::string& s) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
143 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
144 m_zone = s; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
145 return *this; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
146 } |
3253 | 147 |
20791
f7084eae3318
maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents:
20375
diff
changeset
|
148 #if ! defined STRFTIME_BUF_INITIAL_SIZE |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
20791
diff
changeset
|
149 # define STRFTIME_BUF_INITIAL_SIZE 128 |
3253 | 150 #endif |
151 | |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
152 std::string |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
153 base_tm::strftime (const std::string& fmt) const |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
154 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
155 std::string retval; |
3253 | 156 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
157 if (! fmt.empty ()) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
158 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
159 struct ::tm t; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
160 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
161 t.tm_sec = m_sec; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
162 t.tm_min = m_min; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
163 t.tm_hour = m_hour; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
164 t.tm_mday = m_mday; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
165 t.tm_mon = m_mon; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
166 t.tm_year = m_year; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
167 t.tm_wday = m_wday; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
168 t.tm_yday = m_yday; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
169 t.tm_isdst = m_isdst; |
3253 | 170 |
20375
31f89b12aaf7
strftime: Add support for "%z" conversion specifier (bug #45592)
Mike Miller <mtmiller@octave.org>
parents:
19697
diff
changeset
|
171 #if defined (HAVE_TM_GMTOFF) |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
172 t.tm_gmtoff = m_gmtoff; |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
173 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
174 |
3887 | 175 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
176 char *ps = strsave (m_zone.c_str ()); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
177 t.tm_zone = ps; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
178 #endif |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
179 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
180 const char *fmt_str = fmt.c_str (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
181 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
182 char *buf = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
183 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
184 size_t chars_written = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
185 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
186 while (chars_written == 0) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
187 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
188 delete [] buf; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
189 buf = new char [bufsize]; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
190 buf[0] = '\0'; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
191 |
21888
00f1249f2483
create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents:
21852
diff
changeset
|
192 chars_written |
00f1249f2483
create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents:
21852
diff
changeset
|
193 = octave_strftime_wrapper (buf, bufsize, fmt_str, &t, 0, 0); |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
194 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
195 bufsize *= 2; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
196 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
197 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
198 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
199 delete [] ps; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
200 #endif |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
201 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
202 retval = buf; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
203 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
204 delete [] buf; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
205 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
206 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
207 return retval; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
208 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
209 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
210 void |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
211 base_tm::init (void *p) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
212 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
213 if (! p) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
214 return; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
215 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
216 struct ::tm *t = static_cast<struct ::tm*> (p); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
217 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
218 m_sec = t->tm_sec; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
219 m_min = t->tm_min; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
220 m_hour = t->tm_hour; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
221 m_mday = t->tm_mday; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
222 m_mon = t->tm_mon; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
223 m_year = t->tm_year; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
224 m_wday = t->tm_wday; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
225 m_yday = t->tm_yday; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
226 m_isdst = t->tm_isdst; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
227 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
228 #if defined (HAVE_TM_GMTOFF) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
229 m_gmtoff = t->tm_gmtoff; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
230 #endif |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
231 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
232 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
233 if (t->tm_zone) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
234 m_zone = t->tm_zone; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
235 #elif defined (HAVE_TZNAME) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
236 if (t->tm_isdst == 0 || t->tm_isdst == 1) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
237 m_zone = tzname[t->tm_isdst]; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
238 #endif |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
239 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
240 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
241 void |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
242 localtime::init (const time& ot) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
243 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
244 m_usec = ot.usec (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
245 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
246 time_t t = ot.unix_time (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
247 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
248 base_tm::init (gnulib::localtime (&t)); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
249 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
250 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
251 void |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
252 gmtime::init (const time& ot) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
253 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
254 m_usec = ot.usec (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
255 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
256 time_t t = ot.unix_time (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
257 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
258 base_tm::init (gnulib::gmtime (&t)); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
259 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
260 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
261 void |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
262 strptime::init (const std::string& str, const std::string& fmt) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
263 { |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
264 struct ::tm t; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
265 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
266 t.tm_sec = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
267 t.tm_min = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
268 t.tm_hour = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
269 t.tm_mday = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
270 t.tm_mon = -1; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
271 t.tm_year = std::numeric_limits<int>::min (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
272 t.tm_wday = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
273 t.tm_yday = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
274 t.tm_isdst = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
275 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
276 #if defined (HAVE_TM_GMTOFF) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
277 t.tm_gmtoff = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
278 #endif |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
279 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
280 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
281 char *ps = strsave (""); |
3709 | 282 t.tm_zone = ps; |
3253 | 283 #endif |
284 | |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
285 const char *p = str.c_str (); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
286 |
21888
00f1249f2483
create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents:
21852
diff
changeset
|
287 char *q = octave_strptime_wrapper (p, fmt.c_str (), &t); |
3253 | 288 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
289 // Fill in wday and yday, but only if mday is valid and the mon and year |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
290 // are filled in, avoiding issues with mktime and invalid dates. |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
291 if (t.tm_mday != 0 && t.tm_mon >= 0 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
292 && t.tm_year != std::numeric_limits<int>::min ()) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
293 { |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
294 t.tm_isdst = -1; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
295 gnulib::mktime (&t); |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
296 } |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
297 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
298 if (t.tm_mon < 0) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
299 t.tm_mon = 0; |
3253 | 300 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
301 if (t.tm_year == std::numeric_limits<int>::min ()) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
302 t.tm_year = 0; |
3253 | 303 |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
304 if (q) |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
305 nchars = q - p + 1; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
306 else |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
307 nchars = 0; |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
308 |
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
309 base_tm::init (&t); |
3253 | 310 |
3887 | 311 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 312 delete [] ps; |
3344 | 313 #endif |
3709 | 314 } |
21730
30c53770f47e
use namespace for system time classes
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
315 } |
3253 | 316 } |