New public function gnc_g_date_time_new_from_timespec_local

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22610 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
John Ralls
2012-12-01 22:43:07 +00:00
parent 5247cd1cad
commit 21124e2ae3
2 changed files with 41 additions and 12 deletions
+18 -1
View File
@@ -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 @@ gnc_localtime_r (const gint64 *secs, struct tm* time)
{
index = 1;
daylight = 1;
time->tm_isdst = 1;
}
#ifdef HAVE_STRUCT_TM_GMTOFF
@@ -447,6 +449,16 @@ gnc_difftime (const gint64 secs1, const gint64 secs2)
/****************************************************************************/
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 @@ gnc_iso8601_to_timespec_gmt(const char *str)
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 @@ gnc_iso8601_to_timespec_gmt(const char *str)
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 */
+23 -11
View File
@@ -4,6 +4,7 @@
* Copyright (C) 1997 Robin D. Clark <rclark@cs.hmc.edu>
* Copyright (C) 1998-2000, 2003 Linas Vepstas <linas@linas.org>
* Copyright 2005 Neil Williams <linux@codehelp.co.uk>
* Copyright 2012 John Ralls <jralls@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 @@ GType timespec_get_type( void );
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 @@ typedef enum
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 @@ gdouble gnc_difftime (const gint64 secs1, const gint64 secs2);
*/
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 @@ struct timespec64
};
#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 ******************************************************/