r22610 - gnucash/trunk/src/libqof/qof - New public function gnc_g_date_time_new_from_timespec_local
John Ralls
jralls at code.gnucash.org
Sat Dec 1 17:43:07 EST 2012
Author: jralls
Date: 2012-12-01 17:43:07 -0500 (Sat, 01 Dec 2012)
New Revision: 22610
Trac: http://svn.gnucash.org/trac/changeset/22610
Modified:
gnucash/trunk/src/libqof/qof/gnc-date.c
gnucash/trunk/src/libqof/qof/gnc-date.h
Log:
New public function gnc_g_date_time_new_from_timespec_local
Modified: gnucash/trunk/src/libqof/qof/gnc-date.c
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.c 2012-12-01 22:42:59 UTC (rev 22609)
+++ gnucash/trunk/src/libqof/qof/gnc-date.c 2012-12-01 22:43:07 UTC (rev 22610)
@@ -28,6 +28,7 @@
#include "config.h"
#include <glib.h>
+#include <glib/gprintf.h>
/* to be renamed qofdate.c */
#include <ctype.h>
@@ -303,6 +304,7 @@
{
index = 1;
daylight = 1;
+ time->tm_isdst = 1;
}
#ifdef HAVE_STRUCT_TM_GMTOFF
@@ -447,6 +449,16 @@
/****************************************************************************/
+GDateTime*
+gnc_g_date_time_new_from_timespec_local (Timespec ts)
+{
+ GDateTime *gdt1 = gnc_g_date_time_new_from_unix_local (ts.tv_sec);
+ double nsecs = ((double)ts.tv_nsec + 0.5)/ 1000000000.0L;
+ GDateTime *gdt2 = g_date_time_add_seconds (gdt1, nsecs);
+ g_date_time_unref (gdt1);
+ g_assert (g_date_time_to_unix (gdt2) == ts.tv_sec + (nsecs >= 1.0 ? (gint64)nsecs : 0));
+ return gdt2;
+}
const char*
gnc_date_dateformat_to_string(QofDateFormat format)
@@ -1392,10 +1404,12 @@
Timespec time = { 0L, 0L };
GDateTime *gdt;
gint hour = 0, minute = 0, day = 0, month = 0, year = 0;
- gchar zone[6] = "\0\0\0\0\0\0";
+ gchar zone[12];
gdouble second = 0.0;
gint fields;
+ memset (zone, 0, sizeof (zone));
+
if (!str)
return time;
@@ -1406,7 +1420,10 @@
else if (fields > 6 && strlen (zone) > 0) /* Date string included a timezone */
{
GTimeZone *tz = g_time_zone_new (zone);
+ time64 secs;
+ second += 5e-10;
gdt = g_date_time_new (tz, year, month, day, hour, minute, second);
+ secs = g_date_time_to_unix (gdt);
g_time_zone_unref (tz);
}
else /* No zone info, assume UTC */
Modified: gnucash/trunk/src/libqof/qof/gnc-date.h
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.h 2012-12-01 22:42:59 UTC (rev 22609)
+++ gnucash/trunk/src/libqof/qof/gnc-date.h 2012-12-01 22:43:07 UTC (rev 22610)
@@ -4,6 +4,7 @@
* Copyright (C) 1997 Robin D. Clark <rclark at cs.hmc.edu>
* Copyright (C) 1998-2000, 2003 Linas Vepstas <linas at linas.org>
* Copyright 2005 Neil Williams <linux at codehelp.co.uk>
+ * Copyright 2012 John Ralls <jralls at ceridwen.us>
****************************************************************************/
/********************************************************************\
* This program is free software; you can redistribute it and/or *
@@ -70,6 +71,16 @@
#include <glib-object.h>
#include <time.h>
+/** The Timespec is just like the unix 'struct timespec'
+ * except that we use a 64-bit unsigned int to
+ * store the seconds. This should adequately cover dates in the
+ * distant future as well as the distant past, as long as they're not
+ * more than a couple dozen times the age of the universe
+ * Values of this type can range from -9,223,372,036,854,775,808 to
+ * 9,223,372,036,854,775,807.
+ */
+typedef struct timespec64 Timespec;
+
/** @name GValue
@{
*/
@@ -81,7 +92,7 @@
extern const char *gnc_default_strftime_date_format;
/** The maximum length of a string created by the date printers */
-#define MAX_DATE_LENGTH 31
+#define MAX_DATE_LENGTH 34
/** Constants *******************************************************/
/** \brief UTC date format string.
@@ -131,12 +142,14 @@
GNCDATE_MONTH_ABBREV,
GNCDATE_MONTH_NAME
} GNCDateMonthFormat;
-
/* Replacements for POSIX functions which use time_t. Time_t is still
* 32 bits in Microsoft Windows, Apple OSX, and some BSD versions even
* when the rest of the system is 64-bits, as well as all 32-bit
* versions of Unix. 32-bit time_t overflows at 03:14:07 UTC on
* Tuesday, 19 January 2038 and so cannot represent dates after that.
+ *
+ * These functions use GLib's GDateTime internally, and include a
+ * workaround for the lack of Win32 support before GLib 2.36.
*/
/** \brief fill out a time struct from a 64-bit time value.
* \param secs: Seconds since 00:00:01 UTC 01 January 1970 (negative values
@@ -211,6 +224,14 @@
*/
void gnc_tm_free (struct tm* time);
+/** \brief Create a GDateTime from a Timespec
+ * \param ts: A local (int64-based) Timespec
+ * \note: GDateTimes use microseconds, not nanoseconds, so in theory we lose precision. In practice, there's no portable way to get either.
+ * \note: Works around the lack of Win32 support in GTimeZone before GLib 2.36.
+ * \return A GDateTime pointer. Free it with g_date_time_unref () when you're done with it.
+ */
+GDateTime* gnc_g_date_time_new_from_timespec_local (Timespec tm);
+
/** \name String / DateFormat conversion. */
//@{
@@ -257,15 +278,6 @@
};
#endif /* SWIG */
-/** The Timespec is just like the unix 'struct timespec'
- * except that we use a 64-bit unsigned int to
- * store the seconds. This should adequately cover dates in the
- * distant future as well as the distant past, as long as they're not
- * more than a couple dozen times the age of the universe
- * Values of this type can range from -9,223,372,036,854,775,808 to
- * 9,223,372,036,854,775,807.
- */
-typedef struct timespec64 Timespec;
/* Prototypes ******************************************************/
More information about the gnucash-changes
mailing list