datetime to timestamp

John Machin sjmachin at lexicon.net
Fri Aug 11 17:51:59 EDT 2006


Tim Peters wrote:
> [Simen Haugen]
> >>> 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...?)
>
> [John Machin]
> >> Is the timetuple() method what you want?
> >>
> >> #>>> import datetime
> >> #>>> n = datetime.datetime.now()
> >> #>>> n
> >> datetime.datetime(2006, 8, 11, 23, 32, 43, 109000)
> >> #>>> n.timetuple()
> >> (2006, 8, 11, 23, 32, 43, 4, 223, -1)
>
> [also John]
> > Aaaarrrggghhh no it's not what you want
>
> Yes, it is ;-)
>
> > -- looks like you have to do the arithmetic yourself, starting with toordinal()
>
> It's just one step from the time tuple:
>
>     import time
>     time.mktime(some_datetime_object.timetuple())
>

Thanks, Tim. In mitigation, yer honour, I'd like to draw your attention
to the local time (23:32); the defendant was rushing to do the OP's
RTFantasticM for him before retiring for the night, and not doing it
well enough :-)
Cheers,
John




More information about the Python-list mailing list