예제 #1
0
파일: utils_date.c 프로젝트: rosedu/osmo
gboolean
utl_date_set_valid_day (GDate *date, gint day)
{
    gint days = utl_date_get_days_in_month (date);

    if (day > days) {
        g_date_set_day (date, days);
		return TRUE;
	}

	g_date_set_day (date, day);
	return FALSE;
}
예제 #2
0
static Recurrence*
_get_day_of_month_recurrence(GncFrequency *gf, GDate *start_date, int multiplier, char *combo_name, char *combo_weekend_name)
{
    Recurrence *r;
    GtkWidget *day_of_month_combo = glade_xml_get_widget(gf->gxml, combo_name);
    int day_of_month_index = gtk_combo_box_get_active(GTK_COMBO_BOX(day_of_month_combo));
    GtkWidget *weekend_adjust_combo = glade_xml_get_widget(gf->gxml, combo_weekend_name);
    int weekend_adjust = gtk_combo_box_get_active(GTK_COMBO_BOX(weekend_adjust_combo));
    GDateWeekday selected_day_of_week;
    GDate *day_of_week_date;
    int selected_index, selected_week;
    r = g_new0(Recurrence, 1);
    if (day_of_month_index > LAST_DAY_OF_MONTH_OPTION_INDEX + 7)
    {
        selected_index = day_of_month_index - LAST_DAY_OF_MONTH_OPTION_INDEX - 7;
        day_of_week_date = g_date_new_julian(g_date_get_julian(start_date));
        selected_week = (selected_index - 1) / 7 == 4 ? 3 : (selected_index - 1) / 7;
        selected_day_of_week = selected_index - 7 * selected_week;
        g_date_set_day(day_of_week_date, 1);
        while (g_date_get_weekday(day_of_week_date) != selected_day_of_week)
            g_date_add_days(day_of_week_date, 1);
        g_date_add_days(day_of_week_date, 7 * selected_week);
        recurrenceSet(r, multiplier, PERIOD_NTH_WEEKDAY, day_of_week_date, WEEKEND_ADJ_NONE);
    }
    else if (day_of_month_index > LAST_DAY_OF_MONTH_OPTION_INDEX)
    {
        day_of_week_date = g_date_new_julian(g_date_get_julian(start_date));
        selected_day_of_week = day_of_month_index - LAST_DAY_OF_MONTH_OPTION_INDEX;
        // increment until we align on the DOW, but stay inside the month
        g_date_set_day(day_of_week_date, 1);
        while (g_date_get_weekday(day_of_week_date) != selected_day_of_week)
            g_date_add_days(day_of_week_date, 1);
        recurrenceSet(r, multiplier, PERIOD_LAST_WEEKDAY, day_of_week_date, weekend_adjust);
    }
    else if (day_of_month_index == LAST_DAY_OF_MONTH_OPTION_INDEX)
    {
        GDate *day_of_month = g_date_new_julian(g_date_get_julian(start_date));
        recurrenceSet(r, multiplier, PERIOD_END_OF_MONTH, day_of_month, weekend_adjust);
    }
    else
    {
        int allowable_date = -1;
        GDate *day_of_month = g_date_new_julian(g_date_get_julian(start_date));
        allowable_date = MIN(day_of_month_index + 1,
                             g_date_get_days_in_month(g_date_get_month(day_of_month),
                                     g_date_get_year(day_of_month)));
        g_date_set_day(day_of_month, allowable_date);
        recurrenceSet(r, multiplier, PERIOD_MONTH, day_of_month, weekend_adjust);
    }
    return r;
}
예제 #3
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
/** Convert a GDate to the last day of the prebvious month.  This
 *  routine has no knowledge of how many days are in a month, whether
 *  its a leap year, etc.  All that information is contained in the
 *  glib date functions.
 *
 *  @param date The GDate to modify.
 */
void
gnc_gdate_set_prev_month_end (GDate *date)
{
    /* This will correctly handle the varying month lengths */
    g_date_set_day(date, 1);
    g_date_subtract_days(date, 1);
}
예제 #4
0
파일: utils.c 프로젝트: rosedu/osmo
guint
utl_get_weekend_days_in_month_my (guint month, guint year)
{
	GDate *tmpdate = NULL;
	guint i, day, days, weekend_days;

	g_return_val_if_fail (g_date_valid_dmy (1, month, year) == TRUE, 0);

	tmpdate = g_date_new_dmy (1, month, year);
	g_return_val_if_fail (tmpdate != NULL, 0);

	days = utl_date_get_days_in_month (tmpdate);
	weekend_days = 0;

	for (i = 1; i <= days; i++) {
		g_date_set_day (tmpdate, i);
		day = g_date_get_weekday (tmpdate);
		if (day == G_DATE_SATURDAY || day == G_DATE_SUNDAY) {
			weekend_days++;
		}
	}

	g_date_free (tmpdate);
	return weekend_days;
}
예제 #5
0
파일: gtkdateentry.c 프로젝트: hdd/homebank
static void gtk_dateentry_tokens(GtkWidget *gtkentry, gpointer user_data)
{
GtkDateEntry *dateentry = user_data;
const gchar *str;
GDateParseTokens pt;

 	str = gtk_entry_get_text (GTK_ENTRY (dateentry->entry));

	g_date_fill_parse_tokens(str, &pt);
	DB( g_print(" -> parsetoken return is %d values :%d %d %d\n", pt.num_ints, pt.n[0], pt.n[1], pt.n[2]) );

	// initialize with today's date
	g_date_set_time_t(dateentry->date, time(NULL));
	
	switch( pt.num_ints )
	{
		case 1:
			DB( g_print(" -> seizured 1 number\n") );
			if(g_date_valid_day(pt.n[0]))
				g_date_set_day(dateentry->date, pt.n[0]);
			break;
		case 2:
			DB( g_print(" -> seizured 2 numbers\n") );
			if( dmy_order[0] != G_DATE_YEAR )
			{
				if( dmy_order[0] == G_DATE_DAY )
				{
					if(g_date_valid_day(pt.n[0]))
					    g_date_set_day(dateentry->date, pt.n[0]);
					if(g_date_valid_month(pt.n[1]))
						g_date_set_month(dateentry->date, pt.n[1]);
				}
				else
				{
					if(g_date_valid_day(pt.n[1]))
					    g_date_set_day(dateentry->date, pt.n[1]);
					if(g_date_valid_month(pt.n[0]))
						g_date_set_month(dateentry->date, pt.n[0]);
				}
			}
			break;
	}


	
}
예제 #6
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
/** Convert a GDate to the last day of the month.  This routine has no
 *  knowledge of how many days are in a month, whether its a leap
 *  year, etc.  All that information is contained in the glib date
 *  functions.
 *
 *  @param date The GDate to modify.
 */
