Parsing fancy for the New Year

Started by cormullion, January 01, 2010, 01:45:18 AM

Previous topic - Next topic

cormullion

By way of wishing you all a Happy New Year, here's a date-related 'amusement/challenge' for you.



Visit http://unbalanced-parentheses.nfshost.com/lambdalator">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.

Kazimir Majorinc

#1
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!
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

cormullion

#2
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 ... :)

TedWalther

#3
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.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

xytroxon

#4
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">http://www.php.net/manual/en/funcref.php



PHP Source Code:

http://svn.php.net/viewvc/php/php-src/trunk/ext/">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://www.php.net/manual/en/book.datetime.php

http://svn.php.net/viewvc/php/php-src/trunk/ext/date/">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
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

cormullion

#5
:) I stopped reading at line 4238 of php_date.c ...

TedWalther

#6
line 4238?  Whew!  That whole file looks like it could be reduced to 300 lines, maybe even less for our needs.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

Ryon

#7
Some nerds spend a lot of effort finding a date.
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"