gnucash maint: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Tue Oct 4 14:01:34 EDT 2016


Updated	 via  https://github.com/Gnucash/gnucash/commit/901c89df (commit)
	 via  https://github.com/Gnucash/gnucash/commit/5a7c791c (commit)
	from  https://github.com/Gnucash/gnucash/commit/cd8182fc (commit)



commit 901c89dffad9fa9ee19960b91a585fc90a67e29e
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Oct 4 20:00:48 2016 +0200

    Fix CSV importer to handle GMT + 13 Timezone (New Zealand Daylight Time).
    
    Also remove special time calculations and use gnc_dmy2timespec_neutral()
    for consistency with rest of GnuCash.

diff --git a/src/import-export/csv-imp/gnc-csv-model.c b/src/import-export/csv-imp/gnc-csv-model.c
index 1fe14e7..0636924 100644
--- a/src/import-export/csv-imp/gnc-csv-model.c
+++ b/src/import-export/csv-imp/gnc-csv-model.c
@@ -98,11 +98,9 @@ static StfParseOptions_t* default_parse_options (void)
  */
 static time64 parse_date_with_year (const char* date_str, int format)
 {
-    time64 rawtime; /* The integer time */
-    struct tm retvalue, test_retvalue; /* The time in a broken-down structure */
-
-    int i, j, mem_length, orig_year = -1, orig_month = -1, orig_day = -1;
 
+    int i, j, mem_length, year = -1, month = -1, day = -1;
+    Timespec ts;
     /* Buffer for containing individual parts (e.g. year, month, day) of a date */
     char date_segment[5];
 
@@ -155,15 +153,6 @@ static time64 parse_date_with_year (const char* date_str, int format)
         }
     }
 
-    /* Put some sane values in retvalue by using a fixed time for
-     * the non-year-month-day parts of the date. */
-    gnc_time (&rawtime);
-    gnc_localtime_r (&rawtime, &retvalue);
-    retvalue.tm_hour = 11;
-    retvalue.tm_min = 0;
-    retvalue.tm_sec = 0;
-    retvalue.tm_isdst = -1;
-
     /* j traverses pmatch (index 0 contains the entire string, so we
      * start at index 1 for the first meaningful match). */
     j = 1;
@@ -187,51 +176,32 @@ static time64 parse_date_with_year (const char* date_str, int format)
             switch (segment_type)
             {
             case 'y':
-                retvalue.tm_year = atoi (date_segment);
+                year = atoi (date_segment);
 
                 /* Handle two-digit years. */
-                if (retvalue.tm_year < 100)
+                if (year < 100)
                 {
                     /* We allow two-digit years in the range 1969 - 2068. */
-                    if (retvalue.tm_year < 69)
-                        retvalue.tm_year += 100;
+                    if (year < 69)
+                        year += 2000;
+                    else
+                        year += 1900;
                 }
-                else
-                    retvalue.tm_year -= 1900;
-                orig_year = retvalue.tm_year;
                 break;
 
             case 'm':
-                orig_month = retvalue.tm_mon = atoi (date_segment) - 1;
+                month =atoi (date_segment);
                 break;
 
             case 'd':
-                orig_day = retvalue.tm_mday = atoi (date_segment);
+                day = atoi (date_segment);
                 break;
             }
             j++;
         }
     }
