[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / glib / gtimer.c
index bfc559a..4d24cae 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-/* 
+/*
  * MT safe
  */
 
 
 #include <stdlib.h>
 
-#ifdef HAVE_UNISTD_H
+#ifdef G_OS_UNIX
 #include <unistd.h>
-#endif /* HAVE_UNISTD_H */
+#endif /* G_OS_UNIX */
 
-#ifndef G_OS_WIN32
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
 #include <time.h>
+#ifndef G_OS_WIN32
 #include <errno.h>
 #endif /* G_OS_WIN32 */
 
 #include <windows.h>
 #endif /* G_OS_WIN32 */
 
-#include "glib.h"
-#include "gthread.h"
-#include "galias.h"
+#include "gtimer.h"
 
-#define G_NSEC_PER_SEC 1000000000
+#include "gmem.h"
+#include "gstrfuncs.h"
+#include "gtestutils.h"
+#include "gmain.h"
 
-#define GETTIME(v) (v = g_thread_gettime ())
+/**
+ * SECTION:timers
+ * @title: Timers
+ * @short_description: keep track of elapsed time
+ *
+ * #GTimer records a start time, and counts microseconds elapsed since
+ * that time. This is done somewhat differently on different platforms,
+ * and can be tricky to get exactly right, so #GTimer provides a
+ * portable/convenient interface.
+ **/
 
+/**
+ * GTimer:
+ *
+ * Opaque datatype that records a start time.
+ **/
 struct _GTimer
 {
   guint64 start;
@@ -63,7 +78,14 @@ struct _GTimer
   guint active : 1;
 };
 
-
+/**
+ * g_timer_new:
+ *
+ * Creates a new timer, and starts timing (i.e. g_timer_start() is
+ * implicitly called for you).
+ *
+ * Returns: a new #GTimer.
+ **/
 GTimer*
 g_timer_new (void)
 {
@@ -72,11 +94,17 @@ g_timer_new (void)
   timer = g_new (GTimer, 1);
   timer->active = TRUE;
 
-  GETTIME (timer->start);
+  timer->start = g_get_monotonic_time ();
 
   return timer;
 }
 
+/**
+ * g_timer_destroy:
+ * @timer: a #GTimer to destroy.
+ *
+ * Destroys a timer, freeing associated resources.
+ **/
 void
 g_timer_destroy (GTimer *timer)
 {
@@ -85,6 +113,15 @@ g_timer_destroy (GTimer *timer)
   g_free (timer);
 }
 
+/**
+ * g_timer_start:
+ * @timer: a #GTimer.
+ *
+ * Marks a start time, so that future calls to g_timer_elapsed() will
+ * report the time since g_timer_start() was called. g_timer_new()
+ * automatically marks the start time, so no need to call
+ * g_timer_start() immediately after creating the timer.
+ **/
 void
 g_timer_start (GTimer *timer)
 {
@@ -92,9 +129,16 @@ g_timer_start (GTimer *timer)
 
   timer->active = TRUE;
 
-  GETTIME (timer->start);
+  timer->start = g_get_monotonic_time ();
 }
 
+/**
+ * g_timer_stop:
+ * @timer: a #GTimer.
+ *
+ * Marks an end time, so calls to g_timer_elapsed() will return the
+ * difference between this end time and the start time.
+ **/
 void
 g_timer_stop (GTimer *timer)
 {
@@ -102,17 +146,35 @@ g_timer_stop (GTimer *timer)
 
   timer->active = FALSE;
 
-  GETTIME (timer->end);
+  timer->end = g_get_monotonic_time ();
 }
 
+/**
+ * g_timer_reset:
+ * @timer: a #GTimer.
+ *
+ * This function is useless; it's fine to call g_timer_start() on an
+ * already-started timer to reset the start time, so g_timer_reset()
+ * serves no purpose.
+ **/
 void
 g_timer_reset (GTimer *timer)
 {
   g_return_if_fail (timer != NULL);
 
-  GETTIME (timer->start);
+  timer->start = g_get_monotonic_time ();
 }
 
+/**
+ * g_timer_continue:
+ * @timer: a #GTimer.
+ *
+ * Resumes a timer that has previously been stopped with
+ * g_timer_stop(). g_timer_stop() must be called before using this
+ * function.
+ *
+ * Since: 2.4
+ **/
 void
 g_timer_continue (GTimer *timer)
 {
@@ -128,13 +190,30 @@ g_timer_continue (GTimer *timer)
 
   elapsed = timer->end - timer->start;
 
-  GETTIME (timer->start);
+  timer->start = g_get_monotonic_time ();
 
   timer->start -= elapsed;
 
   timer->active = TRUE;
 }
 
+/**
+ * g_timer_elapsed:
+ * @timer: a #GTimer.
+ * @microseconds: return location for the fractional part of seconds
+ *                elapsed, in microseconds (that is, the total number
+ *                of microseconds elapsed, modulo 1000000), or %NULL
+ *
+ * If @timer has been started but not stopped, obtains the time since
+ * the timer was started. If @timer has been stopped, obtains the
+ * elapsed time between the time it was started and the time it was
+ * stopped. The return value is the number of seconds elapsed,
+ * including any fractional part. The @microseconds out parameter is
+ * essentially useless.
+ *
+ * Returns: seconds elapsed as a floating point value, including any
+ *          fractional part.
+ **/
 gdouble
 g_timer_elapsed (GTimer *timer,
                 gulong *microseconds)
@@ -145,74 +224,41 @@ g_timer_elapsed (GTimer *timer,
   g_return_val_if_fail (timer != NULL, 0);
 
   if (timer->active)
-    GETTIME (timer->end);
+    timer->end = g_get_monotonic_time ();
 
   elapsed = timer->end - timer->start;
 
-  total = elapsed / 1e9;
+  total = elapsed / 1e6;
 
   if (microseconds)
-    *microseconds = (elapsed / 1000) % 1000000;
+    *microseconds = elapsed % 1000000;
 
   return total;
 }
 
+/**
+ * g_usleep:
+ * @microseconds: number of microseconds to pause
+ *
+ * Pauses the current thread for the given number of microseconds.
+ *
+ * There are 1 million microseconds per second (represented by the
+ * #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
+ * depending on hardware and operating system; don't rely on the exact
+ * length of the sleep.
+ */
 void
 g_usleep (gulong microseconds)
 {
 #ifdef G_OS_WIN32
   Sleep (microseconds / 1000);
-#else /* !G_OS_WIN32 */
-# ifdef HAVE_NANOSLEEP
+#else
   struct timespec request, remaining;
   request.tv_sec = microseconds / G_USEC_PER_SEC;
   request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
   while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
     request = remaining;
-# else /* !HAVE_NANOSLEEP */
-#  ifdef HAVE_NSLEEP
-  /* on AIX, nsleep is analogous to nanosleep */
-  struct timespec request, remaining;
-  request.tv_sec = microseconds / G_USEC_PER_SEC;
-  request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
-  while (nsleep (&request, &remaining) == -1 && errno == EINTR)
-    request = remaining;
-#  else /* !HAVE_NSLEEP */
-  if (g_thread_supported ())
-    {
-      static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
-      static GCond* cond = NULL;
-      GTimeVal end_time;
-      
-      g_get_current_time (&end_time);
-      if (microseconds > G_MAXLONG)
-       {
-         microseconds -= G_MAXLONG;
-         g_time_val_add (&end_time, G_MAXLONG);
-       }
-      g_time_val_add (&end_time, microseconds);
-
-      g_static_mutex_lock (&mutex);
-      
-      if (!cond)
-       cond = g_cond_new ();
-      
-      while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
-                               &end_time))
-       /* do nothing */;
-      
-      g_static_mutex_unlock (&mutex);
-    }
-  else
-    {
-      struct timeval tv;
-      tv.tv_sec = microseconds / G_USEC_PER_SEC;
-      tv.tv_usec = microseconds % G_USEC_PER_SEC;
-      select(0, NULL, NULL, NULL, &tv);
-    }
-#  endif /* !HAVE_NSLEEP */
-# endif /* !HAVE_NANOSLEEP */
-#endif /* !G_OS_WIN32 */
+#endif
 }
 
 /**
@@ -251,8 +297,8 @@ g_time_val_add (GTimeVal *time_, glong microseconds)
     }
 }
 
-/* converts a broken down date representation, relative to UTC, to
- * a timestamp; it uses timegm() if it's available.
+/* converts a broken down date representation, relative to UTC,
+ * to a timestamp; it uses timegm() if it's available.
  */
 static time_t
 mktime_utc (struct tm *tm)
