Fix typo
[platform/upstream/glib.git] / glib / gdatetime.c
index 634f73d..cbbf620 100644 (file)
@@ -3,20 +3,27 @@
  * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
  * Copyright (C) 2010 Emmanuele Bassi <ebassi@linux.intel.com>
+ * Copyright © 2010 Codethink Limited
  *
- * This is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * licence, or (at your option) any later version.
  *
- * This is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
+ * This is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA.
+ *
+ * Authors: Christian Hergert <chris@dronelabs.com>
+ *          Thiago Santos <thiago.sousa.santos@collabora.co.uk>
+ *          Emmanuele Bassi <ebassi@linux.intel.com>
+ *          Ryan Lortie <desrt@desrt.ca>
  */
 
 /* Algorithms within this file are based on the Calendar FAQ by
  *   to its correctness.
  */
 
+/* Prologue {{{1 */
+
 #include "config.h"
 
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+#ifdef HAVE_LANGINFO_TIME
+#include <langinfo.h>
 #endif
 
-#ifndef G_OS_WIN32
-#include <sys/time.h>
-#include <time.h>
-#endif /* !G_OS_WIN32 */
-
 #include "gdatetime.h"
 
+#include "gslice.h"
 #include "gatomic.h"
+#include "gcharset.h"
+#include "gconvert.h"
 #include "gfileutils.h"
 #include "ghash.h"
 #include "gmain.h"
 #include "gstrfuncs.h"
 #include "gtestutils.h"
 #include "gthread.h"
+#include "gtimezone.h"
 
 #include "glibintl.h"
 
+#ifndef G_OS_WIN32
+#include <sys/time.h>
+#include <time.h>
+#endif /* !G_OS_WIN32 */
+
 /**
  * SECTION:date-time
  * @title: GDateTime
- * @short_description: A structure representing Date and Time
+ * @short_description: a structure representing Date and Time
+ * @see_also: #GTimeZone
  *
- * #GDateTime is a structure that combines a date and time into a single
- * structure. It provides many conversion and methods to manipulate dates
- * and times. Time precision is provided down to microseconds.
+ * #GDateTime is a structure that combines a Gregorian date and time
+ * into a single structure.  It provides many conversion and methods to
+ * manipulate dates and times.  Time precision is provided down to
+ * microseconds and the time can range (proleptically) from 0001-01-01
+ * 00:00:00 to 9999-12-31 23:59:59.999999.  #GDateTime follows POSIX
+ * time in the sense that it is oblivious to leap seconds.
  *
- * #GDateTime is an immutable object: once it has been created it cannot be
- * modified further. All modifiers will create a new #GDateTime.
+ * #GDateTime is an immutable object; once it has been created it cannot
+ * be modified further.  All modifiers will create a new #GDateTime.
+ * Nearly all such functions can fail due to the date or time going out
+ * of range, in which case %NULL will be returned.
  *
  * #GDateTime is reference counted: the reference count is increased by calling
  * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
  * reference count drops to 0, the resources allocated by the #GDateTime
  * structure are released.
  *
- * Internally, #GDateTime uses the Proleptic Gregorian Calendar, the first
- * representable date is 0001-01-01. However, the public API uses the
- * internationally accepted Gregorian Calendar.
+ * Many parts of the API may produce non-obvious results.  As an
+ * example, adding two months to January 31st will yield March 31st
+ * whereas adding one month and then one month again will yield either
+ * March 28th or March 29th.  Also note that adding 24 hours is not
+ * always the same as adding one day (since days containing daylight
+ * savings time transitions are either 23 or 25 hours in length).
  *
  * #GDateTime is available since GLib 2.26.
  */
 
+struct _GDateTime
+{
+  /* Microsecond timekeeping within Day */
+  guint64 usec;
+
+  /* TimeZone information */
+  GTimeZone *tz;
+  gint interval;
+
+  /* 1 is 0001-01-01 in Proleptic Gregorian */
+  gint32 days;
+
+  volatile gint ref_count;
+};
+
+/* Time conversion {{{1 */
+
 #define UNIX_EPOCH_START     719163
