r22830 - gnucash/trunk/src/libqof/qof - Fix erroneous conversion from time64 to struct tm because the Sunday weekday has a different number in GDateTime vs. struct tm.

Christian Stimming cstim at code.gnucash.org
Fri Mar 22 17:40:01 EDT 2013


Author: cstim
Date: 2013-03-22 17:40:01 -0400 (Fri, 22 Mar 2013)
New Revision: 22830
Trac: http://svn.gnucash.org/trac/changeset/22830

Modified:
   gnucash/trunk/src/libqof/qof/gnc-date.c
   gnucash/trunk/src/libqof/qof/test/test-gnc-date.c
Log:
Fix erroneous conversion from time64 to struct tm because the Sunday weekday has a different number in GDateTime vs. struct tm.

In struct tm, the weekday is in the range 0..6 with Sunday=0, but
in GDateTime the weekday is in the range 1..7 with Sunday=7. This should
better be added to the GDateTime documentation at g_date_time_get_day_of_week,
but apparently nobody documented this so far.

Modified: gnucash/trunk/src/libqof/qof/gnc-date.c
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.c	2013-03-22 21:39:49 UTC (rev 22829)
+++ gnucash/trunk/src/libqof/qof/gnc-date.c	2013-03-22 21:40:01 UTC (rev 22830)
@@ -279,7 +279,8 @@
      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);
+     // Watch out: struct tm has wday=0..6 with Sunday=0, but GDateTime has wday=1..7 with Sunday=7.
+     time->tm_wday = g_date_time_get_day_of_week (gdt) % 7;
      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;
@@ -396,7 +397,8 @@
 				      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);
+     // Watch out: struct tm has wday=0..6 with Sunday=0, but GDateTime has wday=1..7 with Sunday=7.
+     time->tm_wday = g_date_time_get_day_of_week (gdt) % 7;
      time->tm_yday = g_date_time_get_day_of_year (gdt);
      time->tm_isdst = g_date_time_is_daylight_savings (gdt);
 
@@ -419,7 +421,8 @@
 				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);
+     // Watch out: struct tm has wday=0..6 with Sunday=0, but GDateTime has wday=1..7 with Sunday=7.
+     time->tm_wday = g_date_time_get_day_of_week (gdt) % 7;
      time->tm_yday = g_date_time_get_day_of_year (gdt);
      time->tm_isdst = g_date_time_is_daylight_savings (gdt);
 

Modified: gnucash/trunk/src/libqof/qof/test/test-gnc-date.c
===================================================================
--- gnucash/trunk/src/libqof/qof/test/test-gnc-date.c	2013-03-22 21:39:49 UTC (rev 22829)
+++ gnucash/trunk/src/libqof/qof/test/test-gnc-date.c	2013-03-22 21:40:01 UTC (rev 22830)
@@ -57,8 +57,11 @@
 static void
 test_gnc_localtime (void)
 {
-    time64 secs[5] = {-43238956734LL, -1123692LL, 432761LL,
-		      723349832LL, 887326459367LL};
+    time64 secs[6] = {-43238956734LL, -1123692LL, 432761LL,
+                      723349832LL, 887326459367LL,
+                     1364160236LL // This is "Sunday 2013-03-24" (to verify the Sunday
+                                  // difference between g_date_time and tm->tm_wday)
+                     };
     guint ind;
     gchar *msg = "gnc_localtime_r: assertion `gdt != NULL' failed";
     gint loglevel = G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL;
@@ -82,7 +85,8 @@
 	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));
+        // Watch out: struct tm has wday=0..6 with Sunday=0, but GDateTime has wday=1..7 with Sunday=7.
+        g_assert_cmpint (time->tm_wday, ==, (g_date_time_get_day_of_week (gdt) % 7));
 	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);



More information about the gnucash-changes mailing list