@@ -288,12 +334,17 @@ mktime_utc (struct tm *tm)
 /**
  * g_time_val_from_iso8601:
  * @iso_date: an ISO 8601 encoded date string
- * @time_: a #GTimeVal
+ * @time_: (out): a #GTimeVal
  *
  * Converts a string containing an ISO 8601 encoded date and time
  * to a #GTimeVal and puts it into @time_.
  *
- * Return value: %TRUE if the conversion was successful.
+ * @iso_date must include year, month, day, hours, minutes, and
+ * seconds. It can optionally include fractions of a second and a time
+ * zone indicator. (In the absence of any time zone indication, the
+ * timestamp is assumed to be in local time.)
+ *
+ * Returns: %TRUE if the conversion was successful.
  *
  * Since: 2.12
  */
@@ -301,15 +352,15 @@ gboolean
 g_time_val_from_iso8601 (const gchar *iso_date,
                         GTimeVal    *time_)
 {
-  struct tm tm;
+  struct tm tm = {0};
   long val;
 
   g_return_val_if_fail (iso_date != NULL, FALSE);
   g_return_val_if_fail (time_ != NULL, FALSE);
 
-  /* Ensure that the first character is a digit,
-   * the first digit of the date, otherwise we don't
-   * have an ISO 8601 date */
+  /* Ensure that the first character is a digit, the first digit
+   * of the date, otherwise we don't have an ISO 8601 date
+   */
   while (g_ascii_isspace (*iso_date))
     iso_date++;
 
@@ -328,7 +379,7 @@ g_time_val_from_iso8601 (const gchar *iso_date,
       tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
       
       if (*iso_date++ != '-')
-               return FALSE;
+        return FALSE;
       
       tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
     }
@@ -340,9 +391,15 @@ g_time_val_from_iso8601 (const gchar *iso_date,
       tm.tm_year = val / 10000 - 1900;
     }
 
-  if (*iso_date++ != 'T')
+  if (*iso_date != 'T')
     return FALSE;
-  
+
+  iso_date++;
+
+  /* If there is a 'T' then there has to be a time */
+  if (!g_ascii_isdigit (*iso_date))
+    return FALSE;
+
   val = strtoul (iso_date, (char **)&iso_date, 10);
   if (*iso_date == ':')
     {
@@ -364,10 +421,9 @@ g_time_val_from_iso8601 (const gchar *iso_date,
       tm.tm_hour = val / 10000;
     }
 
-  time_->tv_sec = mktime_utc (&tm);
   time_->tv_usec = 0;
   
-  if (*iso_date == '.')
+  if (*iso_date == ',' || *iso_date == '.')
     {
       glong mul = 100000;
 
@@ -378,21 +434,31 @@ g_time_val_from_iso8601 (const gchar *iso_date,
         }
     }
     
-  if (*iso_date == '+' || *iso_date == '-')
+  /* Now parse the offset and convert tm to a time_t */
+  if (*iso_date == 'Z')
+    {
+      iso_date++;
+      time_->tv_sec = mktime_utc (&tm);
+    }
+  else if (*iso_date == '+' || *iso_date == '-')
     {
       gint sign = (*iso_date == '+') ? -1 : 1;
       
-      val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
+      val = strtoul (iso_date + 1, (char **)&iso_date, 10);
       
       if (*iso_date == ':')
-       val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
+        val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
       else
         val = 60 * (val / 100) + (val % 100);
 
-      time_->tv_sec += (time_t) (val * sign);
+      time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
+    }
+  else
+    {
+      /* No "Z" or offset, so local time */
+      tm.tm_isdst = -1; /* locale selects DST */
+      time_->tv_sec = mktime (&tm);
     }
-  else if (*iso_date++ != 'Z')
-    return FALSE;
 
   while (g_ascii_isspace (*iso_date))
     iso_date++;
@@ -404,10 +470,28 @@ g_time_val_from_iso8601 (const gchar *iso_date,
  * g_time_val_to_iso8601:
  * @time_: a #GTimeVal
  * 
- * Converts @time_ into an ISO 8601 encoded string, relative to the
- * Coordinated Universal Time (UTC).
+ * Converts @time_ into an RFC 3339 encoded string, relative to the
+ * Coordinated Universal Time (UTC). This is one of the many formats
+ * allowed by ISO 8601.
+ *
+ * ISO 8601 allows a large number of date/time formats, with or without
+ * punctuation and optional elements. The format returned by this function
+ * is a complete date and time, with optional punctuation included, the
+ * UTC time zone represented as "Z", and the @tv_usec part included if
+ * and only if it is nonzero, i.e. either
+ * "YYYY-MM-DDTHH:MM:SSZ" or "YYYY-MM-DDTHH:MM:SS.fffffZ".
  *
- * Return value: a newly allocated string containing an ISO 8601 date
+ * This corresponds to the Internet date/time format defined by
+ * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt),
+ * and to either of the two most-precise formats defined by
+ * the W3C Note
+ * [Date and Time Formats](http://www.w3.org/TR/NOTE-datetime-19980827).
+ * Both of these documents are profiles of ISO 8601.
+ *
+ * Use g_date_time_format() or g_strdup_printf() if a different
+ * variation of ISO 8601 format is required.
+ *
+ * Returns: a newly allocated string containing an ISO 8601 date
  *
  * Since: 2.12
  */
@@ -419,19 +503,18 @@ g_time_val_to_iso8601 (GTimeVal *time_)
 #ifdef HAVE_GMTIME_R
   struct tm tm_;
 #endif
+  time_t secs;
   
   g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
 
-#ifdef _WIN64
-  {
-    time_t secs = time_->tv_sec;
-    tm = gmtime (&secs);
-  }
+ secs = time_->tv_sec;
+#ifdef _WIN32
+ tm = gmtime (&secs);
 #else
 #ifdef HAVE_GMTIME_R
-  tm = gmtime_r (&time_->tv_sec, &tm_);
+  tm = gmtime_r (&secs, &tm_);
 #else
-  tm = gmtime (&time_->tv_sec);
+  tm = gmtime (&secs);
 #endif
 #endif
 
@@ -465,6 +548,3 @@ g_time_val_to_iso8601 (GTimeVal *time_)
   
   return retval;
 }
-
-#define __G_TIMER_C__
-#include "galiasdef.c"