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

Steven D'Aprano steve at pearwood.info
Sun Oct 7 05:56:34 CEST 2012


On 07/10/12 13:37, Richard D. Moores wrote:
> On Sat, Oct 6, 2012 at 5:22 PM, eryksun<eryksun at gmail.com>  wrote:
>
>>      >>>  from datetime import date
>>      >>>  date(2014, 2, 18).strftime("%A")
>>      'Tuesday'
>>
>> http://docs.python.org/library/datetime#strftime-and-strptime-behavior
>
> Or for Python 3.3,
> <http://docs.python.org/py3k/library/datetime.html?highlight=strftime#strftime-strptime-behavior>.
>
> I remain bewildered. Where did these strangely named things come from,
> strftime and strptime?


The C programming language on Unix systems.

It is a little-known fact that Unix sys admins, and C programmers, can
only type a fixed number of keys before their brains explode. Sad but
true. Since nobody knows how many keys that will be, but only that it is
fixed at birth, they have a horror of typing four characters when two
would do. Hence the standard Unix directories /usr and /mnt instead of
/user and /mount, and the Unix commands:

"cp" instead of "copy"
"ls" instead of "list"
"rm" instead of "remove"
"umount" instead of "unmount"
"chown" and "chgrp" instead of "change owner" and "change group"

to mention only a few.

This is also why the usual answer to any question asked of a Unix sys
admin is likely to be "RTFM", which is best translated as:

"Everything you ask can be found in the fine instruction manual or help
files already available to you, or by searching on the Internet. Although
it may take you five or six months of study to understand it, and I could
explain it to you in a few sentences, I fear that my brain will explode
halfway through and then you won't get your answer."

Hence strftime and strptime for "string format time" and
"string parse time".


> I see that
>
>>>> from datetime import date
>>>> date(2014, 2, 18).strftime("%A")
> 'Tuesday'
>
> gives me what I was after, but I need to understand it, and I
> understand very little of that section, "8.1.8. strftime() and
> strptime() Behavior".


The thing to remember is that strftime and strptime are essentially
mini-programming languages themselves, a little like regular
expressions. Another way of thinking about them is as similar to
Excel's custom number formats, only more extensive.

py> date(2014, 2, 18).strftime("Today is %A %d of %B in the year %Y.")
'Today is Tuesday 18 of February in the year 2014.'


You can find a (partial?) list of format specifiers here:

http://www.faximum.com/manual.d/client.server.d/manpages.23.html

strptime handles the reverse process:

py> time.strptime("Today is Tuesday 18 of February in the year 2014.",
...               "Today is %A %d of %B in the year %Y.")
time.struct_time(tm_year=2014, tm_mon=2, tm_mday=18, tm_hour=0, tm_min=0,
tm_sec=0, tm_wday=1, tm_yday=49, tm_isdst=-1)



> Take the first sentence in that section:
> "date, datetime, and time objects all support a strftime(format)
> method, to create a string representing the time under the control of
> an explicit format string. Broadly speaking, d.strftime(fmt) acts like
> the time module’s time.strftime(fmt, d.timetuple()) although not all
> objects support a timetuple() method."
>
> Total gibberish. I feel like I've hit a brick wall. Where can I go to
> learn to understand it? I need some very basic, specific information.

Start at the beginning: Python has date, datetime and time objects, right?

py> import datetime
py> datetime.date, datetime.time, datetime.datetime
(<type 'datetime.date'>, <type 'datetime.time'>, <type 'datetime.datetime'>)


All three of those objects define a method "strftime" which takes a format
code, like "%a %d of %b, %Y", and converts the date/time/datetime object
into a string using that format.

Whatever d is (a date, a time, a datetime), calling d.strftime with an
explicit format string is like extracting the time fields into a tuple:

tmp = d.timetuple()  # if this actually exists

and then formatting it using the master function:

time.strftime(format_string, tmp)


only more convenient.



-- 
Steven



More information about the Tutor mailing list