datetime to timestamp

Sion Arrowsmith siona at chiark.greenend.org.uk
Fri Aug 11 10:51:51 EDT 2006


John Machin  <sjmachin at lexicon.net> wrote:
>On 11/08/2006 11:35 PM, John Machin wrote:
>> On 11/08/2006 11:10 PM, Simen Haugen wrote:
>>> How can I convert a python datetime to a timestamp? It's easy to convert
>>> a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
>>> other way around...?)
>> Is the timetuple() method what you want?
> [ ... ]
>Aaaarrrggghhh no it's not what you want -- [ ... ]

It is if you only want it to the second. It just needs a time.mktime():

>>> n = time.time()
>>> n
1155307613.4550381
>>> d = datetime.datetime.fromtimestamp(n)
>>> time.mktime(d.timetuple())
1155307613.0

(timetuple() is responsible for the loss of the fractional part.)

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list