Re: Best way to parse a time today?
Re: Best way to parse a time today?
- Subject: Re: Best way to parse a time today?
- From: Rick Mann <email@hidden>
- Date: Thu, 13 Oct 2011 10:39:57 -0700
I think the concern is this. Say today is the day that a time change occurs. In the US, this happens at 2 am. When you move the clocks forward, the instant the clock would roll from 1:59:59.999 to 2:00:00.000, it actually rolls to 3:00:00.000. All of the times in the half-open interval (2:00, 3:00] are "invalid."
So, if on that day, I try to parse the string "02:17", what's the right result? My guess would be "03:17", but I'd have to test that case to be sure it does that.
--
Rick
On Oct 13, 2011, at 5:34 , Tom Hohensee wrote:
> I use the same approach the Roger pointed out and have not encountered any problems as well. However, I not only set the time but the date (month, day, year) as well. The DST boundries should not matter since I am decomposing today's date [NSDate date], which is set by the end user's time settings in system preferences. Is this not correct?
>
> Thanks.
>
> Tom
>
> On Oct 12, 2011, at 9:01 PM, Roger Dalal wrote:
>
>> Dave:
>>
>> Would it be possible for you to present an improved approach, please? I use this solution frequently, and have not yet encountered any issues, but now you have me worried! What approach do you suggest?
>>
>> Roger Dalal
>>
>>
>> On Oct 12, 2011, at 9:49 PM, Dave DeLong wrote:
>>
>>> Be careful with this approach, since there are some weird edge cases where that time may not exist on the proposed day (think DST boundaries).
>>>
>>> Dave
>>>
>>> Sent from my iPhone
>>>
>>> On Oct 12, 2011, at 6:46 PM, Roger Dalal <email@hidden> wrote:
>>>
>>>> Rick:
>>>>
>>>> The following code, which is likely what you are doing, will return the time in 1970 (NSDate's reference date) because you have not specified a date:
>>>>
>>>> NSString *timeString = @"14:50 PDT";
>>>> NSDateFormatter *df = [[NSDateFormatter alloc ] init];
>>>> [df setDateFormat:@"HH':'mm zzz"];
>>>> NSDate *date = [df dateFromString:timeString];
>>>> [df release];
>>>>
>>>>
>>>> Instead, you need to use date components to set the day as well as the time, per the following:
>>>>
>>>> NSString *timeString = @"14:50 PDT";
>>>> NSDateFormatter *df = [[NSDateFormatter alloc ] init];
>>>> [df setDateFormat:@"HH':'mm zzz"];
>>>> NSDate *date = [df dateFromString:timeString];
>>>>
>>>> NSCalendar *calendar = [NSCalendar currentCalendar];
>>>> NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[NSDate date]];
>>>> NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate: date ];
>>>>
>>>> [dateComponents setHour:[timeComponents hour]];
>>>> [dateComponents setMinute:[timeComponents minute]];
>>>> [dateComponents setSecond:[timeComponents second]];
>>>>
>>>> NSDate *timeToday = [calendar dateFromComponents:dateComponents];
>>>> [df release];
>>>>
>>>> Change 'fromDate' in NSDateComponents *dateComponents to whatever date you want in order to create your time on a different day.
>>>>
>>>> Best Wishes.
>>>>
>>>> Roger Dalal
>>>>
>>>>
>>>>
>>>>
>>>> On Oct 12, 2011, at 9:30 PM, Rick Mann wrote:
>>>>
>>>>> I have a situation where I have to parse times like "14:50 PDT". If I just set up an NSDateFormatter with dateFormat = @"HH:mm z", I end up with a time of day in 1970.
>>>>>
>>>>> What's the best way to get it to give me that time of day today?
>>>>>
>>>>> Thanks,
>>>>> Rick
>>>>>
>>>>> _______________________________________________
>>>>>
>>>>> Cocoa-dev mailing list (email@hidden)
>>>>>
>>>>> Please do not post admin requests or moderator comments to the list.
>>>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>>>
>>>>> Help/Unsubscribe/Update your Subscription:
>>>>>
>>>>> This email sent to email@hidden
>>>>
>>>> _______________________________________________
>>>>
>>>> Cocoa-dev mailing list (email@hidden)
>>>>
>>>> Please do not post admin requests or moderator comments to the list.
>>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>>
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden