Pacific-Design.com

    
Home Index

1. Python

2. UTC Local Time

Python / UTC Local Time /

Convert UTC to Local Time

#!/usr/bin/python
from datetime import datetime
import pytz

def convert_utc_local(UTC):
    utc         = pytz.utc
    local       = pytz.timezone('America/Los_Angeles')
    substring   = UTC[:19]
    datetime1   = datetime.strptime(substring, "%Y-%m-%dT%H:%M:%S")
    local_time  = local.localize(datetime1)
    utc_time    = local_time.astimezone(utc)
    return local_time


lax = convert_utc_local('2017-02-04T08:12:34+0000')
print "Los Angeles Time: ", lax

Los Angeles Time:  2017-02-04 08:12:34-08:00