Convert Unix timestamp to Readable Date/time

Discussion in 'Python' started by kaklis@gmail.com, Jul 22, 2010.

  1. Guest

    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: Thu, 22 Jul 2010 10:56:14 GMT which is what i want.
    What am i doing wrong?
    Thanks

    Antonis
    , Jul 22, 2010
    #1
    1. Advertising

  2. Chris Rebert Guest

    On Thu, Jul 22, 2010 at 5:54 AM, <> 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:    Thu, 22 Jul 2010 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
    Chris Rebert, Jul 22, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fibre Optic
    Replies:
    1
    Views:
    674
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Feb 23, 2007
  2. Thierry Lam
    Replies:
    0
    Views:
    291
    Thierry Lam
    Oct 27, 2008
  3. Phillip B Oldham

    Convert date/time to unix timestamp?

    Phillip B Oldham, Feb 10, 2009, in forum: Python
    Replies:
    2
    Views:
    4,447
    M.-A. Lemburg
    Feb 10, 2009
  4. Andyza
    Replies:
    6
    Views:
    1,331
    Evertjan.
    Mar 5, 2009
  5. Sadia Bashir
    Replies:
    0
    Views:
    98
    Sadia Bashir
    Jan 30, 2014
Loading...

Share This Page