comparison src/DLD-FUNCTIONS/time.cc @ 3254:28d5f556b8cf

[project @ 1999-07-15 16:02:57 by jwe]
author jwe
date Thu, 15 Jul 1999 16:02:57 +0000
parents 98d0ee053ba4
children 4d33b1e56bff
comparison
equal deleted inserted replaced
3253:07d2d307c43e 3254:28d5f556b8cf
24 #include <config.h> 24 #include <config.h>
25 #endif 25 #endif
26 26
27 #include <string> 27 #include <string>
28 28
29 #include<iostream.h>
30
31 #include "defun-dld.h" 29 #include "defun-dld.h"
32 #include "error.h" 30 #include "error.h"
33 #include "oct-map.h" 31 #include "oct-map.h"
34 #include "systime.h" 32 #include "oct-time.h"
35 #include "ov.h" 33 #include "ov.h"
36 #include "oct-obj.h" 34 #include "oct-obj.h"
37 #include "utils.h"
38 35
39 // Date and time functions. 36 // Date and time functions.
40 37
41 static Octave_map 38 static Octave_map
42 mk_tm_map (struct tm *tm, double fraction) 39 mk_tm_map (const octave_base_tm& t)
43 { 40 {
44 Octave_map m; 41 Octave_map m;
45 42
46 m ["usec"] = fraction * 1e6; 43 m ["usec"] = static_cast<double> (t.usec ());
47 m ["sec"] = static_cast<double> (tm->tm_sec); 44 m ["sec"] = static_cast<double> (t.sec ());
48 m ["min"] = static_cast<double> (tm->tm_min); 45 m ["min"] = static_cast<double> (t.min ());
49 m ["hour"] = static_cast<double> (tm->tm_hour); 46 m ["hour"] = static_cast<double> (t.hour ());
50 m ["mday"] = static_cast<double> (tm->tm_mday); 47 m ["mday"] = static_cast<double> (t.mday ());
51 m ["mon"] = static_cast<double> (tm->tm_mon); 48 m ["mon"] = static_cast<double> (t.mon ());
52 m ["year"] = static_cast<double> (tm->tm_year); 49 m ["year"] = static_cast<double> (t.year ());
53 m ["wday"] = static_cast<double> (tm->tm_wday); 50 m ["wday"] = static_cast<double> (t.wday ());
54 m ["yday"] = static_cast<double> (tm->tm_yday); 51 m ["yday"] = static_cast<double> (t.yday ());
55 m ["isdst"] = static_cast<double> (tm->tm_isdst); 52 m ["isdst"] = static_cast<double> (t.isdst ());
56 53 m ["zone"] = t.zone ();
57 #if defined (HAVE_TM_ZONE)
58 m ["zone"] = tm->tm_zone;
59 #elif defined (HAVE_TZNAME)
60 if (tm->tm_isdst == 0 || tm->tm_isdst == 1)
61 m ["zone"] = tzname[tm->tm_isdst];
62 #endif
63 54
64 return m; 55 return m;
65 } 56 }
66 57
67 static struct tm* 58 static octave_base_tm
68 extract_tm (Octave_map &m, double& fraction) 59 extract_tm (Octave_map &m)
69 { 60 {
70 static struct tm tm; 61 octave_base_tm tm;
71 62
72 fraction = (m ["usec"] . double_value ()) / 1e6; 63 tm.usec (static_cast<int> (m ["usec"] . double_value ()));
73 tm.tm_sec = static_cast<int> (m ["sec"] . double_value ()); 64 tm.sec (static_cast<int> (m ["sec"] . double_value ()));
74 tm.tm_min = static_cast<int> (m ["min"] . double_value ()); 65 tm.min (static_cast<int> (m ["min"] . double_value ()));
75 tm.tm_hour = static_cast<int> (m ["hour"] . double_value ()); 66 tm.hour (static_cast<int> (m ["hour"] . double_value ()));
76 tm.tm_mday = static_cast<int> (m ["mday"] . double_value ()); 67 tm.mday (static_cast<int> (m ["mday"] . double_value ()));
77 tm.tm_mon = static_cast<int> (m ["mon"] . double_value ()); 68 tm.mon (static_cast<int> (m ["mon"] . double_value ()));
78 tm.tm_year = static_cast<int> (m ["year"] . double_value ()); 69 tm.year (static_cast<int> (m ["year"] . double_value ()));
79 tm.tm_wday = static_cast<int> (m ["wday"] . double_value ()); 70 tm.wday (static_cast<int> (m ["wday"] . double_value ()));
80 tm.tm_yday = static_cast<int> (m ["yday"] . double_value ()); 71 tm.yday (static_cast<int> (m ["yday"] . double_value ()));
81 tm.tm_isdst = static_cast<int> (m ["isdst"] . double_value ()); 72 tm.isdst (static_cast<int> (m ["isdst"] . double_value ()));
82 73 tm.zone (m ["zone"] . string_value ());
83 #if defined (HAVE_TM_ZONE) 74
84 static char *tm_zone = 0; 75 return tm;
85
86 string tstr = m ["zone"] . string_value ();
87
88 delete [] tm_zone;
89 tm_zone = strsave (tstr.c_str ());
90
91 tm.tm_zone = tm_zone;
92 #endif
93
94 return &tm;
95 } 76 }
96 77
97 DEFUN_DLD (time, , , 78 DEFUN_DLD (time, , ,
98 "time ()\n\ 79 "time ()\n\
99 \n\ 80 \n\
100 Return current time. On Unix systems, this is the number of\n\ 81 Return current time. On Unix systems, this is the number of\n\
101 seconds since the epoch.") 82 seconds since the epoch.")
102 { 83 {
103 time_t now; 84 octave_time now;
104 double fraction = 0.0; 85
105 86 return now.as_double ();
106 #if defined (HAVE_GETTIMEOFDAY)
107
108 struct timeval tp;
109
110 #if defined (GETTIMEOFDAY_NO_TZ)
111 gettimeofday (&tp);
112 #else
113 gettimeofday (&tp, 0);
114 #endif
115
116 now = tp.tv_sec;
117
118 fraction = tp.tv_usec / 1e6;
119
120 #else
121
122 now = time (0);
123
124 #endif
125
126 return static_cast<double> (now) + fraction;
127 } 87 }
128 88
129 DEFUN_DLD (gmtime, args, , 89 DEFUN_DLD (gmtime, args, ,
130 "gmtime (TIME)\n\ 90 "gmtime (TIME)\n\
131 \n\ 91 \n\
138 if (args.length () == 1) 98 if (args.length () == 1)
139 { 99 {
140 double tmp = args(0).double_value (); 100 double tmp = args(0).double_value ();
141 101
142 if (! error_state) 102 if (! error_state)
143 { 103 retval = octave_value (mk_tm_map (octave_gmtime (tmp)));
144 time_t timeval = static_cast<int> (tmp);
145 double ip;
146 double fraction = modf (tmp, &ip);
147
148 retval = octave_value (mk_tm_map (gmtime (&timeval), fraction));
149 }
150 } 104 }
151 else 105 else
152 print_usage ("gmtime"); 106 print_usage ("gmtime");
153 107
154 return retval; 108 return retval;
167 mday : day of the month (1, 31)\n\ 121 mday : day of the month (1, 31)\n\
168 mon : months since January (0, 11)\n\ 122 mon : months since January (0, 11)\n\
169 year : years since 1900\n\ 123 year : years since 1900\n\
170 wday : days since Sunday (0, 6)\n\ 124 wday : days since Sunday (0, 6)\n\
171 yday : days since January 1 (0, 365)\n\ 125 yday : days since January 1 (0, 365)\n\
172 isdst : Daylight Savings Time flag\n\ 126 isdst : daylight savings time flag\n\
173 zone : Time zone") 127 zone : time zone")
174 { 128 {
175 octave_value_list retval; 129 octave_value_list retval;
176 130
177 if (args.length () == 1) 131 if (args.length () == 1)
178 { 132 {
179 double tmp = args(0).double_value (); 133 double tmp = args(0).double_value ();
180 134
181 if (! error_state) 135 if (! error_state)
182 { 136 retval = octave_value (mk_tm_map (octave_localtime (tmp)));
183 time_t timeval = static_cast<int> (tmp);
184 double ip;
185 double fraction = modf (tmp, &ip);
186
187 retval = octave_value (mk_tm_map (localtime (&timeval), fraction));
188 }
189 } 137 }
190 else 138 else
191 print_usage ("localtime"); 139 print_usage ("localtime");
192 140
193 return retval; 141 return retval;
196 DEFUN_DLD (mktime, args, , 144 DEFUN_DLD (mktime, args, ,
197 "mktime (TMSTRUCT)") 145 "mktime (TMSTRUCT)")
198 { 146 {
199 octave_value_list retval; 147 octave_value_list retval;
200 148
201 if (args.length () == 1 && args(0).is_map ()) 149 if (args.length () == 1)
202 { 150 {
203 Octave_map map = args(0).map_value (); 151 Octave_map map = args(0).map_value ();
204 152
205 double fraction; 153 if (! error_state)
206 154 {
207 struct tm *tm = extract_tm (map, fraction); 155 octave_base_tm tm = extract_tm (map);
208 156
209 if (! error_state) 157 if (! error_state)
210 retval = static_cast<double> (mktime (tm)) + fraction; 158 {
159 octave_time ot (tm);
160
161 retval = ot.as_double ();
162 }
163 else
164 error ("mktime: invalid TMSTRUCT argument");
165 }
166 else
167 error ("mktime: expecting structure argument");
211 } 168 }
212 else 169 else
213 print_usage ("mktime"); 170 print_usage ("mktime");
214 171
215 return retval; 172 return retval;
216 } 173 }
217
218 #if !defined STRFTIME_BUF_INITIAL_SIZE
219 #define STRFTIME_BUF_INITIAL_SIZE 128
220 #endif
221 174
222 DEFUN_DLD (strftime, args, , 175 DEFUN_DLD (strftime, args, ,
223 "strftime (FMT, TMSTRUCT)\n\ 176 "strftime (FMT, TMSTRUCT)\n\
224 \n\ 177 \n\
225 Performs `%' substitutions similar to those in printf. Except where\n\ 178 Performs `%' substitutions similar to those in printf. Except where\n\
281 %y last two digits of year (00..99)\n\ 234 %y last two digits of year (00..99)\n\
282 %Y year (1970...)") 235 %Y year (1970...)")
283 { 236 {
284 octave_value_list retval; 237 octave_value_list retval;
285 238
286 if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) 239 if (args.length () == 2)
287 { 240 {
288 string fmt = args(0).string_value (); 241 string fmt = args(0).string_value ();
289 242
290 Octave_map map = args(1).map_value ();
291
292 double fraction;
293
294 struct tm *tm = extract_tm (map, fraction);
295
296 if (! error_state) 243 if (! error_state)
297 { 244 {
298 const char *fmt_str = fmt.c_str (); 245 Octave_map map = args(1).map_value ();
299 246
300 char *buf = 0; 247 if (! error_state)
301 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE;
302 size_t chars_written = 0;
303
304 while (chars_written == 0)
305 { 248 {
306 delete [] buf; 249 octave_base_tm tm = extract_tm (map);
307 buf = new char[bufsize]; 250
308 buf[0] = '\0'; 251 if (! error_state)
309 252 retval = tm.format_as_string (fmt);
310 chars_written = strftime (buf, bufsize, fmt_str, tm); 253 else
311 254 error ("strftime: invalid TMSTRUCT argument");
312 bufsize *= 2;
313 } 255 }
314 256 else
315 retval = buf; 257 error ("strftime: expecting structure as second argument");
316
317 delete [] buf;
318 } 258 }
259 else
260 error ("strftime: expecting format string as first argument");
319 } 261 }
320 else 262 else
321 print_usage ("strftime"); 263 print_usage ("strftime");
322 264
323 return retval; 265 return retval;