Question: Convert datetime to long number

Gerrit Holl gerrit at nl.linux.org
Sun May 25 16:07:53 EDT 2003


Hi,

John Smith wrote:
> I mean if I do
>
> >>> x=datetime.utcnow(); a=time.time()
> >>> x,a
> (datetime.datetime(2003, 5, 25, 2, 2, 32, 540000), 1053828152.54)
> >>>
>
> what function f I can apply to x such that  f(x) will return 1053828152.54 ?

Tim Peters wrote:
> [Gerrit Holl]
> > You can use the strftime method, and pass "%s" as entry:
> >
> > datetime.datetime.utcnow().strftime("%s")
> >
> > Note that strftime returns an int, not a float.
> 
> Sorry, strftime() returns a string, and doesn't have anything to do with
> timestamps regardless.  You may be thinking of strptime?

Yes, it returns a string, sorry.

Is 1053828152.54 a timestamp? The library documentation of fromtimestamp says:

"""
Return the local date and time corresponding to the
POSIX timestamp, such as is returned by time.time()
"""

That would mean that

    float(dt.strftime("%s")) + dt.microsecond/1000000.0

does what John asked:

 22 >>> x, a = datetime.now(), time.time()
 23 >>> float(x.strftime("%s")) + x.microsecond/1000000.0, a
(1053893218.302195, 1053893218.302245)

Or am I mistaken here?

yours,
Gerrit.

-- 
211. If a woman of the free class lose her child by a blow, he shall
pay five shekels in money. 
        -- Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list