Fix an off-by-2 error in the leap year calculation. (#344905, Dan Winship)
[platform/upstream/glib.git] / glib / gtimer.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32 #include "glibconfig.h"
33
34 #include <stdlib.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39
40 #ifndef G_OS_WIN32
41 #include <sys/time.h>
42 #include <time.h>
43 #include <errno.h>
44 #endif /* G_OS_WIN32 */
45
46 #ifdef G_OS_WIN32
47 #include <windows.h>
48 #endif /* G_OS_WIN32 */
49
50 #include "glib.h"
51 #include "galias.h"
52
53
54 struct _GTimer
55 {
56 #ifdef G_OS_WIN32
57   guint64 start;
58   guint64 end;
59 #else /* !G_OS_WIN32 */
60   struct timeval start;
61   struct timeval end;
62 #endif /* !G_OS_WIN32 */
63
64   guint active : 1;
65 };
66
67 #ifdef G_OS_WIN32
68 #  define GETTIME(v) \
69      GetSystemTimeAsFileTime ((FILETIME *)&v)
70 #else /* !G_OS_WIN32 */
71 #  define GETTIME(v) \
72      gettimeofday (&v, NULL)
73 #endif /* !G_OS_WIN32 */
74
75 GTimer*
76 g_timer_new (void)
77 {
78   GTimer *timer;
79
80   timer = g_new (GTimer, 1);
81   timer->active = TRUE;
82
83   GETTIME (timer->start);
84
85   return timer;
86 }
87
88 void
89 g_timer_destroy (GTimer *timer)
90 {
91   g_return_if_fail (timer != NULL);
92
93   g_free (timer);
94 }
95
96 void
97 g_timer_start (GTimer *timer)
98 {
99   g_return_if_fail (timer != NULL);
100
101   timer->active = TRUE;
102
103   GETTIME (timer->start);
104 }
105
106 void
107 g_timer_stop (GTimer *timer)
108 {
109   g_return_if_fail (timer != NULL);
110
111   timer->active = FALSE;
112
113   GETTIME(timer->end);
114 }
115
116 void
117 g_timer_reset (GTimer *timer)
118 {
119   g_return_if_fail (timer != NULL);
120
121   GETTIME (timer->start);
122 }
123
124 void
125 g_timer_continue (GTimer *timer)
126 {
127 #ifdef G_OS_WIN32
128   guint64 elapsed;
129 #else
130   struct timeval elapsed;
131 #endif /* G_OS_WIN32 */
132
133   g_return_if_fail (timer != NULL);
134   g_return_if_fail (timer->active == FALSE);
135
136   /* Get elapsed time and reset timer start time
137    *  to the current time minus the previously
138    *  elapsed interval.
139    */
140
141 #ifdef G_OS_WIN32
142
143   elapsed = timer->end - timer->start;
144
145   GETTIME (timer->start);
146
147   timer->start -= elapsed;
148
149 #else /* !G_OS_WIN32 */
150
151   if (timer->start.tv_usec > timer->end.tv_usec)
152     {
153       timer->end.tv_usec += G_USEC_PER_SEC;
154       timer->end.tv_sec--;
155     }
156
157   elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
158   elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
159
160   GETTIME (timer->start);
161
162   if (timer->start.tv_usec < elapsed.tv_usec)
163     {
164       timer->start.tv_usec += G_USEC_PER_SEC;
165       timer->start.tv_sec--;
166     }
167
168   timer->start.tv_usec -= elapsed.tv_usec;
169   timer->start.tv_sec -= elapsed.tv_sec;
170
171 #endif /* !G_OS_WIN32 */
172
173   timer->active = TRUE;
174 }
175
176 gdouble
177 g_timer_elapsed (GTimer *timer,
178                  gulong *microseconds)
179 {
180   gdouble total;
181 #ifdef G_OS_WIN32
182   gint64 elapsed;
183 #else
184   struct timeval elapsed;
185 #endif /* G_OS_WIN32 */
186
187   g_return_val_if_fail (timer != NULL, 0);
188
189 #ifdef G_OS_WIN32
190   if (timer->active)
191     GETTIME (timer->end);
192
193   elapsed = timer->end - timer->start;
194
195   total = elapsed / 1e7;
196
197   if (microseconds)
198     *microseconds = (elapsed / 10) % 1000000;
199 #else /* !G_OS_WIN32 */
200   if (timer->active)
201     gettimeofday (&timer->end, NULL);
202
203   if (timer->start.tv_usec > timer->end.tv_usec)
204     {
205       timer->end.tv_usec += G_USEC_PER_SEC;
206       timer->end.tv_sec--;
207     }
208
209   elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
210   elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
211
212   total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
213   if (total < 0)
214     {
215       total = 0;
216
217       if (microseconds)
218         *microseconds = 0;
219     }
220   else if (microseconds)
221     *microseconds = elapsed.tv_usec;
222
223 #endif /* !G_OS_WIN32 */
224
225   return total;
226 }
227
228 void
229 g_usleep (gulong microseconds)
230 {
231 #ifdef G_OS_WIN32
232   Sleep (microseconds / 1000);
233 #else /* !G_OS_WIN32 */
234 # ifdef HAVE_NANOSLEEP
235   struct timespec request, remaining;
236   request.tv_sec = microseconds / G_USEC_PER_SEC;
237   request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
238   while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
239     request = remaining;
240 # else /* !HAVE_NANOSLEEP */
241   if (g_thread_supported ())
242     {
243       static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
244       static GCond* cond = NULL;
245       GTimeVal end_time;
246       
247       g_get_current_time (&end_time);
248       if (microseconds > G_MAXLONG)
249         {
250           microseconds -= G_MAXLONG;
251           g_time_val_add (&end_time, G_MAXLONG);
252         }
253       g_time_val_add (&end_time, microseconds);
254
255       g_static_mutex_lock (&mutex);
256       
257       if (!cond)
258         cond = g_cond_new ();
259       
260       while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
261                                 &end_time))
262         /* do nothing */;
263       
264       g_static_mutex_unlock (&mutex);
265     }
266   else
267     {
268       struct timeval tv;
269       tv.tv_sec = microseconds / G_USEC_PER_SEC;
270       tv.tv_usec = microseconds % G_USEC_PER_SEC;
271       select(0, NULL, NULL, NULL, &tv);
272     }
273 # endif /* !HAVE_NANOSLEEP */
274 #endif /* !G_OS_WIN32 */
275 }
276
277 /**
278  * g_time_val_add:
279  * @time_: a #GTimeVal
280  * @microseconds: number of microseconds to add to @time
281  *
282  * Adds the given number of microseconds to @time_. @microseconds can
283  * also be negative to decrease the value of @time_.
284  **/
285 void 
286 g_time_val_add (GTimeVal *time_, glong microseconds)
287 {
288   g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
289
290   if (microseconds >= 0)
291     {
292       time_->tv_usec += microseconds % G_USEC_PER_SEC;
293       time_->tv_sec += microseconds / G_USEC_PER_SEC;
294       if (time_->tv_usec >= G_USEC_PER_SEC)
295        {
296          time_->tv_usec -= G_USEC_PER_SEC;
297          time_->tv_sec++;
298        }
299     }
300   else
301     {
302       microseconds *= -1;
303       time_->tv_usec -= microseconds % G_USEC_PER_SEC;
304       time_->tv_sec -= microseconds / G_USEC_PER_SEC;
305       if (time_->tv_usec < 0)
306        {
307          time_->tv_usec += G_USEC_PER_SEC;
308          time_->tv_sec--;
309        }      
310     }
311 }
312
313 /* converts a broken down date representation, relative to UTC, to
314  * a timestamp; it uses timegm() if it's available.
315  */
316 static time_t
317 mktime_utc (struct tm *tm)
318 {
319   time_t retval;
320   
321 #ifndef HAVE_TIMEGM
322   static const gint days_before[] =
323   {
324     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
325   };
326 #endif
327
328 #ifndef HAVE_TIMEGM
329   if (tm->tm_mon < 0 || tm->tm_mon > 11)
330     return (time_t) -1;
331
332   retval = (tm->tm_year - 70) * 365;
333   retval += (tm->tm_year - 68) / 4;
334   retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
335   
336   if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
337     retval -= 1;
338   
339   retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
340 #else
341   retval = timegm (tm);
342 #endif /* !HAVE_TIMEGM */
343   
344   return retval;
345 }
346
347 /**
348  * g_time_val_from_iso8601:
349  * @iso_date: a ISO 8601 encoded date string
350  * @time_: a #GTimeVal
351  *
352  * Converts a string containing an ISO 8601 encoded date and time
353  * to a #GTimeVal and puts it into @time_.
354  *
355  * Return value: %TRUE if the conversion was successful.
356  *
357  * Since: 2.12
358  */
359 gboolean
360 g_time_val_from_iso8601 (const gchar *iso_date,
361                          GTimeVal    *time_)
362 {
363   struct tm tm;
364   long val;
365
366   g_return_val_if_fail (iso_date != NULL, FALSE);
367   g_return_val_if_fail (time_ != NULL, FALSE);
368
369   val = strtoul (iso_date, (char **)&iso_date, 10);
370   if (*iso_date == '-')
371     {
372       /* YYYY-MM-DD */
373       tm.tm_year = val - 1900;
374       iso_date++;
375       tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
376       
377       if (*iso_date++ != '-')
378         return FALSE;
379       
380       tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
381     }
382   else
383     {
384       /* YYYYMMDD */
385       tm.tm_mday = val % 100;
386       tm.tm_mon = (val % 10000) / 100 - 1;
387       tm.tm_year = val / 10000 - 1900;
388     }
389
390   if (*iso_date++ != 'T')
391     return FALSE;
392   
393   val = strtoul (iso_date, (char **)&iso_date, 10);
394   if (*iso_date == ':')
395     {
396       /* hh:mm:ss */
397       tm.tm_hour = val;
398       iso_date++;
399       tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
400       
401       if (*iso_date++ != ':')
402         return FALSE;
403       
404       tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
405     }
406   else
407     {
408       /* hhmmss */
409       tm.tm_sec = val % 100;
410       tm.tm_min = (val % 10000) / 100;
411       tm.tm_hour = val / 10000;
412     }
413
414   time_->tv_sec = mktime_utc (&tm);
415   time_->tv_usec = 1;
416   
417   if (*iso_date == '.')
418     time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
419     
420   if (*iso_date == '+' || *iso_date == '-')
421     {
422       gint sign = (*iso_date == '+') ? -1 : 1;
423       
424       val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
425       
426       if (*iso_date == ':')
427         val = 60 * val + strtoul (iso_date + 1, NULL, 10);
428       else
429         val = 60 * (val / 100) + (val % 100);
430
431       time_->tv_sec += (time_t) (val * sign);
432     }
433
434   return TRUE;
435 }
436
437 /**
438  * g_time_val_to_iso8601:
439  * @time_: a #GTimeVal
440  * 
441  * Converts @time_ into a ISO 8601 encoded string, relative to the
442  * Coordinated Universal Time (UTC).
443  *
444  * Return value: a newly allocated string containing a ISO 8601 date
445  *
446  * Since: 2.12
447  */
448 gchar *
449 g_time_val_to_iso8601 (GTimeVal *time_)
450 {
451   gchar *retval;
452
453   g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
454
455 #define ISO_8601_LEN    21
456 #define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
457   retval = g_new0 (gchar, ISO_8601_LEN + 1);
458   
459   strftime (retval, ISO_8601_LEN,
460             ISO_8601_FORMAT,
461             gmtime (&(time_->tv_sec)));
462   
463   return retval;
464 }
465
466 #define __G_TIMER_C__
467 #include "galiasdef.c"