NSDate, timezones and Unix timestamp confusion

和自甴很熟 提交于 2019-12-24 14:21:07

问题


I took over the project of some programmers who unfortunately left a lot of stuff unclear in the code. Basically we have stored some flight events in our database with a departure and arrival time. It the database (as far as we can tell) everything is stored in UTC (as it should) and in the iOS app they use some Realm objects as database model.

I basically want to look for all flights in a 24 hour period, corresponding to the 24 hour day of the user's device.

- (NSDate *)beginningOfDayInDeviceTimeZone {
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = [[NSCalendar currentCalendar] ordinalityOfUnit:(NSCalendarUnitDay) inUnit:(NSCalendarUnitEra) forDate:self];
    return [[NSCalendar currentCalendar] dateFromComponents:components];
}

- (NSDate *)endOfDayInDeviceTimeZone {
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = [[NSCalendar currentCalendar] ordinalityOfUnit:(NSCalendarUnitDay) inUnit:(NSCalendarUnitEra) forDate:self];
    components.day += 1;
    return [[NSCalendar currentCalendar] dateFromComponents:components];
}

So as I understand all times are usually Unix timestamps. So time conversion is only necessary if I want to display times. I assume however, that our programmers messed this up, but this is another story.

I want to query all events in this timeframe. I wanted to use the method our programmers created:

+ (NSMutableArray *)getDataForUser:(NSString *)userEmail betweenStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate {
    NSMutableArray *planDataArray = [NSMutableArray array];

    NSLog(@"startDate %@ endDate: %@", startDate, endDate);
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"userEmail == %@ AND (destinationTime >= %@ AND departureTime <= %@)",userEmail, startDate, endDate, startDate, endDate, startDate];
    NSLog(@"Predicate: %@", predicate); sortedResultsUsingProperty:@"departureTime" ascending:YES];

    if (results.count > 0) {
        for (FMPlanData *planData in results) {
            if (![planData isInvalidated]) {
                [planDataArray addObject:planData];
            }
        }
    }
    return planDataArray;
}

Here are my questions:

  1. Why are there more parameters (startDate and endDate), which are given to the predicate method, than there are %@ in the string for the predicate? What does this do?

  2. The NSLogs are from me. The output of one specific call now is:

2019-04-02 16:17:07.849451+0200 FollowMe[4891:158214] StartDate Tue Apr 2 00:00:00 2019
2019-04-02 16:17:07.849666+0200 FollowMe[4891:158214] EndDate: Wed Apr 3 00:00:00 2019
2019-04-02 16:17:07.849870+0200 FollowMe[4891:158214] Predicate: userEmail == "xxx" AND destinationTime >= CAST(575848800.000000, "NSDate") AND departureTime <= CAST(575935200.000000, "NSDate")

Ok and here I am confused. First of all, I get shown too many events which is wrong. But what I am mostly confused are these CAST(...) Timestamps. If I convert 575935200.000000 into a normal date I get the 1st of April in 1988. Where is my error of thought?

来源:https://stackoverflow.com/questions/55477111/nsdate-timezones-and-unix-timestamp-confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!