void
gnc_gdate_set_month_end (GDate *date)
{
    /* First set the start of next month. */
    g_date_set_day(date, 1);
    g_date_add_months(date, 1);

    /* Then back up one day */
    g_date_subtract_days(date, 1);
}
예제 #7
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
void
gnc_gdate_set_quarter_start (GDate *date)
{
    gint months;

    /* Set the date to the first day of the specified month. */
    g_date_set_day(date, 1);

    /* Back up 0-2 months */
    months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
    g_date_subtract_months(date, months);
}
예제 #8
0
파일: Recurrence.c 프로젝트: 573/gnucash
void
recurrenceSet(Recurrence *r, guint16 mult, PeriodType pt, const GDate *_start, WeekendAdjust wadj)
{
    r->ptype = VALID_PERIOD_TYPE(pt) ? pt : PERIOD_MONTH;
    r->mult = (pt == PERIOD_ONCE) ? 0 : (mult > 0 ? mult : 1);

    if (_start && g_date_valid(_start))
    {
        r->start = *_start;
    }
    else
    {
        gnc_gdate_set_today (&r->start);
    }

    /* Some of the unusual period types also specify phase.  For those
       types, we ensure that the start date agrees with that phase. */
    switch (r->ptype)
    {
    case PERIOD_END_OF_MONTH:
        g_date_set_day(&r->start, g_date_get_days_in_month
                       (g_date_get_month(&r->start),
                        g_date_get_year(&r->start)));
        break;
    case PERIOD_LAST_WEEKDAY:
    {
        GDateDay dim;
        dim = g_date_get_days_in_month(g_date_get_month(&r->start),
                                       g_date_get_year(&r->start));
        while (dim - g_date_get_day(&r->start) >= 7)
            g_date_add_days(&r->start, 7);
    }
    break;
    case PERIOD_NTH_WEEKDAY:
        if ((g_date_get_day(&r->start) - 1) / 7 == 4) /* Fifth week */
            r->ptype = PERIOD_LAST_WEEKDAY;
        break;
    default:
        break;
    }

    switch (r->ptype)
    {
    case PERIOD_MONTH:
    case PERIOD_END_OF_MONTH:
    case PERIOD_YEAR:
        r->wadj = wadj;
        break;
    default:
        r->wadj = WEEKEND_ADJ_NONE;
        break;
    }
}
예제 #9
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
void
gnc_gdate_set_quarter_end (GDate *date)
{
    gint months;

    /* Set the date to the first day of the specified month. */
    g_date_set_day(date, 1);

    /* Add 1-3 months to get the first day of the next quarter.*/
    months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
    g_date_add_months(date, 3 - months);

    /* Now back up one day */
    g_date_subtract_days(date, 1);
}
예제 #10
0
static GDate* iso2GDate(const gchar* date) {
    GDate*  gdate;
    gchar** parts = NULL;
    int     i;

    g_return_val_if_fail(date != NULL, NULL);

    gdate = g_date_new();
    parts = g_strsplit(date, "-", 3);
    if (! is_iso_string(parts))
        return NULL;
    if (!parts)
        return NULL;
    for (i = 0; i < 3; i++) {
        int t = atoi(parts[i]);
        switch (i) {
            case 0: 
                if (t < 1 || t > 9999) {
                    free_all(gdate, parts);
                    return NULL;
                }
                g_date_set_year(gdate, t);
                break;
            case 1:
                if (t < 1 || t > 12) {
                    free_all(gdate, parts);
                    return NULL;
                }
                g_date_set_month(gdate, t);
                break;
            case 2:
                if (t < 1 || t > 31) {
                    free_all(gdate, parts);
                    return NULL;
                }
                g_date_set_day(gdate, t);
                break;
        }
    }
    g_strfreev(parts);
    return gdate;
}
예제 #11
0
파일: utils.c 프로젝트: rosedu/osmo
guint
utl_get_weekend_days_in_month (const GDate *date)
{
	GDate *tmpdate = NULL;
	guint i, day, days, weekend_days;

	tmpdate = g_date_new_dmy (1, g_date_get_month (date), g_date_get_year (date));
	g_return_val_if_fail (tmpdate != NULL, 0);

	days = utl_date_get_days_in_month (tmpdate);
	weekend_days = 0;

	for (i = 1; i <= days; i++) {
		g_date_set_day (tmpdate, i);
		day = g_date_get_weekday (tmpdate);
		if (day == G_DATE_SATURDAY || day == G_DATE_SUNDAY) {
			weekend_days++;
		}
	}

	g_date_free (tmpdate);
	return weekend_days;
}
예제 #12
0
void
calendar_create_fullyear_window (GUI *appGUI)
{
GtkWidget   *vbox1, *vbox2;
GtkWidget   *hseparator;
GtkWidget   *hbuttonbox;
GtkWidget   *close_button;
GtkWidget   *hbox1;
GtkWidget   *prev_button;
GtkWidget   *info_button;
GtkObject   *fy_spinbutton_adj;
GtkWidget   *next_button;
GtkWidget   *fycal_scrolledwindow;
GtkWidget   *fycal_viewport;
GtkWidget   *fycal_alternative_view_checkbutton;
GtkWidget   *vseparator;
GtkWidget   *label;
GtkWidget   *fycal_label, *fycal_vbox;
#ifdef HAVE_LIBICAL
GtkWidget   *button;
#endif  /* HAVE_LIBICAL */
gchar       tmpbuf[BUFFER_SIZE], buffer[BUFFER_SIZE];
gint        i, j, x, y, idx;
GDate       *cdate;
guint       month, year;

	cdate = g_date_new ();
	g_return_if_fail (cdate != NULL);
	g_date_set_time_t (cdate, time (NULL));

	appGUI->cal->fullyear_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (appGUI->cal->fullyear_window), _("Full-year calendar"));
	gtk_window_set_position (GTK_WINDOW (appGUI->cal->fullyear_window), GTK_WIN_POS_CENTER_ALWAYS);
	gtk_window_set_default_size (GTK_WINDOW (appGUI->cal->fullyear_window), config.fy_window_size_x, config.fy_window_size_y);
	gtk_window_set_modal (GTK_WINDOW (appGUI->cal->fullyear_window), TRUE);
	g_signal_connect (G_OBJECT (appGUI->cal->fullyear_window), "delete_event",
	                  G_CALLBACK (fullyear_window_close_cb), appGUI);
	gtk_window_set_transient_for (GTK_WINDOW (appGUI->cal->fullyear_window), GTK_WINDOW (appGUI->main_window));
	gtk_container_set_border_width (GTK_CONTAINER (appGUI->cal->fullyear_window), 8);
	g_signal_connect (G_OBJECT (appGUI->cal->fullyear_window), "key_press_event",
	                  G_CALLBACK (fullyear_key_press_cb), appGUI);

	vbox1 = gtk_vbox_new (FALSE, 0);
	gtk_widget_show (vbox1);
	gtk_container_add (GTK_CONTAINER (appGUI->cal->fullyear_window), vbox1);

	hbox1 = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox1);
	gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 4);

	g_snprintf (tmpbuf, BUFFER_SIZE, "<b>%s:</b>", _("Year"));
	label = gtk_label_new (tmpbuf);
	gtk_widget_show (label);
	gtk_box_pack_start (GTK_BOX (hbox1), label, FALSE, TRUE, 0);
	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
	gtk_misc_set_padding (GTK_MISC (label), 0, 4);

	fy_spinbutton_adj = gtk_adjustment_new (get_gdate_current_year (), JULIAN_GREGORIAN_YEAR, 9999, 1, 10, 10);
	appGUI->cal->fy_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (fy_spinbutton_adj), 1, 0);
	gtk_widget_show (appGUI->cal->fy_spinbutton);
	g_signal_connect(appGUI->cal->fy_spinbutton, "value-changed", G_CALLBACK(change_year_spin_button_cb), appGUI);
	gtk_box_pack_start (GTK_BOX (hbox1), appGUI->cal->fy_spinbutton, FALSE, FALSE, 8);
	gtk_widget_set_size_request (appGUI->cal->fy_spinbutton, 80, -1);
	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (appGUI->cal->fy_spinbutton), TRUE);
	gtk_spin_button_set_update_policy (GTK_SPIN_BUTTON (appGUI->cal->fy_spinbutton), GTK_UPDATE_IF_VALID);

	if (config.default_stock_icons) {
		info_button = gui_stock_button (GTK_STOCK_INFO, FALSE);
	} else {
		info_button = gui_stock_button (OSMO_STOCK_BUTTON_INFO, FALSE);
	}
	GTK_WIDGET_UNSET_FLAGS(info_button, GTK_CAN_FOCUS);
	gtk_widget_show (info_button);
	g_signal_connect (info_button, "clicked", G_CALLBACK (year_info_cb), appGUI);
	gtk_tooltips_set_tip (appGUI->osmo_tooltips, info_button, _("Year info"), NULL);
	gtk_button_set_relief (GTK_BUTTON (info_button), GTK_RELIEF_NONE);
	gtk_box_pack_start (GTK_BOX (hbox1), info_button, FALSE, FALSE, 2);

	vseparator = gtk_vseparator_new ();
	gtk_widget_show (vseparator);
	gtk_box_pack_start (GTK_BOX (hbox1), vseparator, FALSE, FALSE, 8);

	if (config.default_stock_icons) {
		prev_button = gui_stock_button (GTK_STOCK_GO_BACK, FALSE);
	} else {
		prev_button = gui_stock_button (OSMO_STOCK_BUTTON_PREV_YEAR, FALSE);
	}
	GTK_WIDGET_UNSET_FLAGS (prev_button, GTK_CAN_FOCUS);
	gtk_widget_show (prev_button);
	g_signal_connect (prev_button, "clicked", G_CALLBACK (change_to_previous_year_cb), appGUI);
	gtk_tooltips_set_tip (appGUI->osmo_tooltips, prev_button, _("Previous year"), NULL);
	gtk_button_set_relief (GTK_BUTTON (prev_button), GTK_RELIEF_NONE);
	gtk_box_pack_start (GTK_BOX (hbox1), prev_button, FALSE, FALSE, 2);

	if (config.default_stock_icons) {
		appGUI->cal->cyear_button = gui_stock_button (GTK_STOCK_HOME, FALSE);
	} else {
		appGUI->cal->cyear_button = gui_stock_button (OSMO_STOCK_BUTTON_TODAY, FALSE);
	}
	GTK_WIDGET_UNSET_FLAGS (appGUI->cal->cyear_button, GTK_CAN_FOCUS);
	gtk_widget_show (appGUI->cal->cyear_button);
	g_signal_connect (appGUI->cal->cyear_button, "clicked", G_CALLBACK (change_to_current_year_cb), appGUI);
	gtk_tooltips_set_tip (appGUI->osmo_tooltips, appGUI->cal->cyear_button, _("Current year"), NULL);
	gtk_button_set_relief (GTK_BUTTON (appGUI->cal->cyear_button), GTK_RELIEF_NONE);
	gtk_box_pack_start (GTK_BOX (hbox1), appGUI->cal->cyear_button, FALSE, FALSE, 2);

	if (config.default_stock_icons) {
		next_button = gui_stock_button (GTK_STOCK_GO_FORWARD, FALSE);
	} else {
		next_button = gui_stock_button (OSMO_STOCK_BUTTON_NEXT_YEAR, FALSE);
	}
	GTK_WIDGET_UNSET_FLAGS(next_button, GTK_CAN_FOCUS);
	gtk_widget_show (next_button);
	g_signal_connect (next_button, "clicked", G_CALLBACK (change_to_next_year_cb), appGUI);
	gtk_tooltips_set_tip (appGUI->osmo_tooltips, next_button, _("Next year"), NULL);
	gtk_button_set_relief (GTK_BUTTON (next_button), GTK_RELIEF_NONE);
	gtk_box_pack_start (GTK_BOX (hbox1), next_button, FALSE, FALSE, 2);

	vseparator = gtk_vseparator_new ();
	gtk_widget_show (vseparator);
	gtk_box_pack_start (GTK_BOX (hbox1), vseparator, FALSE, FALSE, 8);

	fycal_alternative_view_checkbutton = gtk_check_button_new_with_mnemonic (_("Alternative view"));
	GTK_WIDGET_UNSET_FLAGS (fycal_alternative_view_checkbutton, GTK_CAN_FOCUS);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fycal_alternative_view_checkbutton), config.fy_alternative_view);
	g_signal_connect (G_OBJECT (fycal_alternative_view_checkbutton), "toggled",
	                  G_CALLBACK (alternative_view_cb), appGUI);
	gtk_box_pack_start (GTK_BOX (hbox1), fycal_alternative_view_checkbutton, FALSE, FALSE, 2);
	gtk_widget_show (fycal_alternative_view_checkbutton);

