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/.
32 #include "glibconfig.h"
38 #endif /* HAVE_UNISTD_H */
40 #ifdef HAVE_SYS_TIME_H
46 #endif /* G_OS_WIN32 */
50 #endif /* G_OS_WIN32 */
55 #include "gstrfuncs.h"
56 #include "gtestutils.h"
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.
70 * #GTimer uses a higher-quality clock when thread support is available.
71 * Therefore, calling g_thread_init() while timers are running may lead to
72 * unreliable results. It is best to call g_thread_init() before starting any
73 * timers, if you are using threads at all.
77 #define G_NSEC_PER_SEC 1000000000
79 #define GETTIME(v) (v = g_thread_gettime ())
84 * Opaque datatype that records a start time.
96 * @Returns: a new #GTimer.
98 * Creates a new timer, and starts timing (i.e. g_timer_start() is
99 * implicitly called for you).
106 timer = g_new (GTimer, 1);
107 timer->active = TRUE;
109 GETTIME (timer->start);
116 * @timer: a #GTimer to destroy.
118 * Destroys a timer, freeing associated resources.
121 g_timer_destroy (GTimer *timer)
123 g_return_if_fail (timer != NULL);
132 * Marks a start time, so that future calls to g_timer_elapsed() will
133 * report the time since g_timer_start() was called. g_timer_new()
134 * automatically marks the start time, so no need to call
135 * g_timer_start() immediately after creating the timer.
138 g_timer_start (GTimer *timer)
140 g_return_if_fail (timer != NULL);
142 timer->active = TRUE;
144 GETTIME (timer->start);
151 * Marks an end time, so calls to g_timer_elapsed() will return the
152 * difference between this end time and the start time.
155 g_timer_stop (GTimer *timer)
157 g_return_if_fail (timer != NULL);
159 timer->active = FALSE;
161 GETTIME (timer->end);
168 * This function is useless; it's fine to call g_timer_start() on an
169 * already-started timer to reset the start time, so g_timer_reset()
173 g_timer_reset (GTimer *timer)
175 g_return_if_fail (timer != NULL);
177 GETTIME (timer->start);
184 * Resumes a timer that has previously been stopped with
185 * g_timer_stop(). g_timer_stop() must be called before using this
191 g_timer_continue (GTimer *timer)
195 g_return_if_fail (timer != NULL);
196 g_return_if_fail (timer->active == FALSE);
198 /* Get elapsed time and reset timer start time
199 * to the current time minus the previously
203 elapsed = timer->end - timer->start;
205 GETTIME (timer->start);
207 timer->start -= elapsed;
209 timer->active = TRUE;
215 * @microseconds: return location for the fractional part of seconds
216 * elapsed, in microseconds (that is, the total number
217 * of microseconds elapsed, modulo 1000000), or %NULL
218 * @Returns: seconds elapsed as a floating point value, including any
221 * If @timer has been started but not stopped, obtains the time since
222 * the timer was started. If @timer has been stopped, obtains the
223 * elapsed time between the time it was started and the time it was
224 * stopped. The return value is the number of seconds elapsed,
225 * including any fractional part. The @microseconds out parameter is
226 * essentially useless.
229 * Calling initialization functions, in particular g_thread_init(), while a
230 * timer is running will cause invalid return values from this function.
234 g_timer_elapsed (GTimer *timer,
235 gulong *microseconds)
240 g_return_val_if_fail (timer != NULL, 0);
243 GETTIME (timer->end);
245 elapsed = timer->end - timer->start;
247 total = elapsed / 1e9;
250 *microseconds = (elapsed / 1000) % 1000000;
256 g_usleep (gulong microseconds)
259 Sleep (microseconds / 1000);
260 #else /* !G_OS_WIN32 */
261 # ifdef HAVE_NANOSLEEP
262 struct timespec request, remaining;
263 request.tv_sec = microseconds / G_USEC_PER_SEC;
264 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
265 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
267 # else /* !HAVE_NANOSLEEP */
269 /* on AIX, nsleep is analogous to nanosleep */
270 struct timespec request, remaining;
271 request.tv_sec = microseconds / G_USEC_PER_SEC;
272 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
273 while (nsleep (&request, &remaining) == -1 && errno == EINTR)
275 # else /* !HAVE_NSLEEP */
276 if (g_thread_supported ())
278 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
279 static GCond* cond = NULL;
282 g_get_current_time (&end_time);
283 if (microseconds > G_MAXLONG)
285 microseconds -= G_MAXLONG;
286 g_time_val_add (&end_time, G_MAXLONG);
288 g_time_val_add (&end_time, microseconds);
290 g_static_mutex_lock (&mutex);
293 cond = g_cond_new ();
295 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
299 g_static_mutex_unlock (&mutex);
304 tv.tv_sec = microseconds / G_USEC_PER_SEC;
305 tv.tv_usec = microseconds % G_USEC_PER_SEC;
306 select(0, NULL, NULL, NULL, &tv);
308 # endif /* !HAVE_NSLEEP */
309 # endif /* !HAVE_NANOSLEEP */
310 #endif /* !G_OS_WIN32 */
315 * @time_: a #GTimeVal
316 * @microseconds: number of microseconds to add to @time
318 * Adds the given number of microseconds to @time_. @microseconds can
319 * also be negative to decrease the value of @time_.
322 g_time_val_add (GTimeVal *time_, glong microseconds)
324 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
326 if (microseconds >= 0)
328 time_->tv_usec += microseconds % G_USEC_PER_SEC;
329 time_->tv_sec += microseconds / G_USEC_PER_SEC;
330 if (time_->tv_usec >= G_USEC_PER_SEC)
332 time_->tv_usec -= G_USEC_PER_SEC;
339 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
340 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
341 if (time_->tv_usec < 0)
343 time_->tv_usec += G_USEC_PER_SEC;
349 /* converts a broken down date representation, relative to UTC, to
350 * a timestamp; it uses timegm() if it's available.
353 mktime_utc (struct tm *tm)
358 static const gint days_before[] =
360 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
365 if (tm->tm_mon < 0 || tm->tm_mon > 11)
368 retval = (tm->tm_year - 70) * 365;
369 retval += (tm->tm_year - 68) / 4;
370 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
372 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
375 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
377 retval = timegm (tm);
378 #endif /* !HAVE_TIMEGM */
384 * g_time_val_from_iso8601:
385 * @iso_date: an ISO 8601 encoded date string
386 * @time_: a #GTimeVal
388 * Converts a string containing an ISO 8601 encoded date and time
389 * to a #GTimeVal and puts it into @time_.
391 * Return value: %TRUE if the conversion was successful.
396 g_time_val_from_iso8601 (const gchar *iso_date,
402 g_return_val_if_fail (iso_date != NULL, FALSE);
403 g_return_val_if_fail (time_ != NULL, FALSE);
405 /* Ensure that the first character is a digit,
406 * the first digit of the date, otherwise we don't
407 * have an ISO 8601 date */
408 while (g_ascii_isspace (*iso_date))
411 if (*iso_date == '\0')
414 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
417 val = strtoul (iso_date, (char **)&iso_date, 10);
418 if (*iso_date == '-')
421 tm.tm_year = val - 1900;
423 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
425 if (*iso_date++ != '-')
428 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
433 tm.tm_mday = val % 100;
434 tm.tm_mon = (val % 10000) / 100 - 1;
435 tm.tm_year = val / 10000 - 1900;
438 if (*iso_date != 'T')
441 if (*iso_date == '\0')
448 /* If there is a 'T' then there has to be a time */
449 if (!g_ascii_isdigit (*iso_date))
452 val = strtoul (iso_date, (char **)&iso_date, 10);
453 if (*iso_date == ':')
458 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
460 if (*iso_date++ != ':')
463 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
468 tm.tm_sec = val % 100;
469 tm.tm_min = (val % 10000) / 100;
470 tm.tm_hour = val / 10000;
475 if (*iso_date == ',' || *iso_date == '.')
479 while (g_ascii_isdigit (*++iso_date))
481 time_->tv_usec += (*iso_date - '0') * mul;
486 /* Now parse the offset and convert tm to a time_t */
487 if (*iso_date == 'Z')
490 time_->tv_sec = mktime_utc (&tm);
492 else if (*iso_date == '+' || *iso_date == '-')
494 gint sign = (*iso_date == '+') ? -1 : 1;
496 val = strtoul (iso_date + 1, (char **)&iso_date, 10);
498 if (*iso_date == ':')
499 val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
501 val = 60 * (val / 100) + (val % 100);
503 time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
507 /* No "Z" or offset, so local time */
508 tm.tm_isdst = -1; /* locale selects DST */
509 time_->tv_sec = mktime (&tm);
512 while (g_ascii_isspace (*iso_date))
515 return *iso_date == '\0';
519 * g_time_val_to_iso8601:
520 * @time_: a #GTimeVal
522 * Converts @time_ into an ISO 8601 encoded string, relative to the
523 * Coordinated Universal Time (UTC).
525 * Return value: a newly allocated string containing an ISO 8601 date
530 g_time_val_to_iso8601 (GTimeVal *time_)
539 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
541 secs = time_->tv_sec;
546 tm = gmtime_r (&secs, &tm_);
552 if (time_->tv_usec != 0)
554 /* ISO 8601 date and time format, with fractionary seconds:
555 * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
557 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
568 /* ISO 8601 date and time format:
569 * YYYY-MM-DDTHH:MM:SSZ
571 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",