Page 1 of 1

Parsing fancy for the New Year

Posted: Fri Jan 01, 2010 9:45 am
by cormullion
By way of wishing you all a Happy New Year, here's a date-related 'amusement/challenge' for you.

Visit the Lambdalator and find a date/time format that breaks my flimsy coding!

The handler function is parse-time, and it's like newLISP's parse-date, except that it tries to guess the format you've used, so you don't supply a format string. Here are some examples to get you started:

(parse-time "4:15")
; → Fri Jan 1 04:15:00 2010
(parse-time "1999-12-31T21:59:59-8:00")
; → Fri Dec 31 21:59:59 1999
(parse-time "1995-02")
;→ Wed Feb 1 09:41:43 1995

On NearlyFreeSpeech hosting it works in English only - switching to alternate locales doesn't work, apparently...

(What happened was, I started doing this as part of my Timeutilities FOOP module, but I soon realised trying to anticipate most date/time formats was going to be too much of a chore. I've sort of abandoned the project - haven't even started doing time zones... :))

For Hackers: the context is called ParseTime.

Re: Parsing fancy for the New Year

Posted: Fri Jan 01, 2010 9:26 pm
by Kazimir Majorinc
It works well for English formats I tried.
It doesn't for typical Croatian format "15. 10. 1955." = "15 October 1955"

Happy new year everyone!

Re: Parsing fancy for the New Year

Posted: Fri Jan 01, 2010 10:52 pm
by cormullion
Ah, interesting one. It's not expecting that final period. But without it, it still fails, because 1955 is pre-Unix time, which starts in 1970 ... :)

Re: Parsing fancy for the New Year

Posted: Sat Jan 02, 2010 3:10 am
by TedWalther
Beautiful, I can see the pieces coming together. Could you examine the C function that implements date() in PHP? That does what you are trying to do, and if you use the exact same code as PHP, it would fit the principle of least suprise. Something like that is so useful, I wouldn't be surprised if it got into the base distribution, or even into the base interpreter itself! I know it would make my work of porting things over from PHP easier if it was 100% compatible with PHP's function.

Re: Parsing fancy for the New Year

Posted: Sat Jan 02, 2010 8:15 am
by xytroxon
I knew there must of been a special reason...
I bookmarked these, before the Yuletide season ;p)

PHP Function Reference:
http://www.php.net/manual/en/funcref.php

PHP Source Code:
http://svn.php.net/viewvc/php/php-src/trunk/ext/

And looking around, I found:

date() function:
http://www.php.net/manual/en/book.datetime.php
http://svn.php.net/viewvc/php/php-src/trunk/ext/date/
Files of interest: php_date.c and php_date.h and actual C code routines are in lib/ directory.

-- xytroxon

Re: Parsing fancy for the New Year

Posted: Sat Jan 02, 2010 9:33 am
by cormullion
:) I stopped reading at line 4238 of php_date.c ...

Re: Parsing fancy for the New Year

Posted: Sun Jan 03, 2010 1:10 am
by TedWalther
line 4238? Whew! That whole file looks like it could be reduced to 300 lines, maybe even less for our needs.

Re: Parsing fancy for the New Year

Posted: Sun Jan 03, 2010 2:56 am
by Ryon
Some nerds spend a lot of effort finding a date.