#ifdef HAVE_LIBICAL

	if (appGUI->calendar_only == FALSE) {
		button = gtk_button_new_with_label (_("Browse iCal events"));
		GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
		gtk_widget_show (button);
		gtk_box_pack_end (GTK_BOX (hbox1), button, FALSE, FALSE, 2);
		g_signal_connect (button, "clicked", G_CALLBACK (browse_ical_events_cb), appGUI);
	}

#endif  /* HAVE_LIBICAL */

	fycal_scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (fycal_scrolledwindow);
	gtk_box_pack_start (GTK_BOX (vbox1), fycal_scrolledwindow, TRUE, TRUE, 0);
	gtk_container_set_border_width (GTK_CONTAINER (fycal_scrolledwindow), 4);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (fycal_scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	fycal_viewport = gtk_viewport_new (NULL, NULL);
	gtk_widget_show (fycal_viewport);
	gtk_container_add (GTK_CONTAINER (fycal_scrolledwindow), fycal_viewport);

	vbox2 = gtk_vbox_new (FALSE, 0);
	gtk_widget_show (vbox2);
	gtk_container_add (GTK_CONTAINER (fycal_viewport), vbox2);

	/*-------------------------------------------------------------------------------------*/
	/* First view */

	if (config.fy_simple_view == TRUE) {
		appGUI->cal->fycal_table_1 = gtk_table_new (MAX_MONTHS + 2, FULL_YEAR_COLS + 1, FALSE);
	} else {
		appGUI->cal->fycal_table_1 = gtk_table_new (MAX_MONTHS + 4, FULL_YEAR_COLS + 3, FALSE);
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fycal_alternative_view_checkbutton)) == FALSE) {
		gtk_widget_show (appGUI->cal->fycal_table_1);
	}
	gtk_box_pack_start (GTK_BOX (vbox2), appGUI->cal->fycal_table_1, TRUE, TRUE, 0);

	vseparator = gtk_vseparator_new ();
	gtk_widget_show (vseparator);
	if (config.fy_simple_view == TRUE) {
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), vseparator, 1, 2, 0, 14,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
	} else {
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), vseparator, 1, 2, 0, 16,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
	}

	hseparator = gtk_hseparator_new ();
	gtk_widget_show (hseparator);
	gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), hseparator, 2, 39, 1, 2,
	                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
	                  (GtkAttachOptions) (GTK_FILL), 0, 0);

	hseparator = gtk_hseparator_new ();
	gtk_widget_show (hseparator);
	gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), hseparator, 0, 1, 1, 2,
	                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
	                  (GtkAttachOptions) (GTK_FILL), 0, 0);

	if (config.fy_simple_view == FALSE) {

		vseparator = gtk_vseparator_new ();
		gtk_widget_show (vseparator);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), vseparator, FULL_YEAR_COLS + 2, FULL_YEAR_COLS + 3, 0, 16,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
		hseparator = gtk_hseparator_new ();
		gtk_widget_show (hseparator);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), hseparator, 2, 39, MAX_MONTHS + 2, MAX_MONTHS + 3,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_FILL), 0, 0);
		hseparator = gtk_hseparator_new ();
		gtk_widget_show (hseparator);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), hseparator, 0, 1, MAX_MONTHS + 2, MAX_MONTHS + 3,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_FILL), 0, 0);
		hseparator = gtk_hseparator_new ();
		gtk_widget_show (hseparator);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), hseparator, FULL_YEAR_COLS + 3, FULL_YEAR_COLS + 4, 1, 2,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_FILL), 0, 0);
		hseparator = gtk_hseparator_new ();
		gtk_widget_show (hseparator);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), hseparator, FULL_YEAR_COLS + 3, FULL_YEAR_COLS + 4,
		                  MAX_MONTHS + 2, MAX_MONTHS + 3,
		                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
		                  (GtkAttachOptions) (GTK_FILL), 0, 0);
	}

	g_date_set_day (cdate, 1);

	for (month = G_DATE_JANUARY; month <= G_DATE_DECEMBER; month++) {

		g_date_set_month (cdate, month);
		g_date_strftime (buffer, BUFFER_SIZE, config.fy_simple_view ? "%b" : "%B", cdate);
		g_snprintf (tmpbuf, BUFFER_SIZE, (month % 2) ? "%s" : "<span foreground='medium blue'>%s</span>", buffer);

		label = gtk_label_new (NULL);
		gtk_widget_show (label);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), label, 0, 1, month + 1, month + 2,
		                  (GtkAttachOptions) (GTK_FILL),
		                  (GtkAttachOptions) (0), 0, 0);

		gtk_misc_set_padding (GTK_MISC (label), 8, 0);
		gtk_label_set_markup (GTK_LABEL (label), tmpbuf);

		if (config.fy_simple_view == FALSE) {
			label = gtk_label_new (NULL);
			gtk_widget_show (label);
			gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), label,
			                  FULL_YEAR_COLS + 3, FULL_YEAR_COLS + 4, month + 1, month + 2,
			                  (GtkAttachOptions) (GTK_FILL),
			                  (GtkAttachOptions) (0), 0, 0);

			gtk_misc_set_padding (GTK_MISC (label), 8, 0);
			gtk_label_set_markup (GTK_LABEL (label), tmpbuf);
		}
	}

	/* start with monday */
	g_date_set_dmy (cdate, 1, 1, 2007);

	for (i = 0; i < FULL_YEAR_COLS; i++) {

		g_date_set_day (cdate, (i % DAYS_PER_WEEK) + 1);
		g_date_strftime (buffer, BUFFER_SIZE, "%a", cdate);

		if (i % 7 + 1 == G_DATE_SATURDAY || i % 7 + 1 == G_DATE_SUNDAY) {
			g_snprintf (tmpbuf, BUFFER_SIZE, "<span foreground='firebrick'>%s</span>", buffer);
		} else {
			g_snprintf (tmpbuf, BUFFER_SIZE, "%s", buffer);
		}

		label = gtk_label_new (NULL);
		gtk_widget_show (label);
		gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), label, i + 2, i + 3, 0, 1,
		                  (GtkAttachOptions) (GTK_FILL),
		                  (GtkAttachOptions) (0), 0, 0);
		gtk_misc_set_padding (GTK_MISC (label), 4, 0);
		gtk_label_set_angle (GTK_LABEL (label), 90);
		gtk_label_set_markup (GTK_LABEL (label), tmpbuf);

		if (config.fy_simple_view == FALSE) {
			label = gtk_label_new (NULL);
			gtk_widget_show (label);
			gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), label, i + 2, i + 3, MAX_MONTHS + 3, MAX_MONTHS + 4,
			                  (GtkAttachOptions) (GTK_FILL),
			                  (GtkAttachOptions) (0), 0, 0);
			gtk_misc_set_padding (GTK_MISC (label), 4, 0);
			gtk_label_set_angle (GTK_LABEL (label), 90);
			gtk_label_set_markup (GTK_LABEL (label), tmpbuf);
		}
	}

	for (month = G_DATE_JANUARY; month <= G_DATE_DECEMBER; month++) {

		for (j = 0; j < FULL_YEAR_COLS; j++) {

			i = month - 1;
			idx = i * FULL_YEAR_COLS + j;

			appGUI->cal->calendar_buttons[idx] = gtk_button_new ();
			GTK_WIDGET_UNSET_FLAGS (appGUI->cal->calendar_buttons[idx], GTK_CAN_FOCUS);
			gtk_button_set_relief (GTK_BUTTON (appGUI->cal->calendar_buttons[idx]), GTK_RELIEF_NONE);
			gtk_widget_show (appGUI->cal->calendar_buttons[idx]);
			gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_1), appGUI->cal->calendar_buttons[idx], j+2, j+3, i+2, i+3,
			                  (GtkAttachOptions) (GTK_FILL),
			                  (GtkAttachOptions) (0), 0, 0);
		}
	}

	display_calendar (get_current_year (), appGUI);
	gtk_widget_set_sensitive (appGUI->cal->cyear_button, FALSE);

	/*-------------------------------------------------------------------------------------*/
	/* Second view */

	appGUI->cal->fycal_table_2 = gtk_table_new (3, 4, FALSE);

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fycal_alternative_view_checkbutton)) == TRUE) {
		gtk_widget_show (appGUI->cal->fycal_table_2);
	}
	gtk_box_pack_start (GTK_BOX (vbox2), appGUI->cal->fycal_table_2, TRUE, TRUE, 0);

	for (i = y = 0; y < 3; y++) {

		for (x = 0; x < 4; x++, i++) {

			fycal_vbox = gtk_vbox_new (FALSE, 0);
			gtk_widget_show (fycal_vbox);
			gtk_table_attach (GTK_TABLE (appGUI->cal->fycal_table_2), fycal_vbox, x, x + 1, y, y + 1,
			                  (GtkAttachOptions) (GTK_FILL | GTK_EXPAND),
			                  (GtkAttachOptions) (0), 0, 0);
			fycal_label = gtk_label_new (NULL);
			gtk_widget_show (fycal_label);
			gtk_box_pack_start (GTK_BOX (fycal_vbox), fycal_label, FALSE, FALSE, 0);
			appGUI->cal->fy_calendars[i] = gui_calendar_new ();
			gtk_widget_show (appGUI->cal->fy_calendars[i]);
			GTK_WIDGET_UNSET_FLAGS (appGUI->cal->fy_calendars[i], GTK_CAN_FOCUS);
			gui_calendar_set_display_options (GUI_CALENDAR (appGUI->cal->fy_calendars[i]),
			                                  (config.display_options & (GUI_CALENDAR_SHOW_DAY_NAMES |
			                                  GUI_CALENDAR_WEEK_START_MONDAY)) | GUI_CALENDAR_NO_MONTH_CHANGE);
			gui_calendar_enable_cursor (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), FALSE);
			gui_calendar_set_event_marker_type (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.event_marker_type);
			gui_calendar_set_event_marker_color (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.mark_color);
			gui_calendar_set_selector_color (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.selection_color);
			gui_calendar_set_header_color (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.header_color);
			gui_calendar_set_weekend_color (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.weekend_color);
			gui_calendar_set_day_note_marker_type (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.day_note_marker);
			gui_calendar_set_today_marker_type (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.today_marker_type);
			gui_calendar_set_today_marker_color (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.mark_current_day_color);
			gui_calendar_set_today_marker_alpha (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), config.mark_current_day_alpha);

			year = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (appGUI->cal->fy_spinbutton));
			month = i + 1;

			gui_calendar_select_month (GUI_CALENDAR (appGUI->cal->fy_calendars[i]), month - 1, year);
			mark_events (appGUI->cal->fy_calendars[i], month - 1, year, appGUI);
			g_date_set_dmy (cdate, 1, month, year);
			g_date_strftime (buffer, BUFFER_SIZE, "%B", cdate);
			gtk_label_set_text (GTK_LABEL (fycal_label), buffer);
			gtk_box_pack_start (GTK_BOX (fycal_vbox), appGUI->cal->fy_calendars[i], FALSE, FALSE, 0);
		}
	}

	/*-------------------------------------------------------------------------------------*/

	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_fullyear_window_close_cb), appGUI);
	gtk_container_add (GTK_CONTAINER (hbuttonbox), close_button);

	gtk_widget_show (appGUI->cal->fullyear_window);
}
예제 #13
0
파일: mrp-time.c 프로젝트: Zaba999/planner
void
mrp_time2_align_next (MrpTime *t, MrpTimeUnit unit)
{
    GDateWeekday weekday;
    GDateMonth   month;

    g_return_if_fail (t != NULL);

    switch (unit) {
    case MRP_TIME_UNIT_HOUR:
        t->min = 0;
        t->sec = 0;
        mrp_time2_add_hours (t, 1);
        break;

    case MRP_TIME_UNIT_TWO_HOURS:
        t->min = 0;
        t->sec = 0;
        mrp_time2_add_hours (t, 2 - t->hour % 2);
        break;

    case MRP_TIME_UNIT_HALFDAY:
        t->min = 0;
        t->sec = 0;
        if (t->hour < 12) {
            t->hour = 12;
        } else {
            t->hour = 0;
            mrp_time2_add_days (t, 1);
        }
        break;

    case MRP_TIME_UNIT_DAY:
        t->hour = 0;
        t->min = 0;
        t->sec = 0;
        mrp_time2_add_days (t, 1);
        break;

    case MRP_TIME_UNIT_WEEK:
        /* FIXME: We currently hardcode monday as week start .*/
        weekday = g_date_get_weekday (&t->date);
        t->hour = 0;
        t->min = 0;
        t->sec = 0;
        mrp_time2_add_days (t, 8 - weekday);
        break;

    case MRP_TIME_UNIT_MONTH:
        t->hour = 0;
        t->min = 0;
        t->sec = 0;
        g_date_set_day (&t->date, 1);
        g_date_add_months (&t->date, 1);
        break;

    case MRP_TIME_UNIT_QUARTER:
        t->hour = 0;
        t->min = 0;
        t->sec = 0;
        g_date_set_day (&t->date, 1);
        month = g_date_get_month (&t->date);
        if (month >= 1 && month <= 3) {
            g_date_set_month (&t->date, 4);
        }
        else if (month >= 4 && month <= 6) {
            g_date_set_month (&t->date, 7);
        }
        else if (month >= 7 && month <= 9) {
            g_date_set_month (&t->date, 10);
        }
        else if (month >= 10 && month <= 12) {
            g_date_set_month (&t->date, 1);
            g_date_add_years (&t->date, 1);
        }
        break;

    case MRP_TIME_UNIT_HALFYEAR:
        g_date_set_day (&t->date, 1);
        t->hour = 0;
        t->min = 0;
        t->sec = 0;
        month = g_date_get_month (&t->date);
        if (month >= 1 && month <= 6) {
            g_date_set_month (&t->date, 7);
        } else if (month >= 7 && month <= 12) {
            g_date_set_month (&t->date, 1);
            g_date_add_years (&t->date, 1);
        }
        break;

    case MRP_TIME_UNIT_YEAR:
        t->hour = 0;
        t->min = 0;
        t->sec = 0;
        g_date_set_day (&t->date, 1);
        g_date_set_month (&t->date, 1);
        g_date_add_years (&t->date, 1);
        break;

    case MRP_TIME_UNIT_NONE:
    default:
        g_assert_not_reached ();
    }
}
예제 #14
0
/**
 * called when a calendar receive a key-press-event
 *
 * \param pCalendar
 * \param ev
 * \param entry
 *
 * \return TRUE to block the signal
 * */
