Created
February 3, 2022 01:58
-
-
Save les-peters/c20c4dcdd5d9394271b2093c61dc536b to your computer and use it in GitHub Desktop.
Localtime
This file contains hidden or 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
| question = """ | |
| Given an array of people and their timezones in UTC, write a function to return the local | |
| time for a given person. Extra credit: add options for a 12 or 24 hour clock! | |
| Example: | |
| let humans = [ | |
| { name: 'Clara', timezone: 'UTC−4:00' }, | |
| { name: 'Cami', timezone: 'UTC−7:00' } | |
| { name: 'Ximena', timezone: 'UTC−5:00' } | |
| ] | |
| $ localTime('Cami') | |
| $ 10:26pm // This will change depending on when you run the function | |
| """ | |
| from datetime import datetime, timezone, timedelta | |
| import re | |
| humans = [] | |
| humans.append({ "name": 'Clara', "timezone": 'UTC-4:00' }) | |
| humans.append({ "name": 'Cami', "timezone": 'UTC-7:00' }) | |
| humans.append({ "name": 'Ximena', "timezone": 'UTC-5:00' }) | |
| def localTime(person): | |
| tz_p = re.compile(r'UTC([-+])(\d+):(\d\d)') | |
| for human in humans: | |
| if person == human['name']: | |
| tz_m = tz_p.search(human['timezone']) | |
| if tz_m: | |
| offset = int(tz_m.group(2)) + (int(tz_m.group(3)) / 60) | |
| if tz_m.group(1) == '-': | |
| offset = - offset | |
| mytime = datetime.now(timezone(timedelta(hours=offset), person)) | |
| print(mytime.strftime('%I:%M%p').lower()) | |
| localTime('Cami') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment