0

I have this string which is passed to this method to produce 2 times:

Mon thru Friday 7:00 AM - 10:00 PM

-(void)dealWithTimeStrings2:(NSString*)timeString{
    NSString *s = timeString;

    NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
    int idx = [s rangeOfCharacterFromSet:digits].location;
    NSString *timeStr = [s substringFromIndex:idx];
    NSArray *timeStringsArray2 = [timeStr componentsSeparatedByString:@" - "];
    NSLog(@"timeStringsArray2 %@", timeStringsArray2);

    //NOW TURN INTO DATE AND FORMAT TO 24HR
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];

    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(@"open, now, close %@,%@,%@", openDate,[NSDate date],closeDate);

}

And this produces:

timeStringsArray2 ( "7:00 AM", "10:00 PM" )

someDateString 7:00 AM,10:00 PM

open, now, close 2000-01-01 13:00:00 +0000,2013-07-24 23:51:55 +0000,2000-01-02 04:00:00 +0000

Here is the test code:

-(BOOL)timeCompare:(NSDate*)date1 until:(NSDate*)date2{
    NSDate *now   = [NSDate date];
    BOOL inBetween = ([now compare:date1] == NSOrderedDescending && [now compare:date2] == NSOrderedAscending);

    return inBetween;
}

As you can see, 23:46 is not between 6:00 and 18:00. However, I just ran this at 5:46 my time, which is in between 7:00AM and 10:00PM.

marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • Strings like `7:30 AM` don't match the format `HH:mm`. You would need `hh:mm a`. – rmaddy Jul 24 '13 at 22:38
  • Well I did that but someone told me that was causing the problem of converting timezones. Because from 7:00 AM & 10:00 PM I was getting 2000-01-01 00:00:00 +0000 & 2000-01-01 12:00:00 +0000 which is incorrect! – marciokoko Jul 24 '13 at 22:41
  • 2
    `NSDate` objects are always in UTC and when you log them you will see them in UTC, not locale time. – rmaddy Jul 24 '13 at 22:43
  • 1
    Whoever told you that about timezones didn't know their axx from a hole in the ground. The fact is that you **don't have** a "timezone problem", you have a "timezone comprehension problem". NSDate always contains GMT time, and NSLog will print that GMT time. – Hot Licks Jul 24 '13 at 22:46
  • Oops - I meant "local time", not "locale time". The latter doesn't much sense. – rmaddy Jul 24 '13 at 22:59
  • Ok so what do I have to do to get my date objects in locale time so I can compare them to locale time now? These are opening and closing times which I need to compare to now time, to determine if a store is open or closed. – marciokoko Jul 24 '13 at 23:01
  • You don't have to do anything. Try doing `NSLog(@"The time is %@",[NSDate date]);` to log the current time. You will see that it is expressed as GMT/UTC. – Hot Licks Jul 24 '13 at 23:10
  • I just logged 7AM, 10PM and now, and got this: `2000-01-01 06:00:00 +0000,2000-01-01 18:00:00 +0000,2013-07-24 23:06:56 +0000.` What am I doing wrong because right now its 5PM my time, and its certainly between 7AM and 10PM. But according to this, 23UTC is not between 6UTC & 18UTC. – marciokoko Jul 24 '13 at 23:10
  • What timezone are you in? – Hot Licks Jul 24 '13 at 23:12
  • You need to NSLog the inputs and outputs of the two conversions and copy/paste the results from the console into your question (along with the snippet of code that produced them). I'm suspecting that your inputs may be garbled. (And be sure that you're using "hh:mm a", and not "HH:mm a".) – Hot Licks Jul 24 '13 at 23:19
  • Ok I just updated the original question with the code snippet and results. Its weird to look at times that are not the actual ones though. Thx – marciokoko Jul 24 '13 at 23:50
  • Turns out I misunderstood what I was told. The problem is that I now need to compare these dates. But the 2 formatted dates are in the year 2000 whereas now is in 2013. So the test fails. – marciokoko Jul 25 '13 at 00:01
  • The simple thing to do there is to create a string with today's date in it. Or, just do what Wain suggests and skip the date formatter entirely and crunch hour and minute yourself. – Hot Licks Jul 25 '13 at 00:33
  • Note that 23:51:55 *is* between 13:00 and 04:00 of the next day. – Hot Licks Jul 25 '13 at 00:37
  • Yes but only if the DAY is the same for all three, which it isnt. I added the code im using to test if now is between those two times. – marciokoko Jul 25 '13 at 00:47

1 Answers1

0

You don't really need dates or date formatters at all. You can read the times using your string processing code to get the hour and minute information. You can then get the date components for the current date to compare with.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • You mean with something like this:`http://stackoverflow.com/questions/3385552/nsdatecomponents-componentsfromdate-and-time-zones` – marciokoko Jul 24 '13 at 23:20
  • Ish. You don't want to do the time zone stuff because you want to get the current hour and minute in the users locale time zone. So you don't need to do anything other than get the components from the users current calendar. – Wain Jul 24 '13 at 23:26