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
60 #elif HAVE_CLOCK_GETTIME
61 struct timespec start;
64 #else /* uses gettimeofday */
74 GetSystemTimeAsFileTime ((FILETIME *)&v)
75 #elif HAVE_CLOCK_GETTIME
77 clock_gettime (posix_clock, &v)
80 gettimeofday (&v, NULL)
83 #ifdef HAVE_CLOCK_GETTIME
84 static gint posix_clock = 0;
87 init_posix_clock (void)
89 static gboolean initialized = FALSE;
94 #if !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK >= 0
95 if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
96 posix_clock = CLOCK_MONOTONIC;
99 posix_clock = CLOCK_REALTIME;
109 timer = g_new (GTimer, 1);
110 timer->active = TRUE;
112 #ifdef HAVE_CLOCK_GETTIME
116 GETTIME (timer->start);
122 g_timer_destroy (GTimer *timer)
124 g_return_if_fail (timer != NULL);
130 g_timer_start (GTimer *timer)
132 g_return_if_fail (timer != NULL);
134 timer->active = TRUE;
136 GETTIME (timer->start);
140 g_timer_stop (GTimer *timer)
142 g_return_if_fail (timer != NULL);
144 timer->active = FALSE;
150 g_timer_reset (GTimer *timer)
152 g_return_if_fail (timer != NULL);
154 GETTIME (timer->start);
158 g_timer_continue (GTimer *timer)
162 #elif HAVE_CLOCK_GETTIME
163 struct timespec elapsed;
165 struct timeval elapsed;
168 g_return_if_fail (timer != NULL);
169 g_return_if_fail (timer->active == FALSE);
171 /* Get elapsed time and reset timer start time
172 * to the current time minus the previously
178 elapsed = timer->end - timer->start;
180 GETTIME (timer->start);
182 timer->start -= elapsed;
184 #elif HAVE_CLOCK_GETTIME
186 if (timer->start.tv_nsec > timer->end.tv_nsec)
188 timer->end.tv_nsec += G_NSEC_PER_SEC;
192 elapsed.tv_nsec = timer->end.tv_nsec - timer->start.tv_nsec;
193 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
195 GETTIME (timer->start);
197 if (timer->start.tv_nsec < elapsed.tv_nsec)
199 timer->start.tv_nsec += G_NSEC_PER_SEC;
200 timer->start.tv_sec--;
203 timer->start.tv_nsec -= elapsed.tv_nsec;
204 timer->start.tv_sec -= elapsed.tv_sec;
208 if (timer->start.tv_usec > timer->end.tv_usec)
210 timer->end.tv_usec += G_USEC_PER_SEC;
214 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
215 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
217 GETTIME (timer->start);
219 if (timer->start.tv_usec < elapsed.tv_usec)
221 timer->start.tv_usec += G_USEC_PER_SEC;
222 timer->start.tv_sec--;
225 timer->start.tv_usec -= elapsed.tv_usec;
226 timer->start.tv_sec -= elapsed.tv_sec;
228 #endif /* !G_OS_WIN32 */
230 timer->active = TRUE;
234 g_timer_elapsed (GTimer *timer,
235 gulong *microseconds)
240 #elif HAVE_CLOCK_GETTIME
241 struct timespec elapsed;
243 struct timeval elapsed;
246 g_return_val_if_fail (timer != NULL, 0);
250 GETTIME (timer->end);
252 elapsed = timer->end - timer->start;
254 total = elapsed / 1e7;
257 *microseconds = (elapsed / 10) % 1000000;
258 #elif HAVE_CLOCK_GETTIME
260 GETTIME (timer->end);
262 if (timer->start.tv_nsec > timer->end.tv_nsec)
264 timer->end.tv_nsec += G_NSEC_PER_SEC;
268 elapsed.tv_nsec = timer->end.tv_nsec - timer->start.tv_nsec;
269 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
271 total = elapsed.tv_sec + ((gdouble) elapsed.tv_nsec / (gdouble) G_NSEC_PER_SEC);
279 else if (microseconds)
280 *microseconds = elapsed.tv_nsec / 1000;
284 GETTIME (timer->end);
286 if (timer->start.tv_usec > timer->end.tv_usec)
288 timer->end.tv_usec += G_USEC_PER_SEC;
292 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
293 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
295 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / (gdouble) G_USEC_PER_SEC);
303 else if (microseconds)
304 *microseconds = elapsed.tv_usec;
312 g_usleep (gulong microseconds)
315 Sleep (microseconds / 1000);
316 #else /* !G_OS_WIN32 */
317 # ifdef HAVE_NANOSLEEP
318 struct timespec request, remaining;
319 request.tv_sec = microseconds / G_USEC_PER_SEC;
320 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
321 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
323 # else /* !HAVE_NANOSLEEP */
324 if (g_thread_supported ())
326 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
327 static GCond* cond = NULL;
330 g_get_current_time (&end_time);
331 if (microseconds > G_MAXLONG)
333 microseconds -= G_MAXLONG;
334 g_time_val_add (&end_time, G_MAXLONG);
336 g_time_val_add (&end_time, microseconds);
338 g_static_mutex_lock (&mutex);
341 cond = g_cond_new ();
343 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
347 g_static_mutex_unlock (&mutex);
352 tv.tv_sec = microseconds / G_USEC_PER_SEC;
353 tv.tv_usec = microseconds % G_USEC_PER_SEC;
354 select(0, NULL, NULL, NULL, &tv);
356 # endif /* !HAVE_NANOSLEEP */
357 #endif /* !G_OS_WIN32 */
362 * @time_: a #GTimeVal
363 * @microseconds: number of microseconds to add to @time
365 * Adds the given number of microseconds to @time_. @microseconds can
366 * also be negative to decrease the value of @time_.
369 g_time_val_add (GTimeVal *time_, glong microseconds)
371 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
373 if (microseconds >= 0)
375 time_->tv_usec += microseconds % G_USEC_PER_SEC;
376 time_->tv_sec += microseconds / G_USEC_PER_SEC;
377 if (time_->tv_usec >= G_USEC_PER_SEC)
379 time_->tv_usec -= G_USEC_PER_SEC;
386 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
387 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
388 if (time_->tv_usec < 0)
390 time_->tv_usec += G_USEC_PER_SEC;
396 /* converts a broken down date representation, relative to UTC, to
397 * a timestamp; it uses timegm() if it's available.
400 mktime_utc (struct tm *tm)
405 static const gint days_before[] =
407 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
412 if (tm->tm_mon < 0 || tm->tm_mon > 11)
415 retval = (tm->tm_year - 70) * 365;
416 retval += (tm->tm_year - 68) / 4;
417 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
419 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
422 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
424 retval = timegm (tm);
425 #endif /* !HAVE_TIMEGM */
431 * g_time_val_from_iso8601:
432 * @iso_date: a ISO 8601 encoded date string
433 * @time_: a #GTimeVal
435 * Converts a string containing an ISO 8601 encoded date and time
436 * to a #GTimeVal and puts it into @time_.
438 * Return value: %TRUE if the conversion was successful.
443 g_time_val_from_iso8601 (const gchar *iso_date,
449 g_return_val_if_fail (iso_date != NULL, FALSE);
450 g_return_val_if_fail (time_ != NULL, FALSE);
452 val = strtoul (iso_date, (char **)&iso_date, 10);
453 if (*iso_date == '-')
456 tm.tm_year = val - 1900;
458 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
460 if (*iso_date++ != '-')
463 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
468 tm.tm_mday = val % 100;
469 tm.tm_mon = (val % 10000) / 100 - 1;
470 tm.tm_year = val / 10000 - 1900;
473 if (*iso_date++ != 'T')
476 val = strtoul (iso_date, (char **)&iso_date, 10);
477 if (*iso_date == ':')
482 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
484 if (*iso_date++ != ':')
487 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
492 tm.tm_sec = val % 100;
493 tm.tm_min = (val % 10000) / 100;
494 tm.tm_hour = val / 10000;
497 time_->tv_sec = mktime_utc (&tm);
500 if (*iso_date == '.')
501 time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
503 if (*iso_date == '+' || *iso_date == '-')
505 gint sign = (*iso_date == '+') ? -1 : 1;
507 val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
509 if (*iso_date == ':')
510 val = 60 * val + strtoul (iso_date + 1, NULL, 10);
512 val = 60 * (val / 100) + (val % 100);
514 time_->tv_sec += (time_t) (val * sign);
521 * g_time_val_to_iso8601:
522 * @time_: a #GTimeVal
524 * Converts @time_ into a ISO 8601 encoded string, relative to the
525 * Coordinated Universal Time (UTC).
527 * Return value: a newly allocated string containing a ISO 8601 date
532 g_time_val_to_iso8601 (GTimeVal *time_)
536 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
538 #define ISO_8601_LEN 21
539 #define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
540 retval = g_new0 (gchar, ISO_8601_LEN + 1);
542 strftime (retval, ISO_8601_LEN,
544 gmtime (&(time_->tv_sec)));
549 #define __G_TIMER_C__
550 #include "galiasdef.c"