Convert Unix timestamp to Readable Date/time

Chris Rebert clp2 at rebertia.com
Thu Jul 22 09:01:47 EDT 2010


On Thu, Jul 22, 2010 at 5:54 AM, kaklis at gmail.com <kaklis at gmail.com> wrote:
> Well i have the following number 1279796174846
>  i did the following:
>> mdate = 1279796174846
> tempStr = str(mdate)
> tempStr2 = tempStr[:-3]
> tempInt = int(tempStr2)
> print "Last Login :", datetime.datetime.fromtimestamp(tempInt)
>> that prints out: 2010年07月22日 06:56:14
> But when i check my answer at http://www.onlineconversion.com/unix_time.htm
>> i got:    2010年7月22日 10:56:14 GMT which is what i want.
> What am i doing wrong?

>From http://docs.python.org/library/datetime.html (emphases added):
"""
classmethod datetime.fromtimestamp(timestamp[, tz])
 Return the ***local*** date and time corresponding to the POSIX
timestamp [...]
"""
vs.
"""
classmethod datetime.utcfromtimestamp(timestamp)¶
 Return the ***UTC*** datetime corresponding to the POSIX timestamp [...]
"""
IOW, read the fine docs.
Cheers,
Chris
--
http://blog.rebertia.com


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /