gpt4 book ai didi

ios - 比较时 NSDate 时间 MST 不正确

转载 作者:行者123 更新时间:2023-11-29 03:41:28 25 4
gpt4 key购买 nike

我有一个应用程序,可以从 Web 服务获取 26 条记录并存入 Core Data。我一次获取了所有 26 条记录。然后,我采用名为“hor_LV”的字段之一并对其运行 TimeComparator 类方法,它从该字段获取打开时间和关闭时间,并将其与现在进行比较,瞧。

截至目前,所有 26 个位置都应根据其 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

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

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

最佳答案

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

也许这更直接:

  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)];

关于ios - 比较时 NSDate 时间 MST 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18391496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com