← Back to challenges

Time Around the World

PythonHarddatesformatting

Instructions

In this challenge, the goal is to calculate what time it is in two different cities. You're given a string city_a and the related string timestamp (time in city_a) with the date formatted in full U.S. notation, as in this example:

"July 21, 1983 23:01"

You have to return a new timestamp with date and corresponding time in city_b, formatted as in this example:

"1983-7-22 23:01"

See the table below for a list of given cities and their GMT (Greenwich Mean Time) hours offsets.

GMTCity
- 08:00Los Angeles
- 05:00New York
- 04:30Caracas
- 03:00Buenos Aires
00:00London
+ 01:00Rome
+ 03:00Moscow
+ 03:30Tehran
+ 05:30New Delhi
+ 08:00Beijing
+ 10:00Canberra

Examples

time_difference("Los Angeles", "April 1, 2011 23:23", "Canberra") ➞ "2011-4-2 17:23"
# Can be a new day.

time_difference("London", "July 31, 1983 23:01", "Rome") ➞ "1983-8-1 00:01"
# Can be a new month.

time_difference("New York", "December 31, 1970 13:40", "Beijing") ➞ "1971-1-1 02:40"
# Can be a new year.

Notes

  • Pay attention to hours and minutes, a leading 0 is needed in the returned timestamp when they're a single digit.
  • Pay attention to cities with half hours offsets.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.