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 */
59 #else /* !G_OS_WIN32 */
62 #endif /* !G_OS_WIN32 */
69 GetSystemTimeAsFileTime ((FILETIME *)&v)
70 #else /* !G_OS_WIN32 */
72 gettimeofday (&v, NULL)
73 #endif /* !G_OS_WIN32 */
80 timer = g_new (GTimer, 1);
83 GETTIME (timer->start);
89 g_timer_destroy (GTimer *timer)
91 g_return_if_fail (timer != NULL);
97 g_timer_start (GTimer *timer)
99 g_return_if_fail (timer != NULL);
101 timer->active = TRUE;
103 GETTIME (timer->start);
107 g_timer_stop (GTimer *timer)
109 g_return_if_fail (timer != NULL);
111 timer->active = FALSE;
117 g_timer_reset (GTimer *timer)
119 g_return_if_fail (timer != NULL);
121 GETTIME (timer->start);
125 g_timer_continue (GTimer *timer)
130 struct timeval elapsed;
131 #endif /* G_OS_WIN32 */
133 g_return_if_fail (timer != NULL);
134 g_return_if_fail (timer->active == FALSE);
136 /* Get elapsed time and reset timer start time
137 * to the current time minus the previously
143 elapsed = timer->end - timer->start;
145 GETTIME (timer->start);
147 timer->start -= elapsed;
149 #else /* !G_OS_WIN32 */
151 if (timer->start.tv_usec > timer->end.tv_usec)
153 timer->end.tv_usec += G_USEC_PER_SEC;
157 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
158 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
160 GETTIME (timer->start);
162 if (timer->start.tv_usec < elapsed.tv_usec)
164 timer->start.tv_usec += G_USEC_PER_SEC;
165 timer->start.tv_sec--;
168 timer->start.tv_usec -= elapsed.tv_usec;
169 timer->start.tv_sec -= elapsed.tv_sec;
171 #endif /* !G_OS_WIN32 */
173 timer->active = TRUE;
177 g_timer_elapsed (GTimer *timer,
178 gulong *microseconds)
184 struct timeval elapsed;
185 #endif /* G_OS_WIN32 */
187 g_return_val_if_fail (timer != NULL, 0);
191 GETTIME (timer->end);
193 elapsed = timer->end - timer->start;
195 total = elapsed / 1e7;
198 *microseconds = (elapsed / 10) % 1000000;
199 #else /* !G_OS_WIN32 */
201 gettimeofday (&timer->end, NULL);
203 if (timer->start.tv_usec > timer->end.tv_usec)
205 timer->end.tv_usec += G_USEC_PER_SEC;
209 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
210 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
212 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
220 else if (microseconds)
221 *microseconds = elapsed.tv_usec;
223 #endif /* !G_OS_WIN32 */
229 g_usleep (gulong microseconds)
232 Sleep (microseconds / 1000);
233 #else /* !G_OS_WIN32 */
234 # ifdef HAVE_NANOSLEEP
235 struct timespec request, remaining;
236 request.tv_sec = microseconds / G_USEC_PER_SEC;
237 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
238 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
240 # else /* !HAVE_NANOSLEEP */
241 if (g_thread_supported ())
243 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
244 static GCond* cond = NULL;
247 g_get_current_time (&end_time);
248 if (microseconds > G_MAXLONG)
250 microseconds -= G_MAXLONG;
251 g_time_val_add (&end_time, G_MAXLONG);
253 g_time_val_add (&end_time, microseconds);
255 g_static_mutex_lock (&mutex);
258 cond = g_cond_new ();
260 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
264 g_static_mutex_unlock (&mutex);
269 tv.tv_sec = microseconds / G_USEC_PER_SEC;
270 tv.tv_usec = microseconds % G_USEC_PER_SEC;
271 select(0, NULL, NULL, NULL, &tv);
273 # endif /* !HAVE_NANOSLEEP */
274 #endif /* !G_OS_WIN32 */
279 * @time_: a #GTimeVal
280 * @microseconds: number of microseconds to add to @time
282 * Adds the given number of microseconds to @time_. @microseconds can
283 * also be negative to decrease the value of @time_.
286 g_time_val_add (GTimeVal *time_, glong microseconds)
288 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
290 if (microseconds >= 0)
292 time_->tv_usec += microseconds % G_USEC_PER_SEC;
293 time_->tv_sec += microseconds / G_USEC_PER_SEC;
294 if (time_->tv_usec >= G_USEC_PER_SEC)
296 time_->tv_usec -= G_USEC_PER_SEC;
303 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
304 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
305 if (time_->tv_usec < 0)
307 time_->tv_usec += G_USEC_PER_SEC;
313 /* converts a broken down date representation, relative to UTC, to
314 * a timestamp; it uses timegm() if it's available.
317 mktime_utc (struct tm *tm)
322 static const gint days_before[] =
324 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
329 if (tm->tm_mon < 0 || tm->tm_mon > 11)
332 retval = (tm->tm_year - 70) * 365;
333 retval += (tm->tm_year - 68) / 4;
334 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
336 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
339 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
341 retval = timegm (tm);
342 #endif /* !HAVE_TIMEGM */
348 * g_time_val_from_iso8601:
349 * @iso_date: a ISO 8601 encoded date string
350 * @time_: a #GTimeVal
352 * Converts a string containing an ISO 8601 encoded date and time
353 * to a #GTimeVal and puts it into @time_.
355 * Return value: %TRUE if the conversion was successful.
360 g_time_val_from_iso8601 (const gchar *iso_date,
366 g_return_val_if_fail (iso_date != NULL, FALSE);
367 g_return_val_if_fail (time_ != NULL, FALSE);
369 val = strtoul (iso_date, (char **)&iso_date, 10);
370 if (*iso_date == '-')
373 tm.tm_year = val - 1900;
375 tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
377 if (*iso_date++ != '-')
380 tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
385 tm.tm_mday = val % 100;
386 tm.tm_mon = (val % 10000) / 100 - 1;
387 tm.tm_year = val / 10000 - 1900;
390 if (*iso_date++ != 'T')
393 val = strtoul (iso_date, (char **)&iso_date, 10);
394 if (*iso_date == ':')
399 tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
401 if (*iso_date++ != ':')
404 tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
409 tm.tm_sec = val % 100;
410 tm.tm_min = (val % 10000) / 100;
411 tm.tm_hour = val / 10000;
414 time_->tv_sec = mktime_utc (&tm);
417 if (*iso_date == '.')
418 time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
420 if (*iso_date == '+' || *iso_date == '-')
422 gint sign = (*iso_date == '+') ? -1 : 1;
424 val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
426 if (*iso_date == ':')
427 val = 60 * val + strtoul (iso_date + 1, NULL, 10);
429 val = 60 * (val / 100) + (val % 100);
431 time_->tv_sec += (time_t) (val * sign);
438 * g_time_val_to_iso8601:
439 * @time_: a #GTimeVal
441 * Converts @time_ into a ISO 8601 encoded string, relative to the
442 * Coordinated Universal Time (UTC).
444 * Return value: a newly allocated string containing a ISO 8601 date
449 g_time_val_to_iso8601 (GTimeVal *time_)
453 g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
455 #define ISO_8601_LEN 21
456 #define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
457 retval = g_new0 (gchar, ISO_8601_LEN + 1);
459 strftime (retval, ISO_8601_LEN,
461 gmtime (&(time_->tv_sec)));
466 #define __G_TIMER_C__
467 #include "galiasdef.c"