gint day_notes_list_dbclick_cb (GtkWidget *widget, GdkEventButton *event, gpointer user_data) { GtkTreeIter iter; GtkTreeModel *model; guint32 julian_day; GDate *date; GUI *appGUI = (GUI *) user_data; date = g_date_new (); g_return_val_if_fail (date != NULL, FALSE); if ((event->type == GDK_2BUTTON_PRESS) && (event->button == 1)) { if (gtk_tree_selection_get_selected (appGUI->cal->day_notes_list_selection, &model, &iter)) { gtk_tree_model_get (model, &iter, DN_COLUMN_DATE_JULIAN, &julian_day, -1); if (g_date_valid_julian (julian_day) == TRUE) { g_date_set_julian (date, julian_day); cal_jump_to_date (date, appGUI); update_aux_calendars (appGUI); button_day_notes_window_close_cb (NULL, appGUI); } } g_date_free (date); return TRUE; } g_date_free (date); return FALSE; }
/* set stop date returns false when not able to set */ gboolean gtodo_todo_item_set_stop_date_as_julian(GTodoItem *item, guint32 julian) { if(!g_date_valid_julian(julian)) return FALSE; if(item->stop == NULL) item->stop = g_date_new_julian(julian); else g_date_set_julian(item->stop, julian); return TRUE; }
/* "Julian days" just means an absolute number of days, where Day 1 == * Jan 1, Year 1 */ static void g_date_update_julian (const GDate *const_d) { GDate *d = (GDate *) const_d; GDateYear year; gint index; g_return_if_fail (d != NULL); g_return_if_fail (d->dmy); g_return_if_fail (!d->julian); g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year)); /* What we actually do is: multiply years * 365 days in the year, * add the number of years divided by 4, subtract the number of * years divided by 100 and add the number of years divided by 400, * which accounts for leap year stuff. Code from Steffen Beyer's * DateCalc. */ year = d->year - 1; /* we know d->year > 0 since it's valid */ d->julian_days = year * 365U; d->julian_days += (year >>= 2); /* divide by 4 and add */ d->julian_days -= (year /= 25); /* divides original # years by 100 */ d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */ index = g_date_is_leap_year (d->year) ? 1 : 0; d->julian_days += days_in_year[index][d->month] + d->day; g_return_if_fail (g_date_valid_julian (d->julian_days)); d->julian = TRUE; }
DATE* julian_to_date (guint32 julian_day) { DATE *date; GDate *cdate; date = g_new0 (DATE, 1); if (date == NULL) { return date; } if (g_date_valid_julian(julian_day)) { cdate = g_date_new_julian (julian_day); if (cdate != NULL) { if (g_date_valid (cdate)) { date->year = g_date_get_year (cdate); date->month = g_date_get_month (cdate); date->day = g_date_get_day (cdate); } g_date_free(cdate); } return date; /* you have to free DATE struct after use! */ } else { return NULL; } }
gchar* julian_to_str (guint32 julian_day, gint date_format) { static gchar buffer[BUFFER_SIZE]; GDate *cdate; gint i; gchar *date_format_str[] = { "%d-%m-%Y", "%m-%d-%Y", "%Y-%m-%d", "%Y-%d-%m", "%e %B", "%A", "%e %B %Y" }; if (g_date_valid_julian (julian_day)) { cdate = g_date_new_julian (julian_day); g_return_val_if_fail (cdate != NULL, buffer); if (date_format < DATE_DD_MM_YYYY || date_format > DATE_FULL) { date_format = DATE_DD_MM_YYYY; } g_date_strftime (buffer, BUFFER_SIZE, date_format_str[date_format], cdate); g_date_free (cdate); if (buffer[0] == ' ') { for (i = 1; buffer[i]; i++) buffer[i-1] = buffer[i]; buffer[i-1] = '\0'; } } else { g_strlcpy (buffer, _("No date"), BUFFER_SIZE); } return buffer; }
/* return an julian date ... 1 = no date set */ guint32 gtodo_todo_item_get_stop_date_as_julian(GTodoItem *item) { if(item->stop == NULL || !g_date_valid(item->stop)) return 1; else { if(!g_date_valid_julian(g_date_get_julian(item->stop))) return 1; return g_date_get_julian(item->stop); } }
void g_date_set_julian (GDate *d, guint32 j) { g_return_if_fail (d != NULL); g_return_if_fail (g_date_valid_julian (j)); d->julian_days = j; d->julian = TRUE; d->dmy = FALSE; }
void utl_date_julian_to_dmy (guint32 julian, gint *day, gint *month, gint *year) { g_return_if_fail (g_date_valid_julian (julian)); GDate *d = g_date_new_julian (julian); if (day != NULL) *day = g_date_get_day (d); if (month != NULL) *month = g_date_get_month (d); if (year != NULL) *year = g_date_get_year (d); g_date_free (d); }
/* return an julian date ... 1 = no date set */ guint32 gtodo_todo_item_get_due_date_as_julian(GTodoItem *item) { if(item->due == NULL || !g_date_valid(item->due)) { return GTODO_NO_DUE_DATE; } else { if(!g_date_valid_julian(g_date_get_julian(item->due))) return GTODO_NO_DUE_DATE; return g_date_get_julian(item->due); } }
void gtk_dateentry_set_date(GtkDateEntry * dateentry, guint julian_days) { g_return_if_fail (GTK_IS_DATE_ENTRY (dateentry)); if(g_date_valid_julian(julian_days)) { g_date_set_julian (dateentry->date, julian_days); } else { g_date_set_time_t(dateentry->date, time(NULL)); } gtk_dateentry_datetoentry(dateentry); }
gchar * utl_date_print_j (guint32 julian, gint date_format, gint override_locale) { gchar *date_str; if (g_date_valid_julian (julian)) { GDate *d = g_date_new_julian (julian); date_str = utl_date_print (d, date_format, override_locale); g_date_free (d); } else date_str = g_strdup (_("No date")); return date_str; }
/* set due date returns false when not able to set */ gboolean gtodo_todo_item_set_due_date_as_julian(GTodoItem *item, guint32 julian) { if(julian == GTODO_NO_DUE_DATE) { if(item->due != NULL) { g_date_free(item->due); item->due = NULL; } } if(!g_date_valid_julian((guint32)julian)) return FALSE; if(item->due == NULL) item->due = g_date_new_julian((guint32)julian); else g_date_set_julian(item->due, (guint32)julian); return TRUE; }
gint julian_to_year (guint32 julian_day) { GDate *tmpdate; gint year; g_return_val_if_fail (g_date_valid_julian (julian_day) == TRUE, 0); tmpdate = g_date_new_julian (julian_day); g_return_val_if_fail (tmpdate != NULL, 0); year = g_date_get_year (tmpdate); g_date_free (tmpdate); return year; }
static void g_date_update_dmy (GDate *d) { GDateYear y; GDateMonth m; GDateDay day; guint32 A, B, C, D, E, M; g_return_if_fail (d != NULL); g_return_if_fail (d->julian); g_return_if_fail (!d->dmy); g_return_if_fail (g_date_valid_julian (d->julian_days)); /* Formula taken from the Calendar FAQ; the formula was for the * Julian Period which starts on 1 January 4713 BC, so we add * 1,721,425 to the number of days before doing the formula. * * I'm sure this can be simplified for our 1 January 1 AD period * start, but I can't figure out how to unpack the formula. */ A = d->julian_days + 1721425 + 32045; B = ( 4 *(A + 36524) )/ 146097 - 1; C = A - (146097 * B)/4; D = ( 4 * (C + 365) ) / 1461 - 1; E = C - ((1461*D) / 4); M = (5 * (E - 1) + 2)/153; m = M + 3 - (12*(M/10)); day = E - (153*M + 2)/5; y = 100 * B + D - 4800 + (M/10); #ifdef G_ENABLE_DEBUG if (!g_date_valid_dmy (day, m, y)) { g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n", d->julian_days, day, m, y); } #endif d->month = m; d->day = day; d->year = y; d->dmy = TRUE; }
GDate* g_date_new_julian (guint32 j) { GDate *d; g_return_val_if_fail (g_date_valid_julian (j), NULL); d = g_new (GDate, 1); d->julian = TRUE; d->dmy = FALSE; d->julian_days = j; g_assert (g_date_valid (d)); return d; }
gchar * utl_get_julian_day_name (guint32 julian) { static gchar buffer[BUFFER_SIZE]; GDate *tmpdate = NULL; buffer[0] = '\0'; g_return_val_if_fail (g_date_valid_julian (julian) == TRUE, buffer); tmpdate = g_date_new_julian (julian); g_return_val_if_fail (tmpdate != NULL, buffer); g_date_strftime (buffer, BUFFER_SIZE, "%A", tmpdate); g_date_free (tmpdate); return buffer; }
void cal_print_get_events (gchar *buffer, guint32 julian, GUI *appGUI) { GtkTreePath *path; GtkTreeIter iter; GtkTreeModel *model; GSList *lnode; struct note *a; gint i; gchar *wbuf1, *wbuf2; gchar buf1[BUFFER_SIZE], buf2[BUFFER_SIZE]; GDate *date, *sdate; gint age, syear; guint32 tsk_julian; gint time; gint max_event_length; GRegex *reg; buffer[0] = '\0'; max_event_length = (config.cal_print_event_length + 2 > BUFFER_SIZE) ? BUFFER_SIZE : config.cal_print_event_length + 2; #ifdef TASKS_ENABLED /* tasks */ if (config.cal_print_tasks) { model = GTK_TREE_MODEL (appGUI->tsk->tasks_list_store); g_return_if_fail (model != NULL); path = gtk_tree_path_new_first (); while (gtk_tree_model_get_iter (model, &iter, path) == TRUE) { gtk_tree_model_get (model, &iter, TA_COLUMN_DUE_DATE_JULIAN, &tsk_julian, TA_COLUMN_CATEGORY, &wbuf1, -1); if (tsk_julian == julian && tasks_category_get_state (wbuf1, STATE_CALENDAR, appGUI) == TRUE) { gtk_tree_model_get (model, &iter, TA_COLUMN_DUE_TIME, &time, TA_COLUMN_SUMMARY, &wbuf2, -1); if (time >= 0) { g_snprintf (buf1, max_event_length, "\n[%02d:%02d] %s", time / 3600, time / 60 % 60, wbuf2); } else { g_snprintf (buf1, max_event_length, "\n%s", wbuf2); } g_strlcat (buffer, buf1, BUFFER_SIZE); g_free (wbuf2); } g_free (wbuf1); gtk_tree_path_next (path); } gtk_tree_path_free (path); } #endif /* TASKS_ENABLED */ #ifdef CONTACTS_ENABLED /* birthdays */ if (config.cal_print_birthdays) { model = GTK_TREE_MODEL (appGUI->cnt->contacts_list_store); g_return_if_fail (model != NULL); date = g_date_new (); g_return_if_fail (date != NULL); sdate = g_date_new_julian (julian); g_return_if_fail (sdate != NULL); syear = g_date_get_year (sdate); path = gtk_tree_path_new_first (); while (gtk_tree_model_get_iter (model, &iter, path) == TRUE) { gtk_tree_model_get (model, &iter, COLUMN_BIRTH_DAY_DATE, &tsk_julian, -1); if (g_date_valid_julian (tsk_julian)) { g_date_set_julian (date, tsk_julian); age = syear - g_date_get_year (date); if (age >= 0) { if (g_date_valid_dmy (g_date_get_day (date), g_date_get_month (date), syear) == FALSE) { g_date_subtract_days (date, 1); } g_date_set_year (date, syear); if (g_date_compare (date, sdate) == 0) { gtk_tree_model_get (model, &iter, COLUMN_FIRST_NAME, &wbuf1, COLUMN_LAST_NAME, &wbuf2, -1); utl_name_strcat (wbuf1, wbuf2, buf2); g_snprintf (buf1, max_event_length, "\n%s (%d)", buf2, age); g_strlcat (buffer, buf1, BUFFER_SIZE); } } } gtk_tree_path_next (path); } gtk_tree_path_free (path); g_date_free (sdate); g_date_free (date); } /* name days */ if (config.cal_print_namedays) { model = GTK_TREE_MODEL (appGUI->cnt->contacts_list_store); g_return_if_fail (model != NULL); date = NULL; date = g_date_new (); g_return_if_fail (date != NULL); sdate = NULL; sdate = g_date_new_julian (julian); g_return_if_fail (sdate != NULL); syear = g_date_get_year (sdate); path = gtk_tree_path_new_first (); while (gtk_tree_model_get_iter (model, &iter, path) == TRUE) { gtk_tree_model_get (model, &iter, COLUMN_NAME_DAY_DATE, &tsk_julian, -1); if (g_date_valid_julian (tsk_julian)) { g_date_set_julian (date, tsk_julian); if (g_date_valid_dmy (g_date_get_day (date), g_date_get_month (date), syear) == TRUE) { g_date_set_year (date, syear); if (g_date_compare (date, sdate) == 0) { gtk_tree_model_get (model, &iter, COLUMN_FIRST_NAME, &wbuf1, COLUMN_LAST_NAME, &wbuf2, -1); utl_name_strcat (wbuf1, wbuf2, buf1); g_snprintf (buf2, max_event_length, "\n%s (%s)", buf1, _("Name day")); g_strlcat (buffer, buf2, BUFFER_SIZE); } } } gtk_tree_path_next (path); } gtk_tree_path_free (path); g_date_free (sdate); g_date_free (date); } #endif /* CONTACTS_ENABLED */ /* day note */ if (config.cal_print_day_notes) { if (appGUI->cal->notes_list != NULL) { wbuf1 = NULL; reg = g_regex_new ("\n", 0, 0, NULL); for (i = 0, lnode = appGUI->cal->notes_list; lnode != NULL; lnode = lnode->next, i++) { a = g_slist_nth_data (appGUI->cal->notes_list, i); if (a->date == julian) { wbuf1 = g_regex_replace_literal (reg, a->note, -1, 0, " ", 0, NULL); break; } } g_regex_unref (reg); } if (wbuf1 != NULL) { g_strstrip (wbuf1); g_snprintf (buf1, max_event_length, "\n%s", wbuf1); g_strlcat (buffer, buf1, BUFFER_SIZE); g_free (wbuf1); } } #ifdef HAVE_LIBICAL /* ical */ if (config.cal_print_ical) { } #endif /* HAVE_LIBICAL */ g_strstrip (buffer); }
int main(int argc, char** argv) { GDate* d; guint32 j; GDateMonth m; GDateYear y, prev_y; GDateDay day; gchar buf[101]; gchar* loc; /* Try to get all the leap year cases. */ GDateYear check_years[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 1598, 1599, 1600, 1601, 1602, 1650, 1651, 1897, 1898, 1899, 1900, 1901, 1902, 1903, 1961, 1962, 1963, 1964, 1965, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003 }; guint n_check_years = sizeof(check_years)/sizeof(GDateYear); guint i; gboolean discontinuity; g_print("checking GDate..."); TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9); d = g_date_new(); TEST("Empty constructor produces invalid date", !g_date_valid(d)); g_date_free(d); d = g_date_new_dmy(1,1,1); TEST("January 1, Year 1 created and valid", g_date_valid(d)); j = g_date_get_julian(d); TEST("January 1, Year 1 is Julian date 1", j == 1); TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY); TEST("Returned day is 1", g_date_get_day(d) == 1); TEST("Returned year is 1", g_date_get_year(d) == 1); TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH)); TEST("Month 13 is invalid", !g_date_valid_month(13)); TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY)); TEST("Day 32 is invalid", !g_date_valid_day(32)); TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR)); TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN)); TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY)); TEST("Year 2000 is a leap year", g_date_is_leap_year(2000)); TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999)); TEST("Year 1996 is a leap year", g_date_is_leap_year(1996)); TEST("Year 1600 is a leap year", g_date_is_leap_year(1600)); TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100)); TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800)); g_date_free(d); loc = setlocale(LC_ALL,""); if (loc) g_print("\nLocale set to %s\n", loc); else g_print("\nLocale unchanged\n"); d = g_date_new(); g_date_set_time(d, time(NULL)); TEST("Today is valid", g_date_valid(d)); g_date_strftime(buf,100,"Today is a %A, %x\n", d); g_print("%s", buf); g_date_set_time(d, 1); TEST("Beginning of Unix epoch is valid", g_date_valid(d)); g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d); g_print("%s", buf); g_date_set_julian(d, 1); TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d)); g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n", d); g_print("%s", buf); g_date_set_dmy(d, 10, 1, 2000); g_date_strftime(buf,100,"%x", d); g_date_set_parse(d, buf); /* Note: this test will hopefully work, but no promises. */ TEST("Successfully parsed a %x-formatted string", g_date_valid(d) && g_date_get_month(d) == 1 && g_date_get_day(d) == 10 && g_date_get_year(d) == 2000); if (failed) g_date_debug_print(d); g_date_free(d); j = G_DATE_BAD_JULIAN; i = 0; discontinuity = TRUE; y = check_years[0]; prev_y = G_DATE_BAD_YEAR; while (i < n_check_years) { guint32 first_day_of_year = G_DATE_BAD_JULIAN; guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365; guint sunday_week_of_year = 0; guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y); guint monday_week_of_year = 0; guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y); guint iso8601_week_of_year = 0; if (discontinuity) g_print(" (Break in sequence of requested years to check)\n"); g_print("Checking year %u", y); TEST("Year is valid", g_date_valid_year(y)); TEST("Number of Sunday weeks in year is 52 or 53", sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53); TEST("Number of Monday weeks in year is 52 or 53", monday_weeks_in_year == 52 || monday_weeks_in_year == 53); m = 1; while (m < 13) { guint8 dim = g_date_get_days_in_month(m,y); GDate days[31]; /* This is the fast way, no allocation */ TEST("Sensible number of days in month", (dim > 0 && dim < 32)); TEST("Month between 1 and 12 is valid", g_date_valid_month(m)); day = 1; g_date_clear(days, 31); while (day <= dim) { guint i; GDate tmp; TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y)); /* Create GDate with triplet */ d = &days[day-1]; TEST("Cleared day is invalid", !g_date_valid(d)); g_date_set_dmy(d,day,m,y); TEST("Set day is valid", g_date_valid(d)); if (m == G_DATE_JANUARY && day == 1) { first_day_of_year = g_date_get_julian(d); } g_assert(first_day_of_year != G_DATE_BAD_JULIAN); TEST("Date with DMY triplet is valid", g_date_valid(d)); TEST("Month accessor works", g_date_get_month(d) == m); TEST("Year accessor works", g_date_get_year(d) == y); TEST("Day of month accessor works", g_date_get_day(d) == day); TEST("Day of year is consistent with Julian dates", ((g_date_get_julian(d) + 1 - first_day_of_year) == (g_date_get_day_of_year(d)))); if (failed) { g_print("first day: %u this day: %u day of year: %u\n", first_day_of_year, g_date_get_julian(d), g_date_get_day_of_year(d)); } if (m == G_DATE_DECEMBER && day == 31) { TEST("Last day of year equals number of days in year", g_date_get_day_of_year(d) == days_in_year); if (failed) { g_print("last day: %u days in year: %u\n", g_date_get_day_of_year(d), days_in_year); } } TEST("Day of year is not more than number of days in the year", g_date_get_day_of_year(d) <= days_in_year); TEST("Monday week of year is not more than number of weeks in the year", g_date_get_monday_week_of_year(d) <= monday_weeks_in_year); if (failed) { g_print("Weeks in year: %u\n", monday_weeks_in_year); g_date_debug_print(d); } TEST("Monday week of year is >= than last week of year", g_date_get_monday_week_of_year(d) >= monday_week_of_year); if (g_date_get_weekday(d) == G_DATE_MONDAY) { TEST("Monday week of year on Monday 1 more than previous day's week of year", (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1); if ((m == G_DATE_JANUARY && day <= 4) || (m == G_DATE_DECEMBER && day >= 29)) { TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1", (g_date_get_iso8601_week_of_year(d) == 1)); } else { TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year", (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1); } } else { TEST("Monday week of year on non-Monday 0 more than previous day's week of year", (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0); if (!(day == 1 && m == G_DATE_JANUARY)) { TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (", (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0); } } monday_week_of_year = g_date_get_monday_week_of_year(d); iso8601_week_of_year = g_date_get_iso8601_week_of_year(d); TEST("Sunday week of year is not more than number of weeks in the year", g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year); if (failed) { g_date_debug_print(d); } TEST("Sunday week of year is >= than last week of year", g_date_get_sunday_week_of_year(d) >= sunday_week_of_year); if (g_date_get_weekday(d) == G_DATE_SUNDAY) { TEST("Sunday week of year on Sunday 1 more than previous day's week of year", (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1); } else { TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year", (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0); } sunday_week_of_year = g_date_get_sunday_week_of_year(d); TEST("Date is equal to itself", g_date_compare(d,d) == 0); /*************** Increments ***********/ i = 1; while (i < 402) /* Need to get 400 year increments in */ { /***** Days ******/ tmp = *d; g_date_add_days(d, i); TEST("Adding days gives a value greater than previous", g_date_compare(d, &tmp) > 0); g_date_subtract_days(d, i); TEST("Forward days then backward days returns us to current day", g_date_get_day(d) == day); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } TEST("Forward days then backward days returns us to current month", g_date_get_month(d) == m); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } TEST("Forward days then backward days returns us to current year", g_date_get_year(d) == y); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } /******* Months ********/ tmp = *d; g_date_add_months(d, i); TEST("Adding months gives a larger value", g_date_compare(d, &tmp) > 0); g_date_subtract_months(d, i); TEST("Forward months then backward months returns us to current month", g_date_get_month(d) == m); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } TEST("Forward months then backward months returns us to current year", g_date_get_year(d) == y); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } if (day < 29) { /* Day should be unchanged */ TEST("Forward months then backward months returns us to current day", g_date_get_day(d) == day); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } } else { /* reset the day for later tests */ g_date_set_day(d, day); } /******* Years ********/ tmp = *d; g_date_add_years(d, i); TEST("Adding years gives a larger value", g_date_compare(d,&tmp) > 0); g_date_subtract_years(d, i); TEST("Forward years then backward years returns us to current month", g_date_get_month(d) == m); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } TEST("Forward years then backward years returns us to current year", g_date_get_year(d) == y); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } if (m != 2 && day != 29) { TEST("Forward years then backward years returns us to current day", g_date_get_day(d) == day); if (failed) { g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); g_date_debug_print(d); } } else { g_date_set_day(d, day); /* reset */ } i += 10; } /***** increment test relative to our local Julian count */ if (!discontinuity) { /* We can only run sequence tests between sequential years */ TEST("Julians are sequential with increment 1", j+1 == g_date_get_julian(d)); if (failed) { g_print("Out of sequence, prev: %u expected: %u got: %u\n", j, j+1, g_date_get_julian(d)); } g_date_add_days(d,1); TEST("Next day has julian 1 higher", g_date_get_julian(d) == j + 2); g_date_subtract_days(d, 1); if (j != G_DATE_BAD_JULIAN) { g_date_subtract_days(d, 1); TEST("Previous day has julian 1 lower", g_date_get_julian(d) == j); g_date_add_days(d, 1); /* back to original */ } } discontinuity = FALSE; /* goes away now */ fflush(stdout); fflush(stderr); j = g_date_get_julian(d); /* inc current julian */ ++day; } ++m; } g_print(" done\n"); ++i; prev_y = y; y = check_years[i]; if (prev_y == G_DATE_BAD_YEAR || (prev_y + 1) != y) discontinuity = TRUE; } g_print("\n%u tests passed, %u failed\n",passed, notpassed); return 0; }
void contacts_create_birthdays_window (GUI *appGUI) { GtkWidget *vbox1; GtkWidget *hseparator; GtkWidget *hbuttonbox; GtkWidget *close_button; GtkTreeViewColumn *column; GtkCellRenderer *renderer; GtkWidget *scrolledwindow; gint i, n, id, age; guint32 date; gchar *text, buffer[BUFFER_SIZE], buff[BUFFER_SIZE]; GtkTreeIter iter, n_iter; GDate *cdate_birthday, *cdate_current; guint b_day, b_month, b_year; guint c_day, c_month, c_year; gboolean flag, leap; cdate_birthday = g_date_new (); g_return_if_fail (cdate_birthday != NULL); cdate_current = g_date_new (); g_return_if_fail (cdate_current != NULL); i = n = 0; while (gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, NULL, i++)) { gtk_tree_model_get (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, COLUMN_BIRTH_DAY_DATE, &date, -1); if (date) n++; } if (n == 0) { gui_create_dialog (GTK_MESSAGE_INFO, _("No birthdays defined"), GTK_WINDOW (appGUI->main_window)); return; } appGUI->cnt->birthdays_window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (appGUI->cnt->birthdays_window), _("Birthdays list")); gtk_window_set_position (GTK_WINDOW (appGUI->cnt->birthdays_window), GTK_WIN_POS_CENTER_ON_PARENT); gtk_window_set_default_size (GTK_WINDOW (appGUI->cnt->birthdays_window), config.contacts_birthdays_win_w, config.contacts_birthdays_win_h); gtk_window_set_modal (GTK_WINDOW (appGUI->cnt->birthdays_window), TRUE); g_signal_connect (G_OBJECT (appGUI->cnt->birthdays_window), "delete_event", G_CALLBACK (birthdays_window_close_cb), appGUI); gtk_window_set_transient_for (GTK_WINDOW (appGUI->cnt->birthdays_window), GTK_WINDOW (appGUI->main_window)); gtk_container_set_border_width (GTK_CONTAINER (appGUI->cnt->birthdays_window), 8); g_signal_connect (G_OBJECT (appGUI->cnt->birthdays_window), "key_press_event", G_CALLBACK (birthdays_key_press_cb), appGUI); vbox1 = gtk_vbox_new (FALSE, 0); gtk_widget_show (vbox1); gtk_container_add (GTK_CONTAINER (appGUI->cnt->birthdays_window), vbox1); scrolledwindow = gtk_scrolled_window_new (NULL, NULL); gtk_widget_show (scrolledwindow); gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow, TRUE, TRUE, 0); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_SHADOW_IN); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); appGUI->cnt->birthdays_list_store = gtk_list_store_new (BIRTHDAYS_NUM_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT); appGUI->cnt->birthdays_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (appGUI->cnt->birthdays_list_store)); gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), config.contacts_rules_hint); gtk_widget_show (appGUI->cnt->birthdays_list); GTK_WIDGET_SET_FLAGS (appGUI->cnt->birthdays_list, GTK_CAN_DEFAULT); gtk_container_add (GTK_CONTAINER (scrolledwindow), appGUI->cnt->birthdays_list); appGUI->cnt->birthdays_list_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (appGUI->cnt->birthdays_list)); g_signal_connect (G_OBJECT (appGUI->cnt->birthdays_list), "button_press_event", G_CALLBACK (birthdays_list_dbclick_cb), appGUI); /* create columns */ renderer = gtk_cell_renderer_text_new (); g_object_set (G_OBJECT (renderer), "xpad", 8, NULL); column = gtk_tree_view_column_new_with_attributes (_("Name"), renderer, "text", B_COLUMN_NAME, NULL); gtk_tree_view_column_set_visible (column, TRUE); gtk_tree_view_column_set_expand (column, TRUE); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); gtk_tree_view_column_set_sort_column_id (column, B_COLUMN_NAME); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes (_("Days to birthday"), renderer, "text", B_COLUMN_DAYS_NUM, NULL); gtk_tree_view_column_set_visible (column, FALSE); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); renderer = gtk_cell_renderer_text_new (); g_object_set (G_OBJECT (renderer), "xalign", 1.0, NULL); g_object_set (G_OBJECT (renderer), "xpad", 8, NULL); column = gtk_tree_view_column_new_with_attributes (_("Days to birthday"), renderer, "text", B_COLUMN_DAYS, NULL); gtk_tree_view_column_set_visible (column, TRUE); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); gtk_tree_view_column_set_sort_column_id (column, B_COLUMN_DAYS_NUM); g_signal_emit_by_name (column, "clicked"); column = gtk_tree_view_column_new_with_attributes (_("Age"), renderer, "text", B_COLUMN_AGE, NULL); gtk_tree_view_column_set_visible (column, config.visible_age_column); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); gtk_tree_view_column_set_sort_column_id (column, B_COLUMN_AGE); column = gtk_tree_view_column_new_with_attributes (_("Birthday date"), renderer, "text", B_COLUMN_DATE, NULL); gtk_tree_view_column_set_visible (column, config.visible_birthday_date_column); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes (_("Zodiac sign"), renderer, "text", B_COLUMN_ZODIAC, NULL); gtk_tree_view_column_set_visible (column, config.visible_zodiac_sign_column); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes ("ID", renderer, "text", B_COLUMN_ID, NULL); gtk_tree_view_column_set_visible (column, FALSE); gtk_tree_view_append_column (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), column); gtk_tree_view_set_enable_search (GTK_TREE_VIEW (appGUI->cnt->birthdays_list), FALSE); i = 0; g_date_set_julian (cdate_current, utl_get_current_julian ()); c_day = g_date_get_day (cdate_current); c_month = g_date_get_month (cdate_current); c_year = g_date_get_year (cdate_current); while (gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, NULL, i++)) { gtk_tree_model_get (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, COLUMN_BIRTH_DAY_DATE, &date, -1); if (g_date_valid_julian (date)) { /* calculate age */ g_date_set_julian (cdate_birthday, date); b_day = g_date_get_day (cdate_birthday); b_month = g_date_get_month (cdate_birthday); b_year = g_date_get_year (cdate_birthday); age = (gint) c_year - b_year; if (b_month < c_month || (b_month == c_month && b_day < c_day)) age++; if (age < 1) continue; /* name */ flag = FALSE; g_snprintf (buff, BUFFER_SIZE, "(%s)", _("None")); gtk_tree_model_get (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, COLUMN_LAST_NAME, &text, -1); if (text != NULL) { flag = TRUE; if (strcmp (text, buff) == 0) { text[0] = '\0'; flag = FALSE; } g_strlcpy (buffer, text, BUFFER_SIZE); g_free (text); } gtk_tree_model_get (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, COLUMN_FIRST_NAME, &text, -1); if (text != NULL) { if (strcmp (text, buff) == 0) { text[0] = '\0'; } if (flag == TRUE) { g_strlcat (buffer, " ", BUFFER_SIZE); g_strlcat (buffer, text, BUFFER_SIZE); } else { g_strlcpy (buffer, text, BUFFER_SIZE); } g_free (text); } gtk_tree_model_get (GTK_TREE_MODEL (appGUI->cnt->contacts_list_store), &iter, COLUMN_ID, &id, -1); gtk_list_store_append (appGUI->cnt->birthdays_list_store, &n_iter); gtk_list_store_set (appGUI->cnt->birthdays_list_store, &n_iter, B_COLUMN_ID, id, B_COLUMN_NAME, buffer, B_COLUMN_AGE, age, -1); /* calculate days to birthday */ b_year = c_year; if ((b_month < c_month) || (b_month == c_month && b_day < c_day)) b_year++; leap = FALSE; if (g_date_valid_dmy (b_day, b_month, b_year) == FALSE) { g_date_set_day (cdate_birthday, b_day - 1); leap = TRUE; } g_date_set_year (cdate_birthday, b_year); date = g_date_days_between (cdate_current, cdate_birthday); if (date == 0) { g_snprintf (buffer, BUFFER_SIZE, "%s", _("today")); } else { g_snprintf (buffer, BUFFER_SIZE, leap ? "%d + 1" : "%d", date); } g_date_strftime (buff, BUFFER_SIZE, "%A, ", cdate_birthday); g_strlcat (buff, julian_to_str (g_date_get_julian (cdate_birthday), config.date_format), BUFFER_SIZE); gtk_list_store_set (appGUI->cnt->birthdays_list_store, &n_iter, B_COLUMN_DAYS_NUM, date, B_COLUMN_DAYS, buffer, B_COLUMN_DATE, buff, B_COLUMN_ZODIAC, utl_get_zodiac_name (b_day, b_month), -1); } } g_date_free (cdate_birthday); g_date_free (cdate_current); hseparator = gtk_hseparator_new (); gtk_widget_show (hseparator); gtk_box_pack_start (GTK_BOX (vbox1), hseparator, FALSE, TRUE, 4); hbuttonbox = gtk_hbutton_box_new (); gtk_widget_show (hbuttonbox); gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox, FALSE, TRUE, 0); gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_END); gtk_box_set_spacing (GTK_BOX (hbuttonbox), 4); if (config.default_stock_icons) { close_button = gtk_button_new_from_stock (GTK_STOCK_CLOSE); } else { close_button = gtk_button_new_from_stock (OSMO_STOCK_BUTTON_CLOSE); } gtk_widget_show (close_button); g_signal_connect (close_button, "clicked", G_CALLBACK (button_birthdays_window_close_cb), appGUI); gtk_container_add (GTK_CONTAINER (hbuttonbox), close_button); gtk_widget_show (appGUI->cnt->birthdays_window); gtk_widget_grab_focus (close_button); }
void cal_read_notes (GUI *appGUI) { xmlDocPtr doc; xmlChar *key, *prop; xmlNodePtr node, cnode, category_node; GtkTreeIter iter; GdkPixbuf *image; guint32 julian; gint day, month, year; gchar *color_str, *note; GDate *date; appGUI->cal->notes_list = NULL; date = g_date_new (); g_return_if_fail (date != NULL); if (g_file_test (prefs_get_config_filename (CALENDAR_NOTES_FILENAME, appGUI), G_FILE_TEST_IS_REGULAR) == FALSE) return; if ((doc = xmlParseFile (prefs_get_config_filename (CALENDAR_NOTES_FILENAME, appGUI)))) { if (!(node = xmlDocGetRootElement (doc))) return; if (!xmlStrcmp (node->name, (xmlChar *) CALENDAR_NOTES_NAME)) { /* read note */ node = node->xmlChildrenNode; while (node != NULL) { if (!xmlStrcmp (node->name, (xmlChar *) CALENDAR_DAY_CATEGORIES_NAME)) { category_node = node->xmlChildrenNode; while (category_node != NULL) { if ((!xmlStrcmp (category_node->name, (const xmlChar *) "name"))) { key = xmlNodeListGetString (doc, category_node->xmlChildrenNode, 1); prop = xmlGetProp (category_node, (const xmlChar *) "color"); if (key != NULL) { image = gui_create_color_swatch ((gchar *) prop); gtk_list_store_append (appGUI->opt->calendar_category_store, &iter); gtk_list_store_set (appGUI->opt->calendar_category_store, &iter, 0, image, 1, prop, 2, (gchar *) key, -1); g_object_unref (image); xmlFree (key); } } category_node = category_node->next; } } if (!xmlStrcmp (node->name, (xmlChar *) "note")) { cnode = node->xmlChildrenNode; julian = 0; day = month = year = 0; color_str = NULL; note = NULL; while (cnode != NULL) { utl_xml_get_int ("day", &day, cnode, doc); utl_xml_get_int ("month", &month, cnode, doc); utl_xml_get_int ("year", &year, cnode, doc); utl_xml_get_uint ("date", &julian, cnode, doc); utl_xml_get_str ("color", &color_str, cnode, doc); utl_xml_get_str ("message", ¬e, cnode, doc); cnode = cnode->next; } if (note != NULL && (g_date_valid_dmy (day, month, year) || g_date_valid_julian (julian))) { if (g_date_valid_julian (julian)) g_date_set_julian (date, julian); else g_date_set_dmy (date, day, month, year); cal_add_note (date, color_str, note, appGUI); if (color_str != NULL) g_free (color_str); g_free (note); } } node = node->next; } } xmlFree (node); xmlFreeDoc (doc); } g_date_free (date); }