Re: Obtain an NSDate object from any casually entered user string

2014-05-15 Thread Charles Srstka
On May 14, 2014, at 10:38 PM, Gerriet M. Denkmann gerr...@mdenkmann.de wrote:

 
 On 15 May 2014, at 02:00, Ken Thomases k...@codeweavers.com 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.

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Obtain an NSDate object from any casually entered user string

2014-05-15 Thread Gerriet M. Denkmann

On 15 May 2014, at 19:53, Charles Srstka cocoa...@charlessoft.com wrote:

 On May 14, 2014, at 10:38 PM, Gerriet M. Denkmann gerr...@mdenkmann.de 
 wrote:
 
 
 On 15 May 2014, at 02:00, Ken Thomases k...@codeweavers.com 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Obtain an NSDate object from any casually entered user string

2014-05-15 Thread Charles Srstka
On May 15, 2014, at 8:03 AM, Gerriet M. Denkmann gerr...@mdenkmann.de wrote:

 I always use a 24-hour clock.

Your users may not, though. In the US, at least, 24-hour clocks are definitely 
a minority.

 Is there a way, to make NSDataDetector aware of this fact? Some options I 
 might set?

NSDataDetector doesn't appear to have any options at *all.*

I suppose you could do stuff like postpend AM to the string if the hour is 
less than 12, and PM if it's equal to 12, before running the string through 
NSDataDetector. The amount of string manipulation you'd have to do might defeat 
the purpose of using NSDataDetector, though.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Obtain an NSDate object from any casually entered user string

2014-05-15 Thread Sean McBride
On Thu, 15 May 2014 07:53:01 -0500, Charles Srstka said:

 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.

True.  But it's a pretty strange guess that 2014-05-15 07:52:18.658 is 
afternoon.  If the user uses an am/pm system, then it's 50/50.  If the user 
uses a 24 hour system, there's no ambiguity.  So it's more probably a morning 
time.

Also, the guessing logic should check if the user uses 24 hour clock, which 
maybe it does or maybe not.

Cheers,

-- 

Sean McBride, B. Eng s...@rogue-research.com
Rogue Researchwww.rogue-research.com 
Mac Software Developer  Montréal, Québec, Canada

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Obtain an NSDate object from any casually entered user string

2014-05-14 Thread Jens Alfke

On May 14, 2014, at 6:41 AM, Jonathan Mitchell jonat...@mugginsoft.com wrote:

 Is there a way to obtain an NSDate object from a casually entered user 
 string, say: 1 1 2015 or 25 jul 15?

There’s no easy way. You can create a whole bunch of format strings for every 
exact format you expect, and then try to parse the input using each one in turn 
(tip: the order you test the formats in is important); or you can write a 
custom parser (hopefully using a parser generator like ANTLR.) Or you could 
search around to see if there are any open-source libraries that do this … I 
just checked the About box of Fantastical (which has a really good date parser) 
but didn’t see anything relevant. Good luck!

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Obtain an NSDate object from any casually entered user string

2014-05-14 Thread Ken Thomases
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.

Cheers,
Ken


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Obtain an NSDate object from any casually entered user string

2014-05-14 Thread Gerriet M. Denkmann

On 15 May 2014, at 02:00, Ken Thomases k...@codeweavers.com 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?

Gerriet.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com