0

我有一个应用程序,从 Web 服务中获取 26 条记录并进入核心数据。我一次取回了所有 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 点。

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

4

1 回答 1

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)];
于 2013-08-22T22:48:21.350 回答