TIME_TO_TIME.PY

The time converter function, time_to_time.py, can be used independently from the gpx_converter.py program. It takes a GPX time string, <time>2016-09-08T19:01:46Z</time>, an input time zone integer, and an output time zone integer.

The argument string just needs time units separated by something, not necessarily a GPX time format.

The function returns Local Time in a tupel containing ints (NOT strings) of year, month, day, hour, minutes, seconds.

You can convert a GPX UTC time string to your local time by using an input zone of 0 (UTC time zone) and an output zone that you want.

Making this function was more fun than trying to figure out the Python library or external time libraries!

The source code follows:


"""
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
time_to_time.py

- tested on Python 3.4.2

 arg 1 = time string ('') or something similar
 arg 2 = time zone int of input
 arg 3 = time zone int of output

 returns a tupel of output time integers (year, month, day, hour, min, sec)
  
    Copyright (C) 2017  Jim Fuchs

    - This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    - This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    - You should have received a copy of the GNU General Public License
    along with this program. If not, see http://www.gnu.org/licenses/.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
"""
import re

def time2time(in_time_string, in_zone, out_zone):

    in_time_list = re.findall('\d+', in_time_string)
    out_year_int = in_year_int = int(in_time_list[0])
    out_month_int = in_month_int = int(in_time_list[1])
    out_day_int = in_day_int = int(in_time_list[2])
    out_hour_int = in_hour_int = int(in_time_list[3])
    out_min_int = in_min_int = int(in_time_list[4])
    out_sec_int =  in_sec_int = int(in_time_list[5])

    zone_offset = out_zone - in_zone
    leap_year = ((in_year_int % 4 == 0) and (in_year_int % 100 != 0)) or (in_year_int % 400 == 0)

    # INPUT AND OUTPUT TIME ZONES ARE THE SAME: time is simply output to a tuple with no changes

    # OUTPUT TIME IS BEHIND INPUT TIME (output location is west of input location)
    if (zone_offset < 0):
        # *** 24 HOUR CORRECTION *** (if hour change crosses midnight)
        if (in_hour_int < (abs(zone_offset))):
            # *** HOUR FIX ***
            out_hour_int = zone_offset +in_hour_int +24
            # *** DAY FIX ***
            if (in_month_int != 3):
                if (in_day_int != 1): 
                    out_day_int = in_day_int-1
                elif (in_month_int == 5 or in_month_int == 7 or in_month_int == 10 or in_month_int == 12):
                    out_day_int = 30
                else: out_day_int = 31
            else:
                if (in_day_int != 1): 
                    out_day_int = in_day_int-1
                elif not leap_year:
                    out_day_int = 28
                else: out_day_int = 29
            # *** MONTH FIX ***
            if (in_day_int == 1):
                out_month_int = in_month_int-1      
                if (out_month_int == 0): 
                    out_month_int = 12
            # *** YEAR FIX ***
            if ((in_month_int == 1) and (in_day_int == 1)):
                out_year_int = in_year_int-1

    # OUTPUT TIME IS AHEAD OF INPUT TIME (output location is east of input location)
    if (zone_offset > 0):
        # *** 24 HOUR CORRECTION *** (if hour change crosses midnight)
        if ((24-in_hour_int) <= (abs(zone_offset))):
            # *** HOUR FIX ***
            out_hour_int = zone_offset +in_hour_int -24
            # *** DAY FIX ***
            if (in_month_int != 2):
                out_day_int = in_day_int+1
                if in_day_int == 30:
                    out_day_int = 31
                    if (in_month_int == 4 or in_month_int == 6 or in_month_int == 9 or in_month_int == 11):
                        out_day_int = 1
                elif (in_day_int == 31):
                    out_day_int = 1
            else:
                if (in_day_int < 28):
                    out_day_int = in_day_int+1
                elif ((not leap_year) or (in_day_int == 29)):
                    out_day_int = 1
                else:
                    out_day_int = 29
            # *** MONTH FIX ***
            if (in_month_int != 2):
                if (in_month_int == 4 or in_month_int == 6 or in_month_int == 9 or in_month_int == 11):
                    if (in_day_int == 30):
                        out_month_int = in_month_int+1
                elif (in_day_int == 31):
                        out_month_int = in_month_int+1
                        if out_month_int == 13:
                            out_month_int = 1
            else:
                if (in_day_int > 27): 
                    if ((not leap_year) or (in_day_int == 29)): 
                        out_month_int = 3
            # *** YEAR FIX ***
            if ((in_month_int == 12) and (in_day_int == 31)):
                out_year_int = in_year_int+1

    out_time_tuple = (out_year_int, out_month_int, out_day_int, out_hour_int, out_min_int, out_sec_int)  
    return(out_time_tuple)

screen shot ahead of utc screen shot behind utc





LEFT: terminal output of time_to_time.py if output time zone is ahead of input time zone. Shown are the time zones in and out, the input time < time >...< /time >, and the output tuple.


RIGHT: terminal output of time_to_time.py if output time zone is behind input time zone.

The test program, used for the above examples, illustrates the use of time_to_time.py:


# test_time_to_time.py

import time_to_time

# test ahead of UTC
in_zone = 0
out_zone = 7

String1 = ['']
String1.append('')
String1.append('')
String1.append('')

print('\nTIME ZONE AHEAD OF UTC')

for x in range(4):
    print('\nTime Zone In:', in_zone, '\nTime Zone Out:',out_zone)
    print(String1[x])
    converted_time = time_to_time.time2time(String1[x],  in_zone, out_zone)
    print(converted_time,'\n')

# test behind UTC
in_zone = 0
out_zone = -7

String1 = ['']
String1.append('')
String1.append('')
String1.append('')

print('\nTIME ZONE BEHIND UTC')

for x in range(4):
    print('\nTime Zone In:', in_zone, '\nTime Zone Out:',out_zone)
    print(String1[x])
    converted_time = time_to_time.time2time(String1[x], in_zone, out_zone)
    print(converted_time,'\n')