|
Project Information
Links
|
Looking at rails and the datejs library, I can't help but feel bummed that Python doesn't have an expressive and easy to use date library like that. So I started one. Here are some examples. First import it: >>> from goodday import * You can get today's date, >>> today() Date(01/24/09 12:00AM) or what time is it now. >>> now() Date(01/24/09 01:01PM) What time is it five hours from now? >>> hours(5)._from(now()) Date(01/24/09 06:01PM) One hour and a half from now? >>> hour(1)._and(minutes(30))._from(now()) Date(01/24/09 02:31PM) or equivalently: >>> now() + hour(1) + minutes(30) Date(01/24/09 02:31PM) You can do the same thing with days: >>> days(20)._from(today()) Date(02/13/09 12:00AM) The functions second(s), minute(s), hour(s), day(s), week(s), and year(s) all return TimeInterval objects. >>> days(20) TimeInterval(20 days) >>> hours(5)._and(minutes(30)) TimeInterval(5 hours and 30 minutes) >>> day(1) + hours(4) + minutes(30) TimeInterval(1 day 4 hours and 30 minutes) >>> week(1) TimeInterval(7 days) You can use the in_words method of TimeInterval to approximate the time interval and put it in a human readable format: >>> hours(5)._and(minutes(45)).in_words() 'about 6 hours' >>> (day(1) + hours(4) + minutes(30)).in_words() '1 day' >>> years(6)._and(days(10)).in_words() 'over 6 years' >>> (now() - now()).in_words() 'less than a minute' You can easily format a Date: >>> now().format()
'01/24/09 01:04PM'
>>> today().format("%m/%d/%y")
'01/24/09'
>>> now().format("%H:%M")
'13:04'Parsing dates is done with the DateFormat object: >>> DateFormat("%I:%M%p").parse("12:30PM")
Date(01/01/00 12:30PM)
>>> DateFormat("%m/%d/%y").parse("1/2/10")
Date(01/02/10 12:00AM)
>>> DateFormat("%m/%d/%Y").parse("1/2/1996")
Date(01/02/96 12:00AM)You can browse the source here. |