-    /* Convert back to an integer. If gnc_mktime leaves retvalue unchanged,
-     * everything is okay; otherwise, an error has occurred. */
-    /* We have to use a "test" date value to account for changes in
-     * daylight savings time, which can cause a date change with gnc_mktime
-     * near midnight, causing the code to incorrectly think a date is
-     * incorrect. */
-    test_retvalue = retvalue;
-    gnc_mktime (&test_retvalue);
-    retvalue.tm_isdst = test_retvalue.tm_isdst;
-    rawtime = gnc_mktime (&retvalue);
-    if (retvalue.tm_mday == orig_day &&
-            retvalue.tm_mon == orig_month &&
-            retvalue.tm_year == orig_year)
-    {
-        return rawtime;
-    }
-    else
-    {
-        return -1;
-    }
+    ts = gnc_dmy2timespec_neutral(day, month, year);
+    return ts.tv_sec;
 }
 
 /** Parses a string into a date, given a format. The format cannot
@@ -243,10 +213,8 @@ static time64 parse_date_with_year (const char* date_str, int format)
  */
 static time64 parse_date_without_year (const char* date_str, int format)
 {
-    time64 rawtime; /* The integer time */
-    struct tm retvalue, test_retvalue; /* The time in a broken-down structure */
-
-    int i, j, mem_length, orig_year = -1, orig_month = -1, orig_day = -1;
+    Timespec ts;
+    int i, j, mem_length, year = -1, month = -1, day = -1;
 
     /* Buffer for containing individual parts (e.g. year, month, day) of a date */
     gchar* date_segment;
@@ -264,21 +232,14 @@ static time64 parse_date_without_year (const char* date_str, int format)
     regcomp (&preg, regex, REG_EXTENDED);
     regexec (&preg, date_str, 3, pmatch, 0);
     regfree (&preg);
-
+    /* Set day, month, and year to today. We'll replace day & month with the
+     * values from the string.
+     */
+    gnc_timespec2dmy(timespec_now(), &day, &month, &year);
     /* If there wasn't a match, there was an error. */
     if (pmatch[0].rm_eo == 0)
         return -1;
 
-    /* Put some sane values in retvalue by using a fixed time for
-     * the non-year-month-day parts of the date. */
-    gnc_time (&rawtime);
-    gnc_localtime_r (&rawtime, &retvalue);
-    retvalue.tm_hour = 11;
-    retvalue.tm_min = 0;
-    retvalue.tm_sec = 0;
-    retvalue.tm_isdst = -1;
-    orig_year = retvalue.tm_year;
-
     /* j traverses pmatch (index 0 contains the entire string, so we
      * start at index 1 for the first meaningful match). */
     j = 1;
@@ -303,37 +264,21 @@ static time64 parse_date_without_year (const char* date_str, int format)
             switch (segment_type)
             {
             case 'm':
-                orig_month = retvalue.tm_mon = atoi (date_segment) - 1;
+                month = atoi (date_segment);
                 break;
 
             case 'd':
-                orig_day = retvalue.tm_mday = atoi (date_segment);
+                day = atoi (date_segment);
                 break;
             }
             g_free (date_segment);
             j++;
         }
     }
