r22613 - gnucash/trunk/src/backend/xml - Replace the time_t-based timespec_secs_to_given_string with gnc-date functions.
John Ralls
jralls at code.gnucash.org
Sat Dec 1 17:43:34 EST 2012
Author: jralls
Date: 2012-12-01 17:43:34 -0500 (Sat, 01 Dec 2012)
New Revision: 22613
Trac: http://svn.gnucash.org/trac/changeset/22613
Modified:
gnucash/trunk/src/backend/xml/sixtp-dom-generators.c
gnucash/trunk/src/backend/xml/sixtp-utils.c
gnucash/trunk/src/backend/xml/sixtp-utils.h
Log:
Replace the time_t-based timespec_secs_to_given_string with gnc-date functions.
Thus saving having to write gnc_timegm.
Modified: gnucash/trunk/src/backend/xml/sixtp-dom-generators.c
===================================================================
--- gnucash/trunk/src/backend/xml/sixtp-dom-generators.c 2012-12-01 22:43:25 UTC (rev 22612)
+++ gnucash/trunk/src/backend/xml/sixtp-dom-generators.c 2012-12-01 22:43:34 UTC (rev 22613)
@@ -27,6 +27,7 @@
#include <glib.h>
#include "gnc-xml-helper.h"
+#include <gnc-date.h>
#include "sixtp-dom-generators.h"
#include "sixtp-utils.h"
@@ -120,20 +121,21 @@
return ret;
}
+/* gnc_g_date_time_new_from_timespec_local normalizes the timespec,
+ * but we want to serialize it un-normalized, so we make a partial
+ * copy.
+ */
gchar *
timespec_sec_to_string(const Timespec *ts)
{
- gchar *ret;
-
- ret = g_new(gchar, TIMESPEC_SEC_FORMAT_MAX);
-
- if (!timespec_secs_to_given_string (ts, ret))
- {
- g_free(ret);
- return NULL;
- }
-
- return ret;
+ gchar *time_string;
+ GDateTime *gdt;
+ Timespec sts = { ts->tv_sec, 0};
+ gdt = gnc_g_date_time_new_from_timespec_local (sts);
+ g_return_val_if_fail (gdt != NULL, NULL);
+ time_string = g_date_time_format (gdt, "%Y-%m-%d %H:%M:%S %z");
+ g_date_time_unref (gdt);
+ return time_string;
}
gchar *
Modified: gnucash/trunk/src/backend/xml/sixtp-utils.c
===================================================================
--- gnucash/trunk/src/backend/xml/sixtp-utils.c 2012-12-01 22:43:25 UTC (rev 22612)
+++ gnucash/trunk/src/backend/xml/sixtp-utils.c 2012-12-01 22:43:34 UTC (rev 22613)
@@ -30,7 +30,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <time.h>
#include "sixtp.h"
#include "sixtp-utils.h"
@@ -41,10 +40,8 @@
#ifndef HAVE_STRPTIME
#include "strptime.h"
#endif
-#ifndef HAVE_LOCALTIME_R
-#include "localtime_r.h"
+#include <gnc-date.h>
#endif
-#endif
static QofLogModule log_module = GNC_MOD_IO;
@@ -392,42 +389,6 @@
SIXTP_NO_MORE_HANDLERS);
}
-
-#ifdef HAVE_TIMEGM
-# define gnc_timegm timegm
-#else /* !HAVE_TIMEGM */
-
-/* This code originates from GLib 2.12, gtimer.c and works until the year 2100
- * or the system-dependent maximal date that can be represented by a time_t,
- * whatever comes first. The old implementation called mktime after setting
- * the environment variable TZ to UTC. It did not work on Windows, at least.
- */
-static const gint days_before[] =
-{
- 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
-};
-
-static time_t
-gnc_timegm (struct tm *tm)
-{
- time_t retval;
- if (tm->tm_mon < 0 || tm->tm_mon > 11)
- return (time_t) - 1;
-
- retval = (tm->tm_year - 70) * 365;
- retval += (tm->tm_year - 68) / 4;
- retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
-
- if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
- retval -= 1;
-
- retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
-
- return retval;
-}
-#endif /* HAVE_TIMEGM */
-
-
/****************************************************************************/
/* generic timespec handler.
@@ -529,47 +490,6 @@
return(TRUE);
}
-gboolean
-timespec_secs_to_given_string (const Timespec *ts, gchar *str)
-{
- struct tm parsed_time;
- size_t num_chars;
- time_t tmp_time;
- long int tz;
- int minutes;
- int hours;
- int sign;
-
- if (!ts || !str)
- return FALSE;
-
- tmp_time = ts->tv_sec;
-
- if (!localtime_r(&tmp_time, &parsed_time))
- return FALSE;
-
- num_chars = qof_strftime(str, TIMESPEC_SEC_FORMAT_MAX,
- TIMESPEC_TIME_FORMAT, &parsed_time);
- if (num_chars == 0)
- return FALSE;
-
- str += num_chars;
-
- tz = gnc_timezone (&parsed_time);
-
- /* gnc_timezone is seconds west of UTC */
- sign = (tz > 0) ? -1 : 1;
-
- minutes = ABS (tz) / 60;
- hours = minutes / 60;
- minutes -= hours * 60;
-
- g_snprintf (str, TIMESPEC_SEC_FORMAT_MAX - num_chars,
- " %c%02d%02d", (sign > 0) ? '+' : '-', hours, minutes);
-
- return TRUE;
-}
-
/* Top level timespec node:
input: user end handler *
Modified: gnucash/trunk/src/backend/xml/sixtp-utils.h
===================================================================
--- gnucash/trunk/src/backend/xml/sixtp-utils.h 2012-12-01 22:43:25 UTC (rev 22612)
+++ gnucash/trunk/src/backend/xml/sixtp-utils.h 2012-12-01 22:43:34 UTC (rev 22613)
@@ -87,10 +87,6 @@
gboolean string_to_timespec_secs(const gchar *str, Timespec *ts);
gboolean string_to_timespec_nsecs(const gchar *str, Timespec *ts);
-/* str must have length of at least TIMESPEC_SEC_FORMAT_MAX */
-gboolean timespec_secs_to_given_string (const Timespec *ts, gchar *str);
-
-
gboolean generic_timespec_start_handler(GSList* sibling_data,
gpointer parent_data,
gpointer global_data,
More information about the gnucash-changes
mailing list