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

Discussion in 'Perl Misc' started by Schif Schaf, Sep 20, 2009.

  1. Schif Schaf

    Schif Schaf Guest

    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.)
    Schif Schaf, Sep 20, 2009
    #1
    1. Advertising

  2. Schif Schaf

    Carl Banks Guest

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

    On Sep 19, 7:22 pm, Schif Schaf <> wrote:
    > 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
    Carl Banks, Sep 20, 2009
    #2
    1. Advertising

  3. Schif Schaf

    John Yeung Guest

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

    On Sep 19, 10:57 pm, Carl Banks <> wrote:
    > On Sep 19, 7:22 pm, Schif Schaf <> 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
    John Yeung, Sep 20, 2009
    #3
  4. Re: Am I doing this wrong? Why does this seem so clumsy (time,datetime vs. DateTime)

    On Sep 19, 7:22 pm, Schif Schaf <> wrote:
    > 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:

    [sshaw@localhost ~]$ perl -lMTime::piece -e'$t=Time::piece->strptime
    ("August 2009","%b %Y"); print $t->epoch'
    1249084800
    Skye Shaw!@#$, Sep 20, 2009
    #4
  5. Schif Schaf

    AggieDan04 Guest

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

    On Sep 19, 9:22 pm, Schif Schaf <> wrote:
    > 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.
    AggieDan04, Sep 20, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    704
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    532
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,836
    Smokey Grindel
    Dec 2, 2006
  5. Schif Schaf
    Replies:
    5
    Views:
    327
    Carl Banks
    Sep 21, 2009
Loading...

Share This Page