簡體   English   中英

比較時NSDate時間MST不正確

[英]NSDate Time MST incorrect when comparing

我有一個應用程序,可以從Web服務中提取26條記錄並輸入到Core Data中。 我將所有26條記錄取回。 然后,我選擇一個名為“ hor_LV”的字段,並在其上運行TimeComparator類方法,它從該字段獲取打開時間和關閉時間,並將其與now和voila進行比較。

截至目前,所有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 pm

我知道是什么原因造成的:開放時間730am + 6小時=開放時間13:30:00。 同樣,現在下午6:01變為晚上11:53。 區別在於第二天的晚上9點+ 6小時= 3AM,但是下午12:00的下午2點有效,但是當天的下午12點+ 6小時= 6 PM。

那么我該如何解釋或糾正呢?

似乎在構造“調整后的”日期字符串方面,您正在做大量工作。 具體來說,從字符串開始,從當前時間中提取一些組件,添加一些組件(其中一個(可能是星期幾)可能會引起問題),然后解析日期。

也許這更直接:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM