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.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22830 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2013-03-22 21:40:01 +00:00
parent c50b91505a
commit a70b6dce3f
2 changed files with 13 additions and 6 deletions

View File

@ -279,7 +279,8 @@ gnc_g_date_time_fill_struct_tm (GDateTime *gdt, struct tm* time)
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 @@ gnc_mktime (struct tm* time)
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 @@ gnc_timegm (struct tm* time)
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);

View File

@ -57,8 +57,11 @@ extern void _gnc_date_time_init (_GncDateTime *);
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 @@ test_gnc_localtime (void)
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);