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 */
44 #endif /* G_OS_WIN32 */
48 #endif /* G_OS_WIN32 */
56 * @short_description: keep track of elapsed time
58 * #GTimer records a start time, and counts microseconds elapsed since
59 * that time. This is done somewhat differently on different platforms,
60 * and can be tricky to get exactly right, so #GTimer provides a
61 * portable/convenient interface.
64 * #GTimer uses a higher-quality clock when thread support is available.
65 * Therefore, calling g_thread_init() while timers are running may lead to
66 * unreliable results. It is best to call g_thread_init() before starting any
67 * timers, if you are using threads at all.
71 #define G_NSEC_PER_SEC 1000000000
73 #define GETTIME(v) (v = g_thread_gettime ())
78 * Opaque datatype that records a start time.
90 * @Returns: a new #GTimer.
92 * Creates a new timer, and starts timing (i.e. g_timer_start() is
93 * implicitly called for you).
100 timer = g_new (GTimer, 1);
101 timer->active = TRUE;
103 GETTIME (timer->start);
110 * @timer: a #GTimer to destroy.
112 * Destroys a timer, freeing associated resources.
115 g_timer_destroy (GTimer *timer)
117 g_return_if_fail (timer != NULL);
126 * Marks a start time, so that future calls to g_timer_elapsed() will
127 * report the time since g_timer_start() was called. g_timer_new()
128 * automatically marks the start time, so no need to call
129 * g_timer_start() immediately after creating the timer.
132 g_timer_start (GTimer *timer)
134 g_return_if_fail (timer != NULL);
136 timer->active = TRUE;
138 GETTIME (timer->start);
145 * Marks an end time, so calls to g_timer_elapsed() will return the
146 * difference between this end time and the start time.
149 g_timer_stop (GTimer *timer)
151 g_return_if_fail (timer != NULL);
153 timer->active = FALSE;
155 GETTIME (timer->end);
162 * This function is useless; it's fine to call g_timer_start() on an
163 * already-started timer to reset the start time, so g_timer_reset()
167 g_timer_reset (GTimer *timer)
169 g_return_if_fail (timer != NULL);
171 GETTIME (timer->start);
178 * Resumes a timer that has previously been stopped with
179 * g_timer_stop(). g_timer_stop() must be called before using this
185 g_timer_continue (GTimer *timer)
189 g_return_if_fail (timer != NULL);
190 g_return_if_fail (timer->active == FALSE);
192 /* Get elapsed time and reset timer start time
193 * to the current time minus the previously
197 elapsed = timer->end - timer->start;
199 GETTIME (timer->start);
201 timer->start -= elapsed;
203 timer->active = TRUE;
209 * @microseconds: return location for the fractional part of seconds
210 * elapsed, in microseconds (that is, the total number
211 * of microseconds elapsed, modulo 1000000), or %NULL
212 * @Returns: seconds elapsed as a floating point value, including any
215 * If @timer has been started but not stopped, obtains the time since
216 * the timer was started. If @timer has been stopped, obtains the
217 * elapsed time between the time it was started and the time it was
218 * stopped. The return value is the number of seconds elapsed,
219 * including any fractional part. The @microseconds out parameter is
220 * essentially useless.
223 * Calling initialization functions, in particular g_thread_init(), while a
224 * timer is running will cause invalid return values from this function.
228 g_timer_elapsed (GTimer *timer,
229 gulong *microseconds)
234 g_return_val_if_fail (timer != NULL, 0);
237 GETTIME (timer->end);
239 elapsed = timer->end - timer->start;
241 total = elapsed / 1e9;
244 *microseconds = (elapsed / 1000) % 1000000;
250 g_usleep (gulong microseconds)
253 Sleep (microseconds / 1000);
254 #else /* !G_OS_WIN32 */
255 # ifdef HAVE_NANOSLEEP
256 struct timespec request, remaining;
257 request.tv_sec = microseconds / G_USEC_PER_SEC;
258 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
259 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
261 # else /* !HAVE_NANOSLEEP */
263 /* on AIX, nsleep is analogous to nanosleep */
264 struct timespec request, remaining;
265 request.tv_sec = microseconds / G_USEC_PER_SEC;
266 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
267 while (nsleep (&request, &remaining) == -1 && errno == EINTR)
269 # else /* !HAVE_NSLEEP */
270 if (g_thread_supported ())
272 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
273 static GCond* cond = NULL;
276 g_get_current_time (&end_time);
277 if (microseconds > G_MAXLONG)
279 microseconds -= G_MAXLONG;
280 g_time_val_add (&end_time, G_MAXLONG);
282 g_time_val_add (&end_time, microseconds);
284 g_static_mutex_lock (&mutex);
287 cond = g_cond_new ();
289 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
293 g_static_mutex_unlock (&mutex);
298 tv.tv_sec = microseconds / G_USEC_PER_SEC;
299 tv.tv_usec = microseconds % G_USEC_PER_SEC;
300 select(0, NULL, NULL, NULL, &tv);
302 # endif /* !HAVE_NSLEEP */
303 # endif /* !HAVE_NANOSLEEP */
304 #endif /* !G_OS_WIN32 */
309 * @time_: a #GTimeVal
310 * @microseconds: number of microseconds to add to @time
312 * Adds the given number of microseconds to @time_. @microseconds can
313 * also be negative to decrease the value of @time_.
316 g_time_val_add (GTimeVal *time_, glong microseconds)
318 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
320 if (microseconds >= 0)
322 time_->tv_usec += microseconds % G_USEC_PER_SEC;
323 time_->tv_sec += microseconds / G_USEC_PER_SEC;
324 if (time_->tv_usec >= G_USEC_PER_SEC)
326 time_->tv_usec -= G_USEC_PER_SEC;
333 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
334 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
335 if (time_->tv_usec < 0)
337 time_->tv_usec += G_USEC_PER_SEC;
343 /* converts a broken down date representation, relative to UTC, to
344 * a timestamp; it uses timegm() if it's available.
347 mktime_utc (struct tm *tm)
352 static const gint days_before[] =
354 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
359 if (tm->tm_mon < 0 || tm->tm_mon > 11)
362 retval = (tm->tm_year - 70) * 365;
363 retval += (tm->tm_year - 68) / 4;
364 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
366 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
369 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
371 retval = timegm (tm);
372 #endif /* !HAVE_TIMEGM */
378 * g_time_val_from_iso8601:
379 * @iso_date: an ISO 8601 encoded date string
380 * @time_: a #GTimeVal
382 * Converts a string containing an ISO 8601 encoded date and time
383 * to a #GTimeVal and puts it into @time_.
385 * Return value: %TRUE if the conversion was successful.
390 g_time_val_from_iso8601 (const gchar *iso_date,
396 g_return_val_if_fail (iso_date != NULL, FALSE);
397 g_return_val_if_fail (time_ != NULL, FALSE);
399 /* Ensure that the first character is a digit,
400 * the first digit of the date, otherwise we don't
401 * have an ISO 8601 date */
402 while (g_ascii_isspace (*iso_date))
405 if (*iso_date == '\0')
408 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
411 val = strtoul (iso_date, (char **)&iso_date, 10);
412 if (*iso_date == '-')
415 tm.tm_year = val - 1900;
417 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
419 if (*iso_date++ != '-')
422 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
427 tm.tm_mday = val % 100;
428 tm.tm_mon = (val % 10000) / 100 - 1;
429 tm.tm_year = val / 10000 - 1900;
432 if (*iso_date != 'T')
435 if (*iso_date == '\0')
442 /* If there is a 'T' then there has to be a time */
443 if (!g_ascii_isdigit (*iso_date))
446 val = strtoul (iso_date, (char **)&iso_date, 10);
447 if (*iso_date == ':')
452 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
454 if (*iso_date++ != ':')
457 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
462 tm.tm_sec = val % 100;
463 tm.tm_min = (val % 10000) / 100;
464 tm.tm_hour = val / 10000;
469 if (*iso_date == ',' || *iso_date == '.')
473 while (g_ascii_isdigit (*++iso_date))
475 time_->tv_usec += (*iso_date - '0') * mul;
480 /* Now parse the offset and convert tm to a time_t */
481 if (*iso_date == 'Z')
484 time_->tv_sec = mktime_utc (&tm);
486 else if (*iso_date == '+' || *iso_date == '-')
488 gint sign = (*iso_date == '+') ? -1 : 1;
490 val = strtoul (iso_date + 1, (char **)&iso_date, 10);
492 if (*iso_date == ':')
493 val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
495 val = 60 * (val / 100) + (val % 100);
497 time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
501 /* No "Z" or offset, so local time */
502 tm.tm_isdst = -1; /* locale selects DST */
503 time_->tv_sec = mktime (&tm);
506 while (g_ascii_isspace (*iso_date))
509 return *iso_date == '\0';
513 * g_time_val_to_iso8601:
514 * @time_: a #GTimeVal
516 * Converts @time_ into an ISO 8601 encoded string, relative to the
517 * Coordinated Universal Time (UTC).
519 * Return value: a newly allocated string containing an ISO 8601 date
524 g_time_val_to_iso8601 (GTimeVal *time_)
533 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
535 secs = time_->tv_sec;
540 tm = gmtime_r (&secs, &tm_);
546 if (time_->tv_usec != 0)
548 /* ISO 8601 date and time format, with fractionary seconds:
549 * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
551 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
562 /* ISO 8601 date and time format:
563 * YYYY-MM-DDTHH:MM:SSZ
565 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",