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


S

Schif Schaf

The other day I needed to convert a date like "August 2009" into a
"seconds-since-epoch" value (this would be for the first day of that
month, at the first second of that day).

In Python, I came up with this:

~~~~
#!/usr/bin/env python

import datetime
import time

time_in_sse = time.mktime(
datetime.datetime(2009, 8, 1).timetuple()
)

print time_in_sse
~~~~

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?!). I also wanted to then just do datetime.datetime
(...).secs_since_epoch(), but it didn't support a function like that
-- not one I could find anyway.

Note, to arrive at that above solution, I had to spend a fair amount
of time reading the docs on both the time and datetime modules, and
then wondering why the methods I wanted weren't there. Am I missing
something and maybe used the wrong methods/modules here?

Contrast this to Perl, where the solution I came up with in about 5
minutes was:

~~~~
#!/usr/bin/env perl

use DateTime;
my $dt = DateTime->new(year => 2009, month => 8);
print $dt->epoch, "\n";
~~~~

(it only took 5 minutes because the docs for DateTime tell you exactly
what you want to know right at the top on the first page of the docs.)
 
Ad

Advertisements

C

Carl Banks

The other day I needed to convert a date like "August 2009" into a
"seconds-since-epoch" value (this would be for the first day of that
month, at the first second of that day).

In Python, I came up with this:

~~~~
#!/usr/bin/env python

import datetime
import time

time_in_sse = time.mktime(
    datetime.datetime(2009, 8, 1).timetuple()
)

print time_in_sse
~~~~

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))



Carl Banks
 
J

John Yeung

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
 
The other day I needed to convert a date like "August 2009" into a
"seconds-since-epoch" value (this would be for the first day of that
month, at the first second of that day).

You could use Time::piece:

[[email protected] ~]$ perl -lMTime::piece -e'$t=Time::piece->strptime
("August 2009","%b %Y"); print $t->epoch'
1249084800
 
A

AggieDan04

The other day I needed to convert a date like "August 2009" into a
"seconds-since-epoch" value (this would be for the first day of that
month, at the first second of that day).

In Python, I came up with this:

~~~~
#!/usr/bin/env python

import datetime
import time

time_in_sse = time.mktime(
    datetime.datetime(2009, 8, 1).timetuple()
)

(datetime.datetime(2009, 8, 1) - datetime.datetime(1970, 1, 1)).days *
86400

But still, this should be part of the datetime class.
 
Ad

Advertisements

C

Carl Banks

[This is not a Perl question. F'ups set to c.l.python.]

Heh, I didn't notice the cross-post. Glad I decided against following
up with minor philosophical rant.


Carl Banks
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top