Convert to UTC Timestamp

前端 未结 4 1330
遥遥无期
遥遥无期 2021-01-08 00:09
//parses some string into that format.
datetime1 = datetime.strptime(somestring, \"%Y-%m-%dT%H:%M:%S\")

//gets the seconds from the above date.
timestamp1 = time.mk         


        
4条回答
  •  情深已故
    2021-01-08 01:00

    def getDateAndTime(seconds=None):
     """
      Converts seconds since the Epoch to a time tuple expressing UTC.
      When 'seconds' is not passed in, convert the current time instead.
      :Parameters:
          - `seconds`: time in seconds from the epoch.
      :Return:
          Time in UTC format.
    """
    return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(seconds))`
    

    This converts local time to UTC

    time.mktime(time.localtime(calendar.timegm(utc_time)))
    

    http://feihonghsu.blogspot.com/2008/02/converting-from-local-time-to-utc.html

    If converting a struct_time to seconds-since-the-epoch is done using mktime, this conversion is in local timezone. There's no way to tell it to use any specific timezone, not even just UTC. The standard 'time' package always assumes that a time is in your local timezone.

提交回复
热议问题