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 */
54 #define G_NSEC_PER_SEC 1000000000
56 #define GETTIME(v) (v = g_thread_gettime ())
72 if (!g_thread_supported ())
73 g_warning ("g_timer_new() called, but GThreads not initialized yet. "
74 "Call g_thread_init ().");
76 timer = g_new (GTimer, 1);
79 GETTIME (timer->start);
85 g_timer_destroy (GTimer *timer)
87 g_return_if_fail (timer != NULL);
93 g_timer_start (GTimer *timer)
95 g_return_if_fail (timer != NULL);
99 GETTIME (timer->start);
103 g_timer_stop (GTimer *timer)
105 g_return_if_fail (timer != NULL);
107 timer->active = FALSE;
109 GETTIME (timer->end);
113 g_timer_reset (GTimer *timer)
115 g_return_if_fail (timer != NULL);
117 GETTIME (timer->start);
121 g_timer_continue (GTimer *timer)
125 g_return_if_fail (timer != NULL);
126 g_return_if_fail (timer->active == FALSE);
128 /* Get elapsed time and reset timer start time
129 * to the current time minus the previously
133 elapsed = timer->end - timer->start;
135 GETTIME (timer->start);
137 timer->start -= elapsed;
139 timer->active = TRUE;
143 g_timer_elapsed (GTimer *timer,
144 gulong *microseconds)
149 g_return_val_if_fail (timer != NULL, 0);
152 GETTIME (timer->end);
154 elapsed = timer->end - timer->start;
156 total = elapsed / 1e9;
159 *microseconds = (elapsed / 1000) % 1000000;
165 g_usleep (gulong microseconds)
168 Sleep (microseconds / 1000);
169 #else /* !G_OS_WIN32 */
170 # ifdef HAVE_NANOSLEEP
171 struct timespec request, remaining;
172 request.tv_sec = microseconds / G_USEC_PER_SEC;
173 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
174 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
176 # else /* !HAVE_NANOSLEEP */
178 /* on AIX, nsleep is analogous to nanosleep */
179 struct timespec request, remaining;
180 request.tv_sec = microseconds / G_USEC_PER_SEC;
181 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
182 while (nsleep (&request, &remaining) == -1 && errno == EINTR)
184 # else /* !HAVE_NSLEEP */
185 if (g_thread_supported ())
187 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
188 static GCond* cond = NULL;
191 g_get_current_time (&end_time);
192 if (microseconds > G_MAXLONG)
194 microseconds -= G_MAXLONG;
195 g_time_val_add (&end_time, G_MAXLONG);
197 g_time_val_add (&end_time, microseconds);
199 g_static_mutex_lock (&mutex);
202 cond = g_cond_new ();
204 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
208 g_static_mutex_unlock (&mutex);
213 tv.tv_sec = microseconds / G_USEC_PER_SEC;
214 tv.tv_usec = microseconds % G_USEC_PER_SEC;
215 select(0, NULL, NULL, NULL, &tv);
217 # endif /* !HAVE_NSLEEP */
218 # endif /* !HAVE_NANOSLEEP */
219 #endif /* !G_OS_WIN32 */
224 * @time_: a #GTimeVal
225 * @microseconds: number of microseconds to add to @time
227 * Adds the given number of microseconds to @time_. @microseconds can
228 * also be negative to decrease the value of @time_.
231 g_time_val_add (GTimeVal *time_, glong microseconds)
233 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
235 if (microseconds >= 0)
237 time_->tv_usec += microseconds % G_USEC_PER_SEC;
238 time_->tv_sec += microseconds / G_USEC_PER_SEC;
239 if (time_->tv_usec >= G_USEC_PER_SEC)
241 time_->tv_usec -= G_USEC_PER_SEC;
248 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
249 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
250 if (time_->tv_usec < 0)
252 time_->tv_usec += G_USEC_PER_SEC;
258 /* converts a broken down date representation, relative to UTC, to
259 * a timestamp; it uses timegm() if it's available.
262 mktime_utc (struct tm *tm)
267 static const gint days_before[] =
269 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
274 if (tm->tm_mon < 0 || tm->tm_mon > 11)
277 retval = (tm->tm_year - 70) * 365;
278 retval += (tm->tm_year - 68) / 4;
279 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
281 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
284 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
286 retval = timegm (tm);
287 #endif /* !HAVE_TIMEGM */
293 * g_time_val_from_iso8601:
294 * @iso_date: a ISO 8601 encoded date string
295 * @time_: a #GTimeVal
297 * Converts a string containing an ISO 8601 encoded date and time
298 * to a #GTimeVal and puts it into @time_.
300 * Return value: %TRUE if the conversion was successful.
305 g_time_val_from_iso8601 (const gchar *iso_date,
311 g_return_val_if_fail (iso_date != NULL, FALSE);
312 g_return_val_if_fail (time_ != NULL, FALSE);
314 /* Ensure that the first character is a digit,
315 * the first digit of the date, otherwise we don't
316 * have an ISO 8601 date */
317 while (g_ascii_isspace (*iso_date))
320 if (*iso_date == '\0')
323 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
326 val = strtoul (iso_date, (char **)&iso_date, 10);
327 if (*iso_date == '-')
330 tm.tm_year = val - 1900;
332 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
334 if (*iso_date++ != '-')
337 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
342 tm.tm_mday = val % 100;
343 tm.tm_mon = (val % 10000) / 100 - 1;
344 tm.tm_year = val / 10000 - 1900;
347 if (*iso_date++ != 'T')
350 val = strtoul (iso_date, (char **)&iso_date, 10);
351 if (*iso_date == ':')
356 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
358 if (*iso_date++ != ':')
361 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
366 tm.tm_sec = val % 100;
367 tm.tm_min = (val % 10000) / 100;
368 tm.tm_hour = val / 10000;
371 time_->tv_sec = mktime_utc (&tm);
374 if (*iso_date == '.')
375 time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
377 if (*iso_date == '+' || *iso_date == '-')
379 gint sign = (*iso_date == '+') ? -1 : 1;
381 val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
383 if (*iso_date == ':')
384 val = 60 * val + strtoul (iso_date + 1, NULL, 10);
386 val = 60 * (val / 100) + (val % 100);
388 time_->tv_sec += (time_t) (val * sign);
395 * g_time_val_to_iso8601:
396 * @time_: a #GTimeVal
398 * Converts @time_ into a ISO 8601 encoded string, relative to the
399 * Coordinated Universal Time (UTC).
401 * Return value: a newly allocated string containing a ISO 8601 date
406 g_time_val_to_iso8601 (GTimeVal *time_)
413 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
415 #define ISO_8601_LEN 21
416 #define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
417 retval = g_new0 (gchar, ISO_8601_LEN + 1);
419 strftime (retval, ISO_8601_LEN,
422 gmtime_r (&(time_->tv_sec), &tm_)
424 gmtime (&(time_->tv_sec))
431 #define __G_TIMER_C__
432 #include "galiasdef.c"