【问题标题】:NSDate Time MST incorrect when comparing比较时 NSDate Time MST 不正确
【发布时间】:2013-08-22 21:56:37
【问题描述】:

我有一个应用程序,它从 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:53pm

我知道是什么原因造成的:营业时间 730am + 6hrs = 营业时间 13:30:00。同样,现在下午 6:01 变成了晚上 11:53。不同之处在于关闭时间为晚上 9 点 + 6 小时 = 第二天凌晨 3 点,但关闭时间为下午 12:00,但关闭时间为当天下午 12 点 + 6 小时 = 下午 6 点。

那么我该如何解释或更正呢?

【问题讨论】:

    标签: ios timezone nsdate


    【解决方案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)];
    

    【讨论】:

    • 我根据您的建议更新了我的代码,并相应地更新了我的原始问题。问题仍然存在。我会尝试将您的时间重置到第二天,但这只会使我目前开放的地点关闭,不是吗?我会把那个代码放在 dateFormatter setDefaultDate 之后,对吧?
    • 1) 看起来您的开盘和关盘时间现在是正确的;具体来说,第二个关闭时间是未来的一天(打印为 UTC)。 2)时间比较功能在哪里实现?对吗?
    • 是的,我昨晚确实注意到了这一点。我花了很多时间试图纠正这个问题,以至于我忘记了问题到底是什么,并陷入了这个过程......谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多