简体   繁体   中英

NSDate Time MST incorrect when comparing

I have an app, fetching 26 records from a web service and into Core Data. I get all 26 records back in a fetch. Then I take one of the fields called "hor_LV" and run a TimeComparator class method on it and it takes opening time and closing time from that field and compares it to now and voila.

As of right now, all 26 lcoations should be open according to their hor_LV. But 3 are coming back closed. I hunted down the dates being compared and this is what I get...and here is my TimeComparator class method:

    +(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;
}

and here is a log of a correct record and 2 incorrect ones:

    -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

The second instance is open because open and now are in the day 8-22 yet close time is on 8-23.

The first instance is closed because open and now are 8-22 and so is the close time.

I checked online current MST time and its 5:53PM so that seems fine which is +6 = 11:53pm

I know whats causing it: it takes opening time 730am + 6hrs = opening time 13:30:00. Likewise now 6:01pm becomes 11:53pm. The difference is in closing time at 9PM + 6hrs = 3AM next day but closing time 12:00pm which works but closing time at 12pm + 6hrs = 6pm the same day.

So how can I account or correct that?

It seems you are doing notably extra work in constructing an 'adjusted' date string. Specifically, you start with a string, you extract some components from the current time, you add some components (one of which, the day, may be causing your problem), and then you parse the date.

Perhaps this is more direct:

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

The setDefaultDate: adds in all the stuff that is missing so you get year, month, day and seconds. Maybe the seconds need to be zeroed or, because you are only comparing, maybe they can be left in.

If you suspect that your times have wrapped to the next day, reset the default date before each parse with one of:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM