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

Schif Schaf schifschaf at gmail.com
Sat Sep 19 22:22:42 EDT 2009


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



More information about the Python-list mailing list