r22604 - gnucash/trunk/src/libqof/qof - Implement internal replacements for localtime, mktime, etc.
John Ralls
jralls at code.gnucash.org
Sat Dec 1 17:42:10 EST 2012
Author: jralls
Date: 2012-12-01 17:42:10 -0500 (Sat, 01 Dec 2012)
New Revision: 22604
Trac: http://svn.gnucash.org/trac/changeset/22604
Modified:
gnucash/trunk/src/libqof/qof/gnc-date.c
gnucash/trunk/src/libqof/qof/gnc-date.h
gnucash/trunk/src/libqof/qof/test/test-gnc-date.c
Log:
Implement internal replacements for localtime, mktime, etc.
The builtin time functions suffer from the "2038 bug" caused by
overflowing a 32-bit representation of seconds from 1 Jan 1970. Provide
functions based on GDateTime which use a 64-bit representation (like our
Timespec), replace all of the instances in gnc-date.c, and make other
adjustments needed to ensure that the tests pass on a 32-bit system.
Modified: gnucash/trunk/src/libqof/qof/gnc-date.c
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.c 2012-12-01 22:42:00 UTC (rev 22603)
+++ gnucash/trunk/src/libqof/qof/gnc-date.c 2012-12-01 22:42:10 UTC (rev 22604)
@@ -128,6 +128,7 @@
guint day = g_date_time_get_day_of_month (date);
gint bias, hours, minutes;
gchar *tzstr;
+ g_return_val_if_fail (date != NULL, NULL);
if (dst > 0 && tzinfo.StandardDate.wMonth > 0
&& ((month > tzinfo.DaylightDate.wMonth
&& month < tzinfo.StandardDate.wMonth)
@@ -149,13 +150,15 @@
static GDateTime*
gnc_g_date_time_new_local (gint year, gint month, gint day, gint hour, gint minute, gdouble seconds)
-{
+{
#ifndef G_OS_WIN32
return g_date_time_new_local (year, month, day, hour, minute, seconds);
#else
GTimeZone *tz = gnc_g_time_zone_new_local();
GDateTime *gdt = g_date_time_new (tz, year, month, day,
hour, minute, seconds);
+ if (!gdt)
+ return gdt;
tz = gnc_g_time_zone_adjust_for_dst (tz, gdt);
g_date_time_unref (gdt);
gdt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
@@ -167,7 +170,9 @@
static GDateTime*
gnc_g_date_time_adjust_for_dst (GDateTime *gdt, GTimeZone *tz)
{
- GDateTime *ngdt = g_date_time_to_timezone (gdt, tz);
+ GDateTime *ngdt;
+ g_return_val_if_fail (gdt != NULL, NULL);
+ ngdt = g_date_time_to_timezone (gdt, tz);
g_date_time_unref (gdt);
tz = gnc_g_time_zone_adjust_for_dst (tz, ngdt);
gdt = g_date_time_to_timezone (ngdt, tz);
@@ -184,6 +189,8 @@
#else
GTimeZone *tz = gnc_g_time_zone_new_local ();
GDateTime *gdt = g_date_time_new_from_unix_utc (time);
+ if (!gdt)
+ return gdt;
return gnc_g_date_time_adjust_for_dst (gdt, tz);
#endif
}
@@ -196,6 +203,8 @@
#else
GTimeZone *tz = gnc_g_time_zone_new_local ();
GDateTime *gdt = g_date_time_new_from_timeval_utc (tv);
+ if (!gdt)
+ return gdt;
return gnc_g_date_time_adjust_for_dst (gdt, tz);
#endif
}
@@ -208,6 +217,8 @@
#else
GTimeZone *tz = gnc_g_time_zone_new_local ();
GDateTime *gdt = g_date_time_new_now_local ();
+ if (!gdt)
+ return gdt;
return gnc_g_date_time_adjust_for_dst (gdt, tz);
#endif
}
@@ -245,8 +256,198 @@
gncdt->to_local = gnc_g_date_time_to_local;
}
-/********************************************************************/
+/****************** Posix Replacement Functions ***************************/
+void
+gnc_tm_free (struct tm* time)
+{
+ g_slice_free1 (sizeof (struct tm), time);
+}
+#define MAX_TZ_SIZE
+static void
+gnc_g_date_time_fill_struct_tm (GDateTime *gdt, struct tm* time)
+{
+ g_date_time_get_ymd (gdt, &(time->tm_year), &(time->tm_mon), &(time->tm_mday));
+ time->tm_sec = g_date_time_get_second (gdt);
+ time->tm_min = g_date_time_get_minute (gdt);
+ time->tm_hour = g_date_time_get_hour (gdt);
+ time->tm_wday = g_date_time_get_day_of_week (gdt);
+ time->tm_yday = g_date_time_get_day_of_year (gdt);
+ time->tm_isdst = g_date_time_is_daylight_savings (gdt);
+ time->tm_year -= 1900;
+ --time->tm_mon;
+}
+
+struct tm*
+gnc_localtime (const gint64 *secs)
+{
+ struct tm *time = g_slice_alloc0 (sizeof (struct tm));
+ if (gnc_localtime_r (secs, time) == NULL)
+ {
+ gnc_tm_free (time);
+ return NULL;
+ }
+ return time;
+}
+
+struct tm*
+gnc_localtime_r (const gint64 *secs, struct tm* time)
+{
+ guint index = 0;
+ GDateTime *gdt = gnc_g_date_time_new_from_unix_local (*secs);
+ g_return_val_if_fail (gdt != NULL, NULL);
+
+ gnc_g_date_time_fill_struct_tm (gdt, time);
+ timezone = - g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND;
+ if (g_date_time_is_daylight_savings (gdt))
+ {
+ index = 1;
+ daylight = 1;
+ }
+
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ time->tm_gmtoff = - timezone;
+#endif
+
+ g_date_time_unref (gdt);
+ return time;
+}
+
+struct tm*
+gnc_gmtime (const gint64 *secs)
+{
+ struct tm *time;
+ GDateTime *gdt = g_date_time_new_from_unix_utc (*secs);
+ g_return_val_if_fail (gdt != NULL, NULL);
+ time = g_slice_alloc0 (sizeof (struct tm));
+ gnc_g_date_time_fill_struct_tm (gdt, time);
+ g_date_time_unref (gdt);
+ return time;
+}
+
+static void
+normalize_time_component (gint *inner, gint *outer, guint divisor, gint base)
+{
+ while (*inner < base)
+ {
+ --(*outer);
+ *inner += divisor;
+ }
+ while (*inner > divisor)
+ {
+ ++(*outer);
+ *inner -= divisor;
+ }
+}
+
+static gint
+normalize_month (gint month)
+{
+ month = (month % 12 + 12) % 12;
+ return month == 0 ? 12 : month;
+}
+
+static void
+normalize_struct_tm (struct tm* time)
+{
+ gint year = time->tm_year + 1900;
+ gint last_day;
+ gint64 secs;
+
+ ++time->tm_mon;
+
+ normalize_time_component (&(time->tm_sec), &(time->tm_min), 60, 0);
+ normalize_time_component (&(time->tm_min), &(time->tm_hour), 60, 0);
+ normalize_time_component (&(time->tm_hour), &(time->tm_mday), 24, 0);
+ normalize_time_component (&(time->tm_mon), &year, 12, 1);
+ while (time->tm_mday < 1)
+ {
+ last_day = gnc_date_get_last_mday (normalize_month (--time->tm_mon), year);
+ time->tm_mday += last_day;
+ normalize_time_component (&(time->tm_mon), &year, 12, 1);
+ }
+ last_day = gnc_date_get_last_mday (normalize_month (time->tm_mon), year);
+ while (time->tm_mday > last_day)
+ {
+ ++time->tm_mon;
+ time->tm_mday -= last_day;
+ normalize_time_component (&(time->tm_mon), &year, 12, 1);
+ last_day = gnc_date_get_last_mday (normalize_month (time->tm_mon), year);
+ }
+ time->tm_year = year - 1900;
+}
+
+gint64
+gnc_mktime (struct tm* time)
+{
+ GDateTime *gdt;
+ gint64 secs;
+ normalize_struct_tm (time);
+ gdt = gnc_g_date_time_new_local (time->tm_year + 1900, time->tm_mon,
+ time->tm_mday, time->tm_hour,
+ time->tm_min, (gdouble)(time->tm_sec));
+ time->tm_mon = time->tm_mon > 0 ? time->tm_mon - 1 : 11;
+ time->tm_wday = g_date_time_get_day_of_week (gdt);
+ time->tm_yday = g_date_time_get_day_of_year (gdt);
+ time->tm_isdst = g_date_time_is_daylight_savings (gdt);
+
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ time->tm_gmtoff = g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND;
+#endif
+
+ secs = g_date_time_to_unix (gdt);
+ g_date_time_unref (gdt);
+ return secs;
+}
+
+gint64
+gnc_timegm (struct tm* time)
+{
+ GDateTime *gdt;
+ gint64 secs;
+ normalize_struct_tm (time);
+ gdt = g_date_time_new_utc (time->tm_year + 1900, time->tm_mon,
+ time->tm_mday, time->tm_hour, time->tm_min,
+ (gdouble)(time->tm_sec));
+ time->tm_mon = time->tm_mon > 0 ? time->tm_mon - 1 : 11;
+ time->tm_wday = g_date_time_get_day_of_week (gdt);
+ time->tm_yday = g_date_time_get_day_of_year (gdt);
+ time->tm_isdst = g_date_time_is_daylight_savings (gdt);
+
+ secs = g_date_time_to_unix (gdt);
+ g_date_time_unref (gdt);
+ return secs;
+}
+
+gchar*
+gnc_ctime (const gint64 *secs)
+{
+ GDateTime *gdt = gnc_g_date_time_new_from_unix_local (*secs);
+ gchar *string = g_date_time_format (gdt, "%a %b %H:%M:%S %Y");
+ g_date_time_unref (gdt);
+ return string;
+}
+
+gint64
+gnc_time (gint64 *tbuf)
+{
+ GDateTime *gdt = gnc_g_date_time_new_now_local ();
+ gint64 secs = g_date_time_to_unix (gdt);
+ g_date_time_unref (gdt);
+ if (tbuf != NULL)
+ *tbuf = secs;
+ return secs;
+}
+
+gdouble
+gnc_difftime (const gint64 secs1, const gint64 secs2)
+{
+ return (double)secs2 - (double)secs1;
+}
+
+/****************************************************************************/
+
+
const char*
gnc_date_dateformat_to_string(QofDateFormat format)
{
@@ -436,14 +637,15 @@
{
struct tm tm;
Timespec retval;
- time_t t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
- localtime_r(&t_secs, &tm);
+ gint64 t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
+ gnc_localtime_r(&t_secs, &tm);
gnc_tm_set_day_middle(&tm);
- retval.tv_sec = mktime(&tm);
+ retval.tv_sec = gnc_mktime(&tm);
retval.tv_nsec = 0;
return retval;
}
+/* NB: month is 1-12, year is 0001 - 9999. */
int gnc_date_get_last_mday (int month, int year)
{
static int last_day_of_month[2][12] =
@@ -636,7 +838,7 @@
case QOF_DATE_FORMAT_LOCALE:
{
struct tm tm_str;
- time_t t;
+ gint64 t;
tm_str.tm_mday = day;
tm_str.tm_mon = month - 1; /* tm_mon = 0 through 11 */
@@ -644,8 +846,8 @@
says, it's not a Y2K thing */
gnc_tm_set_day_start (&tm_str);
- t = mktime (&tm_str);
- localtime_r (&t, &tm_str);
+ t = gnc_mktime (&tm_str);
+ gnc_localtime_r (&t, &tm_str);
flen = qof_strftime (buff, len, GNC_D_FMT, &tm_str);
if (flen != 0)
break;
@@ -665,18 +867,20 @@
}
size_t
-qof_print_date_buff (char * buff, size_t len, time_t t)
+qof_print_date_buff (char * buff, size_t len, gint64 t)
{
struct tm theTime;
-
+ gint64 bt = t;
+ size_t actual;
if (!buff) return 0 ;
- if (!localtime_r(&t, &theTime))
+ if (!gnc_localtime_r(&bt, &theTime))
return 0;
- return qof_print_date_dmy_buff (buff, len,
+ actual = qof_print_date_dmy_buff (buff, len,
theTime.tm_mday,
theTime.tm_mon + 1,
theTime.tm_year + 1900);
+ return actual;
}
size_t
@@ -689,7 +893,7 @@
}
char *
-qof_print_date (time_t t)
+qof_print_date (gint64 t)
{
char buff[MAX_DATE_LENGTH];
memset (buff, 0, sizeof (buff));
@@ -701,10 +905,10 @@
gnc_print_date (Timespec ts)
{
static char buff[MAX_DATE_LENGTH];
- time_t t;
+ gint64 t;
memset (buff, 0, sizeof (buff));
- t = ts.tv_sec + (time_t)(ts.tv_nsec / 1000000000.0);
+ t = ts.tv_sec + (gint64)(ts.tv_nsec / 1000000000.0);
qof_print_date_buff (buff, MAX_DATE_LENGTH, t);
@@ -758,7 +962,7 @@
int iday, imonth, iyear;
int now_day, now_month, now_year;
struct tm *now, utc;
- time_t secs;
+ gint64 secs;
if (!buff) return(FALSE);
@@ -801,11 +1005,12 @@
}
/* today's date */
- time (&secs);
- now = localtime (&secs);
+ gnc_time (&secs);
+ now = gnc_localtime (&secs);
now_day = now->tm_mday;
now_month = now->tm_mon + 1;
now_year = now->tm_year + 1900;
+ gnc_tm_free (now);
/* set defaults: if day or month appear to be blank, use today's date */
iday = now_day;
@@ -1014,11 +1219,11 @@
/* Make a guess */
gchar string[256];
struct tm tm;
- time_t secs;
+ gint64 secs;
gchar *s;
- secs = time(NULL);
- localtime_r(&secs, &tm);
+ secs = gnc_time (NULL);
+ gnc_localtime_r(&secs, &tm);
qof_strftime(string, sizeof(string), GNC_D_FMT, &tm);
for (s = string; s != '\0'; s++)
@@ -1200,6 +1405,7 @@
ts.tv_sec = 0;
ts.tv_nsec = 0;
+ memset (&stm, 0, sizeof (stm));
if (!str) return ts;
dupe = g_strdup(str);
stm.tm_year = atoi(str) - 1900;
@@ -1299,67 +1505,31 @@
}
}
- /* Note that mktime returns 'local seconds' which is the true time
+ /* Note that gnc_mktime returns 'local seconds' which is the true time
* minus the timezone offset. We don't want to work with local
* seconds, since they swim around acording to daylight savings, etc.
* We want to work with universal time. Thus, add an offset
- * to undo the damage that mktime causes.
+ * to undo the damage that gnc_mktime causes.
*/
{
struct tm tmp_tm;
struct tm tm;
long int tz;
int tz_hour;
- time_t secs;
+ gint64 secs;
- /* Use a temporary tm struct so the mktime call below
+ /* Use a temporary tm struct so the gnc_mktime call below
* doesn't mess up stm. */
tmp_tm = stm;
tmp_tm.tm_isdst = -1;
- secs = mktime (&tmp_tm);
-
- if (secs < 0)
- {
- /* Workaround buggy mktime implementations that get confused
- on the day daylight saving starts or ends. (OSX) */
- PWARN (" mktime failed to handle daylight saving: "
- "tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d for string=%s",
- stm.tm_hour, stm.tm_year, stm.tm_min,
- stm.tm_sec, stm.tm_isdst, dupe );
- tmp_tm.tm_hour++;
- secs = mktime (&tmp_tm);
- if (secs < 0)
- {
- /* if, for some strange reason, first attempt didn't fix it,
- try reversing the workaround. */
- tmp_tm.tm_hour -= 2;
- secs = mktime (&tmp_tm);
- }
- /* CAS: Even correct implementations of mktime can return
- (time_t)(-1): From the libc info page: "If the specified
- broken-down time cannot be represented as a simple time,
- `mktime' returns a value of `(time_t)(-1)' and does not modify
- the contents of BROKENTIME." This happens for dates after 2038
- when time_t is 32 bits. In those cases, this code above is
- just noisy and has a slight risk of returning the incorrect
- time.
- */
- if (secs < 0)
- {
- /* Seriously buggy mktime - give up. */
- PERR (" unable to recover from buggy mktime ");
- g_free(dupe);
- return ts;
- }
- }
-
- /* The call to localtime is 'bogus', but it forces 'timezone' to
+ secs = gnc_mktime (&tmp_tm);
+ /* The call to gnc_localtime is 'bogus', but it forces 'timezone' to
* be set. Note that we must use the accurate date, since the
* value of 'gnc_timezone' includes daylight savings corrections
* for that date. */
- localtime_r (&secs, &tm);
+ gnc_localtime_r (&secs, &tm);
tz = gnc_timezone (&tmp_tm);
@@ -1367,16 +1537,7 @@
stm.tm_hour -= tz_hour;
stm.tm_min -= (tz % 3600) / 60;
stm.tm_isdst = tmp_tm.tm_isdst;
- ts.tv_sec = mktime (&stm);
- if (ts.tv_sec < 0)
- {
- PWARN (" mktime failed to adjust calculated time:"
- " tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d",
- stm.tm_hour, stm.tm_year, stm.tm_min,
- stm.tm_sec, stm.tm_isdst );
- /* Try and make some sense of the result. */
- ts.tv_sec = secs - tz;
- }
+ ts.tv_sec = gnc_mktime (&stm);
ts.tv_nsec = nsec;
}
g_free(dupe);
@@ -1390,15 +1551,15 @@
gnc_timespec_to_iso8601_buff (Timespec ts, char * buff)
{
int len, tz_hour, tz_min;
- long int secs;
+ gint64 secs;
char cyn;
- time_t tmp;
+ gint64 tmp;
struct tm parsed;
g_return_val_if_fail (buff != NULL, NULL);
tmp = ts.tv_sec;
- localtime_r(&tmp, &parsed);
+ gnc_localtime_r(&tmp, &parsed);
secs = gnc_timezone (&parsed);
@@ -1436,8 +1597,8 @@
gnc_timespec2dmy (Timespec t, int *day, int *month, int *year)
{
struct tm result;
- time_t t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
- localtime_r(&t_secs, &result);
+ gint64 t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
+ gnc_localtime_r(&t_secs, &result);
if (day) *day = result.tm_mday;
if (month) *month = result.tm_mon + 1;
@@ -1454,23 +1615,7 @@
long long secs = 0;
long long era = 0;
- year -= 1900;
-
- /* make a crude attempt to deal with dates outside the range of Dec
- * 1901 to Jan 2038. Note we screw up centennial leap years here so
- * hack alert */
- if ((2 > year) || (136 < year))
- {
- era = year / 32;
- year %= 32;
- if (0 > year)
- {
- year += 32;
- era -= 1;
- }
- }
-
- date.tm_year = year;
+ date.tm_year = year - 1900;
date.tm_mon = month - 1;
date.tm_mday = day;
@@ -1480,10 +1625,8 @@
gnc_tm_set_day_end(&date);
/* compute number of seconds */
- secs = mktime (&date);
+ secs = gnc_mktime (&date);
- secs += era * THIRTY_TWO_YEARS;
-
result.tv_sec = secs;
result.tv_nsec = 0;
@@ -1542,7 +1685,7 @@
void
-timespecFromTime_t( Timespec *ts, time_t t )
+timespecFromTime_t( Timespec *ts, gint64 t )
{
ts->tv_sec = t;
ts->tv_nsec = 0;
@@ -1552,31 +1695,46 @@
timespec_now()
{
Timespec ts;
- ts.tv_sec = time(NULL);
+ ts.tv_sec = gnc_time(NULL);
ts.tv_nsec = 0;
return ts;
}
-time_t
+gint64
timespecToTime_t (Timespec ts)
{
return ts.tv_sec;
}
+/* The GDate setter functions all in the end use g_date_set_time_t,
+ * which in turn relies on localtime and is therefore subject to the
+ * 2038 bug.
+ */
GDate timespec_to_gdate (Timespec ts)
{
GDate result;
- g_date_clear(&result, 1);
- g_date_set_time_t(&result, timespecToTime_t(ts));
- g_assert(g_date_valid(&result));
+ gint day, month, year;
+
+ g_date_clear (&result, 1);
+ gnc_timespec2dmy (ts, &day, &month, &year);
+ g_date_set_dmy (&result, day, month, year);
+ g_assert(g_date_valid (&result));
+
return result;
}
GDate* gnc_g_date_new_today ()
{
- GDate *result = g_date_new();
- g_date_set_time_t(result, time(NULL));
- return result;
+ GDateTime *gdt = gnc_g_date_time_new_now_local ();
+ gint day, month, year;
+ GDate *result;
+
+ g_date_time_get_ymd (gdt, &day, &month, &year);
+ result = g_date_new_dmy (day, month, year);
+ g_date_time_unref (gdt);
+ g_assert(g_date_valid (result));
+
+ return result;
}
Timespec gdate_to_timespec (GDate d)
@@ -1587,39 +1745,43 @@
}
static void
-gnc_tm_get_day_start (struct tm *tm, time_t time_val)
+gnc_tm_get_day_start (struct tm *tm, gint64 time_val)
{
/* Get the equivalent time structure */
- if (!localtime_r(&time_val, tm))
+ if (!gnc_localtime_r(&time_val, tm))
return;
gnc_tm_set_day_start(tm);
}
static void
-gnc_tm_get_day_end (struct tm *tm, time_t time_val)
+gnc_tm_get_day_end (struct tm *tm, gint64 time_val)
{
/* Get the equivalent time structure */
- if (!localtime_r(&time_val, tm))
+ if (!gnc_localtime_r(&time_val, tm))
return;
gnc_tm_set_day_end(tm);
}
-time_t
-gnc_timet_get_day_start (time_t time_val)
+gint64
+gnc_timet_get_day_start (gint64 time_val)
{
struct tm tm;
+ gint64 new_time;
gnc_tm_get_day_start(&tm, time_val);
- return mktime(&tm);
+ new_time = gnc_mktime(&tm);
+ return new_time;
}
-time_t
-gnc_timet_get_day_end (time_t time_val)
+gint64
+gnc_timet_get_day_end (gint64 time_val)
{
struct tm tm;
+ gint64 new_time;
gnc_tm_get_day_end(&tm, time_val);
- return mktime(&tm);
+ new_time = gnc_mktime(&tm);
+ return new_time;
}
@@ -1637,22 +1799,22 @@
gnc_tm_get_day_end(tm, time(NULL));
}
-time_t
+gint64
gnc_timet_get_today_start (void)
{
struct tm tm;
gnc_tm_get_day_start(&tm, time(NULL));
- return mktime(&tm);
+ return gnc_mktime(&tm);
}
-time_t
+gint64
gnc_timet_get_today_end (void)
{
struct tm tm;
gnc_tm_get_day_end(&tm, time(NULL));
- return mktime(&tm);
+ return gnc_mktime(&tm);
}
void
Modified: gnucash/trunk/src/libqof/qof/gnc-date.h
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.h 2012-12-01 22:42:00 UTC (rev 22603)
+++ gnucash/trunk/src/libqof/qof/gnc-date.h 2012-12-01 22:42:10 UTC (rev 22604)
@@ -132,7 +132,85 @@
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.
+ */
+/** \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
+ * are seconds before that moment).
+ * \return A struct tm*, allocated on the heap. Must be freed with gnc_tm_free().
+ * The time is adjusted for the current local time zone.
+ */
+struct tm* gnc_localtime (const gint64 *secs);
+/** \brief fill out a time struct from a 64-bit time value adjusted for the current time zone.
+ * \param secs: Seconds since 00:00:01 UTC 01 January 1970 (negative values
+ * are seconds before that moment)
+ * \param time: A struct tm* for the function to fill.
+ * The time is adjusted for the current local time zone.
+ */
+struct tm* gnc_localtime_r (const gint64 *secs, struct tm* time);
+
+/** \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
+ * are seconds before that moment)
+ * \return A struct tm*, allocated on the heap. Must be freed with gnc_tm_free()
+ * The time is UTC.
+ */
+struct tm* gnc_gmtime (const gint64 *secs);
+
+/** \brief calculate seconds from the epoch given a time struct
+ * \param time: A struct tm* for the function to fill.
+ * The time is understood to be in the current local time zone.
+ * \return Seconds since 00:00:01 UTC 01 January 1970 (negative values
+ * are seconds before that moment).
+ */
+gint64 gnc_mktime (struct tm* time);
+
+/** \brief calculate seconds from the epoch given a time struct
+ * \param time: A struct tm* for the function to fill.
+ * The time is understood to be utc.
+ * \return Seconds since 00:00:01 UTC 01 January 1970 (negative values
+ * are seconds before that moment).
+ */
+gint64 gnc_timegm (struct tm* time);
+
+/** \brief Return a string representation of a date from a 64-bit time value
+ * \param secs: Seconds since 00:00:01 UTC 01 January 1970 (negative values
+ * are seconds before that moment)
+ * \return A string, which must be freed with g_free(), representing the date
+ * in the following format:
+ * Thu Nov 24 18:22:48 1986\n\0
+ * This is equivalent to the strftime format %a %b %H:%M:%S %Y.
+ */
+gchar* gnc_ctime (const gint64 *secs);
+
+/** \brief get the current local time
+ * \param A gint64* which, if not NULL, will be filled in with the same
+ * value as is returned.
+ * \return Seconds since 00:00:01 UTC 01 January 1970 (negative values
+ * are seconds before that moment)
+ */
+gint64 gnc_time (gint64 *tbuf);
+
+/** \brief Find the difference in seconds between two time values
+ * \param secs1: The first time value, in Seconds since
+ * 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
+ * \param secs2: The second time value, in Seconds since
+ * 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
+ * \return The difference in seconds between secs1 and secs2. If secs1 is
+ * later than secs2 the value will be negative.
+ */
+gdouble gnc_difftime (const gint64 secs1, const gint64 secs2);
+
+/** \brief free a struct tm* created with gnc_localtime() or gnc_gmtime()
+ * \param time: The struct tm* to be freed.
+ */
+void gnc_tm_free (struct tm* time);
+
/** \name String / DateFormat conversion. */
//@{
@@ -217,11 +295,11 @@
/** Returns the current clock time as a Timespec, taken from time(2). */
Timespec timespec_now (void);
-/** Turns a time_t into a Timespec */
-void timespecFromTime_t( Timespec *ts, time_t t );
+/** Turns a gint64 into a Timespec */
+void timespecFromTime_t( Timespec *ts, gint64 t );
-/** Turns a Timespec into a time_t */
-time_t timespecToTime_t (Timespec ts);
+/** Turns a Timespec into a gint64 */
+gint64 timespecToTime_t (Timespec ts);
/** Returns a newly allocated date of the current clock time, taken from
* time(2). The caller must g_date_free() the object afterwards. */
@@ -423,7 +501,7 @@
size_t qof_print_date_dmy_buff (gchar * buff, size_t buflen, int day, int month, int year);
/** Convenience: calls through to qof_print_date_dmy_buff(). **/
-size_t qof_print_date_buff (char * buff, size_t buflen, time_t secs);
+size_t qof_print_date_buff (char * buff, size_t buflen, gint64 secs);
/** Convenience; calls through to qof_print_date_dmy_buff(). **/
size_t qof_print_gdate(char *buf, size_t bufflen, const GDate *gd);
@@ -431,7 +509,7 @@
/** Convenience; calls through to qof_print_date_dmy_buff().
* Return: string, which should be freed when no longer needed.
* **/
-char * qof_print_date (time_t secs);
+char * qof_print_date (gint64 secs);
/** Convenience; calls through to qof_print_date_dmy_buff().
* Return: static global string.
@@ -448,7 +526,7 @@
* Returns the number of bytes printed.
*/
-size_t qof_print_date_time_buff (char * buff, size_t len, time_t secs);
+size_t qof_print_date_time_buff (char * buff, size_t len, gint64 secs);
/** qof_scan_date
* Convert a string into day / month / year integers according to
@@ -518,11 +596,11 @@
/** The gnc_timet_get_day_start() routine will take the given time in
* seconds and adjust it to the last second of that day. */
-time_t gnc_timet_get_day_start(time_t time_val);
+gint64 gnc_timet_get_day_start(gint64 time_val);
/** The gnc_timet_get_day_end() routine will take the given time in
* seconds and adjust it to the last second of that day. */
-time_t gnc_timet_get_day_end(time_t time_val);
+gint64 gnc_timet_get_day_end(gint64 time_val);
/** Get the numerical last date of the month. (28, 29, 30, 31) */
int gnc_date_get_last_mday (int month, int year);
@@ -541,13 +619,13 @@
* tm and fills it in with the last second of the today. */
void gnc_tm_get_today_end(struct tm *tm);
-/** The gnc_timet_get_today_start() routine returns a time_t value
+/** The gnc_timet_get_today_start() routine returns a gint64 value
* corresponding to the first second of today. */
-time_t gnc_timet_get_today_start(void);
+gint64 gnc_timet_get_today_start(void);
-/** The gnc_timet_get_today_end() routine returns a time_t value
+/** The gnc_timet_get_today_end() routine returns a gint64 value
* corresponding to the last second of today. */
-time_t gnc_timet_get_today_end(void);
+gint64 gnc_timet_get_today_end(void);
/** The xaccDateUtilGetStampNow() routine returns the current time in
* seconds in textual format.
Modified: gnucash/trunk/src/libqof/qof/test/test-gnc-date.c
===================================================================
--- gnucash/trunk/src/libqof/qof/test/test-gnc-date.c 2012-12-01 22:42:00 UTC (rev 22603)
+++ gnucash/trunk/src/libqof/qof/test/test-gnc-date.c 2012-12-01 22:42:10 UTC (rev 22604)
@@ -35,12 +35,6 @@
# include "strptime.h"
#endif
-#ifdef G_OS_WIN32
-# define MSWIN TRUE
-#else
-# define MSWIN FALSE
-#endif
-
static const gchar *suitename = "/qof/gnc-date";
void test_suite_gnc_date ( void );
@@ -57,6 +51,266 @@
static _GncDateTime gncdt;
extern void _gnc_date_time_init (_GncDateTime *);
+/* gnc_localtime just creates a tm on the heap and calls
+ * gnc_localtime_r with it, so this suffices to test both.
+ */
+static void
+test_gnc_localtime (void)
+{
+ gint64 secs[5] = {-43238956734LL, -1123692LL, 432761LL,
+ 723349832LL, 887326459367LL};
+ guint ind;
+ gchar *msg = "gnc_localtime_r: assertion `gdt != NULL' failed";
+ gint loglevel = G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL;
+ gchar *logdomain = "qof";
+ TestErrorStruct check = {loglevel, logdomain, msg, 0};
+ GLogFunc hdlr = g_log_set_default_handler ((GLogFunc)test_null_handler, &check);
+ g_test_log_set_fatal_handler ((GTestLogFatalFunc)test_checked_handler, &check);
+
+ for (ind = 0; ind < G_N_ELEMENTS (secs); ind++)
+ {
+ struct tm* time = gnc_localtime (&secs[ind]);
+ GDateTime *gdt = gncdt.new_from_unix_local (secs[ind]);
+ if (gdt == NULL)
+ {
+ g_assert (time == NULL);
+ continue;
+ }
+ g_assert_cmpint (time->tm_year + 1900, ==, g_date_time_get_year (gdt));
+ g_assert_cmpint (time->tm_mon + 1, ==, g_date_time_get_month (gdt));
+ g_assert_cmpint (time->tm_mday, ==, g_date_time_get_day_of_month (gdt));
+ g_assert_cmpint (time->tm_hour, ==, g_date_time_get_hour (gdt));
+ g_assert_cmpint (time->tm_min, ==, g_date_time_get_minute (gdt));
+ g_assert_cmpint (time->tm_sec, ==, g_date_time_get_second (gdt));
+ g_assert_cmpint (time->tm_wday, ==, g_date_time_get_day_of_week (gdt));
+ g_assert_cmpint (time->tm_yday, ==, g_date_time_get_day_of_year (gdt));
+ if (g_date_time_is_daylight_savings (gdt))
+ g_assert_cmpint (time->tm_isdst, ==, 1);
+ else
+ g_assert_cmpint (time->tm_isdst, ==, 0);
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ g_assert_cmpint (time->tm_gmtoff, ==, - timezone);
+ g_assert_cmpint (time->tm_gmtoff, ==,
+ g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND);
+#endif
+ g_date_time_unref (gdt);
+ gnc_tm_free (time);
+ }
+ g_assert_cmpint (check.hits, ==, 1);
+ g_log_set_default_handler (hdlr, NULL);
+}
+
+static void
+test_gnc_gmtime (void)
+{
+ gint64 secs[6] = {-43238956734LL, -1123692LL, 432761LL,
+ 723349832LL, 887326459367LL, 1175964426LL};
+ struct tm answers[6] = {
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ { 6, 41, 2, 24, 9, -1301, 4, 297, 0, 0, NULL },
+ { 48, 51, 23, 18, 11, 69, 4, 352, 0, 0, NULL },
+ { 41, 12, 0, 6, 0, 70, 2, 6, 0, 0, NULL },
+ { 32, 30, 2, 3, 11, 92, 4, 338, 0, 0, NULL },
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
+ { 6, 47, 16, 7, 3, 107, 6, 97, 0, 0, NULL },
+#else
+ { 6, 41, 2, 24, 9, -1301, 4, 297, 0 },
+ { 48, 51, 23, 18, 11, 69, 4, 352, 0 },
+ { 41, 12, 0, 6, 0, 70, 2, 6, 0 },
+ { 32, 30, 2, 3, 11, 92, 4, 338, 0 },
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+ { 6, 47, 16, 7, 3, 107, 6, 97, 0 },
+#endif
+ };
+ guint ind;
+ gchar *msg = "gnc_gmtime: assertion `gdt != NULL' failed";
+ gint loglevel = G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL;
+ gchar *logdomain = "qof";
+ TestErrorStruct check = {loglevel, logdomain, msg, 0};
+ GLogFunc hdlr = g_log_set_default_handler ((GLogFunc)test_null_handler, &check);
+ g_test_log_set_fatal_handler ((GTestLogFatalFunc)test_checked_handler, &check);
+
+ for (ind = 0; ind < G_N_ELEMENTS (secs); ind++)
+ {
+ struct tm* time = gnc_gmtime (&secs[ind]);
+ GDateTime *gdt = g_date_time_new_from_unix_utc (secs[ind]);
+ if (gdt == NULL)
+ {
+ g_assert (time == NULL);
+ continue;
+ }
+ g_assert_cmpint (time->tm_year, ==, answers[ind].tm_year);
+ g_assert_cmpint (time->tm_mon, ==, answers[ind].tm_mon);
+ g_assert_cmpint (time->tm_mday, ==, answers[ind].tm_mday);
+ g_assert_cmpint (time->tm_hour, ==, answers[ind].tm_hour);
+ g_assert_cmpint (time->tm_min, ==, answers[ind].tm_min);
+ g_assert_cmpint (time->tm_sec, ==, answers[ind].tm_sec);
+ g_assert_cmpint (time->tm_wday, ==, answers[ind].tm_wday);
+ g_assert_cmpint (time->tm_yday, ==, answers[ind].tm_yday);
+ g_assert_cmpint (time->tm_isdst, ==, 0);
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ g_assert_cmpint (time->tm_gmtoff, ==, 0);
+#endif
+ g_date_time_unref (gdt);
+ gnc_tm_free (time);
+ }
+ g_assert_cmpint (check.hits, ==, 1);
+ g_log_set_default_handler (hdlr, NULL);
+}
+
+static void
+test_gnc_mktime (void)
+{
+ struct {
+ gint64 secs;
+ gint wday;
+ gint yday;
+ } ans[5] = {
+ { -43238956734LL, 4, 297 },
+ { -1123692LL, 4, 352 },
+ { 432761LL, 2, 6 },
+ { 723349832LL, 4, 338 },
+ { 1175964426LL, 6, 97 }
+ };
+
+ struct tm time[5] = {
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ { 6, 41, 2, 24, 9, -1301, 0, 0, -1, 0, NULL },
+ { 48, 51, 23, 18, 11, 69, 0, 0, -1, 0, NULL },
+ { 41, 12, 0, 6, 0, 70, 0, 0, -1, 0, NULL },
+ { 32, 30, 2, 3, 11, 92, 0, 0, -1, 0, NULL },
+ { 6, 47, 16, 7, 3, 107, 0, 0, -1, 0, NULL },
+#else
+ { 6, 41, 2, 24, 9, -1301, 0, 0, -1 },
+ { 48, 51, 23, 18, 11, 69, 0, 0, -1 },
+ { 41, 12, 0, 6, 0, 70, 0, 0, -1 },
+ { 32, 30, 2, 3, 11, 92, 0, 0, -1 },
+ { 6, 47, 16, 7, 3, 107, 0, 0, -1 },
+#endif
+ };
+ guint ind;
+
+ for (ind = 0; ind < G_N_ELEMENTS (time); ind++)
+ {
+ gint64 secs = gnc_mktime (&time[ind]);
+ GDateTime *gdt = gncdt.new_local (time[ind].tm_year + 1900,
+ time[ind].tm_mon + 1,
+ time[ind].tm_mday,
+ time[ind].tm_hour,
+ time[ind].tm_min,
+ (gdouble)time[ind].tm_sec);
+ gint64 offset = g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND;
+ g_assert_cmpint (secs, ==, ans[ind].secs - offset);
+ g_assert_cmpint (time[ind].tm_wday, ==, ans[ind].wday);
+ g_assert_cmpint (time[ind].tm_yday, ==, ans[ind].yday);
+ if (g_date_time_is_daylight_savings (gdt))
+ g_assert_cmpint (time[ind].tm_isdst, ==, 1);
+ else
+ g_assert_cmpint (time[ind].tm_isdst, ==, 0);
+
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ g_assert_cmpint (time[ind].tm_gmtoff, ==, offset);
+#endif
+ g_date_time_unref (gdt);
+ }
+}
+
+/* In addition to computing a time offset from a struct tm, mktime is
+ * supposed to normalize struct tms with out-of-range values. This
+ * second test exercises that facility in gnc_mktime.
+ */
+static void
+test_gnc_mktime_normalization (void)
+{
+ struct answer {
+ gint64 secs;
+ gint wday;
+ gint yday;
+ } ans = { 723349832LL, 4, 338 };
+
+ struct tm normal_time =
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ { 32, 30, 2, 3, 11, 92, 0, 0, -1, 0, NULL };
+#else
+ { 32, 30, 2, 3, 11, 92, 0, 0, -1 };
+#endif
+
+ struct tm time[4] = {
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ { 92, -31, 27, -29, 24, 91, 0, 0, -1, 0, NULL },
+ { -28, 91, -47, 35, -2, 93, 0, 0, -1, 0, NULL },
+ { -28, 91, -47, 66, -3, 93, 0, 0, -1, 0, NULL },
+ { -28, 91, -47, 35, -26, 95, 0, 0, -1, 0, NULL },
+#else
+ { 92, -31, 27, -29, 24, 91, 0, 0, -1 },
+ { -28, 91, -47, 35, -2, 93, 0, 0, -1 },
+ { -28, 91, -47, 66, -3, 93, 0, 0, -1 },
+ { -28, 91, -47, 35, -26, 95, 0, 0, -1 },
+#endif
+ };
+ guint ind;
+ for (ind = 0; ind < G_N_ELEMENTS (time); ind++)
+ {
+ gint64 secs = gnc_mktime (&time[ind]);
+ GDateTime *gdt = gncdt.new_local (time[ind].tm_year + 1900,
+ time[ind].tm_mon + 1,
+ time[ind].tm_mday,
+ time[ind].tm_hour,
+ time[ind].tm_min,
+ (gdouble)time[ind].tm_sec);
+ gint64 offset = g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND;
+ g_assert_cmpfloat (time[ind].tm_sec, ==, normal_time.tm_sec);
+ g_assert_cmpint (time[ind].tm_min, ==, normal_time.tm_min);
+ g_assert_cmpint (time[ind].tm_hour, ==, normal_time.tm_hour);
+ g_assert_cmpint (time[ind].tm_mday, ==, normal_time.tm_mday);
+ g_assert_cmpint (time[ind].tm_mon, ==, normal_time.tm_mon);
+ g_assert_cmpint (time[ind].tm_year, ==, normal_time.tm_year);
+ g_assert_cmpint (secs, ==, ans.secs - offset);
+ g_assert_cmpint (time[ind].tm_wday, ==, ans.wday);
+ g_assert_cmpint (time[ind].tm_yday, ==, ans.yday);
+ if (g_date_time_is_daylight_savings (gdt))
+ g_assert_cmpint (time[ind].tm_isdst, ==, 1);
+ else
+ g_assert_cmpint (time[ind].tm_isdst, ==, 0);
+#ifdef HAVE_STRUCT_TM_GMTOFF
+ g_assert_cmpint (time[ind].tm_gmtoff, ==, offset);
+#endif
+ g_date_time_unref (gdt);
+ }
+}
+
+static void
+test_gnc_ctime (void)
+{
+ gint64 secs[5] = {-43238956734LL, -1123692LL, 432761LL,
+ 723349832LL, 1175964426LL};
+ guint ind;
+ for (ind = 0; ind < G_N_ELEMENTS (secs); ind++)
+ {
+ GDateTime *gdt = gncdt.new_from_unix_local (secs[ind]);
+ gchar* datestr = gnc_ctime (&secs[ind]);
+ g_assert_cmpstr (datestr, ==,
+ g_date_time_format (gdt, "%a %b %H:%M:%S %Y"));
+ g_date_time_unref (gdt);
+ g_free (datestr);
+ }
+}
+
+static void
+test_gnc_time (void)
+{
+ gint64 secs1, secs2;
+ GDateTime *gdt;
+ secs1 = gnc_time (NULL);
+ secs1 = gnc_time (&secs2);
+ gdt = gncdt.new_now_local ();
+ g_assert_cmpint (secs1, ==, secs2);
+ g_assert_cmpint (secs1, ==, g_date_time_to_unix (gdt));
+ g_date_time_unref (gdt);
+}
+
+/* gnc_difftime and gnc_tm_free are just too simple to bother testing. */
+
/* gnc_date_dateformat_to_string
const char *gnc_default_strftime_date_format =
const char*
@@ -417,13 +671,9 @@
g_assert_cmpint (n0.tv_sec, ==, r0.tv_sec);
g_assert_cmpint (na.tv_sec, ==, ra.tv_sec);
g_assert_cmpint (nb.tv_sec, ==, rb.tv_sec);
- if (sizeof (time_t) > 4)
- g_assert_cmpint (nc.tv_sec, ==, rc.tv_sec + 3600);
- else
-/* We'd like to just test that rc.tv_sec < 0, but mingw sets rc to the
- * value of rb. */
- g_assert_cmpint (nc.tv_sec, !=, rc.tv_sec);
+ g_assert_cmpint (nc.tv_sec, ==, rc.tv_sec);
}
+
/* gnc_date_my_last_mday
int gnc_date_my_last_mday (int month, int year)// C: 1 Local: 1:0:0
*/
@@ -525,7 +775,7 @@
static void tm_set_dmy (struct tm *tm, gint year, gint month, gint mday)
{
tm->tm_year = year - 1900;
- tm->tm_mon = month;
+ tm->tm_mon = month - 1;
tm->tm_mday = mday;
}
@@ -535,7 +785,7 @@
gchar buff[MAX_DATE_LENGTH], t_buff[MAX_DATE_LENGTH];
gchar *locale = g_strdup (setlocale (LC_TIME, NULL));
struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0
-#ifndef G_OS_WIN32
+#ifdef HAVE_STRUCT_TM_GMTOFF
, 0, 0
#endif
};
@@ -611,10 +861,8 @@
g_assert_cmpint (qof_print_date_dmy_buff (buff, sizeof (buff), tm.tm_mday,
tm.tm_mon + 1, tm.tm_year + 1900),
==, strlen (buff));
- if (sizeof (time_t) > 4 || MSWIN)
- g_assert_cmpstr (buff, ==, t_buff);
- else
- g_assert_cmpstr (buff, !=, t_buff);
+ g_assert_cmpstr (buff, ==, t_buff);
+
test_gnc_setlocale (LC_TIME, "en_GB");
tm_set_dmy (&tm, 1974, 11, 23);
strftime(t_buff, MAX_DATE_LENGTH, GNC_D_FMT, &tm);
@@ -637,10 +885,7 @@
g_assert_cmpint (qof_print_date_dmy_buff (buff, sizeof (buff), tm.tm_mday,
tm.tm_mon + 1, tm.tm_year + 1900),
==, strlen (buff));
- if (sizeof (time_t) > 4 || MSWIN)
- g_assert_cmpstr (buff, ==, t_buff);
- else
- g_assert_cmpstr (buff, !=, t_buff);
+ g_assert_cmpstr (buff, ==, t_buff);
test_gnc_setlocale (LC_TIME, "fr_FR");
tm_set_dmy (&tm, 1974, 11, 23);
@@ -664,10 +909,7 @@
g_assert_cmpint (qof_print_date_dmy_buff (buff, sizeof (buff), tm.tm_mday,
tm.tm_mon + 1, tm.tm_year + 1900),
==, strlen (buff));
- if (sizeof (time_t) > 4 || MSWIN)
- g_assert_cmpstr (buff, ==, t_buff);
- else
- g_assert_cmpstr (buff, !=, t_buff);
+ g_assert_cmpstr (buff, ==, t_buff);
setlocale (LC_TIME, locale);
g_free (locale);
@@ -686,8 +928,9 @@
#define test_assert_localized_timestring(time, datestr) \
{ \
gchar t_buff[MAX_DATE_LENGTH]; \
- struct tm *ltime = localtime ((time_t*)(&time)); \
+ struct tm *ltime = gnc_localtime ((gint64 *)(&time)); \
strftime (t_buff, sizeof (t_buff), GNC_D_FMT, ltime); \
+ gnc_tm_free (ltime); \
g_assert_cmpstr (datestr, ==, t_buff); \
}
@@ -711,165 +954,107 @@
qof_date_format_set (QOF_DATE_FORMAT_UK);
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, "23/11/1974");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- /* MinGW's localtime_r returns NULL if time is outside of the
- * range 31DEC1969 - 19JAN2038 */
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, "02/02/1961");
+ g_assert_cmpstr (buff, ==, "02/02/1961");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, "16/06/2045");
- else
- g_assert_cmpstr (buff, ==, "11/05/1909");
+ g_assert_cmpstr (buff, ==, "16/06/2045");
qof_date_format_set (QOF_DATE_FORMAT_CE);
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, "23.11.1974");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, "02.02.1961");
+ g_assert_cmpstr (buff, ==, "02.02.1961");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, "16.06.2045");
- else
- g_assert_cmpstr (buff, ==, "11.05.1909");
+ g_assert_cmpstr (buff, ==, "16.06.2045");
qof_date_format_set (QOF_DATE_FORMAT_US);
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, "11/23/1974");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, "02/02/1961");
+ g_assert_cmpstr (buff, ==, "02/02/1961");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, "06/16/2045");
- else
- g_assert_cmpstr (buff, ==, "05/11/1909");
+ g_assert_cmpstr (buff, ==, "06/16/2045");
qof_date_format_set (QOF_DATE_FORMAT_ISO);
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, "1974-11-23");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, "1961-02-02");
+ g_assert_cmpstr (buff, ==, "1961-02-02");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, "2045-06-16");
- else
- g_assert_cmpstr (buff, ==, "1909-05-11");
+ g_assert_cmpstr (buff, ==, "2045-06-16");
qof_date_format_set (QOF_DATE_FORMAT_LOCALE);
test_gnc_setlocale (LC_TIME, "en_US");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, g_date_time_format (gd1, GNC_D_FMT));
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, g_date_time_format (gd2, GNC_D_FMT));
+ g_assert_cmpstr (buff, ==, g_date_time_format (gd2, GNC_D_FMT));
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, g_date_time_format (gd3, GNC_D_FMT));
- else
- test_assert_localized_timestring (tm3, buff)
+ g_assert_cmpstr (buff, ==, g_date_time_format (gd3, GNC_D_FMT));
test_gnc_setlocale (LC_TIME, "en_GB");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, g_date_time_format (gd1, GNC_D_FMT));
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, g_date_time_format (gd2, GNC_D_FMT));
+ g_assert_cmpstr (buff, ==, g_date_time_format (gd2, GNC_D_FMT));
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, g_date_time_format (gd3, GNC_D_FMT));
- else
- test_assert_localized_timestring (tm3, buff)
+ g_assert_cmpstr (buff, ==, g_date_time_format (gd3, GNC_D_FMT));
test_gnc_setlocale (LC_TIME, "fr_FR");
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm1),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm1),
==, strlen (buff));
g_assert_cmpstr (buff, ==, g_date_time_format (gd1, GNC_D_FMT));
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm2),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm2),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else
- g_assert_cmpstr (buff, ==, g_date_time_format (gd2, GNC_D_FMT));
+ g_assert_cmpstr (buff, ==, g_date_time_format (gd2, GNC_D_FMT));
memset ((gpointer)buff, 0, sizeof (buff));
- g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), (time_t)tm3),
+ g_assert_cmpint (qof_print_date_buff (buff, sizeof (buff), tm3),
==, strlen (buff));
- if (MSWIN)
- g_assert_cmpstr (buff, ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, g_date_time_format (gd3, GNC_D_FMT));
- else
- test_assert_localized_timestring (tm3, buff)
+ g_assert_cmpstr (buff, ==, g_date_time_format (gd3, GNC_D_FMT));
setlocale (LC_TIME, locale);
g_free (locale);
@@ -964,19 +1149,8 @@
memset ((gpointer)buff, 0, sizeof (buff));
g_assert_cmpint (qof_print_gdate (buff, sizeof (buff), gd3),
==, strlen (buff));
- if (sizeof (time_t) > 4 || MSWIN)
- {
- g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
- g_assert_cmpstr (buff, ==, t_buff);
- }
- else
- {
- struct tm tm;
- time_t time;
- g_date_to_struct_tm (gd3, &tm);
- time = mktime (&tm);
- test_assert_localized_timestring (time, buff);
- }
+ g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
+ g_assert_cmpstr (buff, ==, t_buff);
test_gnc_setlocale (LC_TIME, "en_GB");
memset ((gpointer)buff, 0, sizeof (buff));
@@ -992,20 +1166,10 @@
memset ((gpointer)buff, 0, sizeof (buff));
g_assert_cmpint (qof_print_gdate (buff, sizeof (buff), gd3),
==, strlen (buff));
- if (sizeof (time_t) > 4 || MSWIN)
- {
- g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
- g_assert_cmpstr (buff, ==, t_buff);
- }
- else
- {
- struct tm tm;
- time_t time;
- g_date_to_struct_tm (gd3, &tm);
- time = mktime (&tm);
- test_assert_localized_timestring (time, buff);
- }
+ g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
+ g_assert_cmpstr (buff, ==, t_buff);
+
test_gnc_setlocale (LC_TIME, "fr_FR");
memset ((gpointer)buff, 0, sizeof (buff));
g_assert_cmpint (qof_print_gdate (buff, sizeof (buff), gd1),
@@ -1020,19 +1184,8 @@
memset ((gpointer)buff, 0, sizeof (buff));
g_assert_cmpint (qof_print_gdate (buff, sizeof (buff), gd3),
==, strlen (buff));
- if (sizeof (time_t) > 4 || MSWIN)
- {
- g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
- g_assert_cmpstr (buff, ==, t_buff);
- }
- else
- {
- struct tm tm;
- time_t time;
- g_date_to_struct_tm (gd3, &tm);
- time = mktime (&tm);
- test_assert_localized_timestring (time, buff);
- }
+ g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
+ g_assert_cmpstr (buff, ==, t_buff);
setlocale (LC_TIME, locale);
g_free (locale);
@@ -1051,10 +1204,7 @@
#define test_assert_qof_print_date_outside_range(time, datestr) \
{ \
gchar *buf = qof_print_date (time); \
- if (MSWIN) \
- g_assert_cmpstr (buf, ==, ""); \
- else \
- g_assert_cmpstr (buf, ==, datestr); \
+ g_assert_cmpstr (buf, ==, datestr); \
g_free (buf); \
}
@@ -1083,87 +1233,49 @@
gint64 tm3 = g_date_time_to_unix (gd3);
qof_date_format_set (QOF_DATE_FORMAT_UK);
- test_assert_qof_print_date ((time_t)tm1, "23/11/1974");
- test_assert_qof_print_date_outside_range ((time_t)tm2, "02/02/1961");
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3, "16/06/2045");
- }
- else
- test_assert_qof_print_date ((time_t)tm3, "11/05/1909");
+ test_assert_qof_print_date (tm1, "23/11/1974");
+ test_assert_qof_print_date_outside_range (tm2, "02/02/1961");
+ test_assert_qof_print_date_outside_range (tm3, "16/06/2045");
qof_date_format_set (QOF_DATE_FORMAT_CE);
- test_assert_qof_print_date ((time_t)tm1, "23.11.1974");
- test_assert_qof_print_date_outside_range ((time_t)tm2, "02.02.1961");
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3, "16.06.2045");
- }
- else
- test_assert_qof_print_date ((time_t)tm3, "11.05.1909");
+ test_assert_qof_print_date (tm1, "23.11.1974");
+ test_assert_qof_print_date_outside_range (tm2, "02.02.1961");
+ test_assert_qof_print_date_outside_range (tm3, "16.06.2045");
-
qof_date_format_set (QOF_DATE_FORMAT_US);
- test_assert_qof_print_date ((time_t)tm1, "11/23/1974");
- test_assert_qof_print_date_outside_range ((time_t)tm2, "02/02/1961");
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3, "06/16/2045");
- }
- else
- test_assert_qof_print_date ((time_t)tm3, "05/11/1909");
+ test_assert_qof_print_date (tm1, "11/23/1974");
+ test_assert_qof_print_date_outside_range (tm2, "02/02/1961");
+ test_assert_qof_print_date_outside_range (tm3, "06/16/2045");
-
qof_date_format_set (QOF_DATE_FORMAT_ISO);
- test_assert_qof_print_date ((time_t)tm1, "1974-11-23");
- test_assert_qof_print_date_outside_range ((time_t)tm2, "1961-02-02");
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3, "2045-06-16");
- }
- else
- test_assert_qof_print_date ((time_t)tm3, "1909-05-11");
+ test_assert_qof_print_date (tm1, "1974-11-23");
+ test_assert_qof_print_date_outside_range (tm2, "1961-02-02");
+ test_assert_qof_print_date_outside_range (tm3, "2045-06-16");
-
qof_date_format_set (QOF_DATE_FORMAT_LOCALE);
test_gnc_setlocale (LC_TIME, "en_US");
- test_assert_qof_print_date ((time_t)tm1,
+ test_assert_qof_print_date (tm1,
g_date_time_format (gd1, GNC_D_FMT));
- test_assert_qof_print_date_outside_range ((time_t)tm2,
+ test_assert_qof_print_date_outside_range (tm2,
g_date_time_format (gd2, GNC_D_FMT));
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3,
+ test_assert_qof_print_date_outside_range (tm3,
g_date_time_format (gd3, GNC_D_FMT));
- }
- else
- test_assert_localized_qof_print_date (tm3);
test_gnc_setlocale (LC_TIME, "en_GB");
- test_assert_qof_print_date ((time_t)tm1,
+ test_assert_qof_print_date (tm1,
g_date_time_format (gd1, GNC_D_FMT));
- test_assert_qof_print_date_outside_range ((time_t)tm2,
+ test_assert_qof_print_date_outside_range (tm2,
g_date_time_format (gd2, GNC_D_FMT));
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3,
+ test_assert_qof_print_date_outside_range (tm3,
g_date_time_format (gd3, GNC_D_FMT));
- }
- else
- test_assert_localized_qof_print_date (tm3);
test_gnc_setlocale (LC_TIME, "fr_FR");
- test_assert_qof_print_date ((time_t)tm1,
+ test_assert_qof_print_date (tm1,
g_date_time_format (gd1, GNC_D_FMT));
- test_assert_qof_print_date_outside_range ((time_t)tm2,
+ test_assert_qof_print_date_outside_range (tm2,
g_date_time_format (gd2, GNC_D_FMT));
- if (sizeof (time_t) > 4 || MSWIN)
- {
- test_assert_qof_print_date_outside_range ((time_t)tm3,
+ test_assert_qof_print_date_outside_range (tm3,
g_date_time_format (gd3, GNC_D_FMT));
- }
- else
- test_assert_localized_qof_print_date (tm3);
setlocale (LC_TIME, locale);
g_free (locale);
@@ -1190,110 +1302,48 @@
qof_date_format_set (QOF_DATE_FORMAT_UK);
g_assert_cmpstr (gnc_print_date (tm1), ==, "23/11/1974");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, "02/02/1961");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "16/06/2045");
- else
- g_assert_cmpstr (gnc_print_date (tm3), ==, "10/05/1909");
+ g_assert_cmpstr (gnc_print_date (tm2), ==, "02/02/1961");
+ g_assert_cmpstr (gnc_print_date (tm3), ==, "16/06/2045");
qof_date_format_set (QOF_DATE_FORMAT_CE);
g_assert_cmpstr (gnc_print_date (tm1), ==, "23.11.1974");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, "02.02.1961");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "16.06.2045");
- else
- g_assert_cmpstr (gnc_print_date (tm3), ==, "10.05.1909");
+ g_assert_cmpstr (gnc_print_date (tm2), ==, "02.02.1961");
+ g_assert_cmpstr (gnc_print_date (tm3), ==, "16.06.2045");
qof_date_format_set (QOF_DATE_FORMAT_US);
g_assert_cmpstr (gnc_print_date (tm1), ==, "11/23/1974");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, "02/02/1961");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "06/16/2045");
- else
- g_assert_cmpstr (gnc_print_date (tm3), ==, "05/10/1909");
+ g_assert_cmpstr (gnc_print_date (tm2), ==, "02/02/1961");
+ g_assert_cmpstr (gnc_print_date (tm3), ==, "06/16/2045");
qof_date_format_set (QOF_DATE_FORMAT_ISO);
g_assert_cmpstr (gnc_print_date (tm1), ==, "1974-11-23");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, "1961-02-02");
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "2045-06-16");
- else
- g_assert_cmpstr (gnc_print_date (tm3), ==, "1909-05-10");
+ g_assert_cmpstr (gnc_print_date (tm2), ==, "1961-02-02");
+ g_assert_cmpstr (gnc_print_date (tm3), ==, "2045-06-16");
qof_date_format_set (QOF_DATE_FORMAT_LOCALE);
test_gnc_setlocale (LC_TIME, "en_US");
g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd1);
g_assert_cmpstr (gnc_print_date (tm1), ==, t_buff);
g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd2);
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, t_buff);
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- {
- g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
- g_assert_cmpstr (gnc_print_date (tm3), ==, t_buff);
- }
- else
- test_assert_localized_timestring (tm3, gnc_print_date (tm3))
+ g_assert_cmpstr (gnc_print_date (tm2), ==, t_buff);
+ g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
+ g_assert_cmpstr (gnc_print_date (tm3), ==, t_buff);
test_gnc_setlocale (LC_TIME, "en_GB");
g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd1);
g_assert_cmpstr (gnc_print_date (tm1), ==, t_buff);
g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd2);
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, t_buff);
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- {
- g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
- g_assert_cmpstr (gnc_print_date (tm3), ==, t_buff);
- }
- else
- test_assert_localized_timestring (tm3, gnc_print_date (tm3))
+ g_assert_cmpstr (gnc_print_date (tm2), ==, t_buff);
+ g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
+ g_assert_cmpstr (gnc_print_date (tm3), ==, t_buff);
test_gnc_setlocale (LC_TIME, "fr_FR");
g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd1);
g_assert_cmpstr (gnc_print_date (tm1), ==, t_buff);
g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd2);
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm2), ==, "");
- else
- g_assert_cmpstr (gnc_print_date (tm2), ==, t_buff);
- if (MSWIN)
- g_assert_cmpstr (gnc_print_date (tm3), ==, "");
- else if (sizeof (time_t) > 4)
- {
- g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
- g_assert_cmpstr (gnc_print_date (tm3), ==, t_buff);
- }
- else
- test_assert_localized_timestring (tm3, gnc_print_date (tm3))
+ g_assert_cmpstr (gnc_print_date (tm2), ==, t_buff);
+ g_date_strftime (t_buff, MAX_DATE_LENGTH, GNC_D_FMT, gd3);
+ g_assert_cmpstr (gnc_print_date (tm3), ==, t_buff);
setlocale (LC_TIME, locale);
g_free (locale);
@@ -1544,24 +1594,8 @@
GDateTime *gdt4 = g_date_time_new (tz05, 1961, 9, 22, 17, 53, 19.0);
GDateTime *gdt5 = g_date_time_new (tz05, 2061, 1, 25, 23, 21, 19.0);
- gchar *msgfmt = "[gnc_iso8601_to_timespec_gmt()] mktime failed to handle daylight saving: tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d for string=%s";
- gchar *logdomain = "qof.engine";
- gint loglevel1 = G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL;
- gint loglevel2 = G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL;
- gchar msg1[256];
- gchar *msg2 = "[gnc_iso8601_to_timespec_gmt()] unable to recover from buggy mktime ";
- TestErrorStruct check1 = { loglevel1, logdomain, msg1, 0 };
- TestErrorStruct check2 = { loglevel2, logdomain, msg2, 0 };
- guint hdlr1 = g_log_set_handler (logdomain, loglevel1,
- (GLogFunc)test_checked_handler, &check1);
- guint hdlr2 = g_log_set_handler (logdomain, loglevel2,
- (GLogFunc)test_checked_handler, &check2);
Timespec t;
- memset (msg1, 0, sizeof msg1);
- test_add_error (&check1);
- test_add_error (&check2);
-
t = gnc_iso8601_to_timespec_gmt (NULL);
g_assert_cmpint (t.tv_sec, ==, 0);
g_assert_cmpint (t.tv_nsec, ==, 0);
@@ -1587,34 +1621,13 @@
g_assert_cmpint (t.tv_sec, ==, g_date_time_to_unix (gdt3));
g_assert_cmpint (t.tv_nsec, ==, get_nanoseconds (gdt3));
- g_sprintf (msg1, msgfmt, 22, 61, 53, 19, -1, "1961-09-22 17:53:19 -05");
- g_test_log_set_fatal_handler ((GTestLogFatalFunc)test_null_handler, NULL);
t = gnc_iso8601_to_timespec_gmt ("1961-09-22 17:53:19 -05");
-/* gnc_iso8601_to_timespec_gmt has a pretty bogus test for mktime
- * implementations that return -1 when they have an error. As a
- * result, it can't handle dates before the beginning of the era.
g_assert_cmpint (t.tv_sec, ==, g_date_time_to_unix (gdt4));
g_assert_cmpint (t.tv_nsec, ==, get_nanoseconds (gdt4));
-*/
- g_assert_cmpint (t.tv_sec, ==, 0);
- g_assert_cmpint (t.tv_nsec, ==, 0);
- g_assert_cmpint (check1.hits, ==, 1);
- g_assert_cmpint (check2.hits, ==, 1);
- g_sprintf (msg1, msgfmt, 28, 161, 21, 19, -1, "2061-01-25 23:21:19.0 -05:00");
t = gnc_iso8601_to_timespec_gmt ("2061-01-25 23:21:19.0 -05:00");
- if (sizeof (time_t) > 4)
- {
- g_assert_cmpint (t.tv_sec, ==, g_date_time_to_unix (gdt5));
- g_assert_cmpint (t.tv_nsec, ==, get_nanoseconds (gdt5));
- }
- else
- {
- g_assert_cmpint (t.tv_sec, ==, 0);
- g_assert_cmpint (t.tv_nsec, ==, 0);
- g_assert_cmpint (check1.hits, ==, 2);
- g_assert_cmpint (check2.hits, ==, 2);
- }
+ g_assert_cmpint (t.tv_sec, ==, g_date_time_to_unix (gdt5));
+ g_assert_cmpint (t.tv_nsec, ==, get_nanoseconds (gdt5));
g_date_time_unref (gdt1);
g_date_time_unref (gdt2);
@@ -1624,8 +1637,6 @@
g_time_zone_unref (zulu);
g_time_zone_unref (tz05);
g_time_zone_unref (tz0840);
- g_log_remove_handler (logdomain, hdlr1);
- g_log_remove_handler (logdomain, hdlr2);
}
/* gnc_timespec_to_iso8601_buff
char *
@@ -1722,19 +1733,13 @@
t = g_date_time_to_timespec (gdt4);
end = gnc_timespec_to_iso8601_buff (t, buff);
time_str = format_timestring (gdt4);
- if (!MSWIN) /* MSWin can't handle this, produces a ridiculous time string */
- g_assert_cmpstr (buff, ==, time_str);
- else
- g_assert_cmpstr (buff, !=, time_str);
+ g_assert_cmpstr (buff, ==, time_str);
g_free (time_str);
t = g_date_time_to_timespec (gdt5);
end = gnc_timespec_to_iso8601_buff (t, buff);
time_str = format_timestring (gdt5);
- if (sizeof (time_t) > 4)
- g_assert_cmpstr (buff, ==, time_str);
- else
- g_assert_cmpstr (buff, !=, time_str);
+ g_assert_cmpstr (buff, ==, time_str);
g_free (time_str);
g_date_time_unref (gdt0);
@@ -1817,37 +1822,20 @@
gdt_local = gncdt.to_local (gdt4);
g_date_time_get_ymd (gdt_local, &yr, &mo, &day);
g_date_time_unref (gdt_local);
-/* Out of range for MSWin */
- if (MSWIN)
- {
- g_assert_cmpint (r_day, !=, day);
- g_assert_cmpint (r_mo, !=, mo);
- g_assert_cmpint (r_yr, !=, yr);
- }
- else
- {
- g_assert_cmpint (r_day, ==, day);
- g_assert_cmpint (r_mo, ==, mo);
- g_assert_cmpint (r_yr, ==, yr);
- }
+ g_assert_cmpint (r_day, ==, day);
+ g_assert_cmpint (r_mo, ==, mo);
+ g_assert_cmpint (r_yr, ==, yr);
+
t = g_date_time_to_timespec (gdt5);
gnc_timespec2dmy (t, &r_day, &r_mo, &r_yr);
gdt_local = gncdt.to_local (gdt5);
g_date_time_get_ymd (gdt_local, &yr, &mo, &day);
g_date_time_unref (gdt_local);
/* 2038 Bug */
- if (sizeof (time_t) > 4)
- {
- g_assert_cmpint (r_day, ==, day);
- g_assert_cmpint (r_mo, ==, mo);
- g_assert_cmpint (r_yr, ==, yr);
- }
- else
- {
- g_assert_cmpint (r_day, !=, day);
- g_assert_cmpint (r_mo, !=, mo);
- g_assert_cmpint (r_yr, !=, yr);
- }
+ g_assert_cmpint (r_day, ==, day);
+ g_assert_cmpint (r_mo, ==, mo);
+ g_assert_cmpint (r_yr, ==, yr);
+
g_date_time_unref (gdt0);
g_date_time_unref (gdt1);
g_date_time_unref (gdt2);
@@ -1890,37 +1878,19 @@
t = g_date_time_to_timespec (gdt2);
g_date_time_get_ymd (gdt2, &yr, &mon, &day);
r_t = gnc_dmy2timespec (day, mon, yr);
- if (sizeof (time_t) > 4)
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
- else
-/* Fails before 1 April 1918 */
-#if defined GDK_QUARTZ
- /* Darwin mktime gets DST wrong before 1APR1918 */
- g_assert_cmpint (r_t.tv_sec - 3600, ==, t.tv_sec);
-#elif defined G_OS_WIN32
- /* MinGW mktime returns -1 for dates before 1JAN1970 */
- g_assert_cmpint (r_t.tv_sec, ==, -1);
-#else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
-#endif
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
t = g_date_time_to_timespec (gdt3);
g_date_time_get_ymd (gdt3, &yr, &mon, &day);
r_t = gnc_dmy2timespec (day, mon, yr);
- if (MSWIN)
- g_assert_cmpint (r_t.tv_sec, !=, t.tv_sec);
- else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
t = g_date_time_to_timespec (gdt4);
g_date_time_get_ymd (gdt4, &yr, &mon, &day);
r_t = gnc_dmy2timespec (day, mon, yr);
- if (MSWIN)
- g_assert_cmpint (r_t.tv_sec, !=, t.tv_sec);
- else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
g_date_time_unref (gdt1);
@@ -1956,35 +1926,19 @@
t = g_date_time_to_timespec (gdt2);
g_date_time_get_ymd (gdt2, &yr, &mon, &day);
r_t = gnc_dmy2timespec_end (day, mon, yr);
- if (sizeof (time_t) > 4)
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
- else
-/* Fails before 31 March 1918 */
-#if defined GDK_QUARTZ
- g_assert_cmpint (r_t.tv_sec - 3600, ==, t.tv_sec);
-#elif defined G_OS_WIN32
- g_assert_cmpint (r_t.tv_sec, ==, -1);
-#else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
-#endif
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
t = g_date_time_to_timespec (gdt3);
g_date_time_get_ymd (gdt3, &yr, &mon, &day);
r_t = gnc_dmy2timespec_end (day, mon, yr);
- if (MSWIN)
- g_assert_cmpint (r_t.tv_sec, !=, t.tv_sec);
- else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
t = g_date_time_to_timespec (gdt4);
g_date_time_get_ymd (gdt4, &yr, &mon, &day);
r_t = gnc_dmy2timespec_end (day, mon, yr);
- if (MSWIN)
- g_assert_cmpint (r_t.tv_sec, !=, t.tv_sec);
- else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
g_date_time_unref (gdt1);
@@ -2078,33 +2032,22 @@
g_date_time_unref (gdt_local);
g_date_set_dmy (&date2, day, mon, yr);
g_assert_cmpint (g_date_get_julian (&date1), ==, g_date_get_julian (&date2));
- /* So this is interesting. g_date_set_time_t uses localtime and
- * therefore won't work on WIN32 for dates before 1970 or after 2037.
- */
- if (!MSWIN)
- {
- t = g_date_time_to_timespec (gdt4);
- date1 = timespec_to_gdate (t);
- gdt_local = gncdt.to_local (gdt4);
- g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
- g_date_time_unref (gdt_local);
- g_date_set_dmy (&date2, day, mon, yr);
- g_assert_cmpint (g_date_get_julian (&date1), ==, g_date_get_julian (&date2));
+ t = g_date_time_to_timespec (gdt4);
+ date1 = timespec_to_gdate (t);
+ gdt_local = gncdt.to_local (gdt4);
+ g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
+ g_date_time_unref (gdt_local);
+ g_date_set_dmy (&date2, day, mon, yr);
+ g_assert_cmpint (g_date_get_julian (&date1), ==, g_date_get_julian (&date2));
- t = g_date_time_to_timespec (gdt5);
- date1 = timespec_to_gdate (t);
- gdt_local = gncdt.to_local (gdt5);
- g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
- g_date_time_unref (gdt_local);
- g_date_set_dmy (&date2, day, mon, yr);
- if (sizeof (time_t) > 4)
- g_assert_cmpint (g_date_get_julian (&date1),
- ==, g_date_get_julian (&date2));
- else
-/* 2038 bug */
- g_assert_cmpint (g_date_get_julian (&date1),
- !=, g_date_get_julian (&date2));
- }
+ t = g_date_time_to_timespec (gdt5);
+ date1 = timespec_to_gdate (t);
+ gdt_local = gncdt.to_local (gdt5);
+ g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
+ g_date_time_unref (gdt_local);
+ g_date_set_dmy (&date2, day, mon, yr);
+ g_assert_cmpint (g_date_get_julian (&date1),
+ ==, g_date_get_julian (&date2));
g_date_time_unref (gdt0);
g_date_time_unref (gdt1);
@@ -2144,37 +2087,21 @@
g_date_time_get_ymd (gdt2, &yr, &mon, &day);
g_date_set_dmy (&gd, day, mon, yr);
r_t = gdate_to_timespec (gd);
- if (sizeof (time_t) > 4)
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
- else
-/* Fails before 1 April 1918 */
-#if defined GDK_QUARTZ
- g_assert_cmpint (r_t.tv_sec - 3600, ==, t.tv_sec);
-#elif defined G_OS_WIN32
- g_assert_cmpint (r_t.tv_sec, ==, -1);
-#else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
-#endif
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
t = g_date_time_to_timespec (gdt3);
g_date_time_get_ymd (gdt3, &yr, &mon, &day);
g_date_set_dmy (&gd, day, mon, yr);
r_t = gdate_to_timespec (gd);
- if (MSWIN)
- g_assert_cmpint (r_t.tv_sec, !=, t.tv_sec);
- else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
t = g_date_time_to_timespec (gdt4);
g_date_time_get_ymd (gdt4, &yr, &mon, &day);
g_date_set_dmy (&gd, day, mon, yr);
r_t = gdate_to_timespec (gd);
- if (MSWIN)
- g_assert_cmpint (r_t.tv_sec, !=, t.tv_sec);
- else
- g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
+ g_assert_cmpint (r_t.tv_sec, ==, t.tv_sec);
g_assert_cmpint (r_t.tv_nsec, ==, t.tv_nsec);
g_date_time_unref (gdt1);
@@ -2224,19 +2151,16 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_begin = gncdt.new_local (yr, mon, day, 0, 0, 0);
t_time = g_date_time_to_unix (gdt_day_begin);
- r_time = gnc_timet_get_day_start ((time_t)time);
+ r_time = gnc_timet_get_day_start (time);
/* This will work in the half of the world where localtime is later than UTC */
- if (MSWIN && r_time < 0)
- g_assert_cmpint (t_time, !=, r_time);
- else
- g_assert_cmpint (t_time, ==, r_time);
+ g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt1);
time = g_date_time_to_unix (gdt1);
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_begin = gncdt.new_local (yr, mon, day, 0, 0, 0);
t_time = g_date_time_to_unix (gdt_day_begin);
- r_time = gnc_timet_get_day_start ((time_t)time);
+ r_time = gnc_timet_get_day_start (time);
g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt2);
@@ -2244,7 +2168,7 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_begin = gncdt.new_local (yr, mon, day, 0, 0, 0);
t_time = g_date_time_to_unix (gdt_day_begin);
- r_time = gnc_timet_get_day_start ((time_t)time);
+ r_time = gnc_timet_get_day_start (time);
g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt3);
@@ -2252,7 +2176,7 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_begin = gncdt.new_local (yr, mon, day, 0, 0, 0);
t_time = g_date_time_to_unix (gdt_day_begin);
- r_time = gnc_timet_get_day_start ((time_t)time);
+ r_time = gnc_timet_get_day_start (time);
g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt4);
@@ -2260,22 +2184,16 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_begin = gncdt.new_local (yr, mon, day, 0, 0, 0);
t_time = g_date_time_to_unix (gdt_day_begin);
- r_time = gnc_timet_get_day_start ((time_t)time);
- if (MSWIN)
- g_assert_cmpint (t_time, !=, r_time);
- else
- g_assert_cmpint (t_time, ==, r_time);
+ r_time = gnc_timet_get_day_start (time);
+ g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt5);
time = g_date_time_to_unix (gdt5);
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_begin = gncdt.new_local (yr, mon, day, 0, 0, 0);
t_time = g_date_time_to_unix (gdt_day_begin);
- r_time = gnc_timet_get_day_start ((time_t)time);
- if (sizeof (time_t) > 4)
- g_assert_cmpint (t_time, ==, r_time);
- else
- g_assert_cmpint (t_time, !=, r_time);
+ r_time = gnc_timet_get_day_start (time);
+ g_assert_cmpint (t_time, ==, r_time);
g_date_time_unref (gdt0);
g_date_time_unref (gdt1);
@@ -2313,18 +2231,15 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_end = gncdt.new_local (yr, mon, day, 23, 59, 59);
t_time = g_date_time_to_unix (gdt_day_end);
- r_time = gnc_timet_get_day_end ((time_t)time);
- if (MSWIN && r_time < 0)
- g_assert_cmpint (t_time, !=, r_time);
- else
- g_assert_cmpint (t_time, ==, r_time);
+ r_time = gnc_timet_get_day_end (time);
+ g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt1);
time = g_date_time_to_unix (gdt1);
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_end = gncdt.new_local (yr, mon, day, 23, 59, 59);
t_time = g_date_time_to_unix (gdt_day_end);
- r_time = gnc_timet_get_day_end ((time_t)time);
+ r_time = gnc_timet_get_day_end (time);
g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt2);
@@ -2332,7 +2247,7 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_end = gncdt.new_local (yr, mon, day, 23, 59, 59);
t_time = g_date_time_to_unix (gdt_day_end);
- r_time = gnc_timet_get_day_end ((time_t)time);
+ r_time = gnc_timet_get_day_end (time);
g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt3);
@@ -2340,7 +2255,7 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_end = gncdt.new_local (yr, mon, day, 23, 59, 59);
t_time = g_date_time_to_unix (gdt_day_end);
- r_time = gnc_timet_get_day_end ((time_t)time);
+ r_time = gnc_timet_get_day_end (time);
g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt4);
@@ -2348,23 +2263,16 @@
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_end = gncdt.new_local (yr, mon, day, 23, 59, 59);
t_time = g_date_time_to_unix (gdt_day_end);
- r_time = gnc_timet_get_day_end ((time_t)time);
- if (MSWIN)
- g_assert_cmpint (t_time, !=, r_time);
- else
- g_assert_cmpint (t_time, ==, r_time);
+ r_time = gnc_timet_get_day_end (time);
+ g_assert_cmpint (t_time, ==, r_time);
gdt_local = gncdt.to_local (gdt5);
time = g_date_time_to_unix (gdt5);
g_date_time_get_ymd (gdt_local, &yr, &mon, &day);
gdt_day_end = gncdt.new_local (yr, mon, day, 23, 59, 59);
t_time = g_date_time_to_unix (gdt_day_end);
- r_time = gnc_timet_get_day_end ((time_t)time);
-/* 2038 Bug */
- if (sizeof (time_t) > 4)
- g_assert_cmpint (t_time, ==, r_time);
- else
- g_assert_cmpint (t_time, !=, r_time);
+ r_time = gnc_timet_get_day_end (time);
+ g_assert_cmpint (t_time, ==, r_time);
g_date_time_unref (gdt0);
g_date_time_unref (gdt1);
@@ -2441,6 +2349,14 @@
{
_gnc_date_time_init (&gncdt);
+
+ GNC_TEST_ADD_FUNC (suitename, "gnc localtime", test_gnc_localtime);
+ GNC_TEST_ADD_FUNC (suitename, "gnc gmtime", test_gnc_gmtime);
+ GNC_TEST_ADD_FUNC (suitename, "gnc mktime", test_gnc_mktime);
+ GNC_TEST_ADD_FUNC (suitename, "gnc mktime normalization", test_gnc_mktime_normalization);
+ GNC_TEST_ADD_FUNC (suitename, "gnc ctime", test_gnc_ctime);
+ GNC_TEST_ADD_FUNC (suitename, "gnc time", test_gnc_time);
+
GNC_TEST_ADD_FUNC (suitename, "gnc date dateformat to string", test_gnc_date_dateformat_to_string);
GNC_TEST_ADD_FUNC (suitename, "gnc date string to dateformat", test_gnc_date_string_to_dateformat);
GNC_TEST_ADD_FUNC (suitename, "gnc date monthformat to string", test_gnc_date_monthformat_to_string);
More information about the gnucash-changes
mailing list