/**
 * time_add_month_with_zone:
 * @time: A time_t value.
 * @months: Number of months to add.
 * @zone: Timezone to use.
 *
 * Adds or subtracts a number of months to/from the given time_t value, using
 * the given timezone.
 *
 * If the day would be off the end of the month (e.g. adding 1 month to
 * 30th January, would lead to an invalid day, 30th February), it moves it
 * down to the last day in the month, e.g. 28th Feb (or 29th in a leap year.)
 *
 * NOTE: this function is only here to make the transition to the timezone
 * functions easier. New code should use icaltimetype values and
 * icaltime_adjust() to add or subtract days, hours, minutes & seconds.
 *
 * Return value: a time_t value containing @time plus the months added.
 */
time_t
time_add_month_with_zone (time_t time, int months, icaltimezone *zone)
{
	struct icaltimetype tt;
	int day, days_in_month;

	/* Convert to an icaltimetype. */
	tt = icaltime_from_timet_with_zone (time, FALSE, zone);

	/* Add on the number of months. */
	tt.month += months;

	/* Save the day, and set it to 1, so we don't overflow into the next
	   month. */
	day = tt.day;
	tt.day = 1;

	/* Normalize it, fixing any month overflow. */
	tt = icaltime_normalize (tt);

	/* If we go past the end of a month, set it to the last day. */
	days_in_month = time_days_in_month (tt.year, tt.month - 1);
	if (day > days_in_month)
		day = days_in_month;

	tt.day = day;

	/* Convert back to a time_t. */
	return icaltime_as_timet_with_zone (tt, zone);
}
Esempio n. 2
0
time_t
time_add_month (time_t time, int months)
{
	struct tm *tm = localtime (&time);
	time_t new_time;
	int mday;

	mday = tm->tm_mday;
	
	tm->tm_mon += months;
	tm->tm_isdst = -1;
	if ((new_time = mktime (tm)) == -1){
		g_warning ("mktime could not handling adding a month with\n");
		print_time_t (time);
		return time;
	}
	tm = localtime (&new_time);
	if (tm->tm_mday < mday){
		tm->tm_mon--;
		tm->tm_mday = time_days_in_month (tm->tm_year+1900, tm->tm_mon);
		return new_time = mktime (tm);
	}
	else
		return new_time;
}
Esempio n. 3
0
static gboolean
calculate_day_month_for_coord (GcalYearView *year_view,
                               gdouble       x,
                               gdouble       y,
                               gint         *out_day,
                               gint         *out_month)
{
  GcalYearViewPrivate *priv = year_view->priv;
  gint row, column, i, sw, box_side, clicked_cell, day, month;

  row = -1;
  column = -1;
  box_side = priv->navigator_grid->box_side;
  sw = 1 - 2 * priv->k;

  /* y selection */
  for (i = 0; i < 4 && ((row == -1) || (column == -1)); i++)
    {
      if (row == -1 &&
          y > priv->navigator_grid->coordinates[i * 4].y + box_side &&
          y < priv->navigator_grid->coordinates[i * 4].y + box_side * 7)
        {
          row = i;
        }
      if (column == -1 &&
          x > priv->navigator_grid->coordinates[i].x + box_side * 0.5 &&
          x < priv->navigator_grid->coordinates[i].x + box_side * 7.5)
        {
          column = i;
        }
    }

  if (row == -1 || column == -1)
    return FALSE;

  month = row * 4 + column;
  row = (y - (priv->navigator_grid->coordinates[month].y + box_side)) / box_side;
  column = (x - (priv->navigator_grid->coordinates[month].x + box_side * 0.5)) / box_side;
  clicked_cell = row * 7 + column;
  day = 7 * ((clicked_cell + 7 * priv->k) / 7) + sw * (clicked_cell % 7) + (1 - priv->k);
  day -= ((time_day_of_week (1, month, priv->date->year) - priv->first_weekday + 7) % 7);

  if (day < 1 || day > time_days_in_month (priv->date->year, month))
    return FALSE;

  *out_day = day;
  *out_month = month;
  return TRUE;
}