gboolean gsb_calendar_entry_calendar_key_press ( GtkCalendar *pCalendar,
						 GdkEventKey *ev,
						 GtkWidget *entry )
{
    guint day, month, year;
    GDate *date;
    GtkWidget *pTopLevelWidget;

    /* get the popup to destroy it if need */
    pTopLevelWidget = gtk_widget_get_toplevel ( GTK_WIDGET ( pCalendar ) );

    /* most of the time, we will use date so can get it here,
     * think about free it if not used */
    gtk_calendar_get_date ( pCalendar, &year, &month, &day );
    month++;
    date = g_date_new_dmy (day, month, year);

    switch ( ev -> keyval )
    {
	case GDK_Escape :
	    /* just close the calendar if it's a popup */
	    if ( GTK_WIDGET_TOPLEVEL ( pTopLevelWidget ) )
		gtk_widget_destroy ( pTopLevelWidget );
	    g_date_free (date);
	    return TRUE;
	    break ;

	case GDK_Return :
	case GDK_KP_Enter :
	    /* get the date an close the calendar */
	    gtk_entry_set_text ( GTK_ENTRY ( entry ),
				 gsb_format_date ( day, month, year ));
	    if ( GTK_WIDGET_TOPLEVEL ( pTopLevelWidget ) )
		gtk_widget_destroy ( pTopLevelWidget );
	    g_date_free (date);
	    return TRUE;
	    break ;

	    /* from now, it will change date so just use date, modify it and fill day, month, year
	     * we will set the calendar at the end of that function
	     * so after now, only keys which change the date */
	case GDK_Left :
	case GDK_KP_Left:
	case GDK_minus:
	case GDK_KP_Subtract:
	    /* day before */
	    g_date_subtract_days (date, 1);
	    break ;

	case GDK_Right :
	case GDK_KP_Right:
	case GDK_plus:
	case GDK_KP_Add:
	    /* day after */
	    g_date_add_days (date, 1);
	    break ;

	case GDK_Up :
	case GDK_KP_Up :
	    /* prev week */
	    g_date_subtract_days (date, 7);
	    break ;

	case GDK_Down :
	case GDK_KP_Down :
	    /* next week */
	    g_date_add_days (date, 7);
	    break ;

	case GDK_Home :
	case GDK_KP_Home :
	    /* go to first day of the month */
	    g_date_set_day (date, 1);
	    break ;

	case GDK_End :
	case GDK_KP_End :
	    /* go to last day of the month */
	    g_date_set_day (date,
			    g_date_get_days_in_month (month, year));
	    break ;

	case GDK_Page_Up :
	case GDK_KP_Page_Up :
	    /* prev month */
	    g_date_subtract_months (date, 1);
	    break ;

	case GDK_Page_Down :
	case GDK_KP_Page_Down :
	    /* next month */
	    g_date_add_months (date, 1);
	    break ;

	default:
	    return TRUE;
    }

    day = g_date_get_day (date);
    month = g_date_get_month (date);
    year = g_date_get_year (date);
    g_date_free (date);

    /* to avoid a warning */
    gtk_calendar_select_day( pCalendar , 15 );

    month--;
    gtk_calendar_select_month ( pCalendar , month, year );
    gtk_calendar_select_day( pCalendar , day );
    return TRUE;
}
예제 #15
0
파일: Recurrence.c 프로젝트: 573/gnucash
/* This is the only real algorithm related to recurrences.  It goes:
   Step 1) Go forward one period from the reference date.
   Step 2) Back up to align to the phase of the start date.
*/
void
recurrenceNextInstance(const Recurrence *r, const GDate *ref, GDate *next)
{
    PeriodType pt;
    const GDate *start;
    guint mult;
    WeekendAdjust wadj;

    g_return_if_fail(r);
    g_return_if_fail(ref);
    g_return_if_fail(g_date_valid(&r->start));
    g_return_if_fail(g_date_valid(ref));

    /* If the ref date comes before the start date then the next
       occurrence is always the start date, and we're done. */
    start = &r->start;
    if (g_date_compare(ref, start) < 0)
    {
        g_date_set_julian(next, g_date_get_julian(start));
        return;
    }
    g_date_set_julian(next, g_date_get_julian(ref)); /* start at refDate */

    /* Step 1: move FORWARD one period, passing exactly one occurrence. */
    mult = r->mult;
    pt = r->ptype;
    wadj = r->wadj;
    switch (pt)
    {
    case PERIOD_YEAR:
        mult *= 12;
        /* fall through */
    case PERIOD_MONTH:
    case PERIOD_NTH_WEEKDAY:
    case PERIOD_LAST_WEEKDAY:
    case PERIOD_END_OF_MONTH:
        /* Takes care of short months. */
        if (r->wadj == WEEKEND_ADJ_BACK &&
                (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) &&
                (g_date_get_weekday(next) == G_DATE_SATURDAY || g_date_get_weekday(next) == G_DATE_SUNDAY))
        {
            /* Allows the following Friday-based calculations to proceed if 'next'
               is between Friday and the target day. */
            g_date_subtract_days(next, g_date_get_weekday(next) == G_DATE_SATURDAY ? 1 : 2);
        }
        if (r->wadj == WEEKEND_ADJ_BACK &&
                (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) &&
                g_date_get_weekday(next) == G_DATE_FRIDAY)
        {
            GDate tmp_sat;
            GDate tmp_sun;
            g_date_set_julian(&tmp_sat, g_date_get_julian(next));
            g_date_set_julian(&tmp_sun, g_date_get_julian(next));
            g_date_add_days(&tmp_sat, 1);
            g_date_add_days(&tmp_sun, 2);

            if (pt == PERIOD_END_OF_MONTH)
            {
                if (g_date_is_last_of_month(next) ||
                        g_date_is_last_of_month(&tmp_sat) ||
                        g_date_is_last_of_month(&tmp_sun))
                    g_date_add_months(next, mult);
                else
                    /* one fewer month fwd because of the occurrence in this month */
                    g_date_add_months(next, mult - 1);
            }
            else
            {
                if (g_date_get_day(&tmp_sat) == g_date_get_day(start))
                {
                    g_date_add_days(next, 1);
                    g_date_add_months(next, mult);
                }
                else if (g_date_get_day(&tmp_sun) == g_date_get_day(start))
                {
                    g_date_add_days(next, 2);
                    g_date_add_months(next, mult);
                }
                else if (g_date_get_day(next) >= g_date_get_day(start))
                {
                    g_date_add_months(next, mult);
                }
                else if (g_date_is_last_of_month(next))
                {
                    g_date_add_months(next, mult);
                }
                else if (g_date_is_last_of_month(&tmp_sat))
                {
                    g_date_add_days(next, 1);
                    g_date_add_months(next, mult);
                }
                else if (g_date_is_last_of_month(&tmp_sun))
                {
                    g_date_add_days(next, 2);
                    g_date_add_months(next, mult);
                }
                else
                {
                    /* one fewer month fwd because of the occurrence in this month */
                    g_date_add_months(next, mult - 1);
                }
            }
        }
        else if ( g_date_is_last_of_month(next) ||
                  ((pt == PERIOD_MONTH || pt == PERIOD_YEAR) &&
                   g_date_get_day(next) >= g_date_get_day(start)) ||
                  ((pt == PERIOD_NTH_WEEKDAY || pt == PERIOD_LAST_WEEKDAY) &&
                   nth_weekday_compare(start, next, pt) <= 0) )
            g_date_add_months(next, mult);
        else
            /* one fewer month fwd because of the occurrence in this month */
            g_date_add_months(next, mult - 1);
        break;
    case PERIOD_WEEK:
        mult *= 7;
        /* fall through */
    case PERIOD_DAY:
        g_date_add_days(next, mult);
        break;
    case PERIOD_ONCE:
        g_date_clear(next, 1);  /* We already caught the case where ref is */
        return;                 /* earlier than start, so this is invalid. */
    default:
        PERR("Invalid period type");
        break;
    }

    /* Step 2: Back up to align to the base phase. To ensure forward
       progress, we never subtract as much as we added (x % mult < mult). */
    switch (pt)
    {
    case PERIOD_YEAR:
    case PERIOD_MONTH:
    case PERIOD_NTH_WEEKDAY:
    case PERIOD_LAST_WEEKDAY:
    case PERIOD_END_OF_MONTH:
    {
        guint dim, n_months;

        n_months = 12 * (g_date_get_year(next) - g_date_get_year(start)) +
                   (g_date_get_month(next) - g_date_get_month(start));
        g_date_subtract_months(next, n_months % mult);

        /* Ok, now we're in the right month, so we just have to align
           the day in one of the three possible ways. */
        dim = g_date_get_days_in_month(g_date_get_month(next),
                                       g_date_get_year(next));
        if (pt == PERIOD_LAST_WEEKDAY || pt == PERIOD_NTH_WEEKDAY)
        {
            gint wdresult = nth_weekday_compare(start, next, pt);
            if (wdresult < 0)
            {
                wdresult = -wdresult;
                g_date_subtract_days(next, wdresult);
            }
            else
                g_date_add_days(next, wdresult);
        }
        else if (pt == PERIOD_END_OF_MONTH || g_date_get_day(start) >= dim)
            g_date_set_day(next, dim);  /* last day in the month */
        else
            g_date_set_day(next, g_date_get_day(start)); /*same day as start*/

        /* Adjust for dates on the weekend. */
        if (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH)
        {
            if (g_date_get_weekday(next) == G_DATE_SATURDAY || g_date_get_weekday(next) == G_DATE_SUNDAY)
            {
                switch (wadj)
                {
                case WEEKEND_ADJ_BACK:
                    g_date_subtract_days(next, g_date_get_weekday(next) == G_DATE_SATURDAY ? 1 : 2);
                    break;
                case WEEKEND_ADJ_FORWARD:
                    g_date_add_days(next, g_date_get_weekday(next) == G_DATE_SATURDAY ? 2 : 1);
                    break;
                case WEEKEND_ADJ_NONE:
                default:
                    break;
                }
            }
        }

    }
    break;
    case PERIOD_WEEK:
    case PERIOD_DAY:
        g_date_subtract_days(next, g_date_days_between(start, next) % mult);
        break;
    default:
        PERR("Invalid period type");
        break;
    }
}
예제 #16
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
void
gnc_gdate_set_year_end (GDate *date)
{
    g_date_set_month(date, G_DATE_DECEMBER);
    g_date_set_day(date, 31);
}
예제 #17
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
void
gnc_gdate_set_year_start (GDate *date)
{
    g_date_set_month(date, G_DATE_JANUARY);
    g_date_set_day(date, 1);
}
예제 #18
0
void
calendar_draw_page (GtkPrintOperation *operation, GtkPrintContext *context, gint npage, gpointer user_data)
{
	PangoLayout *layout;
	PangoFontDescription *month_name_font, *day_name_font, *day_num_font, *event_font;
	cairo_t *cr;
	GDate *date;
	gdouble page_width, page_height, day_width, day_height;
	gint text_width, text_height, header_height, event_height, mnf_height, dnf_height, duf_height;
	gint day, month, i, j;
	guint32 julian;
	gboolean monday, actual;

	gchar buffer[BUFFER_SIZE];

	gint padding = config.cal_print_padding;

	GUI *appGUI = (GUI *) user_data;

	date = g_date_new_julian (g_date_get_julian (appGUI->cal->date));
	g_return_if_fail (date != NULL);

	cr = gtk_print_context_get_cairo_context (context);
	layout = gtk_print_context_create_pango_layout (context);

	month_name_font = pango_font_description_from_string (config.cal_print_month_name_font);
	day_name_font = pango_font_description_from_string (config.cal_print_day_name_font);
	day_num_font = pango_font_description_from_string (config.cal_print_day_num_font);
	event_font = pango_font_description_from_string (config.cal_print_event_font);

	pango_layout_set_text (layout, "Aj", -1);

	pango_layout_set_font_description (layout, month_name_font);
	pango_layout_get_pixel_size (layout, NULL, &mnf_height);
	mnf_height *= 1.2;

	pango_layout_set_font_description (layout, day_name_font);
	pango_layout_get_pixel_size (layout, NULL, &dnf_height);
	dnf_height *= 1.2;

	pango_layout_set_font_description (layout, day_num_font);
	pango_layout_get_pixel_size (layout, NULL, &duf_height);

	page_width = gtk_print_context_get_width (context);
	day_width = page_width / 7;

	page_height = gtk_print_context_get_height (context);
	header_height = mnf_height + dnf_height;
	day_height = (page_height - header_height) / 6;
	event_height = day_height - duf_height - padding * 3;

	cairo_set_line_width (cr, 1);
	monday = (config.display_options & GUI_CALENDAR_WEEK_START_MONDAY) ? TRUE : FALSE;


	/* Month and year */
	pango_layout_set_font_description (layout, month_name_font);
	g_date_strftime (buffer, BUFFER_SIZE, "%B %Y", date);
	pango_layout_set_text (layout, buffer, -1);
	pango_layout_get_pixel_size (layout, &text_width, NULL);

	cairo_move_to (cr, (page_width - text_width) / 2, 0);
	pango_cairo_show_layout (cr, layout);


	/* Day names */
	pango_layout_set_font_description (layout, day_name_font);

	for (i = 0; i < 7; i++) {
		g_snprintf (buffer, BUFFER_SIZE, "%s", utl_get_day_name (i + 7 + monday, FALSE));
		pango_layout_set_text (layout, buffer, -1);
		pango_layout_get_pixel_size (layout, &text_width, NULL);
		cairo_move_to (cr, day_width * i + (day_width - text_width) / 2, mnf_height);
		pango_cairo_show_layout (cr, layout);
	}


	/* Day */
	g_date_set_day (date, 1);
	day = g_date_get_weekday (date);
	month = g_date_get_month (date);

	day = monday ? day - 1 : day % 7;

	if (day > 0)
		g_date_subtract_days (date, day);

	day = g_date_get_day (date);
	julian = g_date_get_julian (date);

	pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
	pango_layout_set_width (layout, (day_width - padding * 2) * PANGO_SCALE);
	pango_layout_set_height (layout, event_height * PANGO_SCALE);
	pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
	pango_layout_set_indent (layout, -4 * PANGO_SCALE);

	for (i = 0; i < 6; i++) {

		for (j = 0; j < 7; j++) {

			actual = (month == g_date_get_month (date)) ? TRUE : FALSE;
			day = g_date_get_day (date);

			cairo_rectangle (cr, day_width * j, header_height + day_height * i, day_width, day_height);

			if (actual) {
				cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
			} else {
				cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
			}

			cairo_fill_preserve (cr);
			cairo_set_source_rgb (cr, 0, 0, 0);
			cairo_stroke (cr);

			pango_layout_set_font_description (layout, day_num_font);

			if (actual) {

				cairo_move_to (cr, day_width * j + padding, header_height + day_height * i + padding);

				if ((j == 0 && !monday) || (j == 5 && monday) || j == 6) {
					g_snprintf (buffer, BUFFER_SIZE, "<span color=\"red\">%d</span>", day);
				} else {
					g_snprintf (buffer, BUFFER_SIZE, "%d", day);
				}

				pango_layout_set_markup (layout, buffer, -1);
				pango_cairo_show_layout (cr, layout);

				cal_print_get_events (buffer, julian, appGUI);

				pango_layout_set_markup (layout, "", -1);
				pango_layout_set_text (layout, buffer, -1);
				pango_layout_set_font_description (layout, event_font);
				pango_layout_get_pixel_size (layout, NULL, &text_height);
				cairo_move_to (cr, day_width * j + padding, header_height + day_height * (i + 1) - text_height - padding);
				pango_cairo_show_layout (cr, layout);

			} else {

				cairo_move_to (cr, day_width * j + padding, header_height + day_height * i + padding);
				g_snprintf (buffer, BUFFER_SIZE, "<span color=\"white\">%d</span>", day);
				pango_layout_set_markup (layout, buffer, -1);
				pango_cairo_show_layout (cr, layout);

			}

			g_date_add_days (date, 1);
			julian++;

		}
	}

	g_date_free (date);
	pango_font_description_free (month_name_font);
	pango_font_description_free (day_name_font);
	pango_font_description_free (day_num_font);
	pango_font_description_free (event_font);
	g_object_unref (layout);
}
예제 #19
0
/**
 * Update the example calendar; make sure to take into account the end
 * specification.
 **/
