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