gmain: use Linux eventfd() for main context wake up
[glib.git] / glib / gtimer.c
blob970ad2c5ccc74a26a98946f06d63496973ebe52b
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
28 * MT safe
31 #include "config.h"
32 #include "glibconfig.h"
34 #include <stdlib.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43 #include <time.h>
44 #ifndef G_OS_WIN32
45 #include <errno.h>
46 #endif /* G_OS_WIN32 */
48 #ifdef G_OS_WIN32
49 #include <windows.h>
50 #endif /* G_OS_WIN32 */
52 #include "gtimer.h"
54 #include "gmem.h"
55 #include "gstrfuncs.h"
56 #include "gtestutils.h"
57 #include "gmain.h"
59 /**
60 * SECTION:timers
61 * @title: Timers
62 * @short_description: keep track of elapsed time
64 * #GTimer records a start time, and counts microseconds elapsed since
65 * that time. This is done somewhat differently on different platforms,
66 * and can be tricky to get exactly right, so #GTimer provides a
67 * portable/convenient interface.
68 **/
70 /**
71 * GTimer:
73 * Opaque datatype that records a start time.
74 **/
75 struct _GTimer
77 guint64 start;
78 guint64 end;
80 guint active : 1;
83 /**
84 * g_timer_new:
85 * @Returns: a new #GTimer.
87 * Creates a new timer, and starts timing (i.e. g_timer_start() is
88 * implicitly called for you).
89 **/
90 GTimer*
91 g_timer_new (void)
93 GTimer *timer;
95 timer = g_new (GTimer, 1);
96 timer->active = TRUE;
98 timer->start = g_get_monotonic_time ();
100 return timer;
104 * g_timer_destroy:
105 * @timer: a #GTimer to destroy.
107 * Destroys a timer, freeing associated resources.
109 void
110 g_timer_destroy (GTimer *timer)
112 g_return_if_fail (timer != NULL);
114 g_free (timer);
118 * g_timer_start:
119 * @timer: a #GTimer.
121 * Marks a start time, so that future calls to g_timer_elapsed() will
122 * report the time since g_timer_start() was called. g_timer_new()
123 * automatically marks the start time, so no need to call
124 * g_timer_start() immediately after creating the timer.
126 void
127 g_timer_start (GTimer *timer)
129 g_return_if_fail (timer != NULL);
131 timer->active = TRUE;
133 timer->start = g_get_monotonic_time ();
137 * g_timer_stop:
138 * @timer: a #GTimer.
140 * Marks an end time, so calls to g_timer_elapsed() will return the
141 * difference between this end time and the start time.
143 void
144 g_timer_stop (GTimer *timer)
146 g_return_if_fail (timer != NULL);
148 timer->active = FALSE;
150 timer->end = g_get_monotonic_time ();
154 * g_timer_reset:
155 * @timer: a #GTimer.
157 * This function is useless; it's fine to call g_timer_start() on an
158 * already-started timer to reset the start time, so g_timer_reset()
159 * serves no purpose.
161 void
162 g_timer_reset (GTimer *timer)
164 g_return_if_fail (timer != NULL);
166 timer->start = g_get_monotonic_time ();
170 * g_timer_continue:
171 * @timer: a #GTimer.
173 * Resumes a timer that has previously been stopped with
174 * g_timer_stop(). g_timer_stop() must be called before using this
175 * function.
177 * Since: 2.4
179 void
180 g_timer_continue (GTimer *timer)
182 guint64 elapsed;
184 g_return_if_fail (timer != NULL);
185 g_return_if_fail (timer->active == FALSE);
187 /* Get elapsed time and reset timer start time
188 * to the current time minus the previously
189 * elapsed interval.
192 elapsed = timer->end - timer->start;
194 timer->start = g_get_monotonic_time ();
196 timer->start -= elapsed;
198 timer->active = TRUE;
202 * g_timer_elapsed:
203 * @timer: a #GTimer.
204 * @microseconds: return location for the fractional part of seconds
205 * elapsed, in microseconds (that is, the total number
206 * of microseconds elapsed, modulo 1000000), or %NULL
207 * @Returns: seconds elapsed as a floating point value, including any
208 * fractional part.
210 * If @timer has been started but not stopped, obtains the time since
211 * the timer was started. If @timer has been stopped, obtains the
212 * elapsed time between the time it was started and the time it was
213 * stopped. The return value is the number of seconds elapsed,
214 * including any fractional part. The @microseconds out parameter is
215 * essentially useless.
217 * <warning><para>
218 * Calling initialization functions, in particular g_thread_init(), while a
219 * timer is running will cause invalid return values from this function.
220 * </para></warning>
222 gdouble
223 g_timer_elapsed (GTimer *timer,
224 gulong *microseconds)
226 gdouble total;
227 gint64 elapsed;
229 g_return_val_if_fail (timer != NULL, 0);
231 if (timer->active)
232 timer->end = g_get_monotonic_time ();
234 elapsed = timer->end - timer->start;
236 total = elapsed / 1e6;
238 if (microseconds)
239 *microseconds = elapsed % 1000000;
241 return total;
244 void
245 g_usleep (gulong microseconds)
247 #ifdef G_OS_WIN32
248 Sleep (microseconds / 1000);
249 #else
250 struct timespec request, remaining;
251 request.tv_sec = microseconds / G_USEC_PER_SEC;
252 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
253 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
254 request = remaining;
255 #endif
259 * g_time_val_add:
260 * @time_: a #GTimeVal
261 * @microseconds: number of microseconds to add to @time
263 * Adds the given number of microseconds to @time_. @microseconds can
264 * also be negative to decrease the value of @time_.
266 void
267 g_time_val_add (GTimeVal *time_, glong microseconds)
269 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
271 if (microseconds >= 0)
273 time_->tv_usec += microseconds % G_USEC_PER_SEC;
274 time_->tv_sec += microseconds / G_USEC_PER_SEC;
275 if (time_->tv_usec >= G_USEC_PER_SEC)
277 time_->tv_usec -= G_USEC_PER_SEC;
278 time_->tv_sec++;
281 else
283 microseconds *= -1;
284 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
285 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
286 if (time_->tv_usec < 0)
288 time_->tv_usec += G_USEC_PER_SEC;
289 time_->tv_sec--;
294 /* converts a broken down date representation, relative to UTC, to
295 * a timestamp; it uses timegm() if it's available.
297 static time_t
298 mktime_utc (struct tm *tm)
300 time_t retval;
302 #ifndef HAVE_TIMEGM
303 static const gint days_before[] =
305 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
307 #endif
309 #ifndef HAVE_TIMEGM
310 if (tm->tm_mon < 0 || tm->tm_mon > 11)
311 return (time_t) -1;
313 retval = (tm->tm_year - 70) * 365;
314 retval += (tm->tm_year - 68) / 4;
315 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
317 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
318 retval -= 1;
320 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
321 #else
322 retval = timegm (tm);
323 #endif /* !HAVE_TIMEGM */
325 return retval;
329 * g_time_val_from_iso8601:
330 * @iso_date: an ISO 8601 encoded date string
331 * @time_: a #GTimeVal
333 * Converts a string containing an ISO 8601 encoded date and time
334 * to a #GTimeVal and puts it into @time_.
336 * Return value: %TRUE if the conversion was successful.
338 * Since: 2.12
340 gboolean
341 g_time_val_from_iso8601 (const gchar *iso_date,
342 GTimeVal *time_)
344 struct tm tm = {0};
345 long val;
347 g_return_val_if_fail (iso_date != NULL, FALSE);
348 g_return_val_if_fail (time_ != NULL, FALSE);
350 /* Ensure that the first character is a digit,
351 * the first digit of the date, otherwise we don't
352 * have an ISO 8601 date */
353 while (g_ascii_isspace (*iso_date))
354 iso_date++;
356 if (*iso_date == '\0')
357 return FALSE;
359 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
360 return FALSE;
362 val = strtoul (iso_date, (char **)&iso_date, 10);
363 if (*iso_date == '-')
365 /* YYYY-MM-DD */
366 tm.tm_year = val - 1900;
367 iso_date++;
368 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
370 if (*iso_date++ != '-')
371 return FALSE;
373 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
375 else
377 /* YYYYMMDD */
378 tm.tm_mday = val % 100;
379 tm.tm_mon = (val % 10000) / 100 - 1;
380 tm.tm_year = val / 10000 - 1900;
383 if (*iso_date != 'T')
385 /* Date only */
386 if (*iso_date == '\0')
387 return TRUE;
388 return FALSE;
391 iso_date++;
393 /* If there is a 'T' then there has to be a time */
394 if (!g_ascii_isdigit (*iso_date))
395 return FALSE;
397 val = strtoul (iso_date, (char **)&iso_date, 10);
398 if (*iso_date == ':')
400 /* hh:mm:ss */
401 tm.tm_hour = val;
402 iso_date++;
403 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
405 if (*iso_date++ != ':')
406 return FALSE;
408 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
410 else
412 /* hhmmss */
413 tm.tm_sec = val % 100;
414 tm.tm_min = (val % 10000) / 100;
415 tm.tm_hour = val / 10000;
418 time_->tv_usec = 0;
420 if (*iso_date == ',' || *iso_date == '.')
422 glong mul = 100000;
424 while (g_ascii_isdigit (*++iso_date))
426 time_->tv_usec += (*iso_date - '0') * mul;
427 mul /= 10;
431 /* Now parse the offset and convert tm to a time_t */
432 if (*iso_date == 'Z')
434 iso_date++;
435 time_->tv_sec = mktime_utc (&tm);
437 else if (*iso_date == '+' || *iso_date == '-')
439 gint sign = (*iso_date == '+') ? -1 : 1;
441 val = strtoul (iso_date + 1, (char **)&iso_date, 10);
443 if (*iso_date == ':')
444 val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
445 else
446 val = 60 * (val / 100) + (val % 100);
448 time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
450 else
452 /* No "Z" or offset, so local time */
453 tm.tm_isdst = -1; /* locale selects DST */
454 time_->tv_sec = mktime (&tm);
457 while (g_ascii_isspace (*iso_date))
458 iso_date++;
460 return *iso_date == '\0';
464 * g_time_val_to_iso8601:
465 * @time_: a #GTimeVal
467 * Converts @time_ into an ISO 8601 encoded string, relative to the
468 * Coordinated Universal Time (UTC).
470 * Return value: a newly allocated string containing an ISO 8601 date
472 * Since: 2.12
474 gchar *
475 g_time_val_to_iso8601 (GTimeVal *time_)
477 gchar *retval;
478 struct tm *tm;
479 #ifdef HAVE_GMTIME_R
480 struct tm tm_;
481 #endif
482 time_t secs;
484 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
486 secs = time_->tv_sec;
487 #ifdef _WIN32
488 tm = gmtime (&secs);
489 #else
490 #ifdef HAVE_GMTIME_R
491 tm = gmtime_r (&secs, &tm_);
492 #else
493 tm = gmtime (&secs);
494 #endif
495 #endif
497 if (time_->tv_usec != 0)
499 /* ISO 8601 date and time format, with fractionary seconds:
500 * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
502 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
503 tm->tm_year + 1900,
504 tm->tm_mon + 1,
505 tm->tm_mday,
506 tm->tm_hour,
507 tm->tm_min,
508 tm->tm_sec,
509 time_->tv_usec);
511 else
513 /* ISO 8601 date and time format:
514 * YYYY-MM-DDTHH:MM:SSZ
516 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",
517 tm->tm_year + 1900,
518 tm->tm_mon + 1,
519 tm->tm_mday,
520 tm->tm_hour,
521 tm->tm_min,
522 tm->tm_sec);
525 return retval;