static void
sxftd_update_example_cal( SXFromTransInfo *sxfti )
{
    struct tm *tmpTm;
    time_t tmp_tt;
    GDate date, startDate, nextDate;
    GList *schedule = NULL;
    getEndTuple get;

    get = sxftd_get_end_info( sxfti );

    tmp_tt = gnc_date_edit_get_date( sxfti->startDateGDE );
    tmpTm = g_new0( struct tm, 1 );
    *tmpTm = *localtime( &tmp_tt );
    g_date_clear(&date, 1);
    g_date_set_day( &date, tmpTm->tm_mday );
    g_date_set_month( &date, tmpTm->tm_mon + 1 );
    g_date_set_year( &date, tmpTm->tm_year + 1900 );
    g_free( tmpTm );

    sxftd_update_schedule(sxfti, &date, &schedule);

    /* go one day before what's in the box so we can get the correct start
     * date. */
    startDate = date;
    g_date_subtract_days(&date, 1);
    g_date_clear(&nextDate, 1);
    recurrenceListNextInstance(schedule, &date, &nextDate);

    {
        GtkWidget *w;
        gchar *name;
        /* get the name */
        w = glade_xml_get_widget(sxfti->gxml, SXFTD_NAME_ENTRY);
        name = gtk_editable_get_chars(GTK_EDITABLE(w), 0, -1);
        gnc_dense_cal_store_update_name(sxfti->dense_cal_model, name);
        g_free(name);
    }

    {
        gchar *schedule_desc;
        schedule_desc = recurrenceListToCompactString(schedule);
        gnc_dense_cal_store_update_info(sxfti->dense_cal_model, schedule_desc);
        g_free(schedule_desc);
    }

    switch (get.type)
    {
    case NEVER_END:
        gnc_dense_cal_store_update_recurrences_no_end(sxfti->dense_cal_model, &startDate, schedule);
        break;
    case END_ON_DATE:
        gnc_dense_cal_store_update_recurrences_date_end(sxfti->dense_cal_model, &startDate, schedule, &get.end_date);
        break;
    case END_AFTER_N_OCCS:
        gnc_dense_cal_store_update_recurrences_count_end(sxfti->dense_cal_model, &startDate, schedule, get.n_occurrences);
        break;
    default:
        g_warning("unknown get.type [%d]\n", get.type);
        break;
    }

    gnc_dense_cal_set_month( sxfti->example_cal, g_date_get_month( &startDate ) );
    gnc_dense_cal_set_year( sxfti->example_cal, g_date_get_year( &startDate ) );

    recurrenceListFree(&schedule);
}
예제 #20
0
gboolean
gnc_handle_date_accelerator (GdkEventKey *event,
                             struct tm *tm,
                             const char *date_str)
{
    GDate gdate;

    g_return_val_if_fail (event != NULL, FALSE);
    g_return_val_if_fail (tm != NULL, FALSE);
    g_return_val_if_fail (date_str != NULL, FALSE);

    if (event->type != GDK_KEY_PRESS)
        return FALSE;

    if ((tm->tm_mday <= 0) || (tm->tm_mon == -1) || (tm->tm_year == -1))
        return FALSE;

    // Make sure we have a valid date before we proceed
    if (!g_date_valid_dmy (tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900))
        return FALSE;

    g_date_set_dmy (&gdate,
                    tm->tm_mday,
                    tm->tm_mon + 1,
                    tm->tm_year + 1900);

    /*
     * Check those keys where the code does different things depending
     * upon the modifiers.
     */
    switch (event->keyval)
    {
    case GDK_KEY_KP_Add:
    case GDK_KEY_plus:
    case GDK_KEY_equal:
        if (event->state & GDK_SHIFT_MASK)
            g_date_add_days (&gdate, 7);
        else if (event->state & GDK_MOD1_MASK)
            g_date_add_months (&gdate, 1);
        else if (event->state & GDK_CONTROL_MASK)
            g_date_add_years (&gdate, 1);
        else
            g_date_add_days (&gdate, 1);
        g_date_to_struct_tm (&gdate, tm);
        return TRUE;

    case GDK_KEY_minus:
    case GDK_KEY_KP_Subtract:
    case GDK_KEY_underscore:
        if ((strlen (date_str) != 0) && (dateSeparator () == '-'))
        {
            const char *c;
            gunichar uc;
            int count = 0;

            /* rough check for existing date */
            c = date_str;
            while (*c)
            {
                uc = g_utf8_get_char (c);
                if (uc == '-')
                    count++;
                c = g_utf8_next_char (c);
            }

            if (count < 2)
                return FALSE;
        }

        if (event->state & GDK_SHIFT_MASK)
            g_date_subtract_days (&gdate, 7);
        else if (event->state & GDK_MOD1_MASK)
            g_date_subtract_months (&gdate, 1);
        else if (event->state & GDK_CONTROL_MASK)
            g_date_subtract_years (&gdate, 1);
        else
            g_date_subtract_days (&gdate, 1);
        g_date_to_struct_tm (&gdate, tm);
        return TRUE;

    default:
        break;
    }

    /*
     * Control and Alt key combinations should be ignored by this
     * routine so that the menu system gets to handle them.  This
     * prevents weird behavior of the menu accelerators (i.e. work in
     * some widgets but not others.)
     */
    if (event->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK))
        return FALSE;

    /* Now check for the remaining keystrokes. */
    switch (event->keyval)
    {
    case GDK_KEY_braceright:
    case GDK_KEY_bracketright:
        /* increment month */
        g_date_add_months (&gdate, 1);
        break;

    case GDK_KEY_braceleft:
    case GDK_KEY_bracketleft:
        /* decrement month */
        g_date_subtract_months (&gdate, 1);
        break;

    case GDK_KEY_M:
    case GDK_KEY_m:
        /* beginning of month */
        g_date_set_day (&gdate, 1);
        break;

    case GDK_KEY_H:
    case GDK_KEY_h:
        /* end of month */
        g_date_set_day (&gdate, 1);
        g_date_add_months (&gdate, 1);
        g_date_subtract_days (&gdate, 1);
        break;

    case GDK_KEY_Y:
    case GDK_KEY_y:
        /* beginning of year */
        g_date_set_day (&gdate, 1);
        g_date_set_month (&gdate, 1);
        break;

    case GDK_KEY_R:
    case GDK_KEY_r:
        /* end of year */
        g_date_set_day (&gdate, 1);
        g_date_set_month (&gdate, 1);
        g_date_add_years (&gdate, 1);
        g_date_subtract_days (&gdate, 1);
        break;

    case GDK_KEY_T:
    case GDK_KEY_t:
        /* today */
        gnc_gdate_set_today (&gdate);
        break;

    default:
        return FALSE;
    }

    g_date_to_struct_tm (&gdate, tm);

    return TRUE;
}
예제 #21
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
/** Convert a GDate to the first day of the prebvious month.  This
 *  routine has no knowledge of how many days are in a month, whether
 *  its a leap year, etc.  All that information is contained in the
 *  glib date functions.
 *
 *  @param date The GDate to modify.
 */
