[Tutor] Through a glass, darkly: the datetime module

Steven D'Aprano steve at pearwood.info
Sun Oct 7 04:02:48 CEST 2012


On 07/10/12 12:13, Richard D. Moores wrote:
> On Sat, Oct 6, 2012 at 4:35 PM, Alan Gauld<alan.gauld at btinternet.com>  wrote:
>> On 07/10/12 00:19, Richard D. Moores wrote:
>>
>>> That "1" means Tuesday, right? But how can I use calendar to print out
>>> that word, "TUESDAY"?
>>
>>
>> days = ("Monday",
>>          "Tuesday",
>>          "Wednesday",
>>          "Thursday",
>>          "Friday",
>>          "Saturday",
>>          "Sunday")
>>
>> print '2014/2/18 is: ', days[calendar.weekday(2014, 2, 18)]
>
> I was thinking that that kind of thing would be built into calendar. Thanks.


It is, and it is localised to whatever date system the user is using,
which is a much better idea than forcing English dates.

py> import calendar
py> import locale
py> locale.setlocale(locale.LC_ALL, 'fr_FR')  # French
'fr_FR'
py> calendar.day_name[1]
'mardi'
py> locale.setlocale(locale.LC_ALL, 'de_DE')  # German
'de_DE'
py> calendar.day_name[1]
'Dienstag'
py> calendar.day_name[4]
'Freitag'
py> locale.setlocale(locale.LC_ALL, 'en_GB')  # British English.
'en_GB'
py> calendar.day_name[4]
'Friday'





-- 
Steven


More information about the Tutor mailing list