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 */
53 #include "gstrfuncs.h"
54 #include "gtestutils.h"
60 * @short_description: keep track of elapsed time
62 * #GTimer records a start time, and counts microseconds elapsed since
63 * that time. This is done somewhat differently on different platforms,
64 * and can be tricky to get exactly right, so #GTimer provides a
65 * portable/convenient interface.
71 * Opaque datatype that records a start time.
83 * @Returns: a new #GTimer.
85 * Creates a new timer, and starts timing (i.e. g_timer_start() is
86 * implicitly called for you).
93 timer = g_new (GTimer, 1);
96 timer->start = g_get_monotonic_time ();
103 * @timer: a #GTimer to destroy.
105 * Destroys a timer, freeing associated resources.
108 g_timer_destroy (GTimer *timer)
110 g_return_if_fail (timer != NULL);
119 * Marks a start time, so that future calls to g_timer_elapsed() will
120 * report the time since g_timer_start() was called. g_timer_new()
121 * automatically marks the start time, so no need to call
122 * g_timer_start() immediately after creating the timer.
125 g_timer_start (GTimer *timer)
127 g_return_if_fail (timer != NULL);
129 timer->active = TRUE;
131 timer->start = g_get_monotonic_time ();
138 * Marks an end time, so calls to g_timer_elapsed() will return the
139 * difference between this end time and the start time.
142 g_timer_stop (GTimer *timer)
144 g_return_if_fail (timer != NULL);
146 timer->active = FALSE;
148 timer->end = g_get_monotonic_time ();
155 * This function is useless; it's fine to call g_timer_start() on an
156 * already-started timer to reset the start time, so g_timer_reset()
160 g_timer_reset (GTimer *timer)
162 g_return_if_fail (timer != NULL);
164 timer->start = g_get_monotonic_time ();
171 * Resumes a timer that has previously been stopped with
172 * g_timer_stop(). g_timer_stop() must be called before using this
178 g_timer_continue (GTimer *timer)
182 g_return_if_fail (timer != NULL);
183 g_return_if_fail (timer->active == FALSE);
185 /* Get elapsed time and reset timer start time
186 * to the current time minus the previously
190 elapsed = timer->end - timer->start;
192 timer->start = g_get_monotonic_time ();
194 timer->start -= elapsed;
196 timer->active = TRUE;
202 * @microseconds: return location for the fractional part of seconds
203 * elapsed, in microseconds (that is, the total number
204 * of microseconds elapsed, modulo 1000000), or %NULL
205 * @Returns: seconds elapsed as a floating point value, including any
208 * If @timer has been started but not stopped, obtains the time since
209 * the timer was started. If @timer has been stopped, obtains the
210 * elapsed time between the time it was started and the time it was
211 * stopped. The return value is the number of seconds elapsed,
212 * including any fractional part. The @microseconds out parameter is
213 * essentially useless.
216 * Calling initialization functions, in particular g_thread_init(), while a
217 * timer is running will cause invalid return values from this function.
221 g_timer_elapsed (GTimer *timer,
222 gulong *microseconds)
227 g_return_val_if_fail (timer != NULL, 0);
230 timer->end = g_get_monotonic_time ();
232 elapsed = timer->end - timer->start;
234 total = elapsed / 1e9;
237 *microseconds = (elapsed / 1000) % 1000000;
243 g_usleep (gulong microseconds)
246 Sleep (microseconds / 1000);
248 struct timespec request, remaining;
249 request.tv_sec = microseconds / G_USEC_PER_SEC;
250 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
251 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
258 * @time_: a #GTimeVal
259 * @microseconds: number of microseconds to add to @time
261 * Adds the given number of microseconds to @time_. @microseconds can
262 * also be negative to decrease the value of @time_.
265 g_time_val_add (GTimeVal *time_, glong microseconds)
267 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
269 if (microseconds >= 0)
271 time_->tv_usec += microseconds % G_USEC_PER_SEC;
272 time_->tv_sec += microseconds / G_USEC_PER_SEC;
273 if (time_->tv_usec >= G_USEC_PER_SEC)
275 time_->tv_usec -= G_USEC_PER_SEC;
282 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
283 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
284 if (time_->tv_usec < 0)
286 time_->tv_usec += G_USEC_PER_SEC;
292 /* converts a broken down date representation, relative to UTC, to
293 * a timestamp; it uses timegm() if it's available.
296 mktime_utc (struct tm *tm)
301 static const gint days_before[] =
303 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
308 if (tm->tm_mon < 0 || tm->tm_mon > 11)
311 retval = (tm->tm_year - 70) * 365;
312 retval += (tm->tm_year - 68) / 4;
313 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
315 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
318 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
320 retval = timegm (tm);
321 #endif /* !HAVE_TIMEGM */
327 * g_time_val_from_iso8601:
328 * @iso_date: an ISO 8601 encoded date string
329 * @time_: a #GTimeVal
331 * Converts a string containing an ISO 8601 encoded date and time
332 * to a #GTimeVal and puts it into @time_.
334 * Return value: %TRUE if the conversion was successful.
339 g_time_val_from_iso8601 (const gchar *iso_date,
345 g_return_val_if_fail (iso_date != NULL, FALSE);
346 g_return_val_if_fail (time_ != NULL, FALSE);
348 /* Ensure that the first character is a digit,
349 * the first digit of the date, otherwise we don't
350 * have an ISO 8601 date */
351 while (g_ascii_isspace (*iso_date))
354 if (*iso_date == '\0')
357 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
360 val = strtoul (iso_date, (char **)&iso_date, 10);
361 if (*iso_date == '-')
364 tm.tm_year = val - 1900;
366 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
368 if (*iso_date++ != '-')
371 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
376 tm.tm_mday = val % 100;
377 tm.tm_mon = (val % 10000) / 100 - 1;
378 tm.tm_year = val / 10000 - 1900;
381 if (*iso_date != 'T')
384 if (*iso_date == '\0')
391 /* If there is a 'T' then there has to be a time */
392 if (!g_ascii_isdigit (*iso_date))
395 val = strtoul (iso_date, (char **)&iso_date, 10);
396 if (*iso_date == ':')
401 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
403 if (*iso_date++ != ':')
406 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
411 tm.tm_sec = val % 100;
412 tm.tm_min = (val % 10000) / 100;
413 tm.tm_hour = val / 10000;
418 if (*iso_date == ',' || *iso_date == '.')
422 while (g_ascii_isdigit (*++iso_date))
424 time_->tv_usec += (*iso_date - '0') * mul;
429 /* Now parse the offset and convert tm to a time_t */
430 if (*iso_date == 'Z')
433 time_->tv_sec = mktime_utc (&tm);
435 else if (*iso_date == '+' || *iso_date == '-')
437 gint sign = (*iso_date == '+') ? -1 : 1;
439 val = strtoul (iso_date + 1, (char **)&iso_date, 10);
441 if (*iso_date == ':')
442 val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
444 val = 60 * (val / 100) + (val % 100);
446 time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
450 /* No "Z" or offset, so local time */
451 tm.tm_isdst = -1; /* locale selects DST */
452 time_->tv_sec = mktime (&tm);
455 while (g_ascii_isspace (*iso_date))
458 return *iso_date == '\0';
462 * g_time_val_to_iso8601:
463 * @time_: a #GTimeVal
465 * Converts @time_ into an ISO 8601 encoded string, relative to the
466 * Coordinated Universal Time (UTC).
468 * Return value: a newly allocated string containing an ISO 8601 date
473 g_time_val_to_iso8601 (GTimeVal *time_)
482 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
484 secs = time_->tv_sec;
489 tm = gmtime_r (&secs, &tm_);
495 if (time_->tv_usec != 0)
497 /* ISO 8601 date and time format, with fractionary seconds:
498 * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
500 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
511 /* ISO 8601 date and time format:
512 * YYYY-MM-DDTHH:MM:SSZ
514 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",