Last active
February 26, 2018 14:30
-
-
Save santiagobasulto/b9ac9697d4741fa7e477fece23f2d7a3 to your computer and use it in GitHub Desktop.
Generate time options for different timezones
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 datetime, date, time, timedelta | |
from pytz import timezone | |
START_DATE = date(2017, 10, 9) | |
START_TIME = time(8, 0) | |
START_DATETIME = datetime.combine(START_DATE, START_TIME) | |
INTERVAL_IN_HOURS = 2 | |
OPTION_TEMPLATE = "{start} - {end} ({tz})" | |
eastern = timezone('US/Eastern') | |
pacific = timezone('US/Pacific') | |
london = timezone('Europe/London') | |
start_eastern_dt = START_DATETIME.replace(tzinfo=eastern) | |
current_eastern_dt = start_eastern_dt | |
def get_option(start_dt, end_dt, original_tz, new_tz): | |
start = start_dt | |
end = end_dt | |
if original_tz != new_tz: | |
start = start_dt.astimezone(new_tz) | |
end = end_dt.astimezone(new_tz) | |
option = OPTION_TEMPLATE.format( | |
start=start.strftime("%I%p"), | |
end=end.strftime("%I%p"), | |
tz=new_tz.zone.split('/')[1]) | |
return option | |
def print_intervals_in_tzs( | |
original_tz, timezones, start_dt, interval_in_hours, options_count): | |
current_dt = start_dt | |
for _ in range(options_count): | |
start = current_dt | |
end = current_dt + timedelta(hours=interval_in_hours) | |
options = [get_option(start, end, original_tz, tz) for tz in timezones] | |
print(" | ".join(options)) | |
current_dt = end | |
if __name__ == '__main__': | |
print_intervals_in_tzs( | |
eastern, | |
timezones=[eastern, pacific, london], | |
start_dt=start_eastern_dt, | |
interval_in_hours=INTERVAL_IN_HOURS, | |
options_count=7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: