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: a 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
val = strtoul (iso_date, (char **)&iso_date, 10);
311
if (*iso_date == '-')
314
tm.tm_year = val - 1900;
316
tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
318
if (*iso_date++ != '-')
321
tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
326
tm.tm_mday = val % 100;
327
tm.tm_mon = (val % 10000) / 100 - 1;
328
tm.tm_year = val / 10000 - 1900;
331
if (*iso_date++ != 'T')
334
val = strtoul (iso_date, (char **)&iso_date, 10);
335
if (*iso_date == ':')
340
tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
342
if (*iso_date++ != ':')
345
tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
350
tm.tm_sec = val % 100;
351
tm.tm_min = (val % 10000) / 100;
352
tm.tm_hour = val / 10000;
355
time_->tv_sec = mktime_utc (&tm);
358
if (*iso_date == '.')
359
time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
361
if (*iso_date == '+' || *iso_date == '-')
363
gint sign = (*iso_date == '+') ? -1 : 1;
365
val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
367
if (*iso_date == ':')
368
val = 60 * val + strtoul (iso_date + 1, NULL, 10);
370
val = 60 * (val / 100) + (val % 100);
372
time_->tv_sec += (time_t) (val * sign);
379
* g_time_val_to_iso8601:
380
* @time_: a #GTimeVal
382
* Converts @time_ into a ISO 8601 encoded string, relative to the
383
* Coordinated Universal Time (UTC).
385
* Return value: a newly allocated string containing a ISO 8601 date
390
g_time_val_to_iso8601 (GTimeVal *time_)
394
g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
396
#define ISO_8601_LEN 21
397
#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
398
retval = g_new0 (gchar, ISO_8601_LEN + 1);
400
strftime (retval, ISO_8601_LEN,
402
gmtime (&(time_->tv_sec)));
407
#define __G_TIMER_C__
408
#include "galiasdef.c"