How to specify time zone (UTC) when converting to Unix time? (Python)

前端 未结 4 1189
遥遥无期
遥遥无期 2021-01-03 04:08

I have a utc timestamp in the IS8601 format and am trying to convert it to unix time. This is my console session:

In [9]: mydate
Out[9]: \'2009-07-17T01:21:0         


        
4条回答
  •  自闭症患者
    2021-01-03 04:35

    naive_utc_dt = parseddate.replace(tzinfo=None)
    timestamp = (naive_utc_dt - datetime(1970, 1, 1)).total_seconds()
    # -> 1247793660.0
    

    See more details in another answer to similar question.

    And back:

    utc_dt = datetime.utcfromtimestamp(timestamp)
    # -> datetime.datetime(2009, 7, 17, 1, 21)
    

提交回复
热议问题