比较时NSDate Time MST不正确

时间:2013-08-22 21:56:37

标签: ios timezone nsdate

我有一个应用程序,从Web服务获取26条记录并进入Core Data。我在获取中获取了所有26条记录。然后我选择一个名为“hor_LV”的字段并在其上运行一个TimeComparator类方法,它需要从该字段开始时间和关闭时间,并将其与现在进行比较,然后瞧。

截至目前,所有26个lcoations应根据他们的hor_LV开放。但是3回来了。我追捕被比较的日期,这就是我得到的......这是我的TimeComparator类方法:

    +(BOOL)dealWithTimeStrings2:(NSString*)timeString{
    //1. Receive Time String in format date - openTime - closeTime
    NSString *s = timeString;

    NSString *stripped = [s stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSArray *timeStringsArray2 = [stripped componentsSeparatedByString:@"-"];

    NSLog(@"timeStringsArray2 %@", timeStringsArray2);

    //3. Create NSDateFormatter
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"hh:mma"];
    [dateFormatter setDefaultDate: [NSDate new]];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"MST"]];

    //3.5 SET LOCALE
    NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    assert(enUSPOSIXLocale != nil);
    [dateFormatter setLocale:enUSPOSIXLocale];

    //4. Get strings from Array
    NSString *openDateString = (NSString*)[timeStringsArray2 objectAtIndex:0];
    NSString *closeDateString = (NSString*)[timeStringsArray2 objectAtIndex:1];
    NSLog(@"someDateString %@,%@", openDateString,closeDateString);

    NSDate *openDate = [dateFormatter dateFromString: openDateString];
    NSDate *closeDate = [dateFormatter dateFromString: closeDateString];
    NSLog(@"DEALWITHTIMESTRINGS = open, now, close %@,%@,%@", openDate,[NSDate date], closeDate);

    BOOL status;

    //8. Send dates to timeCompare method & return some value
    if ([self timeCompare:openDate until:closeDate]) {
        NSLog(@"TIMECOMPARATOR = timeCompare>OPEN");
        status = YES;
    } else {
        NSLog(@"TIMECOMPARATOR = timeCompare>CLOSED");
        status = NO;
    }

    return status;
}

这是一个正确记录和2个不正确记录的日志:

    -timeStringsArray2 (
    "7:30AM",
    "12:00PM"
)
-someDateString 7:30AM,12:00PM
-DEALWITHTIMESTRINGS = open, now, close 2013-08-22 13:30:01 +0000,2013-08-22 23:53:01 +0000,2013-08-22 18:00:01 +0000
-TIMECOMPARE = open:2013-08-22 13:30:01 +0000 now:2013-08-22 23:53:01 +0000 close:2013-08-22 18:00:01 +0000
-TIMECOMPARATOR = timeCompare>CLOSED

-timeStringsArray2 (
    "7:30AM",
    "9:00PM"
)
-someDateString 7:30AM,9:00PM
-DEALWITHTIMESTRINGS = open, now, close 2013-08-22 13:30:01 +0000,2013-08-22 23:53:01 +0000,2013-08-23 03:00:01 +0000
-TIMECOMPARE = open:2013-08-22 13:30:01 +0000 now:2013-08-22 23:53:01 +0000 close:2013-08-23 03:00:01 +0000
-TIMECOMPARATOR = timeCompare>OPEN

第二个实例是开放的,因为打开,现在是在8-22,但是关闭时间是8-23。

第一个实例因为打开而关闭,现在是8-22,关闭时间也是如此。

我检查了当前的当前MST时间及其下午5:53所以这似乎很好,这是+6 = 11:53 pm

我知道是什么导致它:它需要开放时间730am + 6hrs =开放时间13:30:00。同样现在下午6点01分变成晚上11:53。不同之处在于关闭时间为晚上9点+6小时=凌晨3点,但截止时间为中午12点,但截止时间为12点+ 6点=当天下午6点。

那么如何解释或纠正?

1 个答案:

答案 0 :(得分:1)

在构建“调整后的”日期字符串时,您似乎正做了额外的工作。具体来说,你从字符串开始,从当前时间提取一些组件,添加一些组件(其中一个,当天,可能导致你的问题),然后你解析日期。

也许这更直接:

  NSDateFormatter *dateFormatter = [NSDateFormatter new];
  [dateFormatter setDateFormat: @"h:mma"];
  [dateFormatter setDefaultDate: [NSDate new]];
  [dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"MST"]];
  // Notice use of setDefaultDate: and the setDateFormat: argument

  // Now parse your two date strings (one shown) in your original format
  NSString *timeOne = @"7:00AM";

  NSDate *dateOne = [dateFormatter dateFromString: timeOne];

  // Do the comparison; for debug, print stuff.
  NSLog (@"\n  Time: %@\n  Date: %@", timeOne, date);

setDefaultDate:添加了所有缺失的内容,因此您可以获得年,月,日和秒。也许秒需要归零,或者因为你只是比较,也许它们可以留在。

如果您怀疑自己的时间已经到了第二天,请在每次解析之前重置默认日期:

  NSDate *now = [NSDate new];
  NSDate *nxt = [now dateByAddingTimeInterval: (24 * 60 * 60)];