Mercurial > hg > octave-lyh
annotate liboctave/system/oct-time.cc @ 17105:c2b2c7ddf93c
shading.m: Don't apply shading to contour hggroups.
* scripts/plot/shading.m: Search through graphic handle
hierarchy and apply shading to patch and surface objects
which are *NOT* in contour hggroups.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 28 Jul 2013 17:15:37 -0700 |
parents | 648dabbb4c6b |
children |
rev | line source |
---|---|
3253 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13984
diff
changeset
|
3 Copyright (C) 1999-2012 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 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
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 #include <sys/types.h> |
33 #include <unistd.h> | |
34 | |
10279 | 35 #include "strftime.h" |
9946 | 36 |
3253 | 37 #include "lo-error.h" |
7231 | 38 #include "lo-math.h" |
3253 | 39 #include "lo-utils.h" |
13975
16158606112d
avoid memory error in strptime
John W. Eaton <jwe@octave.org>
parents:
13974
diff
changeset
|
40 #include "oct-locbuf.h" |
3253 | 41 #include "oct-time.h" |
42 | |
43 octave_time::octave_time (const octave_base_tm& tm) | |
11501
331fcc41ca23
data member initialization fixes
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
44 : ot_unix_time (), ot_usec () |
3253 | 45 { |
46 struct tm t; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
47 |
3253 | 48 t.tm_sec = tm.sec (); |
49 t.tm_min = tm.min (); | |
50 t.tm_hour = tm.hour (); | |
51 t.tm_mday = tm.mday (); | |
52 t.tm_mon = tm.mon (); | |
53 t.tm_year = tm.year (); | |
54 t.tm_wday = tm.wday (); | |
55 t.tm_yday = tm.yday (); | |
56 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
|
57 |
1126c2907878
avoid accessing tm_gmtoff from struct tm unless it is present
John W. Eaton <jwe@octave.org>
parents:
13982
diff
changeset
|
58 #if defined (HAVE_STRUCT_TM_GMTOFF) |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
59 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
|
60 #endif |
3253 | 61 |
3887 | 62 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3504 | 63 std::string s = tm.zone (); |
3344 | 64 char *ps = strsave (s.c_str ()); |
65 t.tm_zone = ps; | |
3253 | 66 #endif |
67 | |
11005
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
68 ot_unix_time = gnulib::mktime (&t); |
3253 | 69 |
3887 | 70 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3344 | 71 delete [] ps; |
72 #endif | |
73 | |
3253 | 74 ot_usec = tm.usec (); |
75 } | |
76 | |
3504 | 77 std::string |
3255 | 78 octave_time::ctime (void) const |
79 { | |
80 return octave_localtime (*this) . asctime (); | |
81 } | |
82 | |
3253 | 83 void |
84 octave_time::stamp (void) | |
85 { | |
86 struct timeval tp; | |
87 | |
10411 | 88 gnulib::gettimeofday (&tp, 0); |
3253 | 89 |
90 ot_unix_time = tp.tv_sec; | |
4085 | 91 ot_usec = tp.tv_usec; |
3253 | 92 } |
93 | |
3736 | 94 // From the mktime() manual page: |
95 // | |
96 // The mktime() function converts a broken-down time structure, | |
97 // expressed as local time, to calendar time representation. | |
98 // | |
99 // <snip> | |
100 // | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
101 // If structure members are outside their legal interval, they |
3736 | 102 // will be normalized (so that, e.g., 40 October is changed into |
103 // 9 November). | |
104 // | |
105 // So, we no longer check limits here. | |
106 | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
107 #define DEFINE_SET_FIELD_FCN(type, f, lo, hi) \ |
3253 | 108 octave_base_tm& \ |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
109 octave_base_tm::f (type v) \ |
3736 | 110 { \ |
111 tm_ ## f = v; \ | |
112 \ | |
113 return *this; \ | |
114 } | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
115 |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
116 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
117 DEFINE_SET_FIELD_FCN (int, f, lo, hi) |
3253 | 118 |
119 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) | |
120 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) | |
121 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) | |
122 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) | |
123 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) | |
124 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
125 DEFINE_SET_INT_FIELD_FCN (year, std::numeric_limits<int>::min (), |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
126 std::numeric_limitd<int>::max ()) |
3253 | 127 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
128 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) | |
129 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
130 DEFINE_SET_FIELD_FCN (long, gmtoff, -86400, 0) |
3253 | 131 |
132 octave_base_tm& | |
3504 | 133 octave_base_tm::zone (const std::string& s) |
3253 | 134 { |
135 tm_zone = s; | |
136 return *this; | |
137 } | |
138 | |
139 #if !defined STRFTIME_BUF_INITIAL_SIZE | |
140 #define STRFTIME_BUF_INITIAL_SIZE 128 | |
141 #endif | |
142 | |
3504 | 143 std::string |
144 octave_base_tm::strftime (const std::string& fmt) const | |
3253 | 145 { |
3504 | 146 std::string retval; |
3253 | 147 |
3709 | 148 if (! fmt.empty ()) |
149 { | |
150 struct tm t; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
151 |
3709 | 152 t.tm_sec = tm_sec; |
153 t.tm_min = tm_min; | |
154 t.tm_hour = tm_hour; | |
155 t.tm_mday = tm_mday; | |
156 t.tm_mon = tm_mon; | |
157 t.tm_year = tm_year; | |
158 t.tm_wday = tm_wday; | |
159 t.tm_yday = tm_yday; | |
160 t.tm_isdst = tm_isdst; | |
3253 | 161 |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
162 #if defined (HAVE_STRUCT_TM_GMTOFF) |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
163 t.tm_gmtoff = tm_gmtoff; |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
164 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
165 |
3887 | 166 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 167 char *ps = strsave (tm_zone.c_str ()); |
168 t.tm_zone = ps; | |
3253 | 169 #endif |
170 | |
3709 | 171 const char *fmt_str = fmt.c_str (); |
3253 | 172 |
3709 | 173 char *buf = 0; |
174 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; | |
175 size_t chars_written = 0; | |
3253 | 176 |
3709 | 177 while (chars_written == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
178 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
179 delete [] buf; |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
180 buf = new char [bufsize]; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
181 buf[0] = '\0'; |
3253 | 182 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
183 chars_written = nstrftime (buf, bufsize, fmt_str, &t, 0, 0); |
3253 | 184 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
185 bufsize *= 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
186 } |
3253 | 187 |
3887 | 188 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 189 delete [] ps; |
3344 | 190 #endif |
191 | |
3709 | 192 retval = buf; |
3253 | 193 |
3709 | 194 delete [] buf; |
195 } | |
3253 | 196 |
197 return retval; | |
198 } | |
199 | |
200 void | |
201 octave_base_tm::init (void *p) | |
202 { | |
7407 | 203 if (! p) |
204 return; | |
205 | |
3253 | 206 struct tm *t = static_cast<struct tm*> (p); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
207 |
3253 | 208 tm_sec = t->tm_sec; |
209 tm_min = t->tm_min; | |
210 tm_hour = t->tm_hour; | |
211 tm_mday = t->tm_mday; | |
212 tm_mon = t->tm_mon; | |
213 tm_year = t->tm_year; | |
214 tm_wday = t->tm_wday; | |
215 tm_yday = t->tm_yday; | |
216 tm_isdst = t->tm_isdst; | |
217 | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
218 #if defined (HAVE_STRUCT_TM_GMTOFF) |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
219 tm_gmtoff = t->tm_gmtoff; |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
220 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
221 |
3887 | 222 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
7270 | 223 if (t->tm_zone) |
224 tm_zone = t->tm_zone; | |
3253 | 225 #elif defined (HAVE_TZNAME) |
226 if (t->tm_isdst == 0 || t->tm_isdst == 1) | |
227 tm_zone = tzname[t->tm_isdst]; | |
228 #endif | |
229 } | |
230 | |
231 void | |
232 octave_localtime::init (const octave_time& ot) | |
233 { | |
234 tm_usec = ot.usec (); | |
235 | |
7065 | 236 time_t t = ot.unix_time (); |
3253 | 237 |
238 octave_base_tm::init (localtime (&t)); | |
239 } | |
240 | |
241 void | |
242 octave_gmtime::init (const octave_time& ot) | |
243 { | |
244 tm_usec = ot.usec (); | |
245 | |
7065 | 246 time_t t = ot.unix_time (); |
3253 | 247 |
248 octave_base_tm::init (gmtime (&t)); | |
249 } | |
250 | |
3465 | 251 void |
3504 | 252 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465 | 253 { |
254 struct tm t; | |
255 | |
256 t.tm_sec = 0; | |
257 t.tm_min = 0; | |
258 t.tm_hour = 0; | |
259 t.tm_mday = 0; | |
6996 | 260 t.tm_mon = -1; |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
261 t.tm_year = std::numeric_limits<int>::min (); |
3465 | 262 t.tm_wday = 0; |
263 t.tm_yday = 0; | |
264 t.tm_isdst = 0; | |
265 | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
266 #if defined (HAVE_STRUCT_TM_GMTOFF) |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
267 t.tm_gmtoff = 0; |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
268 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
269 |
3887 | 270 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3465 | 271 char *ps = strsave (""); |
272 t.tm_zone = ps; | |
273 #endif | |
274 | |
13982
6cdfbe90e2ab
remove kluge for apparent libc bug that is really a valgrind bug
John W. Eaton <jwe@octave.org>
parents:
13975
diff
changeset
|
275 const char *p = str.c_str (); |
3465 | 276 |
10411 | 277 char *q = gnulib::strptime (p, fmt.c_str (), &t); |
3465 | 278 |
6995 | 279 // Fill in wday and yday, but only if mday is valid and the mon and year |
280 // are filled in, avoiding issues with mktime and invalid dates. | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
281 if (t.tm_mday != 0 && t.tm_mon >= 0 |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
282 && t.tm_year != std::numeric_limits<int>::min ()) |
6995 | 283 { |
284 t.tm_isdst = -1; | |
11005
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
285 gnulib::mktime (&t); |
6995 | 286 } |
6941 | 287 |
6996 | 288 if (t.tm_mon < 0) |
289 t.tm_mon = 0; | |
290 | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15020
diff
changeset
|
291 if (t.tm_year == std::numeric_limits<int>::min ()) |
6996 | 292 t.tm_year = 0; |
293 | |
5675 | 294 if (q) |
295 nchars = q - p + 1; | |
296 else | |
297 nchars = 0; | |
3465 | 298 |
299 octave_base_tm::init (&t); | |
300 | |
3887 | 301 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
7058 | 302 delete [] ps; |
3465 | 303 #endif |
304 } |