Difference between using pytz.localize and datetime.replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, time, datetime | |
import pytz | |
pacific = pytz.timezone('US/Pacific') | |
eastern = pytz.timezone('US/Eastern') | |
def get_naive_date_time(): | |
date_start = date(2016, 9, 16) | |
time_start = time(10, 0) | |
return datetime.combine(date_start, time_start) | |
def print_all_times(date_time): | |
print date_time | |
print "UTC:", date_time.astimezone(pytz.utc) | |
print "Eastern:", date_time.astimezone(eastern) | |
if __name__ == '__main__': | |
naive_date_time = get_naive_date_time() | |
print "Using tz replace" | |
print_all_times(naive_date_time.replace(tzinfo=pacific)) | |
print "Using localize" | |
print_all_times(pacific.localize(naive_date_time)) |
for those finding this while researching a problem read this
http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using tz replace
Using localize