Re: Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)
[This is not a Perl question. F'ups set to c.l.python.] Quoth Schif Schaf schifsc...@gmail.com: 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). Note that this is not unique: you need to at least specify a time zone. Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)
On Sep 19, 9:22 pm, Schif Schaf schifsc...@gmail.com 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)
On Sep 19, 11:02 pm, Ben Morrow b...@morrow.me.uk wrote: [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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)
On Sep 19, 7:22 pm, Schif Schaf schifsc...@gmail.com 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)
On Sep 19, 10:57 pm, Carl Banks pavlovevide...@gmail.com wrote: On Sep 19, 7:22 pm, Schif Schaf schifsc...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)
On Sep 19, 7:22 pm, Schif Schaf schifsc...@gmail.com 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: [ss...@localhost ~]$ perl -lMTime::Piece -e'$t=Time::Piece-strptime (August 2009,%b %Y); print $t-epoch' 1249084800 -- http://mail.python.org/mailman/listinfo/python-list