mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Replace the time_t-based timespec_secs_to_given_string with gnc-date functions.
Thus saving having to write gnc_timegm. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22613 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
parent
2c26cb7777
commit
441b73d7d9
@ -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 @@ commodity_ref_to_dom_tree(const char *tag, const gnc_commodity *c)
|
||||
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 *
|
||||
|
@ -30,7 +30,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "sixtp.h"
|
||||
#include "sixtp-utils.h"
|
||||
@ -41,9 +40,7 @@
|
||||
#ifndef HAVE_STRPTIME
|
||||
#include "strptime.h"
|
||||
#endif
|
||||
#ifndef HAVE_LOCALTIME_R
|
||||
#include "localtime_r.h"
|
||||
#endif
|
||||
#include <gnc-date.h>
|
||||
#endif
|
||||
|
||||
static QofLogModule log_module = GNC_MOD_IO;
|
||||
@ -392,42 +389,6 @@ simple_chars_only_parser_new(sixtp_end_handler end_handler)
|
||||
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 @@ string_to_timespec_nsecs(const gchar *str, Timespec *ts)
|
||||
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 *
|
||||
|
@ -87,10 +87,6 @@ sixtp* simple_chars_only_parser_new(sixtp_end_handler end_handler);
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user