0

我试图比较 NSDates,主要是时间,以确定在即将到来的方法中应该返回哪个字符串,但首先这里是设置我正在比较的日期的调用。

- (id)init {
    if ( self = [ super init ] ) {
        self.tripTime = 0.0;
        self.timer =
        [ [ BestRouteTimer alloc ] init ];
        self.morningBeforeTrafic = [ self dateWithHour:0 andMinute:0 ];
        self.morningTrafic = [ self dateWithHour:6 andMinute:30 ];
        self.morningAfterTrafic = [ self dateWithHour:9 andMinute:30 ];
        self.afternoon = [ self dateWithHour:12 andMinute:0 ];
        self.eveningTrafic = [ self dateWithHour:15 andMinute:30 ];
        self.eveningAfterTrafic = [ self dateWithHour:18 andMinute:30 ];
        self.timeOfDay = [ self determineTimeofDayWithTime:[NSDate date ] ] ;
    }
    return self;
}

比较方法

// Will take the time passed in and return a string
//   to represent the time of day
- (NSString *) determineTimeofDayWithTime:(NSDate *)time {
    NSLog(@"the time of day is \n%@",
          //[ self standardizeDate:time ].description );
    [ time description ] );
    NSLog(@"The before morning trafic is %@",
    //      [ self standardizeDate:self.morningBeforeTrafic ].description );
    [self.morningBeforeTrafic description ]);
    NSLog(@"The morning trafic is %@",
    //      [ self standardizeDate:self.morningTrafic ].description );
    [self.morningTrafic description ]);
    NSLog(@"The after morning trafic is %@",
          //[ self standardizeDate:self.morningAfterTrafic ].description );
    [self.morningAfterTrafic description ]);
    NSLog(@"The afternoon trafic is %@",
          //[ self standardizeDate:self.afternoon ].description );
    [self.afternoon description ]);
    NSLog(@"The evening trafic is %@",
          //[ self standardizeDate:self.afternoon ].description );
    [self.eveningTrafic description ]);
    NSLog(@"The evening after trafic is %@",
          //[ self standardizeDate:self.eveningAfterTrafic ].description );
          [self.eveningAfterTrafic description ]);

    if ( ( [ time compare:self.morningBeforeTrafic ] == NSOrderedAscending ||
          [ self.morningBeforeTrafic compare:time ] == NSOrderedSame  ) &&
        [ time compare:self.morningTrafic ] == NSOrderedAscending ) {
        return @"Morning, before traffic.";
    }

    else if ( ( [ self.morningTrafic compare:time ] == NSOrderedAscending ||
               [ self.morningTrafic compare:time ] == NSOrderedSame  ) &&
             [ time compare:self.morningAfterTrafic ] == NSOrderedAscending ) {
        return @"Morning Traffic!";
    }

    else if ( ( [ self.morningAfterTrafic compare:time ] == NSOrderedAscending ||
               [ self.morningAfterTrafic compare:time ] == NSOrderedSame  ) &&
             [ time compare:self.afternoon ] == NSOrderedAscending ) {
        return @"Morning, after traffic ";
    }

    else if ( ( [ time compare:self.afternoon ] == NSOrderedAscending ||
               [ self.afternoon compare:time ] == NSOrderedSame  ) &&
             [ self.eveningTrafic compare:time ] == NSOrderedAscending ) {
        return @"Afternoon";
    }

    else if ( ( [ self.eveningTrafic compare:time ] == NSOrderedAscending ||
               [ self.eveningTrafic compare:time ] == NSOrderedSame  ) &&
             [ time compare:self.eveningAfterTrafic ] == NSOrderedAscending ) {
        return @"Evening Traffic!";
    }

    else
        return @"Evening, after traffic";

}

最后,这是生成日期的方法

- (NSDate *)dateWithHour:(int)hour andMinute:(int)minutes {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar ];

    [ calendar setTimeZone:[ NSTimeZone localTimeZone ] ];

    NSDate *today = [NSDate date];
    NSDateComponents *todayComps =
    [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit |NSTimeZoneCalendarUnit) fromDate:today ];
    todayComps.timeZone = [ NSTimeZone localTimeZone ];
    todayComps.minute = minutes;
    todayComps.hour = hour;
    NSDate *newDate = [ calendar dateFromComponents:todayComps ];
    NSLog(@"The time zone is %@", todayComps.timeZone.description );
    NSLog(@"The new date is %@", newDate.description );

    return newDate;
}

这是日志语句

2013-11-12 10:29:16.609 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.611 BestRouteFinal[2768:60b] The new date is 2013-11-12 07:00:00 +0000
2013-11-12 10:29:16.613 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.615 BestRouteFinal[2768:60b] The new date is 2013-11-12 13:30:00 +0000
2013-11-12 10:29:16.617 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.619 BestRouteFinal[2768:60b] The new date is 2013-11-12 16:30:00 +0000
2013-11-12 10:29:16.622 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.623 BestRouteFinal[2768:60b] The new date is 2013-11-12 19:00:00 +0000
2013-11-12 10:29:16.627 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.629 BestRouteFinal[2768:60b] The new date is 2013-11-12 22:30:00 +0000
2013-11-12 10:29:16.632 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.633 BestRouteFinal[2768:60b] The new date is 2013-11-13 01:30:00 +0000
2013-11-12 10:29:16.635 BestRouteFinal[2768:60b] the time of day is 
2013-11-12 17:29:16 +0000
2013-11-12 10:29:16.637 BestRouteFinal[2768:60b] The before morning trafic is 2013-11-12 07:00:00 +0000
2013-11-12 10:29:16.639 BestRouteFinal[2768:60b] The morning trafic is 2013-11-12 13:30:00 +0000
2013-11-12 10:29:16.641 BestRouteFinal[2768:60b] The after morning trafic is 2013-11-12 16:30:00 +0000
2013-11-12 10:29:16.642 BestRouteFinal[2768:60b] The afternoon trafic is 2013-11-12 19:00:00 +0000
2013-11-12 10:29:16.644 BestRouteFinal[2768:60b] The evening trafic is 2013-11-12 22:30:00 +0000
2013-11-12 10:29:16.646 BestRouteFinal[2768:60b] The evening after trafic is 2013-11-13 01:30:00 +0000

如您所见,时间差了 7 个小时。我对 ios 相当陌生,这是迄今为止我遇到的最复杂的事情。我搜索了很多帖子并尝试实施各种解决方案,但没有运气。我已经在这里待了一个多星期,不知道我还能尝试什么。

4

1 回答 1

0

NSDate没有时区的概念,因此无论您在注销时做什么,它都会默认为您提供基于 GMT 的时间。

例如,对我来说,现在是美国东部标准时间下午 12 点 44 分(或者在您阅读本文时)。我使用上面的代码编写了以下内容:

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit |NSTimeZoneCalendarUnit) fromDate:[NSDate date]];
comps.timeZone = [NSTimeZone localTimeZone];
NSDate *newDate = [cal dateFromComponents:comps];

NSLog(@"New date %@", newDate);

输出:

New date 2013-11-12 17:44:00 +0000

因此,它忽略了我将时区设置为本地时区的事实。但是,如果我将它放在格式化程序中并将其注销,您会注意到时间现在是“正确的”:

NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateStyle = NSDateFormatterLongStyle;
formatter.timeStyle = NSDateFormatterLongStyle;
NSLog(@"Date string %@", [formatter stringFromDate:newDate]);

输出:

Date string November 12, 2013 at 12:44:00 PM EST

希望有帮助。

于 2013-11-12T17:47:28.453 回答