void
gnc_gdate_set_prev_month_start (GDate *date)
{
    g_date_set_day(date, 1);
    g_date_subtract_months(date, 1);
}
예제 #22
0
파일: gnc-date.cpp 프로젝트: jralls/gnucash
void
gnc_gdate_set_month_start (GDate *date)
{
    g_date_set_day(date, 1);
}
예제 #23
0
/**
 * check the scheduled transactions if the are in time limit
 * and record the automatic transactions
 * 
 * \param
 * 
 * \return
 * */
void gsb_scheduler_check_scheduled_transactions_time_limit ( void )
{
    GDate *date;
    GSList *tmp_list;
    gboolean automatic_transactions_taken = FALSE;

    devel_debug (NULL);

    /* the scheduled transactions to take will be check here,
     * but the scheduled transactions taken will be add to the already appended ones */

    scheduled_transactions_to_take = NULL;

    /* get the date today + nb_days_before_scheduled */

    /* the date untill we execute the scheduled transactions is :
     * - either today + nb_days_before_scheduled if warn n days before the scheduled
     * - either the end of the month in nb_days_before_scheduled days (so current month or next month)
     *   */
    date = gdate_today ();
    g_date_add_days ( date,
		      nb_days_before_scheduled );
    /* now date is in nb_days_before_scheduled, if we want the transactions of the month,
     * we change date to the end of its month */
    if (execute_scheduled_of_month)
    {
	gint last_day;
	
	last_day = g_date_get_days_in_month ( g_date_get_month (date),
					      g_date_get_year (date));
	g_date_set_day (date, last_day);
    }

    /* check all the scheduled transactions,
     * if automatic, it's taken
     * if manual, appended into scheduled_transactions_to_take */
    tmp_list = gsb_data_scheduled_get_scheduled_list ();

    while ( tmp_list )
    {
	gint scheduled_number;

	scheduled_number = gsb_data_scheduled_get_scheduled_number (tmp_list -> data);

	/* we check that scheduled transaction only if it's not a child of a split */
	if ( !gsb_data_scheduled_get_mother_scheduled_number (scheduled_number)
	     &&
	     gsb_data_scheduled_get_date (scheduled_number)
	     &&
	     g_date_compare ( gsb_data_scheduled_get_date (scheduled_number),
			      date ) <= 0 )
	{
	    if ( gsb_data_scheduled_get_automatic_scheduled (scheduled_number))
	    {
		/* this is an automatic scheduled, we get it */
		gint transaction_number;

		/* take automatically the scheduled transaction untill today */
		transaction_number = gsb_scheduler_create_transaction_from_scheduled_transaction (scheduled_number,
												  0 );
		if ( gsb_data_scheduled_get_split_of_scheduled (scheduled_number))
		    gsb_scheduler_execute_children_of_scheduled_transaction ( scheduled_number,
									      transaction_number );

		scheduled_transactions_taken = g_slist_append ( scheduled_transactions_taken,
								GINT_TO_POINTER (transaction_number));
		automatic_transactions_taken = TRUE;

		/* set the scheduled transaction to the next date,
		 * if it's not finished, we check them again if it need to be
		 * executed more than one time (the easiest way is to check
		 * all again, i don't think it will have thousand of scheduled transactions, 
		 * so no much waste of time...) */
		if (gsb_scheduler_increase_scheduled (scheduled_number))
		{
		    scheduled_transactions_to_take = NULL;
		    tmp_list = gsb_data_scheduled_get_scheduled_list ();
		}
		else
		    /* the scheduled is finish, so we needn't to check it again ... */
		    tmp_list = tmp_list -> next;
	    }
	    else
	    {
		/* it's a manual scheduled transaction, we put it in the slist */
		scheduled_transactions_to_take = g_slist_append ( scheduled_transactions_to_take ,
								  GINT_TO_POINTER (scheduled_number));
		tmp_list = tmp_list -> next;
	    }
	}
	else
	    tmp_list = tmp_list -> next;
    }

    if ( automatic_transactions_taken )
    {
	mise_a_jour_liste_echeances_auto_accueil = 1;
        gsb_file_set_modified ( TRUE );
    }

    if ( scheduled_transactions_to_take )
	mise_a_jour_liste_echeances_manuelles_accueil = 1;

    g_date_free ( date );
}
예제 #24
0
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;
}
예제 #25
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);
}
예제 #26
0
static int summarize(struct stats_file *data_file,
			struct stats_file *history_file,
			struct stats_file *temp_file,
			int account_period_offset)
{
	struct stats_iter data_iter;
	struct stats_iter history_iter;
	struct stats_record *cur, *next;

	GDate today, date_change_step_size;

	/*
	 * First calculate the date when switch from monthly
	 * accounting period size to daily size
	 */
	g_date_set_time_t(&today, time(NULL));

	date_change_step_size = today;
	if (g_date_get_day(&today) - account_period_offset >= 0)
		g_date_subtract_months(&date_change_step_size, 2);
	else
		g_date_subtract_months(&date_change_step_size, 3);

	g_date_set_day(&date_change_step_size, account_period_offset);


	/* Now process history file */
	cur = NULL;

	if (history_file) {
		history_iter.file = history_file;
		history_iter.begin = get_iterator_begin(history_iter.file);
		history_iter.end = get_iterator_end(history_iter.file);
		history_iter.it = history_iter.begin;

		cur = process_file(&history_iter, temp_file, NULL,
					&date_change_step_size,
					account_period_offset);
	}

	data_iter.file = data_file;
	data_iter.begin = get_iterator_begin(data_iter.file);
	data_iter.end = get_iterator_end(data_iter.file);
	data_iter.it = data_iter.begin;

	/*
	 * Ensure date_file records are newer than the history_file
	 * record
	 */
	if (cur) {
		next = get_next_record(&data_iter);
		while (next && cur->ts > next->ts)
			next = get_next_record(&data_iter);
	}

	/* And finally process the new data records */
	cur = process_file(&data_iter, temp_file, cur,
				&date_change_step_size, account_period_offset);

	if (cur)
		append_record(temp_file, cur);

	return 0;
}