Parsing fancy for the New Year

Featuring the Dragonfly web framework
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Parsing fancy for the New Year

Post 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.

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Re: Parsing fancy for the New Year

Post 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!

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Parsing fancy for the New Year

Post 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 ... :)

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Parsing fancy for the New Year

Post 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.
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
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: Parsing fancy for the New Year

Post 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
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Parsing fancy for the New Year

Post by cormullion »

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

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Parsing fancy for the New Year

Post by TedWalther »

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
Posts: 248
Joined: Thu Sep 26, 2002 12:57 am

Re: Parsing fancy for the New Year

Post by Ryon »

Some nerds spend a lot of effort finding a date.

Locked