Fixed bug in calculation of age caused by Y2K38 problem.
[birthday] / src / birthday.c
index 8ee41ab..a5ba7ae 100644 (file)
@@ -261,23 +261,26 @@ on_key_press_event (GtkWidget *widget,
 }
 
 static unsigned int
-calc_age (EContactDate *bdate)
+calc_age (EContactDate *bdate, time_t current_date)
 {
+       struct tm *current_date_tm;
        struct tm bday_tm;
-       struct tm *age_tm;
-       time_t t_age;
        int age = 0;
 
+       current_date_tm = gmtime (&current_date);
+
        bday_tm.tm_sec = 0;
        bday_tm.tm_min = 0;
        bday_tm.tm_hour = 0;
        bday_tm.tm_mday = bdate->day;
        bday_tm.tm_mon = bdate->month - 1;
-       bday_tm.tm_year = bdate->year - 1900;
+       bday_tm.tm_year = current_date_tm->tm_year;
 
-       t_age = time (NULL) - mktime (&bday_tm);
-       age_tm = gmtime (&t_age);
-       age = age_tm->tm_year - 70;
+       if (mktime (&bday_tm) > current_date) {
+               age = (current_date_tm->tm_year + 1900) - bdate->year - 1;
+       } else {
+               age = (current_date_tm->tm_year + 1900) - bdate->year;
+       }
 
        if (age < 0)
                age = 0;
@@ -286,14 +289,13 @@ calc_age (EContactDate *bdate)
 }
 
 static unsigned int
-calc_next_bday (EContactDate *bdate)
+calc_next_bday (EContactDate *bdate, time_t current_date)
 {
        struct tm current_bday_tm, next_bday_tm;
        struct tm *current_date_tm;
-       time_t t_current_date, t_current_bday, t_next_bday;
+       time_t current_bday, next_bday;
 
-       t_current_date = time (NULL);
-       current_date_tm = localtime (&t_current_date);
+       current_date_tm = gmtime (&current_date);
 
        current_bday_tm.tm_sec = 0;
        current_bday_tm.tm_min = 0;
@@ -301,21 +303,21 @@ calc_next_bday (EContactDate *bdate)
        current_bday_tm.tm_mday = bdate->day;
        current_bday_tm.tm_mon = bdate->month - 1;
        current_bday_tm.tm_year = current_date_tm->tm_year;
-       t_current_bday = mktime (&current_bday_tm);
+       current_bday = mktime (&current_bday_tm);
 
-       if (t_current_date > t_current_bday) {
+       if (current_date > current_bday) {
                next_bday_tm.tm_sec = 0;
                next_bday_tm.tm_min = 0;
                next_bday_tm.tm_hour = 0;
                next_bday_tm.tm_mday = bdate->day;
                next_bday_tm.tm_mon = bdate->month - 1;
                next_bday_tm.tm_year = current_date_tm->tm_year + 1;
-               t_next_bday = mktime (&next_bday_tm);
+               next_bday = mktime (&next_bday_tm);
        } else {
-               t_next_bday = t_current_bday;
+               next_bday = current_bday;
        }
 
-       return (t_next_bday - t_current_date) / 86400;
+       return (next_bday - current_date) / 86400;
 }
 
 static gchar *
@@ -351,12 +353,15 @@ create_bday_liststore (BirthdayData *priv, GList *contacts)
        gchar *text_font = NULL;
        gchar *text_color = NULL;
        guint n_contacts = 0;
+       time_t current_date;
 
        g_return_val_if_fail (priv, NULL);
 
        text_font = get_text_font_by_name ("SmallSystemFont");
        text_color = get_text_color_by_name ("SecondaryTextColor");
 
+       current_date = time (NULL);
+
        store = gtk_list_store_new(NUM_COLS,
                                   GDK_TYPE_PIXBUF,     /* COLUMN_AVATAR */
                                   G_TYPE_STRING,       /* COLUMN_DISPLAY */
@@ -433,8 +438,8 @@ create_bday_liststore (BirthdayData *priv, GList *contacts)
                        birthday_tm.tm_year = bdate->year - 1900;
                        strftime (birthday_text, 11, _HL("wdgt_va_date"), &birthday_tm);
 
-                       age = calc_age(bdate);
-                       next_birthday = calc_next_bday(bdate);
+                       age = calc_age(bdate, current_date);
+                       next_birthday = calc_next_bday(bdate, current_date);
                        next_birthday_text = g_strdup_printf(ngettext ("next birthday in %d day", "next birthday in %d days", next_birthday), next_birthday);
                        display_column = g_strdup_printf("%s <span font_desc=\"%s\" foreground=\"%s\"><sup>(%d)</sup>\n%s, %s</span>",
                                                         fullname, text_font, text_color, age, birthday_text, next_birthday_text);