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 */
327 /* on AIX, nsleep is analogous to nanosleep */
328 struct timespec request, remaining;
329 request.tv_sec = microseconds / G_USEC_PER_SEC;
330 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
331 while (nsleep (&request, &remaining) == -1 && errno == EINTR)
333 # else /* !HAVE_NSLEEP */
334 if (g_thread_supported ())
336 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
337 static GCond* cond = NULL;
340 g_get_current_time (&end_time);
341 if (microseconds > G_MAXLONG)
343 microseconds -= G_MAXLONG;
344 g_time_val_add (&end_time, G_MAXLONG);
346 g_time_val_add (&end_time, microseconds);
348 g_static_mutex_lock (&mutex);
351 cond = g_cond_new ();
353 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
357 g_static_mutex_unlock (&mutex);
362 tv.tv_sec = microseconds / G_USEC_PER_SEC;
363 tv.tv_usec = microseconds % G_USEC_PER_SEC;
364 select(0, NULL, NULL, NULL, &tv);
366 # endif /* !HAVE_NSLEEP */
367 # endif /* !HAVE_NANOSLEEP */
368 #endif /* !G_OS_WIN32 */
373 * @time_: a #GTimeVal
374 * @microseconds: number of microseconds to add to @time
376 * Adds the given number of microseconds to @time_. @microseconds can
377 * also be negative to decrease the value of @time_.
380 g_time_val_add (GTimeVal *time_, glong microseconds)
382 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
384 if (microseconds >= 0)
386 time_->tv_usec += microseconds % G_USEC_PER_SEC;
387 time_->tv_sec += microseconds / G_USEC_PER_SEC;
388 if (time_->tv_usec >= G_USEC_PER_SEC)
390 time_->tv_usec -= G_USEC_PER_SEC;
397 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
398 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
399 if (time_->tv_usec < 0)
401 time_->tv_usec += G_USEC_PER_SEC;
407 /* converts a broken down date representation, relative to UTC, to
408 * a timestamp; it uses timegm() if it's available.
411 mktime_utc (struct tm *tm)
416 static const gint days_before[] =
418 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
423 if (tm->tm_mon < 0 || tm->tm_mon > 11)
426 retval = (tm->tm_year - 70) * 365;
427 retval += (tm->tm_year - 68) / 4;
428 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
430 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
433 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
435 retval = timegm (tm);
436 #endif /* !HAVE_TIMEGM */
442 * g_time_val_from_iso8601:
443 * @iso_date: a ISO 8601 encoded date string
444 * @time_: a #GTimeVal
446 * Converts a string containing an ISO 8601 encoded date and time
447 * to a #GTimeVal and puts it into @time_.
449 * Return value: %TRUE if the conversion was successful.
454 g_time_val_from_iso8601 (const gchar *iso_date,
460 g_return_val_if_fail (iso_date != NULL, FALSE);
461 g_return_val_if_fail (time_ != NULL, FALSE);
463 val = strtoul (iso_date, (char **)&iso_date, 10);
464 if (*iso_date == '-')
467 tm.tm_year = val - 1900;
469 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
471 if (*iso_date++ != '-')
474 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
479 tm.tm_mday = val % 100;
480 tm.tm_mon = (val % 10000) / 100 - 1;
481 tm.tm_year = val / 10000 - 1900;
484 if (*iso_date++ != 'T')
487 val = strtoul (iso_date, (char **)&iso_date, 10);
488 if (*iso_date == ':')
493 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
495 if (*iso_date++ != ':')
498 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
503 tm.tm_sec = val % 100;
504 tm.tm_min = (val % 10000) / 100;
505 tm.tm_hour = val / 10000;
508 time_->tv_sec = mktime_utc (&tm);
511 if (*iso_date == '.')
512 time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
514 if (*iso_date == '+' || *iso_date == '-')
516 gint sign = (*iso_date == '+') ? -1 : 1;
518 val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
520 if (*iso_date == ':')
521 val = 60 * val + strtoul (iso_date + 1, NULL, 10);
523 val = 60 * (val / 100) + (val % 100);
525 time_->tv_sec += (time_t) (val * sign);
532 * g_time_val_to_iso8601:
533 * @time_: a #GTimeVal
535 * Converts @time_ into a ISO 8601 encoded string, relative to the
536 * Coordinated Universal Time (UTC).
538 * Return value: a newly allocated string containing a ISO 8601 date
543 g_time_val_to_iso8601 (GTimeVal *time_)
547 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
549 #define ISO_8601_LEN 21
550 #define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
551 retval = g_new0 (gchar, ISO_8601_LEN + 1);
553 strftime (retval, ISO_8601_LEN,
555 gmtime (&(time_->tv_sec)));
560 #define __G_TIMER_C__
561 #include "galiasdef.c"