-    /* Convert back to an integer. If gnc_mktime leaves retvalue unchanged,
-     * everything is okay; otherwise, an error has occurred. */
-    /* We have to use a "test" date value to account for changes in
-     * daylight savings time, which can cause a date change with gnc_mktime
-     * near midnight, causing the code to incorrectly think a date is
-     * incorrect. */
-    test_retvalue = retvalue;
-    gnc_mktime (&test_retvalue);
-    retvalue.tm_isdst = test_retvalue.tm_isdst;
-    rawtime = gnc_mktime (&retvalue);
-    if (retvalue.tm_mday == orig_day &&
-            retvalue.tm_mon == orig_month &&
-            retvalue.tm_year == orig_year)
-    {
-        return rawtime;
-    }
-    else
-    {
+    if (month > 12 || day > 31)
         return -1;
-    }
+    ts = gnc_dmy2timespec_neutral(day, month, year);
+    return ts.tv_sec;
 }
 
 /** Parses a string into a date, given a format. This function

commit 5a7c791c96cc7bf84002f6894d44a2ef12dd46d4
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Oct 4 19:59:00 2016 +0200

    Bug 772382 - Date off-by-one after DST change
    
    The time needs to be 10:59, not 11:00: 13 hours after 11:00 is 24:00,
    which is really 00:00 the next day.

diff --git a/src/libqof/qof/gnc-date.c b/src/libqof/qof/gnc-date.c
index 3da2eca..5f4133c 100644
--- a/src/libqof/qof/gnc-date.c
+++ b/src/libqof/qof/gnc-date.c
@@ -1576,15 +1576,14 @@ gnc_dmy2timespec_neutral (int day, int month, int year)
     GDateTime *gdt = gnc_g_date_time_new_local (year, month, day, 12, 0, 0.0);
     int interval = g_time_zone_find_interval (zone, G_TIME_TYPE_STANDARD,
                                               g_date_time_to_unix(gdt));
-    int offset = g_time_zone_get_offset(gnc_g_time_zone_new_local(),
-                                        interval) / 3600;
+    int offset = g_time_zone_get_offset(zone, interval) / 3600;
     g_date_time_unref (gdt);
     memset (&date, 0, sizeof(struct tm));
     date.tm_year = year - 1900;
     date.tm_mon = month - 1;
     date.tm_mday = day;
-    date.tm_hour = offset < -11 ? -offset : offset > 13 ? 24 - offset : 11;
-    date.tm_min = 0;
+    date.tm_hour = offset < -11 ? -offset : offset > 13 ? 23 - offset : 10;
+    date.tm_min = 59;
     date.tm_sec = 0;
 
     ts.tv_sec = gnc_timegm(&date);
diff --git a/src/libqof/qof/test/test-gnc-date.c b/src/libqof/qof/test/test-gnc-date.c
index 4210177..fa3625d 100644
--- a/src/libqof/qof/test/test-gnc-date.c
+++ b/src/libqof/qof/test/test-gnc-date.c
@@ -1973,10 +1973,10 @@ test_gnc_dmy2timespec_end (void)
 static void
 test_gnc_dmy2timespec_neutral (void)
 {
-    GDateTime *gdt1 = gncdt.new_utc (1999, 7, 21, 11, 0, 0);
-    GDateTime *gdt2 = gncdt.new_utc (1918, 3, 31, 11, 0, 0);
-    GDateTime *gdt3 = gncdt.new_utc (1918, 4, 1, 11, 0, 0);
-    GDateTime *gdt4 = gncdt.new_utc (2057, 11, 20, 11, 0, 0);
+    GDateTime *gdt1 = gncdt.new_utc (1999, 7, 21, 10, 59, 0);
+    GDateTime *gdt2 = gncdt.new_utc (1918, 3, 31, 10, 59, 0);
+    GDateTime *gdt3 = gncdt.new_utc (1918, 4, 1, 10, 59, 0);
+    GDateTime *gdt4 = gncdt.new_utc (2057, 11, 20, 10, 59, 0);
 
     gint day, mon, yr;
     Timespec t, r_t;
@@ -2129,10 +2129,10 @@ Timespec gdate_to_timespec (GDate d)// C: 7 in 6  Local: 0:0:0
 static void
 test_gdate_to_timespec (void)
 {
-    GDateTime *gdt1 = gncdt.new_utc (1999, 7, 21, 11, 0, 0);
-    GDateTime *gdt2 = gncdt.new_utc (1918, 3, 31, 11, 0, 0);
-    GDateTime *gdt3 = gncdt.new_utc (1918, 4, 1, 11, 0, 0);
-    GDateTime *gdt4 = gncdt.new_utc (2057, 11, 20, 11, 0, 0);
+    GDateTime *gdt1 = gncdt.new_utc (1999, 7, 21, 10, 59, 0);
+    GDateTime *gdt2 = gncdt.new_utc (1918, 3, 31, 10, 59, 0);
+    GDateTime *gdt3 = gncdt.new_utc (1918, 4, 1, 10, 59, 0);
+    GDateTime *gdt4 = gncdt.new_utc (2057, 11, 20, 10, 59, 0);
 
     gint day, mon, yr;
     Timespec t, r_t;



Summary of changes:
 src/import-export/csv-imp/gnc-csv-model.c | 101 +++++++-----------------------
 src/libqof/qof/gnc-date.c                 |   7 +--
 src/libqof/qof/test/test-gnc-date.c       |  16 ++---
 3 files changed, 34 insertions(+), 90 deletions(-)



More information about the gnucash-changes mailing list