Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@santiagobasulto
Last active February 26, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santiagobasulto/b9ac9697d4741fa7e477fece23f2d7a3 to your computer and use it in GitHub Desktop.
Save santiagobasulto/b9ac9697d4741fa7e477fece23f2d7a3 to your computer and use it in GitHub Desktop.
Generate time options for different timezones
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)
@santiagobasulto
Copy link
Author

Output:

08AM - 10AM (Eastern) | 05AM - 07AM (Pacific) | 01PM - 03PM (London)
10AM - 12PM (Eastern) | 07AM - 09AM (Pacific) | 03PM - 05PM (London)
12PM - 02PM (Eastern) | 09AM - 11AM (Pacific) | 05PM - 07PM (London)
02PM - 04PM (Eastern) | 11AM - 01PM (Pacific) | 07PM - 09PM (London)
04PM - 06PM (Eastern) | 01PM - 03PM (Pacific) | 09PM - 11PM (London)
06PM - 08PM (Eastern) | 03PM - 05PM (Pacific) | 11PM - 01AM (London)
08PM - 10PM (Eastern) | 05PM - 07PM (Pacific) | 01AM - 03AM (London)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment