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 timer = g_new (GTimer, 1);
75 GETTIME (timer->start);
81 g_timer_destroy (GTimer *timer)
83 g_return_if_fail (timer != NULL);
89 g_timer_start (GTimer *timer)
91 g_return_if_fail (timer != NULL);
95 GETTIME (timer->start);
99 g_timer_stop (GTimer *timer)
101 g_return_if_fail (timer != NULL);
103 timer->active = FALSE;
105 GETTIME (timer->end);
109 g_timer_reset (GTimer *timer)
111 g_return_if_fail (timer != NULL);
113 GETTIME (timer->start);
117 g_timer_continue (GTimer *timer)
121 g_return_if_fail (timer != NULL);
122 g_return_if_fail (timer->active == FALSE);
124 /* Get elapsed time and reset timer start time
125 * to the current time minus the previously
129 elapsed = timer->end - timer->start;
131 GETTIME (timer->start);
133 timer->start -= elapsed;
135 timer->active = TRUE;
139 g_timer_elapsed (GTimer *timer,
140 gulong *microseconds)
145 g_return_val_if_fail (timer != NULL, 0);
148 GETTIME (timer->end);
150 elapsed = timer->end - timer->start;
152 total = elapsed / 1e9;
155 *microseconds = (elapsed / 1000) % 1000000;
161 g_usleep (gulong microseconds)
164 Sleep (microseconds / 1000);
165 #else /* !G_OS_WIN32 */
166 # ifdef HAVE_NANOSLEEP
167 struct timespec request, remaining;
168 request.tv_sec = microseconds / G_USEC_PER_SEC;
169 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
170 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
172 # else /* !HAVE_NANOSLEEP */
174 /* on AIX, nsleep is analogous to nanosleep */
175 struct timespec request, remaining;
176 request.tv_sec = microseconds / G_USEC_PER_SEC;
177 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
178 while (nsleep (&request, &remaining) == -1 && errno == EINTR)
180 # else /* !HAVE_NSLEEP */
181 if (g_thread_supported ())
183 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
184 static GCond* cond = NULL;
187 g_get_current_time (&end_time);
188 if (microseconds > G_MAXLONG)
190 microseconds -= G_MAXLONG;
191 g_time_val_add (&end_time, G_MAXLONG);
193 g_time_val_add (&end_time, microseconds);
195 g_static_mutex_lock (&mutex);
198 cond = g_cond_new ();
200 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
204 g_static_mutex_unlock (&mutex);
209 tv.tv_sec = microseconds / G_USEC_PER_SEC;
210 tv.tv_usec = microseconds % G_USEC_PER_SEC;
211 select(0, NULL, NULL, NULL, &tv);
213 # endif /* !HAVE_NSLEEP */
214 # endif /* !HAVE_NANOSLEEP */
215 #endif /* !G_OS_WIN32 */
220 * @time_: a #GTimeVal
221 * @microseconds: number of microseconds to add to @time
223 * Adds the given number of microseconds to @time_. @microseconds can
224 * also be negative to decrease the value of @time_.
227 g_time_val_add (GTimeVal *time_, glong microseconds)
229 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
231 if (microseconds >= 0)
233 time_->tv_usec += microseconds % G_USEC_PER_SEC;
234 time_->tv_sec += microseconds / G_USEC_PER_SEC;
235 if (time_->tv_usec >= G_USEC_PER_SEC)
237 time_->tv_usec -= G_USEC_PER_SEC;
244 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
245 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
246 if (time_->tv_usec < 0)
248 time_->tv_usec += G_USEC_PER_SEC;
254 /* converts a broken down date representation, relative to UTC, to
255 * a timestamp; it uses timegm() if it's available.
258 mktime_utc (struct tm *tm)
263 static const gint days_before[] =
265 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
270 if (tm->tm_mon < 0 || tm->tm_mon > 11)
273 retval = (tm->tm_year - 70) * 365;
274 retval += (tm->tm_year - 68) / 4;
275 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
277 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
280 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
282 retval = timegm (tm);
283 #endif /* !HAVE_TIMEGM */
289 * g_time_val_from_iso8601:
290 * @iso_date: an ISO 8601 encoded date string
291 * @time_: a #GTimeVal
293 * Converts a string containing an ISO 8601 encoded date and time
294 * to a #GTimeVal and puts it into @time_.
296 * Return value: %TRUE if the conversion was successful.
301 g_time_val_from_iso8601 (const gchar *iso_date,
307 g_return_val_if_fail (iso_date != NULL, FALSE);
308 g_return_val_if_fail (time_ != NULL, FALSE);
310 /* Ensure that the first character is a digit,
311 * the first digit of the date, otherwise we don't
312 * have an ISO 8601 date */
313 while (g_ascii_isspace (*iso_date))
316 if (*iso_date == '\0')
319 if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
322 val = strtoul (iso_date, (char **)&iso_date, 10);
323 if (*iso_date == '-')
326 tm.tm_year = val - 1900;
328 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
330 if (*iso_date++ != '-')
333 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
338 tm.tm_mday = val % 100;
339 tm.tm_mon = (val % 10000) / 100 - 1;
340 tm.tm_year = val / 10000 - 1900;
343 if (*iso_date++ != 'T')
346 val = strtoul (iso_date, (char **)&iso_date, 10);
347 if (*iso_date == ':')
352 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
354 if (*iso_date++ != ':')
357 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
362 tm.tm_sec = val % 100;
363 tm.tm_min = (val % 10000) / 100;
364 tm.tm_hour = val / 10000;
369 if (*iso_date == ',' || *iso_date == '.')
373 while (g_ascii_isdigit (*++iso_date))
375 time_->tv_usec += (*iso_date - '0') * mul;
380 /* Now parse the offset and convert tm to a time_t */
381 if (*iso_date == 'Z')
384 time_->tv_sec = mktime_utc (&tm);
386 else if (*iso_date == '+' || *iso_date == '-')
388 gint sign = (*iso_date == '+') ? -1 : 1;
390 val = strtoul (iso_date + 1, (char **)&iso_date, 10);
392 if (*iso_date == ':')
393 val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
395 val = 60 * (val / 100) + (val % 100);
397 time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
401 /* No "Z" or offset, so local time */
402 tm.tm_isdst = -1; /* locale selects DST */
403 time_->tv_sec = mktime (&tm);
406 while (g_ascii_isspace (*iso_date))
409 return *iso_date == '\0';
413 * g_time_val_to_iso8601:
414 * @time_: a #GTimeVal
416 * Converts @time_ into an ISO 8601 encoded string, relative to the
417 * Coordinated Universal Time (UTC).
419 * Return value: a newly allocated string containing an ISO 8601 date
424 g_time_val_to_iso8601 (GTimeVal *time_)
433 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
435 secs = time_->tv_sec;
440 tm = gmtime_r (&secs, &tm_);
446 if (time_->tv_usec != 0)
448 /* ISO 8601 date and time format, with fractionary seconds:
449 * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
451 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
462 /* ISO 8601 date and time format:
463 * YYYY-MM-DDTHH:MM:SSZ
465 retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",
477 #define __G_TIMER_C__
478 #include "galiasdef.c"