Question

If I create a new event on ICal and set it to be a all day event - my app shows that it lasts 2 days.

I used nslog to output startdate and enddate and for event on 8.5.2012 my application says event ends on 9.5.2012 at 21.00(9pm).

What causes this and what is the best way to convert start/enddate in my application to same values that are shown on iCal. Promblem exists only with all day events.

Was it helpful?

Solution

Okay.. Here I have made some functions so other people strugling with the same problem could easily get some aid.

- (NSDate *)changeDayBy:(NSDate *)current:(NSUInteger) change
{
    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
    [components setDay: change];
    return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:current options:0];
}

- (BOOL) endDateNeedsAFix:(CalEvent *)event
    {
        if ( !event.isAllDay )
            return NO;

        NSDateComponents *comps1 = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:event.startDate] ;
        NSDateComponents *comps2 = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:event.endDate];

        return [[[NSCalendar currentCalendar] dateFromComponents: comps2] timeIntervalSinceDate: [[NSCalendar currentCalendar] dateFromComponents: comps1]] / ( 60*60*24) > 0 ? YES : NO;

    }

    - (NSDate*) endDateFixed:(CalEvent *)event
    {
        if ( !event.isAllDay )
            return event.endDate;

        NSDateComponents *comps1 = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:event.startDate];
        NSDateComponents *comps2 = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:event.endDate];

        if ( [[[NSCalendar currentCalendar] dateFromComponents: comps2] timeIntervalSinceDate: [[NSCalendar currentCalendar] dateFromComponents: comps1]] / ( 60*60*24) > 0 )
            return [self changeDayBy:event.endDate :-1];

        return event.endDate;
    }

Function endDateNeedsAFix reports if fix is needed for selected calEvent* event. It's helpful if application has some kind of event editor where end date is shown fixed, before saving changes, check if fix is needed and then use changeDayBy to add +1 to date.

endDateFixed returns NSDate from event's endDate - if not necessary, as unchanged - but if needed - it will be fixed.

OTHER TIPS

According to this: icalbuddy source

if ([event isAllDay])
  {
      // all-day events technically span from <start day> at 00:00 to <end day+1> at 00:00 even though
      // we want them displayed as only spanning from <start day> to <end day>
      NSDate *endDateMinusOneDay = dateByAddingDays([event endDate], -1);
      NSInteger daysDiff = getDayDiff([event startDate], endDateMinusOneDay);

      if (daysDiff > 0)
      {
          elements.value = M_ATTR_STR((
              strConcat(
                  dateStr([event startDate], ONLY_DATE),
                  @" - ",
                  dateStr(endDateMinusOneDay, ONLY_DATE),
                  nil
                  )
              ));
      }

it is supposed to act like that and I need to deal with it. Problem with time ( 21.00 / 9.00 pm) is just the difference in timezone propably and even though - it shouldn't matter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top