+#define INSTANT_TO_UNIX(instant) \
+  ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
+#define UNIX_TO_INSTANT(unix) \
+  (((unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
 
 #define DAYS_IN_4YEARS    1461    /* days in 4 years */
 #define DAYS_IN_100YEARS  36524   /* days in 100 years */
 #define USEC_PER_DAY         (G_GINT64_CONSTANT (86400000000))
 #define SEC_PER_DAY          (G_GINT64_CONSTANT (86400))
 
-#define GREGORIAN_LEAP(y)    ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
-#define JULIAN_YEAR(d)       ((d)->julian / 365.25)
-#define DAYS_PER_PERIOD      (G_GINT64_CONSTANT (2914695))
-
-#define GET_AMPM(d,l)         ((g_date_time_get_hour (d) < 12)  \
-                                       /* Translators: 'before midday' indicator */ \
-                                ? (l ? C_("GDateTime", "am") \
-                                       /* Translators: 'before midday' indicator */ \
-                                     : C_("GDateTime", "AM")) \
-                                  /* Translators: 'after midday' indicator */ \
-                                : (l ? C_("GDateTime", "pm") \
-                                  /* Translators: 'after midday' indicator */ \
-                                     : C_("GDateTime", "PM")))
-
-#define WEEKDAY_ABBR(d)       (get_weekday_name_abbr (g_date_time_get_day_of_week (datetime)))
-#define WEEKDAY_FULL(d)       (get_weekday_name (g_date_time_get_day_of_week (datetime)))
-
-#define MONTH_ABBR(d)         (get_month_name_abbr (g_date_time_get_month (datetime)))
-#define MONTH_FULL(d)         (get_month_name (g_date_time_get_month (datetime)))
-
-/* Translators: this is the preferred format for expressing the date */
-#define GET_PREFERRED_DATE(d) (g_date_time_printf ((d), C_("GDateTime", "%m/%d/%y")))
-
-/* Translators: this is the preferred format for expressing the time */
-#define GET_PREFERRED_TIME(d) (g_date_time_printf ((d), C_("GDateTime", "%H:%M:%S")))
-
 #define SECS_PER_MINUTE (60)
 #define SECS_PER_HOUR   (60 * SECS_PER_MINUTE)
 #define SECS_PER_DAY    (24 * SECS_PER_HOUR)
 #define SECS_PER_YEAR   (365 * SECS_PER_DAY)
 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
 
-typedef struct _GTimeZoneFile   GTimeZoneFile;
+#define GREGORIAN_LEAP(y)    ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
+#define JULIAN_YEAR(d)       ((d)->julian / 365.25)
+#define DAYS_PER_PERIOD      (G_GINT64_CONSTANT (2914695))
 
-struct _GDateTime
+static const guint16 days_in_months[2][13] =
 {
-  /* 1 is 0001-01-01 in Proleptic Gregorian */
-  guint32 days;
-
-  /* Microsecond timekeeping within Day */
-  guint64 usec;
-
-  /* TimeZone information, NULL is UTC */
-  GTimeZone *tz;
-
-  volatile gint ref_count;
+  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+  { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 };
 
-struct _GTimeZone
+static const guint16 days_in_year[2][13] =
 {
-  GTimeZoneFile *tz_file;
+  {  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
+  {  0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
+};
 
-  gchar *name;
+#ifdef HAVE_LANGINFO_TIME
 
-  gint64 offset;
+#define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
+                     nl_langinfo (AM_STR) : \
+                     nl_langinfo (PM_STR))
 
-  guint is_dst      : 1;
-  guint is_utc      : 1;
-  guint is_floating : 1;
+#define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
+#define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
+#define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
+#define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
+#define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
+
+static const gint weekday_item[2][7] =
+{
+  { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 },
+  { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 }
 };
 
-struct _GTimeZoneFile
+static const gint month_item[2][12] =
 {
-  GMappedFile *file;
+  { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 },
+  { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 },
+};
 
-  gchar *filename;
+#define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
+#define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
+#define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
+#define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
 
-  volatile gint ref_count;
-};
+#else
 
-G_LOCK_DEFINE_STATIC (time_zone_files);
-static GHashTable *time_zone_files = NULL;
+#define GET_AMPM(d)          ((g_date_time_get_hour (d) < 12)  \
+                                       /* Translators: 'before midday' indicator */ \
+                                ? C_("GDateTime", "AM") \
+                                  /* Translators: 'after midday' indicator */ \
+                                : C_("GDateTime", "PM"))
 
-static const guint16 days_in_months[2][13] =
-{
-  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
-  { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
-};
+/* Translators: this is the preferred format for expressing the date and the time */
+#define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
 
-static const guint16 days_in_year[2][13] =
-{
-  {  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
-  {  0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
-};
+/* Translators: this is the preferred format for expressing the date */
+#define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
+
+/* Translators: this is the preferred format for expressing the time */
+#define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
+
+/* Translators: this is the preferred format for expressing 12 hour time */
+#define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
+
+#define WEEKDAY_ABBR(d)       (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
+#define WEEKDAY_FULL(d)       (get_weekday_name (g_date_time_get_day_of_week (d)))
+#define MONTH_ABBR(d)         (get_month_name_abbr (g_date_time_get_month (d)))
+#define MONTH_FULL(d)         (get_month_name (g_date_time_get_month (d)))
 
 static const gchar *
 get_month_name (gint month)
@@ -313,10 +347,12 @@ get_weekday_name_abbr (gint day)
   return NULL;
 }
 
+#endif  /* HAVE_LANGINFO_TIME */
+
 static inline gint
-date_to_proleptic_gregorian (gint year,
-                             gint month,
-                             gint day)
+ymd_to_days (gint year,
+             gint month,
+             gint day)
 {
   gint64 days;
 
@@ -332,765 +368,692 @@ date_to_proleptic_gregorian (gint year,
   return days;
 }
 
-static inline void
-g_date_time_add_days_internal (GDateTime *datetime,
-                               gint64     days)
-{
-  datetime->days += days;
-}
-
-static inline void
-g_date_time_add_usec (GDateTime *datetime,
-                      gint64     usecs)
+static void
+g_date_time_get_week_number (GDateTime *datetime,
+                             gint      *week_number,
+                             gint      *day_of_week,
+                             gint      *day_of_year)
 {
-  gint64 u = datetime->usec + usecs;
-  gint d = u / USEC_PER_DAY;
-
-  if (u < 0)
-    d -= 1;
-
-  if (d != 0)
-    g_date_time_add_days_internal (datetime, d);
+  gint a, b, c, d, e, f, g, n, s, month, day, year;
 
-  if (u < 0)
-    datetime->usec = USEC_PER_DAY + (u % USEC_PER_DAY);
-  else
-    datetime->usec = u % USEC_PER_DAY;
-}
+  g_date_time_get_ymd (datetime, &year, &month, &day);
 
-/*< internal >
- * g_date_time_add_ymd:
- * @datetime: a #GDateTime
- * @years: years to add, in the Gregorian calendar
- * @months: months to add, in the Gregorian calendar
- * @days: days to add, in the Gregorian calendar
- *
- * Updates @datetime by adding @years, @months and @days to it
- *
- * This function modifies the passed #GDateTime so public accessors
- * should make always pass a copy
- */
-static inline void
-g_date_time_add_ymd (GDateTime *datetime,
-                     gint       years,
-                     gint       months,
-                     gint       days)
-{
-  gint y = g_date_time_get_year (datetime);
-  gint m = g_date_time_get_month (datetime);
-  gint d = g_date_time_get_day_of_month (datetime);
-  gint step, i;
-  const guint16 *max_days;
-
-  y += years;
-
-  /* subtract one day for leap years */
-  if (GREGORIAN_LEAP (y) && m == 2)
+  if (month <= 2)
     {
-      if (d == 29)
-        d -= 1;
+      a = g_date_time_get_year (datetime) - 1;
+      b = (a / 4) - (a / 100) + (a / 400);
+      c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
+      s = b - c;
+      e = 0;
+      f = day - 1 + (31 * (month - 1));
     }
-
-  /* add months */
-  step = months > 0 ? 1 : -1;
-  for (i = 0; i < ABS (months); i++)
+  else
     {
-      m += step;
-
-      if (m < 1)
-        {
-          y -= 1;
-          m = 12;
-        }
-      else if (m > 12)
-        {
-          y += 1;
-          m = 1;
-        }
+      a = year;
+      b = (a / 4) - (a / 100) + (a / 400);
+      c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
+      s = b - c;
+      e = s + 1;
+      f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
     }
 
-  /* clamp the days */
-  max_days = days_in_months[GREGORIAN_LEAP (y) ? 1 : 0];
-  if (max_days[m] < d)
-    d = max_days[m];
-
-  datetime->days = date_to_proleptic_gregorian (y, m, d);
-  g_date_time_add_days_internal (datetime, days);
-}
-
-#define ZONEINFO_DIR            "zoneinfo"
-#define TZ_MAGIC                "TZif"
-#define TZ_MAGIC_LEN            (strlen (TZ_MAGIC))
-#define TZ_HEADER_SIZE          44
-#define TZ_TIMECNT_OFFSET       32
-#define TZ_TYPECNT_OFFSET       36
-#define TZ_TRANSITIONS_OFFSET   44
-#define TZ_TTINFO_SIZE          6
-#define TZ_TTINFO_GMTOFF_OFFSET 0
-#define TZ_TTINFO_ISDST_OFFSET  4
-#define TZ_TTINFO_NAME_OFFSET   5
-
-static gchar *
-get_tzdata_path (const gchar *tz_name)
-{
-  gchar *retval = NULL;
-  const gchar *tz_dir = g_getenv ("TZDIR");
+  g = (a + b) % 7;
+  d = (f + g - e) % 7;
+  n = f + 3 - d;
 
-  if (tz_dir != NULL)
-    retval = g_build_filename (tz_dir, tz_name, NULL);
-  else
+  if (week_number)
     {
-      if (strcmp (tz_name, "localtime") == 0)
-        retval = g_build_filename ("/", "etc", "localtime", NULL);
+      if (n < 0)
+        *week_number = 53 - ((g - s) / 5);
+      else if (n > 364 + s)
+        *week_number = 1;
       else
-        retval = g_build_filename ("/", "usr", "share", ZONEINFO_DIR, tz_name, NULL);
+        *week_number = (n / 7) + 1;
     }
 
-  return retval;
+  if (day_of_week)
+    *day_of_week = d + 1;
+
+  if (day_of_year)
+    *day_of_year = f + 1;
 }
 
-static gboolean
-parse_tzdata (GTimeZoneFile  *tz_file,
-              gint64          start,
-              gboolean        is_utc,
-              gint64         *_offset,
-              gboolean       *_is_dst,
-              gchar         **_name)
-{
-  gchar *contents;
-  gsize length;
-  guint32 timecnt, typecnt;
-  gint transitions_size, ttinfo_map_size;
-  guint8 *ttinfo_map, *ttinfos;
-  gint start_transition = -1;
-  guint32 *transitions;
-  gint32 offset;
-  guint8 isdst;
-  guint8 name_offset;
-  gint i;
-
-  contents = g_mapped_file_get_contents (tz_file->file);
-  length = g_mapped_file_get_length (tz_file->file);
-
-  if (length < TZ_HEADER_SIZE ||
-      (strncmp (contents, TZ_MAGIC, TZ_MAGIC_LEN) != 0))
-    {
-      return FALSE;
-    }
+/* Lifecycle {{{1 */
 
-  timecnt = GUINT32_FROM_BE (*(guint32 *)(contents + TZ_TIMECNT_OFFSET));
-  typecnt = GUINT32_FROM_BE (*(guint32 *)(contents + TZ_TYPECNT_OFFSET));
+static GDateTime *
+g_date_time_alloc (GTimeZone *tz)
+{
+  GDateTime *datetime;
 
-  transitions = (guint32 *)(contents + TZ_TRANSITIONS_OFFSET);
-  transitions_size = timecnt * sizeof (*transitions);
-  ttinfo_map = (void *)(contents + TZ_TRANSITIONS_OFFSET + transitions_size);
-  ttinfo_map_size = timecnt;
-  ttinfos = (void *)(ttinfo_map + ttinfo_map_size);
+  datetime = g_slice_new0 (GDateTime);
+  datetime->tz = g_time_zone_ref (tz);
+  datetime->ref_count = 1;
 
-  /*
-   * Find the first transition that contains the 'start' time
-   */
-  for (i = 1; i < timecnt && start_transition == -1; i++)
-    {
-      gint32 transition_time = GINT32_FROM_BE (transitions[i]);
+  return datetime;
+}
 
-      /* if is_utc is not set, we need to add this time offset to compare with
-       * start, because it is already on the timezone time */
-      if (!is_utc)
-        {
-          gint32 off;
+/**
+ * g_date_time_ref:
+ * @datetime: a #GDateTime
+ *
+ * Atomically increments the reference count of @datetime by one.
+ *
+ * Returns: the #GDateTime with the reference count increased
+ *
+ * Since: 2.26
+ */
+GDateTime *
+g_date_time_ref (GDateTime *datetime)
+{
+  g_return_val_if_fail (datetime != NULL, NULL);
+  g_return_val_if_fail (datetime->ref_count > 0, NULL);
 
-          off = *(gint32 *)(ttinfos + ttinfo_map[i] * TZ_TTINFO_SIZE +
-                TZ_TTINFO_GMTOFF_OFFSET);
-          off = GINT32_FROM_BE (off);
+  g_atomic_int_inc (&datetime->ref_count);
 
-          transition_time += off;
-        }
+  return datetime;
+}
 
-      if (transition_time > start)
-        {
-          start_transition = ttinfo_map[i - 1];
-          break;
-        }
-    }
+/**
+ * g_date_time_unref:
+ * @datetime: a #GDateTime
+ *
+ * Atomically decrements the reference count of @datetime by one.
+ *
+ * When the reference count reaches zero, the resources allocated by
+ * @datetime are freed
+ *
+ * Since: 2.26
+ */
+void
+g_date_time_unref (GDateTime *datetime)
+{
+  g_return_if_fail (datetime != NULL);
+  g_return_if_fail (datetime->ref_count > 0);
 
-  if (start_transition == -1)
+  if (g_atomic_int_dec_and_test (&datetime->ref_count))
     {
-      if (timecnt)
-        start_transition = ttinfo_map[timecnt - 1];
-      else
-        start_transition = 0;
+      g_time_zone_unref (datetime->tz);
+      g_slice_free (GDateTime, datetime);
     }
-
-  /* Copy the data out of the corresponding ttinfo structs */
-  offset = *(gint32 *)(ttinfos + start_transition * TZ_TTINFO_SIZE +
-           TZ_TTINFO_GMTOFF_OFFSET);
-  offset = GINT32_FROM_BE (offset);
-  isdst = *(ttinfos + start_transition * TZ_TTINFO_SIZE +
-          TZ_TTINFO_ISDST_OFFSET);
-  name_offset = *(ttinfos + start_transition * TZ_TTINFO_SIZE +
-                TZ_TTINFO_NAME_OFFSET);
-
-  if (_name != NULL)
-    *_name = g_strdup ((gchar*) (ttinfos + TZ_TTINFO_SIZE * typecnt + name_offset));
-
-  if (_offset)
-    *_offset = offset;
-
-  if (_is_dst)
-    *_is_dst = isdst;
-
-  return TRUE;
 }
 
-static GTimeZoneFile *
-g_time_zone_file_new (const gchar *filename)
+/* Internal state transformers {{{1 */
+/*< internal >
+ * g_date_time_to_instant:
+ * @datetime: a #GDateTime
+ *
+ * Convert a @datetime into an instant.
+ *
+ * An instant is a number that uniquely describes a particular
+ * microsecond in time, taking time zone considerations into account.
+ * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
+ *
+ * An instant is always positive but we use a signed return value to
+ * avoid troubles with C.
+ */
+static gint64
+g_date_time_to_instant (GDateTime *datetime)
 {
-  GMappedFile *tz_file;
-  GTimeZoneFile *retval;
-
-  tz_file = g_mapped_file_new (filename, FALSE, NULL);
-  if (tz_file == NULL)
-    return NULL;
+  gint64 offset;
 
-  retval = g_slice_new (GTimeZoneFile);
-  retval->filename = g_strdup (filename);
-  retval->file = tz_file;
-  retval->ref_count = 1;
+  offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
+  offset *= USEC_PER_SECOND;
 
-  return retval;
+  return datetime->days * USEC_PER_DAY + datetime->usec - offset;
 }
 
-static GTimeZoneFile *
-g_time_zone_file_ref (GTimeZoneFile *tz_file)
+/*< internal >
+ * g_date_time_from_instant:
+ * @tz: a #GTimeZone
+ * @instant: a instant in time
+ *
+ * Creates a #GDateTime from a time zone and an instant.
+ *
+ * This might fail if the time ends up being out of range.
+ */
+static GDateTime *
+g_date_time_from_instant (GTimeZone *tz,
+                          gint64     instant)
 {
-  if (tz_file == NULL)
+  GDateTime *datetime;
+  gint64 offset;
+
+  if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000))
     return NULL;
 
-  g_atomic_int_inc (&tz_file->ref_count);
+  datetime = g_date_time_alloc (tz);
+  datetime->interval = g_time_zone_find_interval (tz,
+                                                  G_TIME_TYPE_UNIVERSAL,
+                                                  INSTANT_TO_UNIX (instant));
+  offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
+  offset *= USEC_PER_SECOND;
 
-  return tz_file;
-}
+  instant += offset;
 
-static void
-g_time_zone_file_unref (GTimeZoneFile *tz_file)
-{
-  if (tz_file == NULL)
-    return;
+  datetime->days = instant / USEC_PER_DAY;
+  datetime->usec = instant % USEC_PER_DAY;
 
-  if (g_atomic_int_dec_and_test (&tz_file->ref_count))
+  if (datetime->days < 1 || 3652059 < datetime->days)
     {
-      /* remove the TimeZoneFile from the hash table of known files */
-      G_LOCK (time_zone_files);
-      g_hash_table_remove (time_zone_files, tz_file->filename);
-      G_UNLOCK (time_zone_files);
-
-      g_mapped_file_unref (tz_file->file);
-      g_free (tz_file->filename);
-      g_slice_free (GTimeZoneFile, tz_file);
+      g_date_time_unref (datetime);
+      datetime = NULL;
     }
+
+  return datetime;
 }
 
+
 /*< internal >
- * g_time_zone_file_get_for_path:
- * @path: the full path for a time zone file
- *
- * Retrieves a #GTimeZoneFile for the given path.
+ * g_date_time_deal_with_date_change:
+ * @datetime: a #GDateTime
  *
- * If a time zone file for the same path has been already requested,
- * this function will return the same #GTimeZoneFile with its reference
- * count increased by one; otherwise, a new #GTimeZoneFile will be created
- * and added to the known time zone files.
+ * This function should be called whenever the date changes by adding
+ * days, months or years.  It does three things.
  *
- * The time zone files are removed from the list of known files when their
- * reference count reaches 0.
+ * First, we ensure that the date falls between 0001-01-01 and
+ * 9999-12-31 and return %FALSE if it does not.
  *
- * This function holds the lock on the time_zone_files hash table.
+ * Next we update the ->interval field.
  *
- * Return value: a #GTimeZoneFile or %NULL
+ * Finally, we ensure that the resulting date and time pair exists (by
+ * ensuring that our time zone has an interval containing it) and
+ * adjusting as required.  For example, if we have the time 02:30:00 on
+ * March 13 2010 in Toronto and we add 1 day to it, we would end up with
+ * 2:30am on March 14th, which doesn't exist.  In that case, we bump the
+ * time up to 3:00am.
  */
-static GTimeZoneFile *
-g_time_zone_file_get_for_path (const gchar *path)
+static gboolean
+g_date_time_deal_with_date_change (GDateTime *datetime)
 {
-  GTimeZoneFile *retval;
+  GTimeType was_dst;
+  gint64 full_time;
+  gint64 usec;
 
-  G_LOCK (time_zone_files);
+  if (datetime->days < 1 || datetime->days > 3652059)
+    return FALSE;
 
-  if (G_LIKELY (time_zone_files != NULL))
-    {
-      retval = g_hash_table_lookup (time_zone_files, path);
-      if (retval != NULL)
-        {
-          retval = g_time_zone_file_ref (retval);
-          goto out;
-        }
-    }
+  was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval);
 
-  retval = g_time_zone_file_new (path);
-  if (retval != NULL)
-    {
-      if (G_UNLIKELY (time_zone_files == NULL))
-        time_zone_files = g_hash_table_new (g_str_hash, g_str_equal);
+  full_time = datetime->days * USEC_PER_DAY + datetime->usec;
 
-      g_hash_table_insert (time_zone_files, retval->filename, retval);
-    }
 
-out:
-  G_UNLOCK (time_zone_files);
+  usec = full_time % USEC_PER_SECOND;
+  full_time /= USEC_PER_SECOND;
+  full_time -= UNIX_EPOCH_START * SEC_PER_DAY;
+
+  datetime->interval = g_time_zone_adjust_time (datetime->tz,
+                                                was_dst,
+                                                &full_time);
+  full_time += UNIX_EPOCH_START * SEC_PER_DAY;
+  full_time *= USEC_PER_SECOND;
+  full_time += usec;
 
-  return retval;
+  datetime->days = full_time / USEC_PER_DAY;
+  datetime->usec = full_time % USEC_PER_DAY;
+
+  /* maybe daylight time caused us to shift to a different day,
+   * but it definitely didn't push us into a different year */
+  return TRUE;
 }
 
-/**
- * g_time_zone_new:
- * @offset: the timezone offset from UTC, in seconds
- * @is_dst: whether the timezone is in Daylight Saving Time or not
- *
- * Creates a new #GTimeZone for the given @offset, to be used when
- * creating a #GDateTime.
- *
- * The #GTimeZone created will not be floating.
- *
- * Return value: (transfer full): the newly allocated #GTimeZone
- *
- * Since: 2.26
- */
-GTimeZone *
-g_time_zone_new (gint     offset,
-                 gboolean is_dst)
+static GDateTime *
+g_date_time_replace_days (GDateTime *datetime,
+                          gint       days)
 {
-  GTimeZone *tz;
-
-  g_return_val_if_fail (offset >= -12 * SECS_PER_HOUR && offset <= 12 * SECS_PER_HOUR, NULL);
+  GDateTime *new;
 
-  tz = g_slice_new (GTimeZone);
-  tz->offset = offset;
-  tz->is_dst = is_dst;
-  tz->tz_file = NULL;
-  tz->is_floating = FALSE;
+  new = g_date_time_alloc (datetime->tz);
+  new->interval = datetime->interval;
+  new->usec = datetime->usec;
+  new->days = days;
 
-  if (tz->offset == 0)
-    tz->name = g_strdup_printf ("UTC");
-  else
-    tz->name = g_strdup_printf ("UTC%c%02d:%02d",
-                                tz->offset > 0 ? '+' : '-',
-                                (int) tz->offset / 3600,
-                                (int) tz->offset / 60 % 60);
+  if (!g_date_time_deal_with_date_change (new))
+    {
+      g_date_time_unref (new);
+      new = NULL;
+    }
 
-  return tz;
+  return new;
 }
 
-/**
- * g_time_zone_new_for_name:
- * @name: an ASCII string containing the Olson's database name of the
- *   timezone, e.g. "America/New_York" or "Europe/London"
- *
- * Creates a new #GTimeZone for the given @name.
- *
- * The returned timezone is "floating": if queried, it will return invalid
- * values. A floating #GTimeZone is "sunk" when it is bound to a #GDateTime
- * object.
- *
- * For instance:
+/* now/unix/timeval Constructors {{{1 */
+
+/*< internal >
+ * g_date_time_new_from_timeval:
+ * @tz: a #GTimeZone
+ * @tv: a #GTimeVal
  *
- * |[
- * /&ast; the time zone is "floating" &ast;/
- * GTimeZone *tz = g_time_zone_new_for_name ("Europe/London");
+ * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
+ * given time zone @tz.
  *
- * /&ast; this will print: "UTC offset: 0 seconds" &ast;/
- * g_print ("UTC offset: %d seconds\n", g_time_zone_get_offset (tz));
+ * The time contained in a #GTimeVal is always stored in the form of
+ * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
+ * given time zone.
  *
- * /&ast; the GDateTime object copies the time zone &ast;/
- * GDateTime *dt = g_date_time_new_full (2010, 9, 15, 12, 0, 0, tz);
+ * This call can fail (returning %NULL) if @tv represents a time outside
+ * of the supported range of #GDateTime.
  *
- * /&ast; this will print: "UTC offset: 3600 seconds", because Europe/London
- *  &ast; on that date is in daylight saving time
- *  &ast;/
- * g_print ("UTC offset: %d seconds\n",
- *          g_date_time_get_utc_offset (dt) / G_USEC_PER_SEC);
- * ]|
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
  *
- * Return value: (transfer full): the newly created, floating #GTimeZone
- *   object for the given name, or %NULL if none was found
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-GTimeZone *
-g_time_zone_new_for_name (const gchar *name)
+ **/
+static GDateTime *
+g_date_time_new_from_timeval (GTimeZone      *tz,
+                              const GTimeVal *tv)
 {
-  GTimeZone *tz;
-  gchar *filename;
-  GTimeZoneFile *tz_file;
-
-  g_return_val_if_fail (name != NULL, NULL);
-
-  filename = get_tzdata_path (name);
-  if (filename == NULL)
-    return NULL;
-
-  tz_file = g_time_zone_file_get_for_path (filename);
-  g_free (filename);
-
-  if (tz_file == NULL)
-    return NULL;
-
-  tz = g_slice_new (GTimeZone);
-  tz->tz_file = tz_file;
-  tz->name = NULL;
-  tz->offset = 0;
-  tz->is_dst = FALSE;
-  tz->is_utc = (strcmp (name, "UTC") == 0);
-  tz->is_floating = TRUE;
-
-  return tz;
+  return g_date_time_from_instant (tz, tv->tv_usec +
+                                   UNIX_TO_INSTANT (tv->tv_sec));
 }
 
-/**
- * g_time_zone_new_utc:
+/*< internal >
+ * g_date_time_new_from_unix:
+ * @tz: a #GTimeZone
+ * @t: the Unix time
+ *
+ * Creates a #GDateTime corresponding to the given Unix time @t in the
+ * given time zone @tz.
  *
- * Creates a new #GTimeZone for UTC.
+ * Unix time is the number of seconds that have elapsed since 1970-01-01
+ * 00:00:00 UTC, regardless of the time zone given.
  *
- * Return value: (transfer full): a newly created #GTimeZone for UTC.
- *   Use g_time_zone_free() when done.
+ * This call can fail (returning %NULL) if @t represents a time outside
+ * of the supported range of #GDateTime.
+ *
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-GTimeZone *
-g_time_zone_new_utc (void)
+ **/
+static GDateTime *
+g_date_time_new_from_unix (GTimeZone *tz,
+                           gint64     secs)
 {
-  return g_time_zone_new (0, FALSE);
+  return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
 }
 
 /**
- * g_time_zone_new_local:
+ * g_date_time_new_now:
+ * @tz: a #GTimeZone
+ *
+ * Creates a #GDateTime corresponding to this exact instant in the given
+ * time zone @tz.  The time is as accurate as the system allows, to a
+ * maximum accuracy of 1 microsecond.
  *
- * Creates a new #GTimeZone for the local timezone.
+ * This function will always succeed unless the system clock is set to
+ * truly insane values (or unless GLib is still being used after the
+ * year 9999).
  *
- * The returned #GTimeZone is floating.
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
  *
- * Return value: (transfer full): a newly created, floating #GTimeZone.
- *   Use g_time_zone_free() when done.
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-GTimeZone *
-g_time_zone_new_local (void)
+ **/
+GDateTime *
+g_date_time_new_now (GTimeZone *tz)
 {
-  return g_time_zone_new_for_name ("localtime");
+  GTimeVal tv;
+
+  g_get_current_time (&tv);
+
+  return g_date_time_new_from_timeval (tz, &tv);
 }
 
 /**
- * g_time_zone_copy:
- * @time_zone: a #GTimeZone
+ * g_date_time_new_now_local:
  *
- * Copies a #GTimeZone. If @time_zone is floating, the returned copy
- * will be floating as well.
+ * Creates a #GDateTime corresponding to this exact instant in the local
+ * time zone.
  *
- * Return value: (transfer full): the newly created #GTimeZone
+ * This is equivalent to calling g_date_time_new_now() with the time
+ * zone returned by g_time_zone_new_local().
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-GTimeZone *
-g_time_zone_copy (const GTimeZone *time_zone)
+ **/
+GDateTime *
+g_date_time_new_now_local (void)
 {
-  GTimeZone *retval;
-
-  g_return_val_if_fail (time_zone != NULL, NULL);
-
-  retval = g_slice_dup (GTimeZone, time_zone);
-
-  if (time_zone->tz_file != NULL)
-    retval->tz_file = g_time_zone_file_ref (time_zone->tz_file);
+  GDateTime *datetime;
+  GTimeZone *local;
 
-  if (time_zone->name != NULL)
-    retval->name = g_strdup (time_zone->name);
+  local = g_time_zone_new_local ();
+  datetime = g_date_time_new_now (local);
+  g_time_zone_unref (local);
 
-  return retval;
+  return datetime;
 }
 
 /**
- * g_time_zone_free:
- * @time_zone: a #GTimeZone
+ * g_date_time_new_now_utc:
  *
- * Frees the resources associated with a #GTimeZone
+ * Creates a #GDateTime corresponding to this exact instant in UTC.
+ *
+ * This is equivalent to calling g_date_time_new_now() with the time
+ * zone returned by g_time_zone_new_utc().
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-void
-g_time_zone_free (GTimeZone *time_zone)
+ **/
+GDateTime *
+g_date_time_new_now_utc (void)
 {
-  g_return_if_fail (time_zone != NULL);
+  GDateTime *datetime;
+  GTimeZone *utc;
 
-  if (time_zone->tz_file != NULL)
-    g_time_zone_file_unref (time_zone->tz_file);
+  utc = g_time_zone_new_utc ();
+  datetime = g_date_time_new_now (utc);
+  g_time_zone_unref (utc);
 
-  g_free (time_zone->name);
-  g_slice_free (GTimeZone, time_zone);
+  return datetime;
 }
 
 /**
- * g_time_zone_get_name:
- * @time_zone: a #GTimeZone
+ * g_date_time_new_from_unix_local:
+ * @t: the Unix time
+ *
+ * Creates a #GDateTime corresponding to the given Unix time @t in the
+ * local time zone.
  *
- * Retrieves the name of the @time_zone, or %NULL if the #GTimeZone is
- * floating.
+ * Unix time is the number of seconds that have elapsed since 1970-01-01
+ * 00:00:00 UTC, regardless of the local time offset.
  *
- * Return value: (transfer none): the name of the #GTimeZone. The returned
- *   string is owned by the #GTimeZone and it should never be modified or
- *   freed
+ * This call can fail (returning %NULL) if @t represents a time outside
+ * of the supported range of #GDateTime.
+ *
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-G_CONST_RETURN gchar *
-g_time_zone_get_name (const GTimeZone *time_zone)
+ **/
+GDateTime *
+g_date_time_new_from_unix_local (gint64 t)
 {
-  g_return_val_if_fail (time_zone != NULL, NULL);
+  GDateTime *datetime;
+  GTimeZone *local;
 
-  if (!time_zone->is_floating)
-    return time_zone->name;
+  local = g_time_zone_new_local ();
+  datetime = g_date_time_new_from_unix (local, t);
+  g_time_zone_unref (local);
 
-  return NULL;
+  return datetime;
 }
 
 /**
- * g_time_zone_get_offset:
- * @time_zone: a #GTimeZone
+ * g_date_time_new_from_unix_utc:
+ * @t: the Unix time
+ *
+ * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
  *
- * Retrieves the offset of the @time_zone, in seconds from UTC, or 0
- * if the #GTimeZone is floating.
+ * Unix time is the number of seconds that have elapsed since 1970-01-01
+ * 00:00:00 UTC.
  *
- * Return value: the offset from UTC, in seconds
+ * This call can fail (returning %NULL) if @t represents a time outside
+ * of the supported range of #GDateTime.
+ *
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-gint
-g_time_zone_get_offset (const GTimeZone *time_zone)
+ **/
+GDateTime *
+g_date_time_new_from_unix_utc (gint64 t)
 {
-  g_return_val_if_fail (time_zone != NULL, 0);
+  GDateTime *datetime;
+  GTimeZone *utc;
 
-  if (!time_zone->is_floating)
-    return time_zone->offset;
+  utc = g_time_zone_new_utc ();
+  datetime = g_date_time_new_from_unix (utc, t);
+  g_time_zone_unref (utc);
 
-  return 0;
+  return datetime;
 }
 
 /**
- * g_time_zone_get_is_dst:
- * @time_zone: a #GTimeZone
+ * g_date_time_new_from_timeval_local:
+ * @tv: a #GTimeVal
  *
- * Checks whether the @time_zone is in Daylight Saving Time.
- * If the #GTimeZone is floating, %FALSE is always returned.
+ * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
+ * local time zone.
  *
- * Return value: %TRUE if the #GTimeZone is in DST, or %FALSE.
+ * The time contained in a #GTimeVal is always stored in the form of
+ * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
+ * local time offset.
+ *
+ * This call can fail (returning %NULL) if @tv represents a time outside
+ * of the supported range of #GDateTime.
+ *
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-gboolean
-g_time_zone_get_is_dst (const GTimeZone *time_zone)
+ **/
+GDateTime *
+g_date_time_new_from_timeval_local (const GTimeVal *tv)
 {
-  g_return_val_if_fail (time_zone != NULL, FALSE);
+  GDateTime *datetime;
+  GTimeZone *local;
 
-  if (!time_zone->is_floating)
-    return time_zone->is_dst;
+  local = g_time_zone_new_local ();
+  datetime = g_date_time_new_from_timeval (local, tv);
+  g_time_zone_unref (local);
 
-  return FALSE;
+  return datetime;
 }
 
 /**
- * g_time_zone_is_floating:
- * @time_zone: a #GTimeZone
+ * g_date_time_new_from_timeval_utc:
+ * @tv: a #GTimeVal
  *
- * Checks whether @time_zone is floating
+ * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
  *
- * Return value: %TRUE if the #GTimeZone is floating, and %FALSE otherwise
+ * The time contained in a #GTimeVal is always stored in the form of
+ * seconds elapsed since 1970-01-01 00:00:00 UTC.
  *
- * Since: 2.26
- */
-gboolean
-g_time_zone_is_floating (const GTimeZone *time_zone)
-{
-  g_return_val_if_fail (time_zone != NULL, FALSE);
-
-  return time_zone->is_floating;
-}
-
-/*< internal >
- * g_time_zone_sink:
- * @time_zone: a #GTimeZone
- * @datetime: a #GDateTime
+ * This call can fail (returning %NULL) if @tv represents a time outside
+ * of the supported range of #GDateTime.
  *
- * Sinks the floating state of @time_zone by associating it with the
- * given #GDateTime.
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
  *
- * If @time_zone is not floating, this function does not do anything
- */
-static void
-g_time_zone_sink (GTimeZone *time_zone,
-                  GDateTime *datetime)
-{
-  gint64 offset, epoch;
-  gboolean is_dst, is_utc;
-  gchar *abbrev;
-
-  if (!time_zone->is_floating)
-    return;
-
-  epoch = g_date_time_to_epoch (datetime);
-  is_utc = time_zone->is_utc;
-  offset = 0;
-  is_dst = FALSE;
-  abbrev = NULL;
-  if (parse_tzdata (time_zone->tz_file, epoch, is_utc, &offset, &is_dst, &abbrev))
-    {
-      time_zone->offset = offset;
-      time_zone->is_dst = is_dst;
-      time_zone->name = abbrev;
-      time_zone->is_utc = (offset == 0);
-      time_zone->is_floating = FALSE;
-    }
-}
-
-static GDateTime *
-g_date_time_new (void)
+ * Returns: a new #GDateTime, or %NULL
+ *
+ * Since: 2.26
+ **/
+GDateTime *
+g_date_time_new_from_timeval_utc (const GTimeVal *tv)
 {
   GDateTime *datetime;
+  GTimeZone *utc;
 
-  datetime = g_slice_new0 (GDateTime);
-  datetime->ref_count = 1;
+  utc = g_time_zone_new_utc ();
+  datetime = g_date_time_new_from_timeval (utc, tv);
+  g_time_zone_unref (utc);
 
   return datetime;
 }
 
-/*< internal >
- * g_date_time_copy:
- * @datetime: a #GDateTime
+/* full new functions {{{1 */
+
+/**
+ * g_date_time_new:
+ * @tz: a #GTimeZone
+ * @year: the year component of the date
+ * @month: the month component of the date
+ * @day: the day component of the date
+ * @hour: the hour component of the date
+ * @minute: the minute component of the date
+ * @seconds: the number of seconds past the minute
  *
- * Creates a copy of @datetime.
+ * Creates a new #GDateTime corresponding to the given date and time in
+ * the time zone @tz.
  *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
- */
-static GDateTime *
-g_date_time_copy (const GDateTime *datetime)
+ * The @year must be between 1 and 9999, @month between 1 and 12 and @day
+ * between 1 and 28, 29, 30 or 31 depending on the month and the year.
+ *
+ * @hour must be between 0 and 23 and @minute must be between 0 and 59.
+ *
+ * @seconds must be at least 0.0 and must be strictly less than 60.0.
+ * It will be rounded down to the nearest microsecond.
+ *
+ * If the given time is not representable in the given time zone (for
+ * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
+ * time) then the time will be rounded up to the nearest existing time
+ * (in this case, 03:00).  If this matters to you then you should verify
+ * the return value for containing the same as the numbers you gave.
+ *
+ * In the case that the given time is ambiguous in the given time zone
+ * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
+ * savings time) then the time falling within standard (ie:
+ * non-daylight) time is taken.
+ *
+ * It not considered a programmer error for the values to this function
+ * to be out of range, but in the case that they are, the function will
+ * return %NULL.
+ *
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
+ *
+ * Returns: a new #GDateTime, or %NULL
+ *
+ * Since: 2.26
+ **/
+GDateTime *
+g_date_time_new (GTimeZone *tz,
+                 gint       year,
+                 gint       month,
+                 gint       day,
+                 gint       hour,
+                 gint       minute,
+                 gdouble    seconds)
 {
-  GDateTime *copied;
+  GDateTime *datetime;
+  gint64 full_time;
 
-  g_return_val_if_fail (datetime != NULL, NULL);
+  g_return_val_if_fail (tz != NULL, NULL);
 
-  copied = g_date_time_new ();
-  copied->days = datetime->days;
-  copied->usec = datetime->usec;
+  if (year < 1 || year > 9999 ||
+      month < 1 || month > 12 ||
+      day < 1 || day > 31 ||
+      hour < 0 || hour > 23 ||
+      minute < 0 || minute > 59 ||
+      seconds < 0.0 || seconds >= 60.0)
+    return NULL;
 
-  if (datetime->tz != NULL)
-    copied->tz = g_time_zone_copy (datetime->tz);
+  datetime = g_date_time_alloc (tz);
+  datetime->days = ymd_to_days (year, month, day);
+  datetime->usec = (hour   * USEC_PER_HOUR)
+                 + (minute * USEC_PER_MINUTE)
+                 + (gint64) (seconds * USEC_PER_SECOND);
 
-  return copied;
-}
+  full_time = SEC_PER_DAY *
+                (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
+              SECS_PER_HOUR * hour +
+              SECS_PER_MINUTE * minute +
+              (int) seconds;
 
-static void
-g_date_time_free (GDateTime *datetime)
-{
-  if (G_UNLIKELY (datetime == NULL))
-    return;
+  datetime->interval = g_time_zone_adjust_time (datetime->tz,
+                                                G_TIME_TYPE_STANDARD,
+                                                &full_time);
 
-  if (datetime->tz != NULL)
-    g_time_zone_free (datetime->tz);
+  full_time += UNIX_EPOCH_START * SEC_PER_DAY;
+  datetime->days = full_time / SEC_PER_DAY;
+  datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
+  datetime->usec += ((int) (seconds * USEC_PER_SECOND)) % USEC_PER_SECOND;
 
-  g_slice_free (GDateTime, datetime);
+  return datetime;
 }
 
 /**
- * g_date_time_ref:
- * @datetime: a #GDateTime
+ * g_date_time_new_local:
+ * @year: the year component of the date
+ * @month: the month component of the date
+ * @day: the day component of the date
+ * @hour: the hour component of the date
+ * @minute: the minute component of the date
+ * @seconds: the number of seconds past the minute
  *
- * Atomically increments the reference count of @datetime by one.
+ * Creates a new #GDateTime corresponding to the given date and time in
+ * the local time zone.
  *
- * Return value: the #GDateTime with the reference count increased
+ * This call is equivalent to calling g_date_time_new() with the time
+ * zone returned by g_time_zone_new_local().
+ *
+ * Returns: a #GDateTime, or %NULL
  *
  * Since: 2.26
- */
+ **/
 GDateTime *
-g_date_time_ref (GDateTime *datetime)
+g_date_time_new_local (gint    year,
+                       gint    month,
+                       gint    day,
+                       gint    hour,
+                       gint    minute,
+                       gdouble seconds)
 {
-  g_return_val_if_fail (datetime != NULL, NULL);
-  g_return_val_if_fail (datetime->ref_count > 0, NULL);
+  GDateTime *datetime;
+  GTimeZone *local;
 
-  g_atomic_int_inc (&datetime->ref_count);
+  local = g_time_zone_new_local ();
+  datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
+  g_time_zone_unref (local);
 
   return datetime;
 }
 
 /**
- * g_date_time_unref:
- * @datetime: a #GDateTime
+ * g_date_time_new_utc:
+ * @year: the year component of the date
+ * @month: the month component of the date
+ * @day: the day component of the date
+ * @hour: the hour component of the date
+ * @minute: the minute component of the date
+ * @seconds: the number of seconds past the minute
  *
- * Atomically decrements the reference count of @datetime by one.
+ * Creates a new #GDateTime corresponding to the given date and time in
+ * UTC.
  *
- * When the reference count reaches zero, the resources allocated by
- * @datetime are freed
+ * This call is equivalent to calling g_date_time_new() with the time
+ * zone returned by g_time_zone_new_utc().
+ *
+ * Returns: a #GDateTime, or %NULL
  *
  * Since: 2.26
- */
-void
-g_date_time_unref (GDateTime *datetime)
-{
-  g_return_if_fail (datetime != NULL);
-  g_return_if_fail (datetime->ref_count > 0);
-
-  if (g_atomic_int_dec_and_test (&datetime->ref_count))
-    g_date_time_free (datetime);
-}
-
-static void
-g_date_time_get_week_number (const GDateTime *datetime,
-                             gint            *week_number,
-                             gint            *day_of_week,
-                             gint            *day_of_year)
+ **/
+GDateTime *
+g_date_time_new_utc (gint    year,
+                     gint    month,
+                     gint    day,
+                     gint    hour,
+                     gint    minute,
+                     gdouble seconds)
 {
-  gint a, b, c, d, e, f, g, n, s, month, day, year;
-
-  g_date_time_get_dmy (datetime, &day, &month, &year);
-
-  if (month <= 2)
-    {
-      a = g_date_time_get_year (datetime) - 1;
-      b = (a / 4) - (a / 100) + (a / 400);
-      c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
-      s = b - c;
-      e = 0;
-      f = day - 1 + (31 * (month - 1));
-    }
-  else
-    {
-      a = year;
-      b = (a / 4) - (a / 100) + (a / 400);
-      c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
-      s = b - c;
-      e = s + 1;
-      f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
-    }
-
-  g = (a + b) % 7;
-  d = (f + g - e) % 7;
-  n = f + 3 - d;
-
-  if (week_number)
-    {
-      if (n < 0)
-        *week_number = 53 - ((g - s) / 5);
-      else if (n > 364 + s)
-        *week_number = 1;
-      else
-        *week_number = (n / 7) + 1;
-    }
+  GDateTime *datetime;
+  GTimeZone *utc;
 
-  if (day_of_week)
-    *day_of_week = d + 1;
+  utc = g_time_zone_new_utc ();
+  datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
+  g_time_zone_unref (utc);
 
-  if (day_of_year)
-    *day_of_year = f + 1;
+  return datetime;
 }
 
+/* Adders {{{1 */
+
 /**
  * g_date_time_add:
  * @datetime: a #GDateTime
@@ -1098,23 +1061,17 @@ g_date_time_get_week_number (const GDateTime *datetime,
  *
  * Creates a copy of @datetime and adds the specified timespan to the copy.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add (const GDateTime *datetime,
-                 GTimeSpan        timespan)
+g_date_time_add (GDateTime *datetime,
+                 GTimeSpan  timespan)
 {
-  GDateTime *dt;
-
-  g_return_val_if_fail (datetime != NULL, NULL);
-
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_usec (dt, timespan);
-
-  return dt;
+  return g_date_time_from_instant (datetime->tz, timespan +
+                                   g_date_time_to_instant (datetime));
 }
 
 /**
@@ -1123,25 +1080,33 @@ g_date_time_add (const GDateTime *datetime,
  * @years: the number of years
  *
  * Creates a copy of @datetime and adds the specified number of years to the
- * copy.
+ * copy. Add negative values to subtract years.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
-GDateTime*
-g_date_time_add_years (const GDateTime *datetime,
-                       gint             years)
+GDateTime *
+g_date_time_add_years (GDateTime *datetime,
+                       gint       years)
 {
-  GDateTime *dt;
+  gint year, month, day;
 
   g_return_val_if_fail (datetime != NULL, NULL);
 
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_ymd (dt, years, 0, 0);
+  if (years < -10000 || years > 10000)
+    return NULL;
+
+  g_date_time_get_ymd (datetime, &year, &month, &day);
+  year += years;
+
+  /* only possible issue is if we've entered a year with no February 29
+   */
+  if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
+    day = 28;
 
-  return dt;
+  return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
 }
 
 /**
@@ -1150,25 +1115,41 @@ g_date_time_add_years (const GDateTime *datetime,
  * @months: the number of months
  *
  * Creates a copy of @datetime and adds the specified number of months to the
- * copy.
+ * copy. Add negative values to subtract months.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add_months (const GDateTime *datetime,
-                        gint             months)
+g_date_time_add_months (GDateTime *datetime,
+                        gint       months)
 {
-  GDateTime *dt;
+  gint year, month, day;
 
   g_return_val_if_fail (datetime != NULL, NULL);
+  g_date_time_get_ymd (datetime, &year, &month, &day);
+
+  if (months < -120000 || months > 120000)
+    return NULL;
+
+  year += months / 12;
+  month += months % 12;
+  if (month < 1)
+    {
+      month += 12;
+      year--;
+    }
+  else if (month > 12)
+    {
+      month -= 12;
+      year++;
+    }
 
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_ymd (dt, 0, months, 0);
+  day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
 
-  return dt;
+  return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
 }
 
 /**
@@ -1177,15 +1158,15 @@ g_date_time_add_months (const GDateTime *datetime,
  * @weeks: the number of weeks
  *
  * Creates a copy of @datetime and adds the specified number of weeks to the
- * copy.
+ * copy. Add negative values to subtract weeks.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add_weeks (const GDateTime *datetime,
+g_date_time_add_weeks (GDateTime *datetime,
                        gint             weeks)
 {
   g_return_val_if_fail (datetime != NULL, NULL);
@@ -1199,25 +1180,23 @@ g_date_time_add_weeks (const GDateTime *datetime,
  * @days: the number of days
  *
  * Creates a copy of @datetime and adds the specified number of days to the
- * copy.
+ * copy. Add negative values to subtract days.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add_days (const GDateTime *datetime,
-                      gint             days)
+g_date_time_add_days (GDateTime *datetime,
+                      gint       days)
 {
-  GDateTime *dt;
-
   g_return_val_if_fail (datetime != NULL, NULL);
 
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_ymd (dt, 0, 0, days);
+  if (days < -3660000 || days > 3660000)
+    return NULL;
 
-  return dt;
+  return g_date_time_replace_days (datetime, datetime->days + days);
 }
 
 /**
@@ -1225,103 +1204,60 @@ g_date_time_add_days (const GDateTime *datetime,
  * @datetime: a #GDateTime
  * @hours: the number of hours to add
  *
- * Creates a copy of @datetime and adds the specified number of hours
+ * Creates a copy of @datetime and adds the specified number of hours.
+ * Add negative values to subtract hours.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add_hours (const GDateTime *datetime,
-                       gint             hours)
+g_date_time_add_hours (GDateTime *datetime,
+                       gint       hours)
 {
-  GDateTime *dt;
-
-  g_return_val_if_fail (datetime != NULL, NULL);
-
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_usec (dt, (gint64) hours * USEC_PER_HOUR);
-
-  return dt;
+  return g_date_time_add (datetime, hours * USEC_PER_HOUR);
 }
 
 /**
- * g_date_time_add_seconds:
+ * g_date_time_add_minutes:
  * @datetime: a #GDateTime
- * @seconds: the number of seconds to add
+ * @minutes: the number of minutes to add
  *
- * Creates a copy of @datetime and adds the specified number of seconds.
+ * Creates a copy of @datetime adding the specified number of minutes.
+ * Add negative values to subtract minutes.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add_seconds (const GDateTime *datetime,
-                         gint             seconds)
+g_date_time_add_minutes (GDateTime *datetime,
+                         gint             minutes)
 {
-  GDateTime *dt;
-
-  g_return_val_if_fail (datetime != NULL, NULL);
-
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_usec (dt, (gint64) seconds * USEC_PER_SECOND);
-
-  return dt;
+  return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
 }
 
-/**
- * g_date_time_add_milliseconds:
- * @datetime: a #GDateTime
- * @milliseconds: the number of milliseconds to add
- *
- * Creates a copy of @datetime adding the specified number of milliseconds.
- *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
- *
- * Since: 2.26
- */
-GDateTime*
-g_date_time_add_milliseconds (const GDateTime *datetime,
-                              gint             milliseconds)
-{
-  GDateTime *dt;
-
-  g_return_val_if_fail (datetime != NULL, NULL);
-
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_usec (dt, (gint64) milliseconds * USEC_PER_MILLISECOND);
-
-  return dt;
-}
 
 /**
- * g_date_time_add_minutes:
+ * g_date_time_add_seconds:
  * @datetime: a #GDateTime
- * @minutes: the number of minutes to add
+ * @seconds: the number of seconds to add
  *
- * Creates a copy of @datetime adding the specified number of minutes.
+ * Creates a copy of @datetime and adds the specified number of seconds.
+ * Add negative values to subtract seconds.
  *
- * Return value: the newly created #GDateTime which should be freed with
+ * Returns: the newly created #GDateTime which should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime*
-g_date_time_add_minutes (const GDateTime *datetime,
-                         gint             minutes)
+g_date_time_add_seconds (GDateTime *datetime,
+                         gdouble    seconds)
 {
-  GDateTime *dt;
-
-  g_return_val_if_fail (datetime != NULL, NULL);
-
-  dt = g_date_time_copy (datetime);
-  g_date_time_add_usec (dt, (gint64) minutes * USEC_PER_MINUTE);
-
-  return dt;
+  return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
 }
 
 /**
@@ -1335,274 +1271,206 @@ g_date_time_add_minutes (const GDateTime *datetime,
  * @seconds: the number of seconds to add
  *
  * Creates a new #GDateTime adding the specified values to the current date and
- * time in @datetime.
+ * time in @datetime. Add negative values to subtract.
  *
- * Return value: the newly created #GDateTime that should be freed with
+ * Returns: the newly created #GDateTime that should be freed with
  *   g_date_time_unref().
  *
  * Since: 2.26
  */
 GDateTime *
-g_date_time_add_full (const GDateTime *datetime,
-                      gint             years,
-                      gint             months,
-                      gint             days,
-                      gint             hours,
-                      gint             minutes,
-                      gint             seconds)
-{
-  GDateTime *dt;
-  gint64 usecs;
+g_date_time_add_full (GDateTime *datetime,
+                      gint       years,
+                      gint       months,
+                      gint       days,
+                      gint       hours,
+                      gint       minutes,
+                      gdouble    seconds)
+{
+  gint year, month, day;
+  gint64 full_time;
+  GDateTime *new;
+  gint interval;
 
   g_return_val_if_fail (datetime != NULL, NULL);
+  g_date_time_get_ymd (datetime, &year, &month, &day);
 
-  dt = g_date_time_copy (datetime);
+  months += years * 12;
 
-  /* add date */
-  g_date_time_add_ymd (dt, years, months, days);
+  if (months < -120000 || months > 120000)
+    return NULL;
 
-  /* add time */
-  usecs = (hours   * USEC_PER_HOUR)
-        + (minutes * USEC_PER_MINUTE)
-        + (seconds * USEC_PER_SECOND);
-  g_date_time_add_usec (dt, usecs);
+  if (days < -3660000 || days > 3660000)
+    return NULL;
 
-  return dt;
-}
+  year += months / 12;
+  month += months % 12;
+  if (month < 1)
+    {
+      month += 12;
+      year--;
+    }
+  else if (month > 12)
+    {
+      month -= 12;
+      year++;
+    }
 
-/**
- * g_date_time_compare:
- * @dt1: first #GDateTime to compare
- * @dt2: second #GDateTime to compare
- *
- * qsort()-style comparison for #GDateTime<!-- -->'s. Both #GDateTime<-- -->'s
- * must be non-%NULL.
- *
- * Return value: 0 for equal, less than zero if dt1 is less than dt2, greater
- *   than zero if dt2 is greator than dt1.
- *
- * Since: 2.26
- */
-gint
-g_date_time_compare (gconstpointer dt1,
-                     gconstpointer dt2)
-{
-  const GDateTime *a, *b;
+  day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
 
-  a = dt1;
-  b = dt2;
+  /* full_time is now in unix (local) time */
+  full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
+    (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
 
-  if ((a->days == b->days )&&
-      (a->usec == b->usec))
-    {
-      return 0;
-    }
-  else if ((a->days > b->days) ||
-           ((a->days == b->days) && a->usec > b->usec))
-    {
-      return 1;
-    }
-  else
-    return -1;
-}
+  interval = g_time_zone_adjust_time (datetime->tz,
+                                      g_time_zone_is_dst (datetime->tz,
+                                                          datetime->interval),
+                                      &full_time);
 
-/**
- * g_date_time_day:
- * @datetime: a #GDateTime
- *
- * Creates a new #GDateTime at Midnight on the date represented by @datetime.
- *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
- *
- * Since: 2.26
- */
-GDateTime*
-g_date_time_day (const GDateTime *datetime)
-{
-  GDateTime *date;
+  /* move to UTC unix time */
+  full_time -= g_time_zone_get_offset (datetime->tz, interval);
 
-  g_return_val_if_fail (datetime != NULL, NULL);
+  /* convert back to an instant, add back fractional seconds */
+  full_time += UNIX_EPOCH_START * SEC_PER_DAY;
+  full_time = full_time * USEC_PER_SECOND +
+              datetime->usec % USEC_PER_SECOND;
 
-  date = g_date_time_copy (datetime);
-  date->usec = 0;
+  /* do the actual addition now */
+  full_time += (hours * USEC_PER_HOUR) +
+               (minutes * USEC_PER_MINUTE) +
+               (gint64) (seconds * USEC_PER_SECOND);
 
-  return date;
-}
+  /* find the new interval */
+  interval = g_time_zone_find_interval (datetime->tz,
+                                        G_TIME_TYPE_UNIVERSAL,
+                                        INSTANT_TO_UNIX (full_time));
 
-/**
- * g_date_time_difference:
- * @begin: a #GDateTime
- * @end: a #GDateTime
- *
- * Calculates the known difference in time between @begin and @end.
- *
- * Since the exact precision cannot always be known due to incomplete
- * historic information, an attempt is made to calculate the difference.
- *
- * Return value: the difference between the two #GDateTime, as a time
- *   span expressed in microseconds.
- *
- * Since: 2.26
- */
-GTimeSpan
-g_date_time_difference (const GDateTime *begin,
-                        const GDateTime *end)
-{
-  g_return_val_if_fail (begin != NULL, 0);
-  g_return_val_if_fail (end != NULL, 0);
+  /* convert back into local time */
+  full_time += USEC_PER_SECOND *
+               g_time_zone_get_offset (datetime->tz, interval);
 
-  return (GTimeSpan) (((gint64) end->days - (gint64) begin->days)
-      * USEC_PER_DAY) + ((gint64) end->usec - (gint64) begin->usec);
+  /* split into days and usec of a new datetime */
+  new = g_date_time_alloc (datetime->tz);
+  new->interval = interval;
+  new->days = full_time / USEC_PER_DAY;
+  new->usec = full_time % USEC_PER_DAY;
+
+  /* XXX validate */
+
+  return new;
 }
 
+/* Compare, difference, hash, equal {{{1 */
 /**
- * g_date_time_equal:
- * @dt1: a #GDateTime
- * @dt2: a #GDateTime
- *
- * Checks to see if @dt1 and @dt2 are equal.
+ * g_date_time_compare:
+ * @dt1: first #GDateTime to compare
+ * @dt2: second #GDateTime to compare
  *
- * Equal here means that they represent the same moment after converting
- * them to the same timezone.
+ * A comparison function for #GDateTimes that is suitable
+ * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
  *
- * Return value: %TRUE if @dt1 and @dt2 are equal
+ * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
+ *   than @dt2.
  *
  * Since: 2.26
  */
-gboolean
-g_date_time_equal (gconstpointer dt1,
-                   gconstpointer dt2)
+gint
+g_date_time_compare (gconstpointer dt1,
+                     gconstpointer dt2)
 {
-  const GDateTime *a, *b;
-  GDateTime *a_utc, *b_utc;
-  gint64 a_epoch, b_epoch;
-
-  a = dt1;
-  b = dt2;
+  gint64 difference;
 
-  a_utc = g_date_time_to_utc (a);
-  b_utc = g_date_time_to_utc (b);
+  difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
 
-  a_epoch = g_date_time_to_epoch (a_utc);
-  b_epoch = g_date_time_to_epoch (b_utc);
+  if (difference < 0)
+    return -1;
 
-  g_date_time_unref (a_utc);
-  g_date_time_unref (b_utc);
+  else if (difference > 0)
+    return 1;
 
-  return a_epoch == b_epoch;
+  else
+    return 0;
 }
 
 /**
- * g_date_time_get_day_of_week:
- * @datetime: a #GDateTime
+ * g_date_time_difference:
+ * @end: a #GDateTime
+ * @begin: a #GDateTime
  *
- * Retrieves the day of the week represented by @datetime within the gregorian
- * calendar. 1 is Sunday, 2 is Monday, etc.
+ * Calculates the difference in time between @end and @begin.  The
+ * #GTimeSpan that is returned is effectively @end - @begin (ie:
+ * positive if the first parameter is larger).
  *
- * Return value: the day of the week
+ * Returns: the difference between the two #GDateTime, as a time
+ *   span expressed in microseconds.
  *
  * Since: 2.26
  */
-gint
-g_date_time_get_day_of_week (const GDateTime *datetime)
+GTimeSpan
+g_date_time_difference (GDateTime *end,
+                        GDateTime *begin)
 {
-  gint a, y, m,
-       year  = 0,
-       month = 0,
-       day   = 0,
-       dow;
-
-  g_return_val_if_fail (datetime != NULL, 0);
-
-  /*
-   * See Calendar FAQ Section 2.6 for algorithm information
-   * http://www.tondering.dk/claus/cal/calendar29.txt
-   */
-
-  g_date_time_get_dmy (datetime, &day, &month, &year);
-  a = (14 - month) / 12;
-  y = year - a;
-  m = month + (12 * a) - 2;
-  dow = ((day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m) / 12) % 7);
+  g_return_val_if_fail (begin != NULL, 0);
+  g_return_val_if_fail (end != NULL, 0);
 
-  /* 1 is Monday and 7 is Sunday */
-  return (dow == 0) ? 7 : dow;
+  return g_date_time_to_instant (end) -
+         g_date_time_to_instant (begin);
 }
 
 /**
- * g_date_time_get_day_of_month:
+ * g_date_time_hash:
  * @datetime: a #GDateTime
  *
- * Retrieves the day of the month represented by @datetime in the gregorian
- * calendar.
+ * Hashes @datetime into a #guint, suitable for use within #GHashTable.
  *
- * Return value: the day of the month
+ * Returns: a #guint containing the hash
  *
  * Since: 2.26
  */
-gint
-g_date_time_get_day_of_month (const GDateTime *datetime)
+guint
+g_date_time_hash (gconstpointer datetime)
 {
-  gint           day_of_year,
-                 i;
-  const guint16 *days;
-  guint16        last = 0;
-
-  g_return_val_if_fail (datetime != NULL, 0);
-
-  days = days_in_year[g_date_time_is_leap_year (datetime) ? 1 : 0];
-  g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
-
-  for (i = 1; i <= 12; i++)
-    {
-      if (days [i] >= day_of_year)
-        return day_of_year - last;
-      last = days [i];
-    }
-
-  g_warn_if_reached ();
-  return 0;
+  return g_date_time_to_instant ((GDateTime *) datetime);
 }
 
 /**
- * g_date_time_get_day_of_year:
- * @datetime: a #GDateTime
+ * g_date_time_equal:
+ * @dt1: a #GDateTime
+ * @dt2: a #GDateTime
  *
- * Retrieves the day of the year represented by @datetime in the Gregorian
- * calendar.
+ * Checks to see if @dt1 and @dt2 are equal.
+ *
+ * Equal here means that they represent the same moment after converting
+ * them to the same time zone.
  *
- * Return value: the day of the year
+ * Returns: %TRUE if @dt1 and @dt2 are equal
  *
  * Since: 2.26
  */
-gint
-g_date_time_get_day_of_year (const GDateTime *datetime)
+gboolean
+g_date_time_equal (gconstpointer dt1,
+                   gconstpointer dt2)
 {
-  gint doy = 0;
-
-  g_return_val_if_fail (datetime != NULL, 0);
-
-  g_date_time_get_week_number (datetime, NULL, NULL, &doy);
-  return doy;
+  return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
 }
 
+/* Year, Month, Day Getters {{{1 */
 /**
- * g_date_time_get_dmy:
+ * g_date_time_get_ymd:
  * @datetime: a #GDateTime.
- * @day: (out): the return location for the day of the month, or %NULL.
- * @month: (out): the return location for the monty of the year, or %NULL.
- * @year: (out): the return location for the gregorian year, or %NULL.
+ * @year: (out) (allow-none): the return location for the gregorian year, or %NULL.
+ * @month: (out) (allow-none): the return location for the month of the year, or %NULL.
+ * @day: (out) (allow-none): the return location for the day of the month, or %NULL.
  *
  * Retrieves the Gregorian day, month, and year of a given #GDateTime.
  *
  * Since: 2.26
- */
+ **/
 void
-g_date_time_get_dmy (const GDateTime *datetime,
-                     gint            *day,
-                     gint            *month,
-                     gint            *year)
+g_date_time_get_ymd (GDateTime *datetime,
+                     gint      *year,
+                     gint      *month,
+                     gint      *day)
 {
   gint the_year;
   gint the_month;
@@ -1684,919 +1552,1102 @@ end:
 }
 
 /**
- * g_date_time_get_hour:
- * @datetime: a #GDateTime
+ * g_date_time_get_year:
+ * @datetime: A #GDateTime
  *
- * Retrieves the hour of the day represented by @datetime
+ * Retrieves the year represented by @datetime in the Gregorian calendar.
  *
- * Return value: the hour of the day
+ * Returns: the year represented by @datetime
  *
  * Since: 2.26
  */
 gint
-g_date_time_get_hour (const GDateTime *datetime)
+g_date_time_get_year (GDateTime *datetime)
 {
+  gint year;
+
   g_return_val_if_fail (datetime != NULL, 0);
 
-  return (datetime->usec / USEC_PER_HOUR);
+  g_date_time_get_ymd (datetime, &year, NULL, NULL);
+
+  return year;
 }
 
 /**
- * g_date_time_get_julian:
+ * g_date_time_get_month:
  * @datetime: a #GDateTime
- * @period: (out): a location for the Julian period
- * @julian: (out): a location for the day in the Julian period
- * @hour: (out): a location for the hour of the day
- * @minute: (out): a location for the minute of the hour
- * @second: (out): a location for hte second of the minute
  *
- * Retrieves the Julian period, day, hour, minute, and second which @datetime
- * represents in the Julian calendar.
+ * Retrieves the month of the year represented by @datetime in the Gregorian
+ * calendar.
+ *
+ * Returns: the month represented by @datetime
  *
  * Since: 2.26
  */
-void
-g_date_time_get_julian (const GDateTime *datetime,
-                        gint            *period,
-                        gint            *julian,
-                        gint            *hour,
-                        gint            *minute,
-                        gint            *second)
-{
-  gint y, m, d, a, b, c, e, f, j;
-  g_return_if_fail (datetime != NULL);
+gint
+g_date_time_get_month (GDateTime *datetime)
+{
+  gint month;
 
-  g_date_time_get_dmy (datetime, &d, &m, &y);
+  g_return_val_if_fail (datetime != NULL, 0);
 
-  /* FIXME: This is probably not optimal and doesn't handle the fact that the
-   * julian calendar has its 0 hour on midday */
+  g_date_time_get_ymd (datetime, NULL, &month, NULL);
 
-  a = y / 100;
-  b = a / 4;
-  c = 2 - a + b;
-  e = 365.25 * (y + 4716);
-  f = 30.6001 * (m + 1);
-  j = c + d + e + f - 1524;
+  return month;
+}
 
-  if (period)
-    *period = 0;
+/**
+ * g_date_time_get_day_of_month:
+ * @datetime: a #GDateTime
+ *
+ * Retrieves the day of the month represented by @datetime in the gregorian
+ * calendar.
+ *
+ * Returns: the day of the month
+ *
+ * Since: 2.26
+ */
+gint
+g_date_time_get_day_of_month (GDateTime *datetime)
+{
+  gint           day_of_year,
+                 i;
+  const guint16 *days;
+  guint16        last = 0;
 
-  if (julian)
-    *julian = j;
+  g_return_val_if_fail (datetime != NULL, 0);
 
-  if (hour)
-    *hour = (datetime->usec / USEC_PER_HOUR);
+  days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
+  g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
 
-  if (minute)
-    *minute = (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
+  for (i = 1; i <= 12; i++)
+    {
+      if (days [i] >= day_of_year)
+        return day_of_year - last;
+      last = days [i];
+    }
 
-  if (second)
-    *second = (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
+  g_warn_if_reached ();
+  return 0;
 }
 
+/* Week of year / day of week getters {{{1 */
 /**
- * g_date_time_get_microsecond:
+ * g_date_time_get_week_numbering_year:
  * @datetime: a #GDateTime
  *
- * Retrieves the microsecond of the date represented by @datetime
+ * Returns the ISO 8601 week-numbering year in which the week containing
+ * @datetime falls.
+ *
+ * This function, taken together with g_date_time_get_week_of_year() and
+ * g_date_time_get_day_of_week() can be used to determine the full ISO
+ * week date on which @datetime falls.
+ *
+ * This is usually equal to the normal Gregorian year (as returned by
+ * g_date_time_get_year()), except as detailed below:
+ *
+ * For Thursday, the week-numbering year is always equal to the usual
+ * calendar year.  For other days, the number is such that every day
+ * within a complete week (Monday to Sunday) is contained within the
+ * same week-numbering year.
+ *
+ * For Monday, Tuesday and Wednesday occurring near the end of the year,
+ * this may mean that the week-numbering year is one greater than the
+ * calendar year (so that these days have the same week-numbering year
+ * as the Thursday occurring early in the next year).
  *
- * Return value: the microsecond of the second
+ * For Friday, Saturaday and Sunday occurring near the start of the year,
+ * this may mean that the week-numbering year is one less than the
+ * calendar year (so that these days have the same week-numbering year
+ * as the Thursday occurring late in the previous year).
+ *
+ * An equivalent description is that the week-numbering year is equal to
+ * the calendar year containing the majority of the days in the current
+ * week (Monday to Sunday).
+ *
+ * Note that January 1 0001 in the proleptic Gregorian calendar is a
+ * Monday, so this function never returns 0.
+ *
+ * Returns: the ISO 8601 week-numbering year for @datetime
  *
  * Since: 2.26
- */
+ **/
 gint
-g_date_time_get_microsecond (const GDateTime *datetime)
+g_date_time_get_week_numbering_year (GDateTime *datetime)
 {
-  g_return_val_if_fail (datetime != NULL, 0);
+  gint year, month, day, weekday;
 
-  return (datetime->usec % USEC_PER_SECOND);
+  g_date_time_get_ymd (datetime, &year, &month, &day);
+  weekday = g_date_time_get_day_of_week (datetime);
+
+  /* January 1, 2, 3 might be in the previous year if they occur after
+   * Thursday.
+   *
+   *   Jan 1:  Friday, Saturday, Sunday    =>  day 1:  weekday 5, 6, 7
+   *   Jan 2:  Saturday, Sunday            =>  day 2:  weekday 6, 7
+   *   Jan 3:  Sunday                      =>  day 3:  weekday 7
+   *
+   * So we have a special case if (day - weekday) <= -4
+   */
+  if (month == 1 && (day - weekday) <= -4)
+    return year - 1;
+
+  /* December 29, 30, 31 might be in the next year if they occur before
+   * Thursday.
+   *
+   *   Dec 31: Monday, Tuesday, Wednesday  =>  day 31: weekday 1, 2, 3
+   *   Dec 30: Monday, Tuesday             =>  day 30: weekday 1, 2
+   *   Dec 29: Monday                      =>  day 29: weekday 1
+   *
+   * So we have a special case if (day - weekday) >= 28
+   */
+  else if (month == 12 && (day - weekday) >= 28)
+    return year + 1;
+
+  else
+    return year;
 }
 
 /**
- * g_date_time_get_millisecond:
+ * g_date_time_get_week_of_year:
  * @datetime: a #GDateTime
  *
- * Retrieves the millisecond of the date represented by @datetime
+ * Returns the ISO 8601 week number for the week containing @datetime.
+ * The ISO 8601 week number is the same for every day of the week (from
+ * Moday through Sunday).  That can produce some unusual results
+ * (described below).
  *
- * Return value: the millisecond of the second
+ * The first week of the year is week 1.  This is the week that contains
+ * the first Thursday of the year.  Equivalently, this is the first week
+ * that has more than 4 of its days falling within the calendar year.
+ *
+ * The value 0 is never returned by this function.  Days contained
+ * within a year but occurring before the first ISO 8601 week of that
+ * year are considered as being contained in the last week of the
+ * previous year.  Similarly, the final days of a calendar year may be
+ * considered as being part of the first ISO 8601 week of the next year
+ * if 4 or more days of that week are contained within the new year.
+ *
+ * Returns: the ISO 8601 week number for @datetime.
  *
  * Since: 2.26
  */
 gint
-g_date_time_get_millisecond (const GDateTime *datetime)
+g_date_time_get_week_of_year (GDateTime *datetime)
 {
+  gint weeknum;
+
   g_return_val_if_fail (datetime != NULL, 0);
 
-  return (datetime->usec % USEC_PER_SECOND) / USEC_PER_MILLISECOND;
+  g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
+
+  return weeknum;
 }
 
 /**
- * g_date_time_get_minute:
+ * g_date_time_get_day_of_week:
  * @datetime: a #GDateTime
  *
- * Retrieves the minute of the hour represented by @datetime
+ * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
+ * Monday, 2 is Tuesday... 7 is Sunday).
  *
- * Return value: the minute of the hour
+ * Returns: the day of the week
  *
  * Since: 2.26
  */
 gint
-g_date_time_get_minute (const GDateTime *datetime)
+g_date_time_get_day_of_week (GDateTime *datetime)
 {
   g_return_val_if_fail (datetime != NULL, 0);
 
-  return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
+  return (datetime->days - 1) % 7 + 1;
 }
 
+/* Day of year getter {{{1 */
 /**
- * g_date_time_get_month:
+ * g_date_time_get_day_of_year:
  * @datetime: a #GDateTime
  *
- * Retrieves the month of the year represented by @datetime in the Gregorian
+ * Retrieves the day of the year represented by @datetime in the Gregorian
  * calendar.
  *
- * Return value: the month represented by @datetime
+ * Returns: the day of the year
  *
  * Since: 2.26
  */
 gint
-g_date_time_get_month (const GDateTime *datetime)
+g_date_time_get_day_of_year (GDateTime *datetime)
 {
-  gint month;
+  gint doy = 0;
 
   g_return_val_if_fail (datetime != NULL, 0);
 
-  g_date_time_get_dmy (datetime, NULL, &month, NULL);
-
-  return month;
+  g_date_time_get_week_number (datetime, NULL, NULL, &doy);
+  return doy;
 }
 
+/* Time component getters {{{1 */
+
 /**
- * g_date_time_get_second:
+ * g_date_time_get_hour:
  * @datetime: a #GDateTime
  *
- * Retrieves the second of the minute represented by @datetime
+ * Retrieves the hour of the day represented by @datetime
  *
- * Return value: the second represented by @datetime
+ * Returns: the hour of the day
  *
  * Since: 2.26
  */
 gint
-g_date_time_get_second (const GDateTime *datetime)
+g_date_time_get_hour (GDateTime *datetime)
 {
   g_return_val_if_fail (datetime != NULL, 0);
 
-  return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
+  return (datetime->usec / USEC_PER_HOUR);
 }
 
 /**
- * g_date_time_get_utc_offset:
+ * g_date_time_get_minute:
  * @datetime: a #GDateTime
  *
- * Retrieves the offset from UTC that the local timezone specified by
- * @datetime represents.
- *
- * If @datetime represents UTC time, then the offset is zero.
+ * Retrieves the minute of the hour represented by @datetime
  *
- * Return value: the offset, expressed as a time span expressed in
- *   microseconds.
+ * Returns: the minute of the hour
  *
  * Since: 2.26
  */
-GTimeSpan
-g_date_time_get_utc_offset (const GDateTime *datetime)
+gint
+g_date_time_get_minute (GDateTime *datetime)
 {
-  gint offset = 0;
-
   g_return_val_if_fail (datetime != NULL, 0);
 
-  if (datetime->tz != NULL)
-    offset = g_time_zone_get_offset (datetime->tz);
-
-  return (gint64) offset * USEC_PER_SECOND;
+  return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
 }
 
 /**
- * g_date_time_get_timezone_name:
+ * g_date_time_get_second:
  * @datetime: a #GDateTime
  *
- * Retrieves the name of the timezone specified by @datetime, if any.
+ * Retrieves the second of the minute represented by @datetime
  *
- * Return value: (transfer none): the name of the timezone. The returned
- *   string is owned by the #GDateTime and it should not be modified or
- *   freed
+ * Returns: the second represented by @datetime
  *
  * Since: 2.26
  */
-G_CONST_RETURN gchar *
-g_date_time_get_timezone_name (const GDateTime *datetime)
+gint
+g_date_time_get_second (GDateTime *datetime)
 {
-  g_return_val_if_fail (datetime != NULL, NULL);
-
-  if (datetime->tz != NULL)
-    return g_time_zone_get_name (datetime->tz);
+  g_return_val_if_fail (datetime != NULL, 0);
 
-  return "UTC";
+  return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
 }
 
 /**
- * g_date_time_get_year:
- * @datetime: A #GDateTime
+ * g_date_time_get_microsecond:
+ * @datetime: a #GDateTime
  *
- * Retrieves the year represented by @datetime in the Gregorian calendar.
+ * Retrieves the microsecond of the date represented by @datetime
  *
- * Return value: the year represented by @datetime
+ * Returns: the microsecond of the second
  *
  * Since: 2.26
  */
 gint
-g_date_time_get_year (const GDateTime *datetime)
+g_date_time_get_microsecond (GDateTime *datetime)
 {
-  gint year;
-
   g_return_val_if_fail (datetime != NULL, 0);
 
-  g_date_time_get_dmy (datetime, NULL, NULL, &year);
-
-  return year;
+  return (datetime->usec % USEC_PER_SECOND);
 }
 
 /**
- * g_date_time_hash:
+ * g_date_time_get_seconds:
  * @datetime: a #GDateTime
  *
- * Hashes @datetime into a #guint, suitable for use within #GHashTable.
+ * Retrieves the number of seconds since the start of the last minute,
+ * including the fractional part.
  *
- * Return value: a #guint containing the hash
+ * Returns: the number of seconds
  *
  * Since: 2.26
- */
-guint
-g_date_time_hash (gconstpointer datetime)
+ **/
+gdouble
+g_date_time_get_seconds (GDateTime *datetime)
 {
-  return (guint) (*((guint64 *) datetime));
+  g_return_val_if_fail (datetime != NULL, 0);
+
+  return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
 }
 
+/* Exporters {{{1 */
 /**
- * g_date_time_is_leap_year:
+ * g_date_time_to_unix:
  * @datetime: a #GDateTime
  *
- * Determines if @datetime represents a date known to fall within
- * a leap year in the Gregorian calendar.
+ * Gives the Unix time corresponding to @datetime, rounding down to the
+ * nearest second.
  *
- * Return value: %TRUE if @datetime is a leap year.
+ * Unix time is the number of seconds that have elapsed since 1970-01-01
+ * 00:00:00 UTC, regardless of the time zone associated with @datetime.
+ *
+ * Returns: the Unix time corresponding to @datetime
  *
  * Since: 2.26
- */
-gboolean
-g_date_time_is_leap_year (const GDateTime *datetime)
+ **/
+gint64
+g_date_time_to_unix (GDateTime *datetime)
 {
-  gint year;
-
-  g_return_val_if_fail (datetime != NULL, FALSE);
-
-  year = g_date_time_get_year (datetime);
-
-  return GREGORIAN_LEAP (year);
+  return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
 }
 
 /**
- * g_date_time_is_daylight_savings:
+ * g_date_time_to_timeval:
  * @datetime: a #GDateTime
+ * @tv: a #GTimeVal to modify
+ *
+ * Stores the instant in time that @datetime represents into @tv.
+ *
+ * The time contained in a #GTimeVal is always stored in the form of
+ * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
+ * zone associated with @datetime.
  *
- * Determines if @datetime represents a date known to fall within daylight
- * savings time in the gregorian calendar.
+ * On systems where 'long' is 32bit (ie: all 32bit systems and all
+ * Windows systems), a #GTimeVal is incapable of storing the entire
+ * range of values that #GDateTime is capable of expressing.  On those
+ * systems, this function returns %FALSE to indicate that the time is
+ * out of range.
  *
- * Return value: %TRUE if @datetime falls within daylight savings time.
+ * On systems where 'long' is 64bit, this function never fails.
+ *
+ * Returns: %TRUE if successful, else %FALSE
  *
  * Since: 2.26
- */
+ **/
 gboolean
-g_date_time_is_daylight_savings (const GDateTime *datetime)
+g_date_time_to_timeval (GDateTime *datetime,
+                        GTimeVal  *tv)
 {
-  g_return_val_if_fail (datetime != NULL, FALSE);
-
-  if (datetime->tz == NULL)
-    return FALSE;
+  tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
+  tv->tv_usec = datetime->usec % USEC_PER_SECOND;
 
-  return datetime->tz->is_dst;
+  return TRUE;
 }
 
+/* Timezone queries {{{1 */
 /**
- * g_date_time_new_from_date:
- * @year: the Gregorian year
- * @month: the Gregorian month
- * @day: the day in the Gregorian month
+ * g_date_time_get_utc_offset:
+ * @datetime: a #GDateTime
  *
- * Creates a new #GDateTime using the specified date within the Gregorian
- * calendar.
+ * Determines the offset to UTC in effect at the time and in the time
+ * zone of @datetime.
+ *
+ * The offset is the number of microseconds that you add to UTC time to
+ * arrive at local time for the time zone (ie: negative numbers for time
+ * zones west of GMT, positive numbers for east).
+ *
+ * If @datetime represents UTC time, then the offset is always zero.
  *
- * Return value: the newly created #GDateTime or %NULL if it is outside of
- *   the representable range.
+ * Returns: the number of microseconds that should be added to UTC to
+ *          get the local time
  *
  * Since: 2.26
- */
-GDateTime *
-g_date_time_new_from_date (gint year,
-                           gint month,
-                           gint day)
+ **/
+GTimeSpan
+g_date_time_get_utc_offset (GDateTime *datetime)
 {
-  GDateTime *dt;
+  gint offset;
 
-  g_return_val_if_fail (year > -4712 && year <= 3268, NULL);
-  g_return_val_if_fail (month > 0 && month <= 12, NULL);
-  g_return_val_if_fail (day > 0 && day <= 31, NULL);
-
-  dt = g_date_time_new ();
+  g_return_val_if_fail (datetime != NULL, 0);
 
-  dt->days = date_to_proleptic_gregorian (year, month, day);
-  dt->tz = g_time_zone_new_local ();
-  g_time_zone_sink (dt->tz, dt);
+  offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
 
-  return dt;
+  return (gint64) offset * USEC_PER_SECOND;
 }
 
 /**
- * g_date_time_new_from_epoch:
- * @t: seconds from the Unix epoch
+ * g_date_time_get_timezone_abbreviation:
+ * @datetime: a #GDateTime
  *
- * Creates a new #GDateTime using the time since Jan 1, 1970 specified by @t.
+ * Determines the time zone abbreviation to be used at the time and in
+ * the time zone of @datetime.
  *
- * The timezone of the returned #GDateTime is the local time.
+ * For example, in Toronto this is currently "EST" during the winter
+ * months and "EDT" during the summer months when daylight savings
+ * time is in effect.
  *
- * Return value: the newly created #GDateTime
+ * Returns: (transfer none): the time zone abbreviation. The returned
+ *          string is owned by the #GDateTime and it should not be
+ *          modified or freed
  *
  * Since: 2.26
- */
-GDateTime*
-g_date_time_new_from_epoch (gint64 t)
+ **/
+const gchar *
+g_date_time_get_timezone_abbreviation (GDateTime *datetime)
 {
-  GDateTime *datetime;
-  GTimeZone *tz;
-  struct tm tm;
-  time_t tt;
-
-  tt = (time_t) t;
-  memset (&tm, 0, sizeof (tm));
-
-  /* XXX: GLib should probably have a wrapper for this */
-#ifdef HAVE_LOCALTIME_R
-  localtime_r (&tt, &tm);
-#else
-  {
-    struct tm *ptm = localtime (&tt);
-
-    if (ptm == NULL)
-      {
-        /* Happens at least in Microsoft's C library if you pass a
-         * negative time_t. Use 2000-01-01 as default date.
-         */
-#ifndef G_DISABLE_CHECKS
-        g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
-#endif
-
-        tm.tm_mon = 0;
-        tm.tm_mday = 1;
-        tm.tm_year = 100;
-      }
-    else
-      memcpy ((void *) &tm, (void *) ptm, sizeof (struct tm));
-  }
-#endif /* HAVE_LOCALTIME_R */
-
-  tz = g_time_zone_new_local ();
-  datetime = g_date_time_new_full (tm.tm_year + 1900,
-                                   tm.tm_mon  + 1,
-                                   tm.tm_mday,
-                                   tm.tm_hour,
-                                   tm.tm_min,
-                                   tm.tm_sec,
-                                   tz);
-  g_time_zone_free (tz);
+  g_return_val_if_fail (datetime != NULL, NULL);
 
-  return datetime;
+  return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
 }
 
 /**
- * g_date_time_new_from_timeval:
- * @tv: #GTimeVal
- *
- * Creates a new #GDateTime using the date and time specified by #GTimeVal.
+ * g_date_time_is_daylight_savings:
+ * @datetime: a #GDateTime
  *
- * The timezone of the returned #GDateTime is UTC.
+ * Determines if daylight savings time is in effect at the time and in
+ * the time zone of @datetime.
  *
- * Return value: the newly created #GDateTime
+ * Returns: %TRUE if daylight savings time is in effect
  *
  * Since: 2.26
- */
-GDateTime *
-g_date_time_new_from_timeval (GTimeVal *tv)
+ **/
+gboolean
+g_date_time_is_daylight_savings (GDateTime *datetime)
 {
-  GDateTime *datetime;
-
-  g_return_val_if_fail (tv != NULL, NULL);
-
-  datetime = g_date_time_new_from_epoch (tv->tv_sec);
-  datetime->usec += tv->tv_usec;
-  datetime->tz = g_time_zone_new_local ();
-  g_time_zone_sink (datetime->tz, datetime);
+  g_return_val_if_fail (datetime != NULL, FALSE);
 
-  return datetime;
+  return g_time_zone_is_dst (datetime->tz, datetime->interval);
 }
 
+/* Timezone convert {{{1 */
 /**
- * g_date_time_new_full:
- * @year: the Gregorian year
- * @month: the Gregorian month
- * @day: the day of the Gregorian month
- * @hour: the hour of the day
- * @minute: the minute of the hour
- * @second: the second of the minute, with eventual fractionary parts
- * @time_zone: (allow-none): a #GTimeZone, or %NULL for UTC
- *
- * Creates a new #GDateTime using the date and times in the Gregorian
- * calendar.
+ * g_date_time_to_timezone:
+ * @datetime: a #GDateTime
+ * @tz: the new #GTimeZone
+ *
+ * Create a new #GDateTime corresponding to the same instant in time as
+ * @datetime, but in the time zone @tz.
  *
- * If @time_zone is not %NULL, the #GDateTime will copy the #GTimeZone
- * and sink it, if floating.
+ * This call can fail in the case that the time goes out of bounds.  For
+ * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
+ * Greenwich will fail (due to the year 0 being out of range).
  *
- * Return value: the newly created #GDateTime
+ * You should release the return value by calling g_date_time_unref()
+ * when you are done with it.
+ *
+ * Returns: a new #GDateTime, or %NULL
  *
  * Since: 2.26
- */
+ **/
 GDateTime *
-g_date_time_new_full (gint             year,
-                      gint             month,
-                      gint             day,
-                      gint             hour,
-                      gint             minute,
-                      gdouble          second,
-                      const GTimeZone *time_zone)
-{
-  GDateTime *dt;
-
-  g_return_val_if_fail (hour >= 0 && hour < 24, NULL);
-  g_return_val_if_fail (minute >= 0 && minute < 60, NULL);
-  g_return_val_if_fail (second >= 0.0 && second <= 60.0, NULL);
-
-  dt = g_date_time_new ();
-  dt->days = date_to_proleptic_gregorian (year, month, day);
-  dt->usec = (hour   * USEC_PER_HOUR)
-           + (minute * USEC_PER_MINUTE)
-           + (second * USEC_PER_SECOND);
-
-  if (time_zone != NULL)
-    {
-      dt->tz = g_time_zone_copy (time_zone);
-      g_time_zone_sink (dt->tz, dt);
-    }
-  else
-    dt->tz = NULL;
-
-  return dt;
+g_date_time_to_timezone (GDateTime *datetime,
+                         GTimeZone *tz)
+{
+  return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
 }
 
 /**
- * g_date_time_new_now:
+ * g_date_time_to_local:
+ * @datetime: a #GDateTime
  *
- * Creates a new #GDateTime representing the current date and time.
+ * Creates a new #GDateTime corresponding to the same instant in time as
+ * @datetime, but in the local time zone.
  *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
+ * This call is equivalent to calling g_date_time_to_timezone() with the
+ * time zone returned by g_time_zone_new_local().
+ *
+ * Returns: the newly created #GDateTime
  *
  * Since: 2.26
- */
-GDateTime*
-g_date_time_new_now (void)
+ **/
+GDateTime *
+g_date_time_to_local (GDateTime *datetime)
 {
-  GTimeVal tv;
+  GDateTime *new;
+  GTimeZone *local;
 
-  g_get_current_time (&tv);
+  local = g_time_zone_new_local ();
+  new = g_date_time_to_timezone (datetime, local);
+  g_time_zone_unref (local);
 
-  return g_date_time_new_from_timeval (&tv);
+  return new;
 }
 
 /**
- * g_date_time_printf:
- * @datetime: A #GDateTime
- * @format: a valid UTF-8 string, containing the format for the #GDateTime
+ * g_date_time_to_utc:
+ * @datetime: a #GDateTime
  *
- * Creates a newly allocated string representing the requested @format.
+ * Creates a new #GDateTime corresponding to the same instant in time as
+ * @datetime, but in UTC.
  *
- * The following format specifiers are supported:
+ * This call is equivalent to calling g_date_time_to_timezone() with the
+ * time zone returned by g_time_zone_new_utc().
  *
- * %%a  The abbreviated weekday name according to the current locale.
- * %%A  The full weekday name according to the current locale.
- * %%b  The abbreviated month name according to the current locale.
- * %%B  The full month name according to the current locale.
- * %%d  The day of the month as a decimal number (range 01 to 31).
- * %%e  The day of the month as a decimal number (range  1 to 31).
- * %%F  Equivalent to %Y-%m-%d (the ISO 8601 date format).
- * %%h  Equivalent to %b.
- * %%H  The hour as a decimal number using a 24-hour clock (range 00 to 23).
- * %%I  The hour as a decimal number using a 12-hour clock (range 01 to 12).
- * %%j  The day of the year as a decimal number (range 001 to 366).
- * %%k  The hour (24-hour clock) as a decimal number (range 0 to 23);
- *      single digits are preceded by a blank.
- * %%l  The hour (12-hour clock) as a decimal number (range 1 to 12);
- *      single digits are preceded by a blank.
- * %%m  The month as a decimal number (range 01 to 12).
- * %%M  The minute as a decimal number (range 00 to 59).
- * %%N  The micro-seconds as a decimal number.
- * %%p  Either "AM" or "PM" according to the given time  value, or the
- *      corresponding  strings  for the current locale.  Noon is treated
- *      as "PM" and midnight as "AM".
- * %%P  Like %%p but lowercase: "am" or "pm" or a corresponding string for
- *      the current locale.
- * %%r  The time in a.m. or p.m. notation.
- * %%R  The time in 24-hour notation (%H:%M).
- * %%s  The number of seconds since the Epoch, that is, since 1970-01-01
- *      00:00:00 UTC.
- * %%S  The second as a decimal number (range 00 to 60).
- * %%t  A tab character.
- * %%u  The day of the week as a decimal, range 1 to 7, Monday being 1.
- * %%W  The week number of the current year as a decimal number.
- * %%x  The preferred date representation for the current locale without
- *      the time.
- * %%X  The preferred date representation for the current locale without
- *      the date.
- * %%y  The year as a decimal number without the century.
- * %%Y  The year as a decimal number including the century.
- * %%z  The time-zone as hour offset from UTC
- * %%Z  The timezone or name or abbreviation
- * %%%  A literal %% character.
- *
- * Return value: a newly allocated string formatted to the requested format or
- *   %NULL in the case that there was an error.  The string should be freed
- *   with g_free().
+ * Returns: the newly created #GDateTime
  *
  * Since: 2.26
- */
-gchar *
-g_date_time_printf (const GDateTime *datetime,
-                    const gchar     *format)
+ **/
+GDateTime *
+g_date_time_to_utc (GDateTime *datetime)
 {
-  GString  *outstr;
-  gchar    *tmp;
-  gunichar  c;
-  glong     utf8len;
-  gboolean  in_mod;
+  GDateTime *new;
+  GTimeZone *utc;
 
-  g_return_val_if_fail (datetime != NULL, NULL);
-  g_return_val_if_fail (format != NULL, NULL);
-  g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
+  utc = g_time_zone_new_utc ();
+  new = g_date_time_to_timezone (datetime, utc);
+  g_time_zone_unref (utc);
 
-  outstr = g_string_sized_new (strlen (format) * 2);
-  utf8len = g_utf8_strlen (format, -1);
-  in_mod = FALSE;
+  return new;
+}
 
-  for (; *format; format = g_utf8_next_char(format))
-    {
-      c = g_utf8_get_char (format);
+/* Format {{{1 */
 
-      switch (c)
-        {
-        case '%':
-          if (!in_mod)
-            {
-              in_mod = TRUE;
-              break;
-            }
-            /* Fall through */
-        default:
-          if (in_mod)
-            {
-              switch (c)
-                {
-                case 'a':
-                  g_string_append (outstr, WEEKDAY_ABBR (datetime));
-                  break;
-                case 'A':
-                  g_string_append (outstr, WEEKDAY_FULL (datetime));
-                  break;
-                case 'b':
-                  g_string_append (outstr, MONTH_ABBR (datetime));
-                  break;
-                case 'B':
-                  g_string_append (outstr, MONTH_FULL (datetime));
-                  break;
-                case 'd':
-                  g_string_append_printf (outstr, "%02d", g_date_time_get_day_of_month (datetime));
-                  break;
-                case 'e':
-                  g_string_append_printf (outstr, "%2d", g_date_time_get_day_of_month (datetime));
-                  break;
-                case 'F':
-                  g_string_append_printf (outstr, "%d-%02d-%02d",
-                                          g_date_time_get_year (datetime),
-                                          g_date_time_get_month (datetime),
-                                          g_date_time_get_day_of_month (datetime));
-                  break;
-                case 'h':
-                  g_string_append (outstr, MONTH_ABBR (datetime));
-                  break;
-                case 'H':
-                  g_string_append_printf (outstr, "%02d", g_date_time_get_hour (datetime));
-                  break;
-                case 'I':
-                  if (g_date_time_get_hour (datetime) == 0)
-                    g_string_append (outstr, "12");
-                  else
-                    g_string_append_printf (outstr, "%02d", g_date_time_get_hour (datetime) % 12);
-                  break;
-                case 'j':
-                  g_string_append_printf (outstr, "%03d", g_date_time_get_day_of_year (datetime));
-                  break;
-                case 'k':
-                  g_string_append_printf (outstr, "%2d", g_date_time_get_hour (datetime));
-                  break;
-                case 'l':
-                  if (g_date_time_get_hour (datetime) == 0)
-                    g_string_append (outstr, "12");
-                  else
-                    g_string_append_printf (outstr, "%2d", g_date_time_get_hour (datetime) % 12);
-                  break;
-                case 'm':
-                  g_string_append_printf (outstr, "%02d", g_date_time_get_month (datetime));
-                  break;
-                case 'M':
-                  g_string_append_printf (outstr, "%02d", g_date_time_get_minute (datetime));
-                  break;
-                case 'N':
-                  g_string_append_printf (outstr, "%"G_GUINT64_FORMAT, datetime->usec % USEC_PER_SECOND);
-                  break;
-                case 'p':
-                  g_string_append (outstr, GET_AMPM (datetime, FALSE));
-                  break;
-                case 'P':
-                  g_string_append (outstr, GET_AMPM (datetime, TRUE));
-                  break;
-                case 'r':
-                  {
-                    gint hour = g_date_time_get_hour (datetime) % 12;
-                    if (hour == 0)
-                      hour = 12;
-                    g_string_append_printf (outstr, "%02d:%02d:%02d %s",
-                                            hour,
-                                            g_date_time_get_minute (datetime),
-                                            g_date_time_get_second (datetime),
-                                            GET_AMPM (datetime, FALSE));
-                  }
-                  break;
-                case 'R':
-                  g_string_append_printf (outstr, "%02d:%02d",
-                                          g_date_time_get_hour (datetime),
-                                          g_date_time_get_minute (datetime));
-                  break;
-                case 's':
-                  g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_epoch (datetime));
-                  break;
-                case 'S':
-                  g_string_append_printf (outstr, "%02d", g_date_time_get_second (datetime));
-                  break;
-                case 't':
-                  g_string_append_c (outstr, '\t');
-                  break;
-                case 'u':
-                  g_string_append_printf (outstr, "%d", g_date_time_get_day_of_week (datetime));
-                  break;
-                case 'W':
-                  g_string_append_printf (outstr, "%d", g_date_time_get_day_of_year (datetime) / 7);
-                  break;
-                case 'x':
-                  {
-                    tmp = GET_PREFERRED_DATE (datetime);
-                    g_string_append (outstr, tmp);
-                    g_free (tmp);
-                  }
-                  break;
-                case 'X':
-                  {
-                    tmp = GET_PREFERRED_TIME (datetime);
-                    g_string_append (outstr, tmp);
-                    g_free (tmp);
-                  }
-                  break;
-                case 'y':
-                  g_string_append_printf (outstr, "%02d", g_date_time_get_year (datetime) % 100);
-                  break;
-                case 'Y':
-                  g_string_append_printf (outstr, "%d", g_date_time_get_year (datetime));
-                  break;
-                case 'z':
-                  if (datetime->tz != NULL)
-                    {
-                      gint64 offset = g_date_time_get_utc_offset (datetime)
-                                    / USEC_PER_SECOND;
-
-                      g_string_append_printf (outstr, "%c%02d%02d",
-                                              offset >= 0 ? '+' : '-',
-                                              (int) offset / 3600,
-                                              (int) offset / 60 % 60);
-                    }
-                  else
-                    g_string_append (outstr, "+0000");
-                  break;
-                case 'Z':
-                  if (datetime->tz != NULL)
-                    g_string_append (outstr, g_date_time_get_timezone_name (datetime));
-                  else
-                    g_string_append (outstr, "UTC");
-                  break;
-                case '%':
-                  g_string_append_c (outstr, '%');
-                  break;
-                case 'n':
-                  g_string_append_c (outstr, '\n');
-                  break;
-                default:
-                  goto bad_format;
-                }
-              in_mod = FALSE;
-            }
-          else
-            g_string_append_unichar (outstr, c);
-        }
-    }
+static gboolean
+format_z (GString *outstr,
+          gint     offset,
+          guint    colons)
+{
+  gint hours;
+  gint minutes;
+  gint seconds;
 
-  return g_string_free (outstr, FALSE);
+  hours = offset / 3600;
+  minutes = ABS (offset) / 60 % 60;
+  seconds = ABS (offset) % 60;
 
-bad_format:
-  g_string_free (outstr, TRUE);
-  return NULL;
-}
+  switch (colons)
+    {
+    case 0:
+      g_string_append_printf (outstr, "%+03d%02d",
+                              hours,
+                              minutes);
+      break;
 
-/**
- * g_date_time_to_local:
- * @datetime: a #GDateTime
- *
- * Creates a new #GDateTime with @datetime converted to local time.
- *
- * Return value: the newly created #GDateTime
- *
- * Since: 2.26
- */
-GDateTime *
-g_date_time_to_local (const GDateTime *datetime)
-{
-  GDateTime *dt;
+    case 1:
+      g_string_append_printf (outstr, "%+03d:%02d",
+                              hours,
+                              minutes);
+      break;
 
-  g_return_val_if_fail (datetime != NULL, NULL);
+    case 2:
+      g_string_append_printf (outstr, "%+03d:%02d:%02d",
+                              hours,
+                              minutes,
+                              seconds);
+      break;
 
-  dt = g_date_time_copy (datetime);
-  if (dt->tz == NULL)
-    {
-      dt->tz = g_time_zone_new_local ();
-      if (dt->tz == NULL)
-        return dt;
+    case 3:
+      g_string_append_printf (outstr, "%+03d", hours);
+
+      if (minutes != 0 || seconds != 0)
+        {
+          g_string_append_printf (outstr, ":%02d", minutes);
+
+          if (seconds != 0)
+            g_string_append_printf (outstr, ":%02d", seconds);
+        }
+      break;
 
-      g_time_zone_sink (dt->tz, dt);
-      g_date_time_add_usec (dt, dt->tz->offset * USEC_PER_SECOND);
+    default:
+      return FALSE;
     }
 
-  return dt;
+  return TRUE;
 }
 
-/**
- * g_date_time_to_epoch:
- * @datetime: a #GDateTime
- *
- * Converts @datetime into an integer representing seconds since the
- * Unix epoch
- *
- * Return value: @datetime as seconds since the Unix epoch
- *
- * Since: 2.26
- */
-gint64
-g_date_time_to_epoch (const GDateTime *datetime)
-{
-  gint64 result;
+static void
+format_number (GString  *str,
+               gboolean  use_alt_digits,
+               gchar    *pad,
+               gint      width,
+               guint32   number)
+{
+  const gchar *ascii_digits[10] = {
+    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
+  };
+  const gchar **digits = ascii_digits;
+  const gchar *tmp[10];
+  gint i = 0;
+
+  g_return_if_fail (width <= 10);
+
+#ifdef HAVE_LANGINFO_OUTDIGIT
+  if (use_alt_digits)
+    {
+      static const gchar *alt_digits[10];
+      static gsize initialised;
+      /* 2^32 has 10 digits */
 
-  g_return_val_if_fail (datetime != NULL, 0);
+      if G_UNLIKELY (g_once_init_enter (&initialised))
+        {
+#define DO_DIGIT(n) \
+        alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
+          DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
+          DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
+#undef DO_DIGIT
+          g_once_init_leave (&initialised, TRUE);
+        }
 
-  if (datetime->days <= 0)
-    return G_MININT64;
+      digits = alt_digits;
+    }
+#endif /* HAVE_LANGINFO_OUTDIGIT */
 
-  result = (datetime->days - UNIX_EPOCH_START) * SEC_PER_DAY
-         + datetime->usec / USEC_PER_SECOND
-         - g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
+  do
+    {
+      tmp[i++] = digits[number % 10];
+      number /= 10;
+    }
+  while (number);
 
-  return result;
-}
+  while (pad && i < width)
+    tmp[i++] = *pad == '0' ? digits[0] : pad;
 
-/**
- * g_date_time_to_timeval:
- * @datetime: a #GDateTime
- * @tv: A #GTimeVal
- *
- * Converts @datetime into a #GTimeVal and stores the result into @timeval.
- *
- * Since: 2.26
- */
-void
-g_date_time_to_timeval (const GDateTime *datetime,
-                        GTimeVal        *tv)
-{
-  g_return_if_fail (datetime != NULL);
-  g_return_if_fail (tv != NULL);
+  /* should really be impossible */
+  g_assert (i <= 10);
 
-  tv->tv_sec = g_date_time_to_epoch (datetime);
-  tv->tv_usec = datetime->usec % USEC_PER_SECOND;
+  while (i)
+    g_string_append (str, tmp[--i]);
 }
 
-/**
- * g_date_time_to_utc:
- * @datetime: a #GDateTime
- *
- * Creates a new #GDateTime that reprents @datetime in Universal coordinated
- * time.
- *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
- *
- * Since: 2.26
+static gboolean g_date_time_format_locale (GDateTime   *datetime,
+                                          const gchar *format,
+                                          GString     *outstr,
+                                          gboolean     locale_is_utf8);
+
+/* g_date_time_format() subroutine that takes a locale-encoded format
+ * string and produces a locale-encoded date/time string.
  */
-GDateTime *
-g_date_time_to_utc (const GDateTime *datetime)
+static gboolean
+g_date_time_locale_format_locale (GDateTime   *datetime,
+                                 const gchar *format,
+                                 GString     *outstr,
+                                 gboolean     locale_is_utf8)
 {
-  GDateTime *dt;
-  GTimeSpan ts;
+  gchar *utf8_format;
+  gboolean success;
 
-  g_return_val_if_fail (datetime != NULL, NULL);
+  if (locale_is_utf8)
+    return g_date_time_format_locale (datetime, format, outstr,
+                                     locale_is_utf8);
 
-  ts = g_date_time_get_utc_offset (datetime) * -1;
-  dt = g_date_time_add (datetime, ts);
-  if (dt->tz != NULL)
-    {
-      g_time_zone_free (dt->tz);
-      dt->tz = NULL;
-    }
+  utf8_format = g_locale_to_utf8 (format, -1, NULL, NULL, NULL);
+  if (!utf8_format)
+    return FALSE;
 
-  return dt;
+  success = g_date_time_format_locale (datetime, utf8_format, outstr,
+                                      locale_is_utf8);
+  g_free (utf8_format);
+  return success;
 }
 
-/**
- * g_date_time_new_today:
- *
- * Createsa new #GDateTime that represents Midnight on the current day.
- *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
- *
- * Since: 2.26
+/* g_date_time_format() subroutine that takes a UTF-8 format
+ * string and produces a locale-encoded date/time string.
  */
-GDateTime*
-g_date_time_new_today (void)
+static gboolean
+g_date_time_format_locale (GDateTime   *datetime,
+                          const gchar *format,
+                          GString     *outstr,
+                          gboolean     locale_is_utf8)
 {
-  GDateTime *dt;
+  guint     len;
+  guint     colons;
+  gchar    *tmp;
+  gunichar  c;
+  gboolean  alt_digits = FALSE;
+  gboolean  pad_set = FALSE;
+  gchar    *pad = "";
+  gchar    *ampm;
+  const gchar *tz;
 
-  dt = g_date_time_new_now ();
-  dt->usec = 0;
+  while (*format)
+    {
+      len = strcspn (format, "%");
+      if (len)
+       {
+         if (locale_is_utf8)
+           g_string_append_len (outstr, format, len);
+         else
+           {
+             tmp = g_locale_from_utf8 (format, len, NULL, NULL, NULL);
+             if (!tmp)
+               return FALSE;
+             g_string_append (outstr, tmp);
+             g_free (tmp);
+           }
+       }
+
+      format += len;
+      if (!*format)
+       break;
+
+      g_assert (*format == '%');
+      format++;
+      if (!*format)
+       break;
+
+      colons = 0;
+      alt_digits = FALSE;
+      pad_set = FALSE;
+
+    next_mod:
+      c = g_utf8_get_char (format);
+      format = g_utf8_next_char (format);
+      switch (c)
+       {
+       case 'a':
+         g_string_append (outstr, WEEKDAY_ABBR (datetime));
+         break;
+       case 'A':
+         g_string_append (outstr, WEEKDAY_FULL (datetime));
+         break;
+       case 'b':
+         g_string_append (outstr, MONTH_ABBR (datetime));
+         break;
+       case 'B':
+         g_string_append (outstr, MONTH_FULL (datetime));
+         break;
+       case 'c':
+         {
+           if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_TIME_FMT,
+                                                  outstr, locale_is_utf8))
+             return FALSE;
+         }
+         break;
+       case 'C':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_year (datetime) / 100);
+         break;
+       case 'd':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_day_of_month (datetime));
+         break;
+       case 'e':
+         format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
+                        g_date_time_get_day_of_month (datetime));
+         break;
+       case 'F':
+         g_string_append_printf (outstr, "%d-%02d-%02d",
+                                 g_date_time_get_year (datetime),
+                                 g_date_time_get_month (datetime),
+                                 g_date_time_get_day_of_month (datetime));
+         break;
+       case 'g':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_week_numbering_year (datetime) % 100);
+         break;
+       case 'G':
+         format_number (outstr, alt_digits, pad_set ? pad : 0, 0,
+                        g_date_time_get_week_numbering_year (datetime));
+         break;
+       case 'h':
+         g_string_append (outstr, MONTH_ABBR (datetime));
+         break;
+       case 'H':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_hour (datetime));
+         break;
+       case 'I':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        (g_date_time_get_hour (datetime) + 11) % 12 + 1);
+         break;
+       case 'j':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 3,
+                        g_date_time_get_day_of_year (datetime));
+         break;
+       case 'k':
+         format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
+                        g_date_time_get_hour (datetime));
+         break;
+       case 'l':
+         format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
+                        (g_date_time_get_hour (datetime) + 11) % 12 + 1);
+         break;
+       case 'n':
+         g_string_append_c (outstr, '\n');
+         break;
+       case 'm':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_month (datetime));
+         break;
+       case 'M':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_minute (datetime));
+         break;
+       case 'O':
+         alt_digits = TRUE;
+         goto next_mod;
+       case 'p':
+         ampm = (gchar *) GET_AMPM (datetime);
+         if (!locale_is_utf8)
+           {
+             ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
+             if (!tmp)
+               return FALSE;
+           }
+         ampm = g_utf8_strup (ampm, -1);
+         if (!locale_is_utf8)
+           {
+             g_free (tmp);
+             tmp = g_locale_from_utf8 (ampm, -1, NULL, NULL, NULL);
+             g_free (ampm);
+             if (!tmp)
+               return FALSE;
+             ampm = tmp;
+           }
+         g_string_append (outstr, ampm);
+         g_free (ampm);
+         break;
+       case 'P':
+         ampm = (gchar *) GET_AMPM (datetime);
+         if (!locale_is_utf8)
+           {
+             ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
+             if (!tmp)
+               return FALSE;
+           }
+         ampm = g_utf8_strdown (ampm, -1);
+         if (!locale_is_utf8)
+           {
+             g_free (tmp);
+             tmp = g_locale_from_utf8 (ampm, -1, NULL, NULL, NULL);
+             g_free (ampm);
+             if (!tmp)
+               return FALSE;
+             ampm = tmp;
+           }
+         g_string_append (outstr, ampm);
+         g_free (ampm);
+         break;
+       case 'r':
+         {
+           if (!g_date_time_locale_format_locale (datetime, PREFERRED_12HR_TIME_FMT,
+                                                  outstr, locale_is_utf8))
+             return FALSE;
+         }
+         break;
+       case 'R':
+         g_string_append_printf (outstr, "%02d:%02d",
+                                 g_date_time_get_hour (datetime),
+                                 g_date_time_get_minute (datetime));
+         break;
+       case 's':
+         g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
+         break;
+       case 'S':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_second (datetime));
+         break;
+       case 't':
+         g_string_append_c (outstr, '\t');
+         break;
+       case 'T':
+         g_string_append_printf (outstr, "%02d:%02d:%02d",
+                                 g_date_time_get_hour (datetime),
+                                 g_date_time_get_minute (datetime),
+                                 g_date_time_get_second (datetime));
+         break;
+       case 'u':
+         format_number (outstr, alt_digits, 0, 0,
+                        g_date_time_get_day_of_week (datetime));
+         break;
+       case 'V':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_week_of_year (datetime));
+         break;
+       case 'w':
+         format_number (outstr, alt_digits, 0, 0,
+                        g_date_time_get_day_of_week (datetime) % 7);
+         break;
+       case 'x':
+         {
+           if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_FMT,
+                                                  outstr, locale_is_utf8))
+             return FALSE;
+         }
+         break;
+       case 'X':
+         {
+           if (!g_date_time_locale_format_locale (datetime, PREFERRED_TIME_FMT,
+                                                  outstr, locale_is_utf8))
+             return FALSE;
+         }
+         break;
+       case 'y':
+         format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
+                        g_date_time_get_year (datetime) % 100);
+         break;
+       case 'Y':
+         format_number (outstr, alt_digits, 0, 0,
+                        g_date_time_get_year (datetime));
+         break;
+       case 'z':
+         {
+           gint64 offset;
+           if (datetime->tz != NULL)
+             offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
+           else
+             offset = 0;
+           if (!format_z (outstr, (int) offset, colons))
+             return FALSE;
+         }
+         break;
+       case 'Z':
+         tz = g_date_time_get_timezone_abbreviation (datetime);
+         if (!locale_is_utf8)
+           {
+             tz = tmp = g_locale_from_utf8 (tz, -1, NULL, NULL, NULL);
+             if (!tmp)
+               return FALSE;
+           }
+         g_string_append (outstr, tz);
+         if (!locale_is_utf8)
+           g_free (tmp);
+         break;
+       case '%':
+         g_string_append_c (outstr, '%');
+         break;
+       case '-':
+         pad_set = TRUE;
+         pad = "";
+         goto next_mod;
+       case '_':
+         pad_set = TRUE;
+         pad = " ";
+         goto next_mod;
+       case '0':
+         pad_set = TRUE;
+         pad = "0";
+         goto next_mod;
+       case ':':
+         /* Colons are only allowed before 'z' */
+         if (*format && *format != 'z' && *format != ':')
+           return FALSE;
+         colons++;
+         goto next_mod;
+       default:
+         return FALSE;
+       }
+    }
 
