How to convert local time string to UTC?

后端 未结 23 1164
离开以前
离开以前 2020-11-22 04:18

How do I convert a datetime string in local time to a string in UTC time?

I\'m sure I\'ve done this before, but can\'t find it and SO will hopefull

相关标签:
23条回答
  • 2020-11-22 04:53

    Using http://crsmithdev.com/arrow/

    arrowObj = arrow.Arrow.strptime('2017-02-20 10:00:00', '%Y-%m-%d %H:%M:%S' , 'US/Eastern')
    
    arrowObj.to('UTC') or arrowObj.to('local') 
    

    This library makes life easy :)

    0 讨论(0)
  • 2020-11-22 04:53

    Briefly, to convert any datetime date to UTC time:

    from datetime import datetime
    
    def to_utc(date):
        return datetime(*date.utctimetuple()[:6])
    

    Let's explain with an example. First, we need to create a datetime from the string:

    >>> date = datetime.strptime("11 Feb 2011 17:33:54 -0800", "%d %b %Y %H:%M:%S %z")
    

    Then, we can call the function:

    >>> to_utc(date)
    datetime.datetime(2011, 2, 12, 1, 33, 54)
    

    Step by step how the function works:

    >>> date.utctimetuple()
    time.struct_time(tm_year=2011, tm_mon=2, tm_mday=12, tm_hour=1, tm_min=33, tm_sec=54, tm_wday=5, tm_yday=43, tm_isdst=0)
    >>> date.utctimetuple()[:6]
    (2011, 2, 12, 1, 33, 54)
    >>> datetime(*date.utctimetuple()[:6])
    datetime.datetime(2011, 2, 12, 1, 33, 54)
    
    0 讨论(0)
  • 2020-11-22 04:56

    I'm having good luck with dateutil (which is widely recommended on SO for other related questions):

    from datetime import *
    from dateutil import *
    from dateutil.tz import *
    
    # METHOD 1: Hardcode zones:
    utc_zone = tz.gettz('UTC')
    local_zone = tz.gettz('America/Chicago')
    # METHOD 2: Auto-detect zones:
    utc_zone = tz.tzutc()
    local_zone = tz.tzlocal()
    
    # Convert time string to datetime
    local_time = datetime.strptime("2008-09-17 14:02:00", '%Y-%m-%d %H:%M:%S')
    
    # Tell the datetime object that it's in local time zone since 
    # datetime objects are 'naive' by default
    local_time = local_time.replace(tzinfo=local_zone)
    # Convert time to UTC
    utc_time = local_time.astimezone(utc_zone)
    # Generate UTC time string
    utc_string = utc_time.strftime('%Y-%m-%d %H:%M:%S')
    

    (Code was derived from this answer to Convert UTC datetime string to local datetime)

    0 讨论(0)
  • 2020-11-22 04:56

    if you prefer datetime.datetime:

    dt = datetime.strptime("2008-09-17 14:04:00","%Y-%m-%d %H:%M:%S")
    utc_struct_time = time.gmtime(time.mktime(dt.timetuple()))
    utc_dt = datetime.fromtimestamp(time.mktime(utc_struct_time))
    print dt.strftime("%Y-%m-%d %H:%M:%S")
    
    0 讨论(0)
  • 2020-11-22 05:01

    I've had the most success with python-dateutil:

    from dateutil import tz
    
    def datetime_to_utc(date):
        """Returns date in UTC w/o tzinfo"""
        return date.astimezone(tz.gettz('UTC')).replace(tzinfo=None) if date.tzinfo else date
    
    0 讨论(0)
  • 2020-11-22 05:02

    I found the best answer on another question here. It only uses python built-in libraries and does not require you to input your local timezone (a requirement in my case)

    import time
    import calendar
    
    local_time = time.strptime("2018-12-13T09:32:00.000", "%Y-%m-%dT%H:%M:%S.%f")
    local_seconds = time.mktime(local_time)
    utc_time = time.gmtime(local_seconds)
    

    I'm reposting the answer here since this question pops up in google instead of the linked question depending on the search keywords.

    0 讨论(0)
提交回复
热议问题