Search code examples
pythonpytz

Create New Timezone in pytz


I'm trying to create a timezone object for "GMT+01", however, I haven't been able to find a way to do that using pytz. I tried using cities that (http://wwp.greenwichmeantime.com/time-zone/gmt-plus-1/) says are in this timezone, but they all give seemingly weird and inconsistent results (they're relative to LMT)

>>> timezone('Africa/Algiers')
<DstTzInfo 'Africa/Algiers' PMT+0:09:00 STD>
>>> timezone('Africa/Brazzaville')
<DstTzInfo 'Africa/Brazzaville' LMT+1:01:00 STD>
>>> timezone('Africa/Conakry')
<DstTzInfo 'Africa/Conakry' LMT-1 day, 23:05:00 STD>
>>> timezone('Africa/Douala')
<DstTzInfo 'Africa/Douala' LMT+0:39:00 STD>

Does anyone know of a way to just create a custom timezone with "GMT+01" or maybe even creating one relative to timezone("GMT"). I tried adding a datetime.timedelta, but that (unsurprisingly) didn't work.


Solution

  • You can do that using pytz by selecting timezone, then try to manipulate strings and extract from datetime the time in function of GMT, then you can select only time with GMT+x in your case, it's GMT+1

    import pytz
    
    from datetime import tzinfo, timedelta, datetime
    
    GMT_1_Time_Zone = []
    for tz in pytz.common_timezones:
        now = datetime.now(pytz.timezone(tz))
        print now
        if "+0100" in now.strftime("%z"):
            GMT_1_Time_Zone.append((tz, "(GMT%s) %s" % (now.strftime("%z"), tz)))
    
    
    print GMT_1_Time_Zone
    

    Output:

    [('Africa/Algiers', '(GMT+0100) Africa/Algiers'), ('Africa/Bangui', '(GMT+0100) Africa/Bangui'), ('Africa/Brazzaville', '(GMT+0100) Africa/Brazzaville'), ('Africa/Ceuta', '(GMT+0100) Africa/Ceuta'), ('Africa/Douala', '(GMT+0100) Africa/Douala'), ('Africa/Kinshasa', '(GMT+0100) Africa/Kinshasa'), ('Africa/Lagos', '(GMT+0100) Africa/Lagos'), ('Africa/Libreville', '(GMT+0100) Africa/Libreville'), ('Africa/Luanda', '(GMT+0100) Africa/Luanda'), ('Africa/Malabo', '(GMT+0100) Africa/Malabo'), ('Africa/Ndjamena', '(GMT+0100) Africa/Ndjamena'), ('Africa/Niamey', '(GMT+0100) Africa/Niamey'), ('Africa/Porto-Novo', '(GMT+0100) Africa/Porto-Novo'), ('Africa/Tripoli', '(GMT+0100) Africa/Tripoli'), ('Africa/Tunis', '(GMT+0100) Africa/Tunis'), ('Arctic/Longyearbyen', '(GMT+0100) Arctic/Longyearbyen'), ('Europe/Amsterdam', '(GMT+0100) Europe/Amsterdam'), ('Europe/Andorra', '(GMT+0100) Europe/Andorra'), ('Europe/Belgrade', '(GMT+0100) Europe/Belgrade'), ('Europe/Berlin', '(GMT+0100) Europe/Berlin'), ('Europe/Bratislava', '(GMT+0100) Europe/Bratislava'), ('Europe/Brussels', '(GMT+0100) Europe/Brussels'), ('Europe/Budapest', '(GMT+0100) Europe/Budapest'), ('Europe/Busingen', '(GMT+0100) Europe/Busingen'), ('Europe/Copenhagen', '(GMT+0100) Europe/Copenhagen'), ('Europe/Gibraltar', '(GMT+0100) Europe/Gibraltar'), ('Europe/Ljubljana', '(GMT+0100) Europe/Ljubljana'), ('Europe/Luxembourg', '(GMT+0100) Europe/Luxembourg'), ('Europe/Madrid', '(GMT+0100) Europe/Madrid'), ('Europe/Malta', '(GMT+0100) Europe/Malta'), ('Europe/Monaco', '(GMT+0100) Europe/Monaco'), ('Europe/Oslo', '(GMT+0100) Europe/Oslo'), ('Europe/Paris', '(GMT+0100) Europe/Paris'), ('Europe/Podgorica', '(GMT+0100) Europe/Podgorica'), ('Europe/Prague', '(GMT+0100) Europe/Prague'), ('Europe/Rome', '(GMT+0100) Europe/Rome'), ('Europe/San_Marino', '(GMT+0100) Europe/San_Marino'), ('Europe/Sarajevo', '(GMT+0100) Europe/Sarajevo'), ('Europe/Skopje', '(GMT+0100) Europe/Skopje'), ('Europe/Stockholm', '(GMT+0100) Europe/Stockholm'), ('Europe/Tirane', '(GMT+0100) Europe/Tirane'), ('Europe/Vaduz', '(GMT+0100) Europe/Vaduz'), ('Europe/Vatican', '(GMT+0100) Europe/Vatican'), ('Europe/Vienna', '(GMT+0100) Europe/Vienna'), ('Europe/Warsaw', '(GMT+0100) Europe/Warsaw'), ('Europe/Zagreb', '(GMT+0100) Europe/Zagreb'), ('Europe/Zurich', '(GMT+0100) Europe/Zurich')]

    UPDATE

    After digging in the problem. I found two better solutions to do that.

    from dateutil import tz
    import datetime
    import pytz
    
    from datetime import datetime
    dt = datetime.strptime("2014-11-02 21:00:00", "%Y-%m-%d %H:%M:%S")
    dt = pytz.timezone('Etc/GMT+1').localize(dt)
    dt.isoformat()
    print dt
    

    using parser

    from dateutil.parser import parse
    dt = parse("2014-11-02 21:00:00" + "GMT+0100")
    dt.isoformat()
    print dt