import time from calendar import timegm def convert_time(string_time): result = string_time.find('.') if result < 0: utc_time = time.strptime(string_time, "%Y-%m-%dT%H:%M:%S%fZ") else: utc_time = time.strptime(string_time, "%Y-%m-%dT%H:%M:%S.%fZ") epoch_time = timegm(utc_time) # utc2_time = time.strptime(string2_time, "%Y-%m-%dT%H:%M:%S.%fZ") # epoch2_time = timegm(utc2_time) # epoch_time = min(epoch1_time, epoch2_time) #choose the earliest trade time return epoch_time