Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)

John Yeung gallium.arsenide at gmail.com
Sat Sep 19 23:58:56 EDT 2009


On Sep 19, 10:57 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> On Sep 19, 7:22 pm, Schif Schaf <schifsc... at gmail.com> wrote:
>
> > I *wanted* to just use time.mktime(), but it wouldn't
> > work unless I could specify the *complete* time tuple
> > value (who would have all that handy?!).
>
> Was it really that hard to add a few zeros to the tuple
> for values you didn't know?
>
> time.mktime((2009, 8, 1, 0, 0, 0, 0, 0, -1))

To be fair, the docs for the time module (I'm looking at the help file
for 2.6.2) are written in such a way that it's not clear you can use
zero for the unknown day of the week or day of the year.  The passage
for time.mktime(t) that states "If the input value cannot be
represented as a valid time, either OverflowError or ValueError will
be raised" *might* imply that your tuple is invalid, because
2009-08-01 was not a Monday and 0 is not even in the valid range for
tm_yday.  In my opinion, this is a deficiency of the docs.

On the other hand, I think the OP, as well as users of Python in
general, should probably not be so timid.  Just try something and see
if it works.  Throw zeros in for the unknown values and maybe you'll
get a ValueError (like your cautious, doc-respecting mind is
expecting) or maybe the routine will actually do the appropriate and
convenient thing and trust your year, month, and day.  In this case,
the latter is happily true.

For what it's worth, it's true that the time module is not
particularly Pythonic.  It's mostly a wrapper for C library functions,
and the docs do imply this near the top.  Personally, I think the
wrapper could stand to be less thin and provide friendlier access
while still using the C library, but I doubt it's a big enough pain
point to spur anyone to improve it.

John



More information about the Python-list mailing list