Blame glib/gtimer.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "glibconfig.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif /* G_OS_UNIX */
Packit ae235b
Packit ae235b
#ifdef HAVE_SYS_TIME_H
Packit ae235b
#include <sys/time.h>
Packit ae235b
#endif
Packit ae235b
#include <time.h>
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
#include <errno.h>
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#include "gtimer.h"
Packit ae235b
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gmain.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:timers
Packit ae235b
 * @title: Timers
Packit ae235b
 * @short_description: keep track of elapsed time
Packit ae235b
 *
Packit ae235b
 * #GTimer records a start time, and counts microseconds elapsed since
Packit ae235b
 * that time. This is done somewhat differently on different platforms,
Packit ae235b
 * and can be tricky to get exactly right, so #GTimer provides a
Packit ae235b
 * portable/convenient interface.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTimer:
Packit ae235b
 *
Packit ae235b
 * Opaque datatype that records a start time.
Packit ae235b
 **/
Packit ae235b
struct _GTimer
Packit ae235b
{
Packit ae235b
  guint64 start;
Packit ae235b
  guint64 end;
Packit ae235b
Packit ae235b
  guint active : 1;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_new:
Packit ae235b
 *
Packit ae235b
 * Creates a new timer, and starts timing (i.e. g_timer_start() is
Packit ae235b
 * implicitly called for you).
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GTimer.
Packit ae235b
 **/
Packit ae235b
GTimer*
Packit ae235b
g_timer_new (void)
Packit ae235b
{
Packit ae235b
  GTimer *timer;
Packit ae235b
Packit ae235b
  timer = g_new (GTimer, 1);
Packit ae235b
  timer->active = TRUE;
Packit ae235b
Packit ae235b
  timer->start = g_get_monotonic_time ();
Packit ae235b
Packit ae235b
  return timer;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_destroy:
Packit ae235b
 * @timer: a #GTimer to destroy.
Packit ae235b
 *
Packit ae235b
 * Destroys a timer, freeing associated resources.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_timer_destroy (GTimer *timer)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (timer != NULL);
Packit ae235b
Packit ae235b
  g_free (timer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_start:
Packit ae235b
 * @timer: a #GTimer.
Packit ae235b
 *
Packit ae235b
 * Marks a start time, so that future calls to g_timer_elapsed() will
Packit ae235b
 * report the time since g_timer_start() was called. g_timer_new()
Packit ae235b
 * automatically marks the start time, so no need to call
Packit ae235b
 * g_timer_start() immediately after creating the timer.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_timer_start (GTimer *timer)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (timer != NULL);
Packit ae235b
Packit ae235b
  timer->active = TRUE;
Packit ae235b
Packit ae235b
  timer->start = g_get_monotonic_time ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_stop:
Packit ae235b
 * @timer: a #GTimer.
Packit ae235b
 *
Packit ae235b
 * Marks an end time, so calls to g_timer_elapsed() will return the
Packit ae235b
 * difference between this end time and the start time.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_timer_stop (GTimer *timer)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (timer != NULL);
Packit ae235b
Packit ae235b
  timer->active = FALSE;
Packit ae235b
Packit ae235b
  timer->end = g_get_monotonic_time ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_reset:
Packit ae235b
 * @timer: a #GTimer.
Packit ae235b
 *
Packit ae235b
 * This function is useless; it's fine to call g_timer_start() on an
Packit ae235b
 * already-started timer to reset the start time, so g_timer_reset()
Packit ae235b
 * serves no purpose.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_timer_reset (GTimer *timer)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (timer != NULL);
Packit ae235b
Packit ae235b
  timer->start = g_get_monotonic_time ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_continue:
Packit ae235b
 * @timer: a #GTimer.
Packit ae235b
 *
Packit ae235b
 * Resumes a timer that has previously been stopped with
Packit ae235b
 * g_timer_stop(). g_timer_stop() must be called before using this
Packit ae235b
 * function.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_timer_continue (GTimer *timer)
Packit ae235b
{
Packit ae235b
  guint64 elapsed;
Packit ae235b
Packit ae235b
  g_return_if_fail (timer != NULL);
Packit ae235b
  g_return_if_fail (timer->active == FALSE);
Packit ae235b
Packit ae235b
  /* Get elapsed time and reset timer start time
Packit ae235b
   *  to the current time minus the previously
Packit ae235b
   *  elapsed interval.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  elapsed = timer->end - timer->start;
Packit ae235b
Packit ae235b
  timer->start = g_get_monotonic_time ();
Packit ae235b
Packit ae235b
  timer->start -= elapsed;
Packit ae235b
Packit ae235b
  timer->active = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_timer_elapsed:
Packit ae235b
 * @timer: a #GTimer.
Packit ae235b
 * @microseconds: return location for the fractional part of seconds
Packit ae235b
 *                elapsed, in microseconds (that is, the total number
Packit ae235b
 *                of microseconds elapsed, modulo 1000000), or %NULL
Packit ae235b
 *
Packit ae235b
 * If @timer has been started but not stopped, obtains the time since
Packit ae235b
 * the timer was started. If @timer has been stopped, obtains the
Packit ae235b
 * elapsed time between the time it was started and the time it was
Packit ae235b
 * stopped. The return value is the number of seconds elapsed,
Packit ae235b
 * including any fractional part. The @microseconds out parameter is
Packit ae235b
 * essentially useless.
Packit ae235b
 *
Packit ae235b
 * Returns: seconds elapsed as a floating point value, including any
Packit ae235b
 *          fractional part.
Packit ae235b
 **/
Packit ae235b
gdouble
Packit ae235b
g_timer_elapsed (GTimer *timer,
Packit ae235b
		 gulong *microseconds)
Packit ae235b
{
Packit ae235b
  gdouble total;
Packit ae235b
  gint64 elapsed;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (timer != NULL, 0);
Packit ae235b
Packit ae235b
  if (timer->active)
Packit ae235b
    timer->end = g_get_monotonic_time ();
Packit ae235b
Packit ae235b
  elapsed = timer->end - timer->start;
Packit ae235b
Packit ae235b
  total = elapsed / 1e6;
Packit ae235b
Packit ae235b
  if (microseconds)
Packit ae235b
    *microseconds = elapsed % 1000000;
Packit ae235b
Packit ae235b
  return total;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_usleep:
Packit ae235b
 * @microseconds: number of microseconds to pause
Packit ae235b
 *
Packit ae235b
 * Pauses the current thread for the given number of microseconds.
Packit ae235b
 *
Packit ae235b
 * There are 1 million microseconds per second (represented by the
Packit ae235b
 * #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
Packit ae235b
 * depending on hardware and operating system; don't rely on the exact
Packit ae235b
 * length of the sleep.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_usleep (gulong microseconds)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  Sleep (microseconds / 1000);
Packit ae235b
#else
Packit ae235b
  struct timespec request, remaining;
Packit ae235b
  request.tv_sec = microseconds / G_USEC_PER_SEC;
Packit ae235b
  request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
Packit ae235b
  while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
Packit ae235b
    request = remaining;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_time_val_add:
Packit ae235b
 * @time_: a #GTimeVal
Packit ae235b
 * @microseconds: number of microseconds to add to @time
Packit ae235b
 *
Packit ae235b
 * Adds the given number of microseconds to @time_. @microseconds can
Packit ae235b
 * also be negative to decrease the value of @time_.
Packit ae235b
 **/
Packit ae235b
void 
Packit ae235b
g_time_val_add (GTimeVal *time_, glong microseconds)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
Packit ae235b
Packit ae235b
  if (microseconds >= 0)
Packit ae235b
    {
Packit ae235b
      time_->tv_usec += microseconds % G_USEC_PER_SEC;
Packit ae235b
      time_->tv_sec += microseconds / G_USEC_PER_SEC;
Packit ae235b
      if (time_->tv_usec >= G_USEC_PER_SEC)
Packit ae235b
       {
Packit ae235b
         time_->tv_usec -= G_USEC_PER_SEC;
Packit ae235b
         time_->tv_sec++;
Packit ae235b
       }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      microseconds *= -1;
Packit ae235b
      time_->tv_usec -= microseconds % G_USEC_PER_SEC;
Packit ae235b
      time_->tv_sec -= microseconds / G_USEC_PER_SEC;
Packit ae235b
      if (time_->tv_usec < 0)
Packit ae235b
       {
Packit ae235b
         time_->tv_usec += G_USEC_PER_SEC;
Packit ae235b
         time_->tv_sec--;
Packit ae235b
       }      
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* converts a broken down date representation, relative to UTC,
Packit ae235b
 * to a timestamp; it uses timegm() if it's available.
Packit ae235b
 */
Packit ae235b
static time_t
Packit ae235b
mktime_utc (struct tm *tm)
Packit ae235b
{
Packit ae235b
  time_t retval;
Packit ae235b
  
Packit ae235b
#ifndef HAVE_TIMEGM
Packit ae235b
  static const gint days_before[] =
Packit ae235b
  {
Packit ae235b
    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
Packit ae235b
  };
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef HAVE_TIMEGM
Packit ae235b
  if (tm->tm_mon < 0 || tm->tm_mon > 11)
Packit ae235b
    return (time_t) -1;
Packit ae235b
Packit ae235b
  retval = (tm->tm_year - 70) * 365;
Packit ae235b
  retval += (tm->tm_year - 68) / 4;
Packit ae235b
  retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
Packit ae235b
  
Packit ae235b
  if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
Packit ae235b
    retval -= 1;
Packit ae235b
  
Packit ae235b
  retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
Packit ae235b
#else
Packit ae235b
  retval = timegm (tm);
Packit ae235b
#endif /* !HAVE_TIMEGM */
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_time_val_from_iso8601:
Packit ae235b
 * @iso_date: an ISO 8601 encoded date string
Packit ae235b
 * @time_: (out): a #GTimeVal
Packit ae235b
 *
Packit ae235b
 * Converts a string containing an ISO 8601 encoded date and time
Packit ae235b
 * to a #GTimeVal and puts it into @time_.
Packit ae235b
 *
Packit ae235b
 * @iso_date must include year, month, day, hours, minutes, and
Packit ae235b
 * seconds. It can optionally include fractions of a second and a time
Packit ae235b
 * zone indicator. (In the absence of any time zone indication, the
Packit ae235b
 * timestamp is assumed to be in local time.)
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the conversion was successful.
Packit ae235b
 *
Packit ae235b
 * Since: 2.12
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_time_val_from_iso8601 (const gchar *iso_date,
Packit ae235b
			 GTimeVal    *time_)
Packit ae235b
{
Packit ae235b
  struct tm tm = {0};
Packit ae235b
  long val;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (iso_date != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (time_ != NULL, FALSE);
Packit ae235b
Packit ae235b
  /* Ensure that the first character is a digit, the first digit
Packit ae235b
   * of the date, otherwise we don't have an ISO 8601 date
Packit ae235b
   */
Packit ae235b
  while (g_ascii_isspace (*iso_date))
Packit ae235b
    iso_date++;
Packit ae235b
Packit ae235b
  if (*iso_date == '\0')
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  val = strtoul (iso_date, (char **)&iso_date, 10);
Packit ae235b
  if (*iso_date == '-')
Packit ae235b
    {
Packit ae235b
      /* YYYY-MM-DD */
Packit ae235b
      tm.tm_year = val - 1900;
Packit ae235b
      iso_date++;
Packit ae235b
      tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
Packit ae235b
      
Packit ae235b
      if (*iso_date++ != '-')
Packit ae235b
        return FALSE;
Packit ae235b
      
Packit ae235b
      tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* YYYYMMDD */
Packit ae235b
      tm.tm_mday = val % 100;
Packit ae235b
      tm.tm_mon = (val % 10000) / 100 - 1;
Packit ae235b
      tm.tm_year = val / 10000 - 1900;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (*iso_date != 'T')
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  iso_date++;
Packit ae235b
Packit ae235b
  /* If there is a 'T' then there has to be a time */
Packit ae235b
  if (!g_ascii_isdigit (*iso_date))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  val = strtoul (iso_date, (char **)&iso_date, 10);
Packit ae235b
  if (*iso_date == ':')
Packit ae235b
    {
Packit ae235b
      /* hh:mm:ss */
Packit ae235b
      tm.tm_hour = val;
Packit ae235b
      iso_date++;
Packit ae235b
      tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
Packit ae235b
      
Packit ae235b
      if (*iso_date++ != ':')
Packit ae235b
        return FALSE;
Packit ae235b
      
Packit ae235b
      tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* hhmmss */
Packit ae235b
      tm.tm_sec = val % 100;
Packit ae235b
      tm.tm_min = (val % 10000) / 100;
Packit ae235b
      tm.tm_hour = val / 10000;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  time_->tv_usec = 0;
Packit ae235b
  
Packit ae235b
  if (*iso_date == ',' || *iso_date == '.')
Packit ae235b
    {
Packit ae235b
      glong mul = 100000;
Packit ae235b
Packit ae235b
      while (g_ascii_isdigit (*++iso_date))
Packit ae235b
        {
Packit ae235b
          time_->tv_usec += (*iso_date - '0') * mul;
Packit ae235b
          mul /= 10;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
    
Packit ae235b
  /* Now parse the offset and convert tm to a time_t */
Packit ae235b
  if (*iso_date == 'Z')
Packit ae235b
    {
Packit ae235b
      iso_date++;
Packit ae235b
      time_->tv_sec = mktime_utc (&tm;;
Packit ae235b
    }
Packit ae235b
  else if (*iso_date == '+' || *iso_date == '-')
Packit ae235b
    {
Packit ae235b
      gint sign = (*iso_date == '+') ? -1 : 1;
Packit ae235b
      
Packit ae235b
      val = strtoul (iso_date + 1, (char **)&iso_date, 10);
Packit ae235b
      
Packit ae235b
      if (*iso_date == ':')
Packit ae235b
        val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
Packit ae235b
      else
Packit ae235b
        val = 60 * (val / 100) + (val % 100);
Packit ae235b
Packit ae235b
      time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* No "Z" or offset, so local time */
Packit ae235b
      tm.tm_isdst = -1; /* locale selects DST */
Packit ae235b
      time_->tv_sec = mktime (&tm;;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (g_ascii_isspace (*iso_date))
Packit ae235b
    iso_date++;
Packit ae235b
Packit ae235b
  return *iso_date == '\0';
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_time_val_to_iso8601:
Packit ae235b
 * @time_: a #GTimeVal
Packit ae235b
 * 
Packit ae235b
 * Converts @time_ into an RFC 3339 encoded string, relative to the
Packit ae235b
 * Coordinated Universal Time (UTC). This is one of the many formats
Packit ae235b
 * allowed by ISO 8601.
Packit ae235b
 *
Packit ae235b
 * ISO 8601 allows a large number of date/time formats, with or without
Packit ae235b
 * punctuation and optional elements. The format returned by this function
Packit ae235b
 * is a complete date and time, with optional punctuation included, the
Packit ae235b
 * UTC time zone represented as "Z", and the @tv_usec part included if
Packit ae235b
 * and only if it is nonzero, i.e. either
Packit ae235b
 * "YYYY-MM-DDTHH:MM:SSZ" or "YYYY-MM-DDTHH:MM:SS.fffffZ".
Packit ae235b
 *
Packit ae235b
 * This corresponds to the Internet date/time format defined by
Packit ae235b
 * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt),
Packit ae235b
 * and to either of the two most-precise formats defined by
Packit ae235b
 * the W3C Note
Packit ae235b
 * [Date and Time Formats](http://www.w3.org/TR/NOTE-datetime-19980827).
Packit ae235b
 * Both of these documents are profiles of ISO 8601.
Packit ae235b
 *
Packit ae235b
 * Use g_date_time_format() or g_strdup_printf() if a different
Packit ae235b
 * variation of ISO 8601 format is required.
Packit ae235b
 *
Packit ae235b
 * If @time_ represents a date which is too large to fit into a `struct tm`,
Packit ae235b
 * %NULL will be returned. This is platform dependent, but it is safe to assume
Packit ae235b
 * years up to 3000 are supported. The return value of g_time_val_to_iso8601()
Packit ae235b
 * has been nullable since GLib 2.54; before then, GLib would crash under the
Packit ae235b
 * same conditions.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): a newly allocated string containing an ISO 8601 date,
Packit ae235b
 *    or %NULL if @time_ was too large
Packit ae235b
 *
Packit ae235b
 * Since: 2.12
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_time_val_to_iso8601 (GTimeVal *time_)
Packit ae235b
{
Packit ae235b
  gchar *retval;
Packit ae235b
  struct tm *tm;
Packit ae235b
#ifdef HAVE_GMTIME_R
Packit ae235b
  struct tm tm_;
Packit ae235b
#endif
Packit ae235b
  time_t secs;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
Packit ae235b
Packit ae235b
  secs = time_->tv_sec;
Packit ae235b
#ifdef _WIN32
Packit ae235b
  tm = gmtime (&secs;;
Packit ae235b
#else
Packit ae235b
#ifdef HAVE_GMTIME_R
Packit ae235b
  tm = gmtime_r (&secs, &tm_);
Packit ae235b
#else
Packit ae235b
  tm = gmtime (&secs;;
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* If the gmtime() call has failed, time_->tv_sec is too big. */
Packit ae235b
  if (tm == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (time_->tv_usec != 0)
Packit ae235b
    {
Packit ae235b
      /* ISO 8601 date and time format, with fractionary seconds:
Packit ae235b
       *   YYYY-MM-DDTHH:MM:SS.MMMMMMZ
Packit ae235b
       */
Packit ae235b
      retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
Packit ae235b
                                tm->tm_year + 1900,
Packit ae235b
                                tm->tm_mon + 1,
Packit ae235b
                                tm->tm_mday,
Packit ae235b
                                tm->tm_hour,
Packit ae235b
                                tm->tm_min,
Packit ae235b
                                tm->tm_sec,
Packit ae235b
                                time_->tv_usec);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* ISO 8601 date and time format:
Packit ae235b
       *   YYYY-MM-DDTHH:MM:SSZ
Packit ae235b
       */
Packit ae235b
      retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",
Packit ae235b
                                tm->tm_year + 1900,
Packit ae235b
                                tm->tm_mon + 1,
Packit ae235b
                                tm->tm_mday,
Packit ae235b
                                tm->tm_hour,
Packit ae235b
                                tm->tm_min,
Packit ae235b
                                tm->tm_sec);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}