Re: Obtain an NSDate object from any casually entered user string
Re: Obtain an NSDate object from any casually entered user string
- Subject: Re: Obtain an NSDate object from any casually entered user string
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Thu, 15 May 2014 20:03:13 +0700
On 15 May 2014, at 19:53, Charles Srstka <email@hidden> wrote:
> On May 14, 2014, at 10:38 PM, Gerriet M. Denkmann <email@hidden> wrote:
>
>>
>> On 15 May 2014, at 02:00, Ken Thomases <email@hidden> wrote:
>>
>>>
>>> On May 14, 2014, at 8:41 AM, Jonathan Mitchell wrote:
>>>
>>>> Is there a way to obtain an NSDate object from a casually entered user string, say: 1 1 2015 or 25 jul 15?
>>>>
>>>> I have looked at the various NSDateFormatter and NSDate API and cannot spot what I am after.
>>>
>>> You might try NSDataDetector with type NSTextCheckingTypeDate.
>>
>> I did just that, and are a bit puzzled.
>>
>> 1. NSDataDetector recognises all date formats I encountered, which is rather good.
>>
>>
>> 2. NSDataDetector ignores fractions of a second:
>> e.g. "2014-05-14 11:33:53.126" gets converted to: "2014-05-14 11:33:53 +0700", which in my case is not really a problem.
>>
>>
>> 3. But sometimes the date of NSDataDetector is 12h ahead (again ignoring fractions of a second):
>>
>> "2014-05-15 07:52:18.658" → "2014-05-15 19:52:18 +0700"
>> "2014-05-14 05:59:46.490 +0700" → "2014-05-14 17:59:46.490 +0700"
>>
>> But not always - these work ok:
>> "2014-05-15 08:22:48.135"
>> "2014-05-15 09:15:35 +0700"
>> Also, all times after about 13:00 are correct.
>>
>> This arbitrary advancement of 12h is obviously NOT acceptable.
>> What am I doing wrong?
>
> That's the trouble with accepting arbitrary user input. If a user enters a time of "1:23", s/he might mean 1:23 AM, 1:23 PM, or 1:23 military time, and there's no real way to determine which is intended without reading the user's mind. So, the system has to guess. Sometimes it may not guess correctly.
>
> If the user specifically enters "1:23 AM" to remove the ambiguity, this should solve the issue.
I always use a 24-hour clock.
Is there a way, to make NSDataDetector aware of this fact? Some options I might set?
Gerriet.
_______________________________________________
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