Search code examples
iosnsdatenscalendarnsdatecomponentsnsformatter

Weird behaviour with only one device and NSDate


After weeks of work, I finally released my application yesterday :-) 20 iphones have downloaded my app, and only one is having a problem. I have absolutely no way to debug it because I cannot have access to the device which has the problem.

First of all, I get the json (which is decoded well, even on THE device), then I extract a date:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];

NSDate *date = [formatter dateFromString:[myData objectForKey:@"date"]];

I want to know when this date was, so I use this function:

[DateManager getDateFromNow:date];

Which leads to this code:

+ (NSString *) getDateFromNow: (NSDate*) aDate{
    NSDate *now = [[NSDate alloc] init];

    // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];

    // Get conversion to months, days, hours, minutes
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:aDate  toDate:now  options:0];

    int second =[conversionInfo second];
    int minute =[conversionInfo minute];
    int hour =[conversionInfo hour];
    int day =[conversionInfo day];

    if(day != 0){
        if(day == 1){
            return [NSString stringWithFormat:@"1 day ago"];
        }else{
            return [NSString stringWithFormat:@"%i days ago", day];
        }
    }else if(hour != 0){
        if(hour == 1){
            return [NSString stringWithFormat:@"1 hour ago"];
        }else{
            return [NSString stringWithFormat:@"%i hours ago", hour];
        }
    }else if(minute !=0){
        if(minute == 1){
            return [NSString stringWithFormat:@"1 minute ago"];
        }else{
            return [NSString stringWithFormat:@"%i minutes ago", minute];
        }
    }else{
        if(second == 1){
            return [NSString stringWithFormat:@"1 second ago"];
        }else{
            return [NSString stringWithFormat:@"%i seconds ago", second];
        }
    }

}

My problem is, that for only one device, the date is calculated as '4932 days ago', and for all other devices it's working well. Here is the date: date: "2014-07-04 13:38:01"

Do you have any clue on what could be wrong?

Many thanks in advance...

edit : 4932 days ago was the first january of 2001. The apple reference date.


Solution

  • I think the problem is systemTimeZone or NSCalendar.

    When you are doing date-string conversion you should set NSCalendar.

    NSCalendar *sysCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
    // set your NSDateFormatter with calendar.
    formatter.calendar = sysCalendar;
    

    It will help and one more thing is you should always set UTC date or timestamp from server end.

    Thanks. Maybe it will help you.