-  return dt;
+  return TRUE;
 }
 
 /**
- * g_date_time_new_utc_now:
+ * g_date_time_format:
+ * @datetime: A #GDateTime
+ * @format: a valid UTF-8 string, containing the format for the
+ *          #GDateTime
  *
- * Creates a new #GDateTime that represents the current instant in Universal
- * Coordinated Time (UTC).
+ * Creates a newly allocated string representing the requested @format.
  *
- * Return value: the newly created #GDateTime which should be freed with
- *   g_date_time_unref().
+ * The format strings understood by this function are a subset of the
+ * strftime() format language as specified by C99.  The \%D, \%U and \%W
+ * conversions are not supported, nor is the 'E' modifier.  The GNU
+ * extensions \%k, \%l, \%s and \%P are supported, however, as are the
+ * '0', '_' and '-' modifiers.
  *
- * Since: 2.26
- */
-GDateTime *
-g_date_time_new_utc_now (void)
-{
-  GDateTime *utc, *now;
-
-  now = g_date_time_new_now ();
-  utc = g_date_time_to_utc (now);
-  g_date_time_unref (now);
-
-  return utc;
-}
-
-/**
- * g_date_time_get_week_of_year:
+ * In contrast to strftime(), this function always produces a UTF-8
+ * string, regardless of the current locale.  Note that the rendering of
+ * many formats is locale-dependent and may not match the strftime()
+ * output exactly.
  *
- * Returns the numeric week of the respective year.
+ * The following format specifiers are supported:
  *
- * Return value: the week of the year
+ * - \%a: the abbreviated weekday name according to the current locale
+ * - \%A: the full weekday name according to the current locale
+ * - \%b: the abbreviated month name according to the current locale
+ * - \%B: the full month name according to the current locale
+ * - \%c: the  preferred date and time rpresentation for the current locale
+ * - \%C: the century number (year/100) as a 2-digit integer (00-99)
+ * - \%d: the day of the month as a decimal number (range 01 to 31)
+ * - \%e: the day of the month as a decimal number (range  1 to 31)
+ * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
+ * - \%g: the last two digits of the ISO 8601 week-based year as a
+ *   decimal number (00-99). This works well with \%V and \%u.
+ * - \%G: the ISO 8601 week-based year as a decimal number. This works
+ *   well with \%V and \%u.
+ * - \%h: equivalent to \%b
+ * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
+ * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
+ * - \%j: the day of the year as a decimal number (range 001 to 366)
+ * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
+ *   single digits are preceded by a blank
+ * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
+ *   single digits are preceded by a blank
+ * - \%m: the month as a decimal number (range 01 to 12)
+ * - \%M: the minute as a decimal number (range 00 to 59)
+ * - \%p: either "AM" or "PM" according to the given time value, or the
+ *   corresponding  strings for the current locale.  Noon is treated as
+ *   "PM" and midnight as "AM".
+ * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
+ *   the current locale
+ * - \%r: the time in a.m. or p.m. notation
+ * - \%R: the time in 24-hour notation (\%H:\%M)
+ * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
+ *   00:00:00 UTC
+ * - \%S: the second as a decimal number (range 00 to 60)
+ * - \%t: a tab character
+ * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
+ * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
+ *    Monday being 1. This works well with \%G and \%V.
+ * - \%V: the ISO 8601 standard week number of the current year as a decimal
+ *   number, range 01 to 53, where week 1 is the first week that has at
+ *   least 4 days in the new year. See g_date_time_get_week_of_year().
+ *   This works well with \%G and \%u.
+ * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
+ *   This is not the ISO 8601 standard format -- use \%u instead.
+ * - \%x: the preferred date representation for the current locale without
+ *   the time
+ * - \%X: the preferred time representation for the current locale without
+ *   the date
+ * - \%y: the year as a decimal number without the century
+ * - \%Y: the year as a decimal number including the century
+ * - \%z: the time zone as an offset from UTC (+hhmm)
+ * - \%:z: the time zone as an offset from UTC (+hh:mm).
+ *   This is a gnulib strftime() extension. Since: 2.38
+ * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
+ *   gnulib strftime() extension. Since: 2.38
+ * - \%:::z: the time zone as an offset from UTC, with : to necessary
+ *   precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
+ * - \%Z: the time zone or name or abbreviation
+ * - \%\%: a literal \% character
+ *
+ * Some conversion specifications can be modified by preceding the
+ * conversion specifier by one or more modifier characters. The
+ * following modifiers are supported for many of the numeric
+ * conversions:
+ * 
+ * - O: Use alternative numeric symbols, if the current locale supports those.
+ * - _: Pad a numeric result with spaces. This overrides the default padding
+ *   for the specifier.
+ * - -: Do not pad a numeric result. This overrides the default padding
+ *   for the specifier.
+ * - 0: Pad a numeric result with zeros. This overrides the default padding
+ *   for the specifier.
+ *
+ * Returns: a newly allocated string formatted to the requested format
+ *     or %NULL in the case that there was an error. The string
+ *     should be freed with g_free().
  *
  * Since: 2.26
  */
-gint
-g_date_time_get_week_of_year (const GDateTime *datetime)
+gchar *
+g_date_time_format (GDateTime   *datetime,
+                    const gchar *format)
 {
-  gint weeknum;
+  GString  *outstr;
+  gchar *utf8;
+  gboolean locale_is_utf8 = g_get_charset (NULL);
 
-  g_return_val_if_fail (datetime != NULL, 0);
+  g_return_val_if_fail (datetime != NULL, NULL);
+  g_return_val_if_fail (format != NULL, NULL);
+  g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
 
-  g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
+  outstr = g_string_sized_new (strlen (format) * 2);
 
-  return weeknum;
+  if (!g_date_time_format_locale (datetime, format, outstr, locale_is_utf8))
+    {
+      g_string_free (outstr, TRUE);
+      return NULL;
+    }
+
+  if (locale_is_utf8)
+    return g_string_free (outstr, FALSE);
+
+  utf8 = g_locale_to_utf8 (outstr->str, outstr->len, NULL, NULL, NULL);
+  g_string_free (outstr, TRUE);
+  return utf8;
 }
+
+
+/* Epilogue {{{1 */
+/* vim:set foldmethod=marker: */