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.
73 * Opaque datatype that records a start time.
85 * @Returns: a new #GTimer.
87 * Creates a new timer, and starts timing (i.e. g_timer_start() is
88 * implicitly called for you).
95 timer = g_new (GTimer, 1);
98 timer->start = g_get_monotonic_time ();
105 * @timer: a #GTimer to destroy.
107 * Destroys a timer, freeing associated resources.
110 g_timer_destroy (GTimer *timer)
112 g_return_if_fail (timer != NULL);
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.
127 g_timer_start (GTimer *timer)
129 g_return_if_fail (timer != NULL);
131 timer->active = TRUE;
133 timer->start = g_get_monotonic_time ();
140 * Marks an end time, so calls to g_timer_elapsed() will return the
141 * difference between this end time and the start time.
144 g_timer_stop (GTimer *timer)
146 g_return_if_fail (timer != NULL);
148 timer->active = FALSE;
150 timer->end = g_get_monotonic_time ();
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()
162 g_timer_reset (GTimer *timer)
164 g_return_if_fail (timer != NULL);
166 timer->start = g_get_monotonic_time ();
173 * Resumes a timer that has previously been stopped with
174 * g_timer_stop(). g_timer_stop() must be called before using this
180 g_timer_continue (GTimer *timer)
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
192 elapsed = timer->end - timer->start;
194 timer->start = g_get_monotonic_time ();
196 timer->start -= elapsed;
198 timer->active = TRUE;
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
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.
218 g_timer_elapsed (GTimer *timer,
219 gulong *microseconds)
224 g_return_val_if_fail (timer != NULL, 0);
227 timer->end = g_get_monotonic_time ();
229 elapsed = timer->end - timer->start;
231 total = elapsed / 1e6;
234 *microseconds = elapsed % 1000000;
241 * @microseconds: number of microseconds to pause
243 * Pauses the current thread for the given number of microseconds.
245 * There are 1 million microseconds per second (represented by the
246 * #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
247 * depending on hardware and operating system; don't rely on the exact
248 * length of the sleep.
251 g_usleep (gulong microseconds)
254 Sleep (microseconds / 1000);
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)
266 * @time_: a #GTimeVal
267 * @microseconds: number of microseconds to add to @time
269 * Adds the given number of microseconds to @time_. @microseconds can
270 * also be negative to decrease the value of @time_.
273 g_time_val_add (GTimeVal *time_, glong microseconds)
275 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
277 if (microseconds >= 0)
279 time_->tv_usec += microseconds % G_USEC_PER_SEC;
280 time_->tv_sec += microseconds / G_USEC_PER_SEC;
281 if (time_->tv_usec >= G_USEC_PER_SEC)
283 time_->tv_usec -= G_USEC_PER_SEC;
290 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
291 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
292 if (time_->tv_usec < 0)
294 time_->tv_usec += G_USEC_PER_SEC;
300 /* converts a broken down date representation, relative to UTC, to
301 * a timestamp; it uses timegm() if it's available.
304 mktime_utc (struct tm *tm)
309 static const gint days_before[] =
311 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
316 if (tm->tm_mon < 0 || tm->tm_mon > 11)
319 retval = (tm->tm_year - 70) * 365;
320 retval += (tm->tm_year - 68) / 4;
321 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
323 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
326 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
328 retval = timegm (tm);
329 #endif /* !HAVE_TIMEGM */
335 * g_time_val_from_iso8601:
336 * @iso_date: an ISO 8601 encoded date string
337 * @time_: (out): a #GTimeVal
339 * Converts a string containing an ISO 8601 encoded date and time
340 * to a #GTimeVal and puts it into @time_.
342 * @iso_date must include year, month, day, hours, minutes, and
343 * seconds. It can optionally include fractions of a second and a time
344 * zone indicator. (In the absence of any time zone indication, the
345 * timestamp is assumed to be in local time.)
347 * Return value: %TRUE if the conversion was successful.
352 g_time_val_from_iso8601 (const gchar *iso_date,
358 g_return_val_if_fail (iso_date != NULL, FALSE);
359 g_return_val_if_fail (time_ != NULL, FALSE);
361 /* Ensure that the first character is a digit,
362 * the first digit of the date, otherwise we don't
363 * have an ISO 8601 date */
364 while (g_ascii_isspace (*iso_date))
367 if (*iso_date == '\0')
370 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
373 val = strtoul (iso_date, (char **)&iso_date, 10);
374 if (*iso_date == '-')
377 tm.tm_year = val - 1900;
379 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
381 if (*iso_date++ != '-')
384 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
389 tm.tm_mday = val % 100;
390 tm.tm_mon = (val % 10000) / 100 - 1;
391 tm.tm_year = val / 10000 - 1900;
394 if (*iso_date != 'T')
397 if (*iso_date == '\0')
404 /* If there is a 'T' then there has to be a time */
405 if (!g_ascii_isdigit (*iso_date))
408 val = strtoul (iso_date, (char **)&iso_date, 10);
409 if (*iso_date == ':')
414 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
416 if (*iso_date++ != ':')
419 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
424 tm.tm_sec = val % 100;
425 tm.tm_min = (val % 10000) / 100;
426 tm.tm_hour = val / 10000;
431 if (*iso_date == ',' || *iso_date == '.')
435 while (g_ascii_isdigit (*++iso_date))
437 time_->tv_usec += (*iso_date - '0') * mul;
442 /* Now parse the offset and convert tm to a time_t */
443 if (*iso_date == 'Z')
446 time_->tv_sec = mktime_utc (&tm);
448 else if (*iso_date == '+' || *iso_date == '-')
450 gint sign = (*iso_date == '+') ? -1 : 1;
452 val = strtoul (iso_date + 1, (char **)&iso_date, 10);
454 if (*iso_date == ':')
455 val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
457 val = 60 * (val / 100) + (val % 100);
459 time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
463 /* No "Z" or offset, so local time */
464 tm.tm_isdst = -1; /* locale selects DST */
465 time_->tv_sec = mktime (&tm);
468 while (g_ascii_isspace (*iso_date))
471 return *iso_date == '\0';
475 * g_time_val_to_iso8601:
476 * @time_: a #GTimeVal
478 * Converts @time_ into an RFC 3339 encoded string, relative to the
479 * Coordinated Universal Time (UTC). This is one of the many formats
480 * allowed by ISO 8601.
482 * ISO 8601 allows a large number of date/time formats, with or without
483 * punctuation and optional elements. The format returned by this function
484 * is a complete date and time, with optional punctuation included, the
485 * UTC time zone represented as "Z", and the @tv_usec part included if
486 * and only if it is nonzero, i.e. either
487 * "YYYY-MM-DDTHH:MM:SSZ" or "YYYY-MM-DDTHH:MM:SS.fffffZ".
489 * This corresponds to the Internet date/time format defined by
490 * <ulink url="https://www.ietf.org/rfc/rfc3339.txt">RFC 3339</ulink>, and
491 * to either of the two most-precise formats defined by
492 * <ulink url="http://www.w3.org/TR/NOTE-datetime-19980827">the W3C Note
493 * "Date and Time Formats"</ulink>. Both of these documents are profiles of
496 * Use g_date_time_format() or g_strdup_printf() if a different
497 * variation of ISO 8601 format is required.
499 * Return value: a newly allocated string containing an ISO 8601 date
504 g_time_val_to_iso8601 (GTimeVal *time_)
513 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
515 secs = time_->tv_sec;
520 tm = gmtime_r (&secs, &tm_);
526 if (time_->tv_usec != 0)
528 /* ISO 8601 date and time format, with fractionary seconds:
529 * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
531 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
542 /* ISO 8601 date and time format:
543 * YYYY-MM-DDTHH:MM:SSZ
545 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",