Logo Search packages:      
Sourcecode: libical version File versions  Download package

void icaltime_adjust ( struct icaltimetype *  tt,
const int  days,
const int  hours,
const int  minutes,
const int  seconds 
)

Internal, shouldn't be part of the public API.

Adds or subtracts a number of days, hours, minutes and seconds.

Adds (or subtracts) a time from a icaltimetype. NOTE: This function is exactly the same as icaltimezone_adjust_change() except for the type of the first parameter.

Definition at line 925 of file icaltime.c.

                                            {

    int second, minute, hour, day;
    int minutes_overflow, hours_overflow, days_overflow = 0, years_overflow;
    int days_in_month;

    /* If we are passed a date make sure to ignore hour minute and second */
    if (tt->is_date)
      goto IS_DATE;

    /* Add on the seconds. */
    second = tt->second + seconds;
    tt->second = second % 60;
    minutes_overflow = second / 60;
    if (tt->second < 0) {
      tt->second += 60;
      minutes_overflow--;
    }

    /* Add on the minutes. */
    minute = tt->minute + minutes + minutes_overflow;
    tt->minute = minute % 60;
    hours_overflow = minute / 60;
    if (tt->minute < 0) {
      tt->minute += 60;
      hours_overflow--;
    }

    /* Add on the hours. */
    hour = tt->hour + hours + hours_overflow;
    tt->hour = hour % 24;
    days_overflow = hour / 24;
    if (tt->hour < 0) {
      tt->hour += 24;
      days_overflow--;
    }

IS_DATE:
    /* Normalize the month. We do this before handling the day since we may
       need to know what month it is to get the number of days in it.
       Note that months are 1 to 12, so we have to be a bit careful. */
    if (tt->month >= 13) {
      years_overflow = (tt->month - 1) / 12;
      tt->year += years_overflow;
      tt->month -= years_overflow * 12;
    } else if (tt->month <= 0) {
      /* 0 to -11 is -1 year out, -12 to -23 is -2 years. */
      years_overflow = (tt->month / 12) - 1;
      tt->year += years_overflow;
      tt->month -= years_overflow * 12;
    }

    /* Add on the days. */
    day = tt->day + days + days_overflow;
    if (day > 0) {
      for (;;) {
          days_in_month = icaltime_days_in_month (tt->month, tt->year);
          if (day <= days_in_month)
            break;

          tt->month++;
          if (tt->month >= 13) {
            tt->year++;
            tt->month = 1;
          }

          day -= days_in_month;
      }
    } else {
      while (day <= 0) {
          if (tt->month == 1) {
            tt->year--;
            tt->month = 12;
          } else {
            tt->month--;
          }

          day += icaltime_days_in_month (tt->month, tt->year);
      }
    }
    tt->day = day;
}


Generated by  Doxygen 1.6.0   Back to index