NSDateComponents 将 UTC 日期转换为本地时区,搞乱了 "today" nsdate 比较

NSDateComponents converting UTC date to local timezone, messing up "today" nsdate comparison

我正在尝试使用 NSDateComponents 来测试是否出现动态日期 "today"。我用它来提取 year/month/day,然后比较两个 NSDate 的这些值。从理论上讲,这应该有效。我在东海岸,所以 EST,UTC -5。

当前实际时间:12:40:53,美国东部时间

这是正确的 NSDates,采用 UTC 格式。时间是正确的 UTC:

这是我的日志,显示了每个日期。它正在将日期转换为本地时区 2016-01-19 07:00:00。由于当前日期在一天的中间,-5 小时偏移不会导致它改变日期。然而,午夜的时间会倒退一天。我怎样才能让它尊重 UTC?

NSLog:

2016-1-19 12:40:53 (America/New_York (EST) offset -18000), 
2016-1-18 19:0:0 (America/New_York (EST) offset -18000)

代码:

NSDateComponents *current = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:_date];
NSDateComponents *activity = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:activityModel.date];

// attempt to set timezones to UTC manually, doesn't change anything   
activity.calendar = [NSCalendar currentCalendar];
activity.calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

current.calendar = [NSCalendar currentCalendar];
current.calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];


NSLog(@"%ld-%ld-%ld %ld:%ld:%ld (%@), %ld-%ld-%ld %ld:%ld:%ld (%@)", (long)[current year],(long)[current month], (long)[current day], (long)[current hour], (long)[current minute], (long)[current second], [current timeZone].description, (long)[activity year], (long)[activity month], (long)[activity day],(long)[activity hour], (long)[activity minute], (long)[activity second],[current timeZone].description);

if([current day] == [activity day] &&
           [current month] == [activity month] &&
           [current year] == [activity year] &&
           [current era] == [activity era]) {

           // do something if activity on current day
}

您正在更改 NSDateComponents 的时区。但是您想在调用 components(或调用 NSCalendar 日期检查例程)之前更改日历的时区,例如:

NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

if ([calendar isDate:_date inSameDayAsDate:activityModel.date]) {
    NSLog(@"same");
} else {
    NSLog(@"diff");
}

但是如果当地时间是 2016-01-19 21:00:00 -0500(即 20 日,UTC)呢?你想让它说它也是你例子中 activity 日期的同一天吗?如果是这样,那么您实际上是在说要对 _date 使用本地日历,对 activityModel.date 使用 UTC,例如:

NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *current = [calendar components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:_date];

calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

NSDateComponents *activity = [calendar components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:activityModel.date];

if ([current day] == [activity day] && [current month] == [activity month] && [current year] == [activity year] && [current era] == [activity era]) {
    NSLog(@"same");
} else {
    NSLog(@"diff");
}