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 #define G_NSEC_PER_SEC 1000000000
55 #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
56 #define USE_CLOCK_GETTIME 1
64 #elif USE_CLOCK_GETTIME
65 struct timespec start;
68 #else /* uses gettimeofday */
78 GetSystemTimeAsFileTime ((FILETIME *)&v)
79 #elif USE_CLOCK_GETTIME
81 clock_gettime (posix_clock, &v)
84 gettimeofday (&v, NULL)
87 #ifdef USE_CLOCK_GETTIME
88 static gint posix_clock = 0;
91 init_posix_clock (void)
93 static gboolean initialized = FALSE;
98 if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
99 posix_clock = CLOCK_MONOTONIC;
101 posix_clock = CLOCK_REALTIME;
111 timer = g_new (GTimer, 1);
112 timer->active = TRUE;
114 #ifdef USE_CLOCK_GETTIME
118 GETTIME (timer->start);
124 g_timer_destroy (GTimer *timer)
126 g_return_if_fail (timer != NULL);
132 g_timer_start (GTimer *timer)
134 g_return_if_fail (timer != NULL);
136 timer->active = TRUE;
138 GETTIME (timer->start);
142 g_timer_stop (GTimer *timer)
144 g_return_if_fail (timer != NULL);
146 timer->active = FALSE;
152 g_timer_reset (GTimer *timer)
154 g_return_if_fail (timer != NULL);
156 GETTIME (timer->start);
160 g_timer_continue (GTimer *timer)
164 #elif USE_CLOCK_GETTIME
165 struct timespec elapsed;
167 struct timeval elapsed;
170 g_return_if_fail (timer != NULL);
171 g_return_if_fail (timer->active == FALSE);
173 /* Get elapsed time and reset timer start time
174 * to the current time minus the previously
180 elapsed = timer->end - timer->start;
182 GETTIME (timer->start);
184 timer->start -= elapsed;
186 #elif USE_CLOCK_GETTIME
188 if (timer->start.tv_nsec > timer->end.tv_nsec)
190 timer->end.tv_nsec += G_NSEC_PER_SEC;
194 elapsed.tv_nsec = timer->end.tv_nsec - timer->start.tv_nsec;
195 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
197 GETTIME (timer->start);
199 if (timer->start.tv_nsec < elapsed.tv_nsec)
201 timer->start.tv_nsec += G_NSEC_PER_SEC;
202 timer->start.tv_sec--;
205 timer->start.tv_nsec -= elapsed.tv_nsec;
206 timer->start.tv_sec -= elapsed.tv_sec;
210 if (timer->start.tv_usec > timer->end.tv_usec)
212 timer->end.tv_usec += G_USEC_PER_SEC;
216 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
217 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
219 GETTIME (timer->start);
221 if (timer->start.tv_usec < elapsed.tv_usec)
223 timer->start.tv_usec += G_USEC_PER_SEC;
224 timer->start.tv_sec--;
227 timer->start.tv_usec -= elapsed.tv_usec;
228 timer->start.tv_sec -= elapsed.tv_sec;
230 #endif /* !G_OS_WIN32 */
232 timer->active = TRUE;
236 g_timer_elapsed (GTimer *timer,
237 gulong *microseconds)
242 #elif USE_CLOCK_GETTIME
243 struct timespec elapsed;
245 struct timeval elapsed;
248 g_return_val_if_fail (timer != NULL, 0);
252 GETTIME (timer->end);
254 elapsed = timer->end - timer->start;
256 total = elapsed / 1e7;
259 *microseconds = (elapsed / 10) % 1000000;
260 #elif USE_CLOCK_GETTIME
262 GETTIME (timer->end);
264 if (timer->start.tv_nsec > timer->end.tv_nsec)
266 timer->end.tv_nsec += G_NSEC_PER_SEC;
270 elapsed.tv_nsec = timer->end.tv_nsec - timer->start.tv_nsec;
271 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
273 total = elapsed.tv_sec + ((gdouble) elapsed.tv_nsec / (gdouble) G_NSEC_PER_SEC);
281 else if (microseconds)
282 *microseconds = elapsed.tv_nsec / 1000;
286 GETTIME (timer->end);
288 if (timer->start.tv_usec > timer->end.tv_usec)
290 timer->end.tv_usec += G_USEC_PER_SEC;
294 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
295 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
297 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / (gdouble) G_USEC_PER_SEC);
305 else if (microseconds)
306 *microseconds = elapsed.tv_usec;
314 g_usleep (gulong microseconds)
317 Sleep (microseconds / 1000);
318 #else /* !G_OS_WIN32 */
319 # ifdef HAVE_NANOSLEEP
320 struct timespec request, remaining;
321 request.tv_sec = microseconds / G_USEC_PER_SEC;
322 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
323 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
325 # else /* !HAVE_NANOSLEEP */
326 if (g_thread_supported ())
328 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
329 static GCond* cond = NULL;
332 g_get_current_time (&end_time);
333 if (microseconds > G_MAXLONG)
335 microseconds -= G_MAXLONG;
336 g_time_val_add (&end_time, G_MAXLONG);
338 g_time_val_add (&end_time, microseconds);
340 g_static_mutex_lock (&mutex);
343 cond = g_cond_new ();
345 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
349 g_static_mutex_unlock (&mutex);
354 tv.tv_sec = microseconds / G_USEC_PER_SEC;
355 tv.tv_usec = microseconds % G_USEC_PER_SEC;
356 select(0, NULL, NULL, NULL, &tv);
358 # endif /* !HAVE_NANOSLEEP */
359 #endif /* !G_OS_WIN32 */
364 * @time_: a #GTimeVal
365 * @microseconds: number of microseconds to add to @time
367 * Adds the given number of microseconds to @time_. @microseconds can
368 * also be negative to decrease the value of @time_.
371 g_time_val_add (GTimeVal *time_, glong microseconds)
373 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
375 if (microseconds >= 0)
377 time_->tv_usec += microseconds % G_USEC_PER_SEC;
378 time_->tv_sec += microseconds / G_USEC_PER_SEC;
379 if (time_->tv_usec >= G_USEC_PER_SEC)
381 time_->tv_usec -= G_USEC_PER_SEC;
388 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
389 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
390 if (time_->tv_usec < 0)
392 time_->tv_usec += G_USEC_PER_SEC;
398 /* converts a broken down date representation, relative to UTC, to
399 * a timestamp; it uses timegm() if it's available.
402 mktime_utc (struct tm *tm)
407 static const gint days_before[] =
409 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
414 if (tm->tm_mon < 0 || tm->tm_mon > 11)
417 retval = (tm->tm_year - 70) * 365;
418 retval += (tm->tm_year - 68) / 4;
419 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
421 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
424 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
426 retval = timegm (tm);
427 #endif /* !HAVE_TIMEGM */
433 * g_time_val_from_iso8601:
434 * @iso_date: a ISO 8601 encoded date string
435 * @time_: a #GTimeVal
437 * Converts a string containing an ISO 8601 encoded date and time
438 * to a #GTimeVal and puts it into @time_.
440 * Return value: %TRUE if the conversion was successful.
445 g_time_val_from_iso8601 (const gchar *iso_date,
451 g_return_val_if_fail (iso_date != NULL, FALSE);
452 g_return_val_if_fail (time_ != NULL, FALSE);
454 val = strtoul (iso_date, (char **)&iso_date, 10);
455 if (*iso_date == '-')
458 tm.tm_year = val - 1900;
460 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
462 if (*iso_date++ != '-')
465 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
470 tm.tm_mday = val % 100;
471 tm.tm_mon = (val % 10000) / 100 - 1;
472 tm.tm_year = val / 10000 - 1900;
475 if (*iso_date++ != 'T')
478 val = strtoul (iso_date, (char **)&iso_date, 10);
479 if (*iso_date == ':')
484 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
486 if (*iso_date++ != ':')
489 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
494 tm.tm_sec = val % 100;
495 tm.tm_min = (val % 10000) / 100;
496 tm.tm_hour = val / 10000;
499 time_->tv_sec = mktime_utc (&tm);
502 if (*iso_date == '.')
503 time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
505 if (*iso_date == '+' || *iso_date == '-')
507 gint sign = (*iso_date == '+') ? -1 : 1;
509 val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
511 if (*iso_date == ':')
512 val = 60 * val + strtoul (iso_date + 1, NULL, 10);
514 val = 60 * (val / 100) + (val % 100);
516 time_->tv_sec += (time_t) (val * sign);
523 * g_time_val_to_iso8601:
524 * @time_: a #GTimeVal
526 * Converts @time_ into a ISO 8601 encoded string, relative to the
527 * Coordinated Universal Time (UTC).
529 * Return value: a newly allocated string containing a ISO 8601 date
534 g_time_val_to_iso8601 (GTimeVal *time_)
538 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
540 #define ISO_8601_LEN 21
541 #define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
542 retval = g_new0 (gchar, ISO_8601_LEN + 1);
544 strftime (retval, ISO_8601_LEN,
546 gmtime (&(time_->tv_sec)));
551 #define __G_TIMER_C__
552 #include "galiasdef.c"