converting epoch time with milliseconds to datetime

后端 未结 2 1044
臣服心动
臣服心动 2020-11-28 07:24

I have used a ruby script to convert iso time stamp to epoch, the files that I am parsing has following time stamp structure:

2009-03-08T00:27:31.807


        
相关标签:
2条回答
  • 2020-11-28 07:51

    Use datetime.datetime.fromtimestamp:

    >>> import datetime
    >>> s = 1236472051807 / 1000.0
    >>> datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f')
    '2009-03-08 09:27:31.807000'
    

    %f directive is only supported by datetime.datetime.strftime, not by time.strftime.

    UPDATE Alternative using %, str.format:

    >>> import time
    >>> s, ms = divmod(1236472051807, 1000)  # (1236472051, 807)
    >>> '%s.%03d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
    '2009-03-08 00:27:31.807'
    >>> '{}.{:03d}'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
    '2009-03-08 00:27:31.807'
    
    0 讨论(0)
  • 2020-11-28 07:57

    those are miliseconds, just divide them by 1000, since gmtime expects seconds ...

    time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1236472051807/1000.0))
    
    0 讨论(0)
提交回复
热议问题