Python date string manipulation based on timezone - DST
Question: My objective is to take string(containg UTC date and time) as the input and
convert it to local timezone based on Timezone difference. I have come up with
the following code

**Code**

    
    
    import time
    print "Timezone Diff", time.timezone/3600
    
    def convertTime(string):
            print "Before Conversion"
            print "year",string[0:4],"month",string[5:7],"day",string[8:10]
            print "hour",string[11:13],"min",string[14:16]
            print "After Conversion"
            print "newhour",int(string[11:13])-(time.timezone/3600)
            newhour = int(string[11:13])-(time.timezone/3600) 
            if newhour>=24:
                print "year",string[0:4],"month",string[5:7],"newday",int(string[8:10])+1
                print "hour",newhour-24,"min",string[14:16]
    convertTime('2013:07:04:14:00')
    

**Output:**

    
    
    Timezone Diff -10
    Before Conversion
    year 2013 month 07 day 04
    hour 14 min 00
    After Conversion
    newhour 24
    year 2013 month 07 newday 5
    hour 0 min 00
    

This code is very basic , and clearly wouldn't work for month /year changes
and not consider leap years. Can anyone suggest me a better approach to this
issue.


Answer: Here's a solution with the
[`datetime`](http://docs.python.org/2/library/datetime.html) and
[`pytz`](http://pytz.sourceforge.net/) modules, using my timezone as an
example:

    
    
    import pytz
    import datetime
    s = '2013:07:04:14:00'
    mydate = datetime.datetime.strptime(s, '%Y:%m:%d:%H:%M')
    mydate = mydate.replace(tzinfo=timezone('Australia/Sydney'))
    print mydate
    

Prints:

    
    
    2013-07-04 14:00:00+10:00
    

You may have to "reshape" the code to work for your exact output, but I hope
this helps in any way!

