World Time Conversion

7

0

World Time Conversion

Challenge:

Given an initial clock time in UTC and a list of countries: for each country in the list, output the local time it is in that country based on the initial time. In cases where a country is big enough to have multiple local time zones (like the United States), you may output any valid local time for that country, provided you state which time zone you are using (i.e. for United States, EST for Eastern Standard Time). You do not have to worry about daylight savings time.

Input:

The current time, in UTC, followed by a list of countries. The format of the input time can be given in any way (12 or 24 hour, with or without seconds, etc), and the countries can all be on the same line or on new lines after the time, your choice. You may also use any characters to delimit the countries (space, comma, etc.). You may choose to take a list of country -> timezone in as input, in which case I would prefer a common list be used across all answers, found here. This part can be excluded from your byte count.

Output:

The local time in each country from the input list. Once again, you may use any characters or newline to delimit the times for each country so long as it is easy to tell one time from another. However, unlike the input, the times must be output in a 12 hour format (so there must be an AM/PM indication). Other than that, you may express the digits in the time in any format (i.e. leading 0's for hours that are single digits, with or without seconds/milliseconds) as long as the character that is used to separate hours and minutes is different than the character that is used to separate the different times. I would prefer if a date was not included in the answer since this is a challenge dealing specifically with different times.

Examples:

Input 1:

12:00 AM [United States]

Output 1:

7:00 PM EST (or any other time zone with the appropriate time)

Input 2:

12:00 PM [United States, Italy, Russia]

Output 2:

4:00 AM PST, 2:00 PM, 3:00 PM MSK

Rules:

This is , so shortest code in bytes wins. Standard loopholes are forbidden.

Common list of country -> timezone:

// Format:  <country>,<UTC offset>,<time-zone abbreviation>
// You may use any alternate format or any other UTC/abbreviation for a country for those that have more than one zone
France,1,CET
United States,-5,EST
Russia,3,MSK
United Kingdom,0,UTC
Australia,10,AEST
Canada,-5,EST
Kingdom of Denmark,1,CET
New Zealand,12,NZST
Brazil,-3,BRT
Mexico,-6,MDT
Indonesia,7,WIST
Kiribati,12,LINT
Chile,-3,CLT
Democratic Republic of the Congo,1,WAT
Ecuador,-5,ECT
Federated States of Micronesia,11,KOST
Kazakhstan,6,ALMT
Kingdom of the Netherlands,1,CET
Mongolia,8,ULAT 
Papua New Guinea,10,AEST
Portugal,0,WET
South Africa,2,SAST
Spain,1,CET
Afghanistan,4.5,AFT
Albania,1,CET
Algeria,1,CET
Andorra,1,CET
Angola,1,WAT
Antigua and Barbuda,-4,AST
Argentina,-3,ART
Armenia,4
Austria,1,CET
Azerbaijan,4
Bahamas,-5,EST
Bahrain,3
Bangladesh,6,BDT
Barbados,-4
Belarus,3,FET
Belgium,1,CET
Belize,-6
Benin,1,WAT
Bhutan,6,BTT
Bolivia,-4
Bosnia and Herzegovina,1,CET
Botswana,2,CAT
Brunei,8
Bulgaria,2,EET
Burkina Faso,0,UTC
Burundi,2,CAT
Cambodia,7
Cameroon,1,WAT
Cape Verde,-1,CVT
Central African Republic,1,WAT
Chad,1,WAT
China,8,CST
Colombia,-5
Comoros,3,EAT
Republic of the Congo,1,WAT
Costa Rica,-6
Croatia,1,CET
Cuba,-5
Cyprus,2,EET
Czech Republic,1,CRT
Djibouti,3,EAT
Dominica,-4
Dominican Republic,-4
East Timor,9
Egypt,2,EET
El Salvador,-6
Equatorial Guinea,1,WAT
Eritrea,3,EAT
Estonia,2,EET
Ethiopia,3,EAT
Fiji,12
Finland,2,EET
Gabon,1,WAT
Gambia,0,GMT
Georgia,4
Germany,1,CET
Ghana,0,GMT
Greece,2,EET
Grenada,-4
Guatemala,-6
Guinea,0,GMT
Guinea-Bissau,0,GMT
Guyana,-4
Haiti,-5
Honduras,-6
Hong Kong,8,HKT
Hungary,1,CET
Iceland,0,GMT
India,5.5,IST
Iran,3.5,IRST
Iraq,3
Ireland,0,WET
Israel,2,IST
Italy,1,CET
Ivory Coast,0,GMT
Jamaica,-5
Japan,9,JST
Jordan,2
Kenya,3,EAT
North Korea,8.5,PYT
South Korea,9,KST
Kuwait,3,AST
Kosovo,1
Kyrgyzstan,6
Laos,7
Latvia,2,EET
Lebanon,2,EET
Lesotho,2
Liberia,0,GMT
Libya,2,EET
Liechtenstein,1,CET
Lithuania,2,EET
Luxembourg,1,CET
Macau China,8,CST
Macedonia,1,CET
Madagascar,3,EAT
Malawi,2,CAT
Malaysia,8,MYT
Maldives,5
Mali,0,GMT
Malta,1,CET
Marshall Islands,12
Mauritania,0,GMT
Mauritius,4,MUT 
Moldova,2,EET
Monaco,1,CET
Montenegro,1,CET
Morocco,0,WET
Mozambique,2,CAT
Myanmar,6.5,MST
Namibia,1,WAT
Nauru,12
Nepal,5.75,NPT
Nicaragua,-6
Niger,1,WAT
Nigeria,1,WAT
Norway,1,CET
Oman,4
Pakistan,5,PKT
Palau,9
Panama,-5
Paraguay,-4
Peru,-5,PET
Philippines,8,PHT
Poland,1,CET
Qatar,3,AST
Romania,2,EET
Rwanda,2,CAT
Saint Kitts and Nevis,-4
Saint Lucia,-4
Saint Vincent and the Grenadines,-4
Samoa,13
San Marino,1,CET
São Tomé and Príncipe,0,GMT
Saudi Arabia,3,AST
Senegal,0,GMT
Serbia,1,CET
Seychelles,4,SCT 
Sierra Leone,0,GMT
Singapore,8,SST
Slovakia,1,CET
Slovenia,1,CET
Solomon Islands,11
Somalia,3,EAT
South Sudan,3,EAT
Sri Lanka,5.5,SLST
Sudan,3,EAT
Suriname,-3
Swaziland,2
Sweden,1,CET
Switzerland,1,CET
Syria,2,EET
Taiwan,8
Tajikistan,5,
Tanzania,3,EAT
Thailand,7,THA
Togo,0,GMT
Tonga,13
Trinidad and Tobago,-4
Tunisia,1,CET
Turkey,2,EET
Turkmenistan,5
Tuvalu,12
Uganda,3,EAT
Ukraine,2,EET
United Arab Emirates,4
Uruguay,-3
Uzbekistan,5,UZT
Vanuatu,11,
Vatican City,1,CET
Venezuela,-4
Vietnam,7,ICT
Yemen,3
Zambia,2,CAT
Zimbabwe,2,CAT

Yodle

Posted 2016-10-13T17:42:00.110

Reputation: 2 378

3

very close to this question, the difference is the "convert country name to timezone" part

– Rod – 2016-10-13T17:51:13.767

@Rod Yeah, I mentioned I found that in my sandbox post, but people didn't comment on it. – Yodle – 2016-10-13T17:56:57.487

1This "list of country -> timezone" - do you have a compiled version? The Wikipedia article has entries like "French Polynesia" in the section for France. – Jonathan Allan – 2016-10-13T17:59:26.653

@JonathanAllan I don't, I was going to leave it up to people as to how they wanted to set it up/format it. – Yodle – 2016-10-13T18:01:36.553

So can "AM" just be "A" and "PM" just be "P"? – Jonathan Allan – 2016-10-13T18:52:48.850

@JonathanAllan I suppose that is fine, still gets the message across. – Yodle – 2016-10-13T18:54:10.393

@Yodle What if a country spans multiple time zones is there a preference to which? – Magic Octopus Urn – 2016-10-13T19:02:15.677

@carusocomputing It specifies that in the question – Jonathan Allan – 2016-10-13T19:06:36.443

@Yodle Ah, missed that. Can you use the Wikipedia API to load the inputs? Do you have to handle all countries? – Magic Octopus Urn – 2016-10-13T19:11:03.887

@carusocomputing You can get the list anyway you want, and I would prefer being able to handle every country in case I want to travel somewhere new! – Yodle – 2016-10-13T19:14:14.570

@Yodle Ah, so just to clarify, the instantiation of the dictionary of countries is not included in byte count? May want to put that in the brief, other than that this is a cool challenge. – Magic Octopus Urn – 2016-10-13T19:19:24.450

1@carusocomputing See the edit, and thanks :) – Yodle – 2016-10-13T19:20:43.760

I think we need an authoritative list of countries/time zones to be able to make any objective comparisons between answers. – Robert Fraser – 2016-10-13T19:33:26.133

@RobertFraser Okay, I will work on compiling one when I am home. – Yodle – 2016-10-13T19:36:02.380

@Yodle You can slice and dice the one in my answer, if you want. – AdmBorkBork – 2016-10-13T19:47:25.493

Do we need to explicitly output the time zone information as well? (EST, etc.) Or is just the time output OK? – AdmBorkBork – 2016-10-13T19:59:08.973

@TimmyD I would like the timezone information included if the country has more than one timezone. And thanks, I'll probably use your list and make more general. Pastebin links are allowed in posts right? – Yodle – 2016-10-13T20:03:20.697

@Yodle Fixed. As far as I'm aware, Pastebin is OK if it doesn't fit in the challenge. Ideally, challenges should be self-contained. – AdmBorkBork – 2016-10-13T20:53:04.393

I've updated the post with a list of country -> timezone. – Yodle – 2016-10-14T14:20:16.943

Answers

5

PowerShell v3+, 71 bytes

param($a,$b,$x)$b|%{$y=$x[$_];"{0:t} $($y[1])"-f(date $a).AddHours($y[0])}

(74 bytes, -3 bytes for input ,$x, the dictionary of country->timezone conversions, equals 71 bytes)

Uses the below dictionary/hashtable for the timezone conversion, which I manually created (with some creative Excel, PowerShell, and Ctrl-H help) based on the Wikipedia link.

$x=@{'France'=(1,'CET');'United States'=(-5,'EST');'Russia'=(3,'Moscow');'United Kingdom'=(0,'UTC');'Australia'=(10,'AEST');'Canada'=(-5,'EST');'Kingdom of Denmark'=(1,'CET');'New Zealand'=(12,'NZST');'Brazil'=(-3,'BRT');'Mexico'=(-6,'Zona Centro');'Indonesia'=(7,'WIST');'Kiribati'=(12,'Gilbert');'Chile'=(-3,'CLT');'Democratic Republic of the Congo'=(1,'WAT');'Ecuador'=(-5,'Ecuador Time');'Federated States of Micronesia'=(11,'Kosrae');'Kazakhstan'=(6,'Almaty');'Kingdom of the Netherlands'=(1,'CET');'Mongolia'=(8,'Ulaanbaatar');'Papua New Guinea'=(10,'AEST');'Portugal'=(0,'WET');'South Africa'=(2,'SAST');'Spain'=(1,'CET');'Afghanistan'=(4.5,'Afghanistan');'Albania'=(1,'CET');'Algeria'=(1,'CET');'Andorra'=(1,'CET');'Angola'=(1,'WAT');'Antigua and Barbuda'=(-4,'AST');'Argentina'=(-3,'ART');'Armenia'=(4,'');'Austria'=(1,'CET');'Azerbaijan'=(4,'');'Bahamas'=(-5,'EST');'Bahrain'=(3,'');'Bangladesh'=(6,'BDT');'Barbados'=(-4,'');'Belarus'=(3,'FET');'Belgium'=(1,'CET');'Belize'=(-6,'');'Benin'=(1,'WAT');'Bhutan'=(6,'BTT');'Bolivia'=(-4,'');'Bosnia and Herzegovina'=(1,'CET');'Botswana'=(2,'CAT');'Brunei'=(8,'');'Bulgaria'=(2,'EET');'Burkina Faso'=(0,'UTC');'Burundi'=(2,'CAT');'Cambodia'=(7,'');'Cameroon'=(1,'WAT');'Cape Verde'=(-1,'CVT');'Central African Republic'=(1,'WAT');'Chad'=(1,'WAT');'China'=(8,'Chinese Standard Time');'Colombia'=(-5,'');'Comoros'=(3,'EAT');'Republic of the Congo'=(1,'WAT');'Costa Rica'=(-6,'');'Croatia'=(1,'CET');'Cuba'=(-5,'');'Cyprus'=(2,'EET');'Czech Republic'=(1,'CRT');'Djibouti'=(3,'EAT');'Dominica'=(-4,'');'Dominican Republic'=(-4,'');'East Timor'=(9,'');'Egypt'=(2,'EET');'El Salvador'=(-6,'');'Equatorial Guinea'=(1,'WAT');'Eritrea'=(3,'EAT');'Estonia'=(2,'EET');'Ethiopia'=(3,'EAT');'Fiji'=(12,'');'Finland'=(2,'EET');'Gabon'=(1,'WAT');'Gambia'=(0,'GMT');'Georgia'=(4,'');'Germany'=(1,'CET');'Ghana'=(0,'GMT');'Greece'=(2,'EET');'Grenada'=(-4,'');'Guatemala'=(-6,'');'Guinea'=(0,'GMT');'Guinea-Bissau'=(0,'GMT');'Guyana'=(-4,'');'Haiti'=(-5,'');'Honduras'=(-6,'');'Hong Kong'=(8,'HKT');'Hungary'=(1,'CET');'Iceland'=(0,'GMT');'India'=(5.5,'IST');'Iran'=(3.5,'IRST');'Iraq'=(3,'');'Ireland'=(0,'WET');'Israel'=(2,'IST');'Italy'=(1,'CET');'Ivory Coast'=(0,'GMT');'Jamaica'=(-5,'');'Japan'=(9,'JST');'Jordan'=(2,'');'Kenya'=(3,'EAT');'North Korea'=(8.5,'Pyongyang');'South Korea'=(9,'KST');'Kuwait'=(3,'AST');'Kosovo'=(1,'');'Kyrgyzstan'=(6,'');'Laos'=(7,'');'Latvia'=(2,'EET');'Lebanon'=(2,'EET');'Lesotho'=(2,'');'Liberia'=(0,'GMT');'Libya'=(2,'EET');'Liechtenstein'=(1,'CET');'Lithuania'=(2,'EET');'Luxembourg'=(1,'CET');'Macau (China)'=(8,'Macau');'Macedonia'=(1,'CET');'Madagascar'=(3,'EAT');'Malawi'=(2,'CAT');'Malaysia'=(8,'Malaysia');'Maldives'=(5,'');'Mali'=(0,'GMT');'Malta'=(1,'CET');'Marshall Islands'=(12,'');'Mauritania'=(0,'GMT');'Mauritius'=(4,'Mauritius');'Moldova'=(2,'EET');'Monaco'=(1,'CET');'Montenegro'=(1,'CET');'Morocco'=(0,'WET');'Mozambique'=(2,'CAT');'Myanmar'=(6.5,'MST');'Namibia'=(1,'WAT');'Nauru'=(12,'');'Nepal'=(5.75,'Nepal');'Nicaragua'=(-6,'');'Niger'=(1,'WAT');'Nigeria'=(1,'WAT');'Norway'=(1,'CET');'Oman'=(4,'');'Pakistan'=(5,'PKT');'Palau'=(9,'');'Panama'=(-5,'');'Paraguay'=(-4,'');'Peru'=(-5,'PET');'Philippines'=(8,'PHT');'Poland'=(1,'CET');'Qatar'=(3,'AST');'Romania'=(2,'EET');'Rwanda'=(2,'CAT');'Saint Kitts and Nevis'=(-4,'');'Saint Lucia'=(-4,'');'Saint Vincent and the Grenadines'=(-4,'');'Samoa'=(13,'');'San Marino'=(1,'CET');'São Tomé and Príncipe'=(0,'GMT');'Saudi Arabia'=(3,'AST');'Senegal'=(0,'GMT');'Serbia'=(1,'CET');'Seychelles'=(4,'Seychelles');'Sierra Leone'=(0,'GMT');'Singapore'=(8,'SST');'Slovakia'=(1,'CET');'Slovenia'=(1,'CET');'Solomon Islands'=(11,'');'Somalia'=(3,'EAT');'South Sudan'=(3,'EAT');'Sri Lanka'=(5.5,'SLST');'Sudan'=(3,'EAT');'Suriname'=(-3,'');'Swaziland'=(2,'');'Sweden'=(1,'CET');'Switzerland'=(1,'CET');'Syria'=(2,'EET');'Taiwan'=(8,'');'Tajikistan'=(5,'');'Tanzania'=(3,'EAT');'Thailand'=(7,'THA');'Togo'=(0,'GMT');'Tonga'=(13,'');'Trinidad and Tobago'=(-4,'');'Tunisia'=(1,'CET');'Turkey'=(2,'EET');'Turkmenistan'=(5,'');'Tuvalu'=(12,'');'Uganda'=(3,'EAT');'Ukraine'=(2,'EET');'United Arab Emirates'=(4,'');'Uruguay'=(-3,'');'Uzbekistan'=(5,'Uzbekistan');'Vanuatu'=(11,'');'Vatican City'=(1,'CET');'Venezuela'=(-4,'');'Vietnam'=(7,'Indochina');'Yemen'=(3,'');'Zambia'=(2,'CAT');'Zimbabwe'=(2,'CAT')}

Takes input $a as a string in 24hr format, $b as an explicit array of strings, and the dictionary $x. Loops through $b, and for each creates a new Date based on $a, then calls .AddHours, indexing into the dictionary to determine how many, and uses the -format operator to pull out the correct information.

Example

PS C:\Tools\Scripts\golfing> .\world-time-conversion.ps1 '12:00' @('United States','Italy','Russia','Nepal') $x
7:00 AM EST
1:00 PM CET
3:00 PM Moscow
5:45 PM Nepal

AdmBorkBork

Posted 2016-10-13T17:42:00.110

Reputation: 41 581

4

Python 2, 154 153 151 148 147 138 134 131 123 121 116 bytes

-4 bytes thanks to @Shebang (missed a div // to / Py3 -> Py2 conversion; print time zone using unpacking; use map to replace two int casts)

def f(t,c,d):
 for x in c:a,b=map(int,t.split(':'));e,z=d[x];v=60*a+b+e;print`v/60%12`+':'+`v%60`,'pa'[v%1440<720],z

repl.it (I only bothered with a tiny subset of countries, as that's a chore and I'm lazy - but I included Nepal to have a non-standard offset in there & Tuvalu with it's +12:00 one).

Input is t the time string in 24 hr format; c the list of countries; and d a dictionary of country string to tuple of minutes offset and time zone name.

Prints a 12hr format plus time zone for each country split by new lines. Leading zeros are not printed AM/PM indicator is just a/p (e.g. 08:06 in Nepal prints as 8:6 a Nepal Time)

Jonathan Allan

Posted 2016-10-13T17:42:00.110

Reputation: 67 804

4Hehe, repl.it/Dvck... Quack xD – Beta Decay – 2016-10-13T18:45:33.697

So cloooooose :) – Jonathan Allan – 2016-10-13T18:46:09.620

@Shebang Oops - yeah - I always write in Python 3 and then move over! Thanks – Jonathan Allan – 2016-10-13T18:50:33.713

Also, you can save another three bytes here: %s'%(v/60%12,v%60,'PA'[v<720],z) to '%(v/60%12,v%60,'PA'[v<720]),z (remove the space in front of the last %s as well) – Kade – 2016-10-13T18:54:20.540

And another byte by doing t=map(int,t.split(':')) and removing the int casts in the next section. – Kade – 2016-10-13T18:56:46.443

I'd counted map as equal in length to int and disregarded doing that heh. – Jonathan Allan – 2016-10-13T19:05:42.687

Nepal is like that kid in middle school who needs special attention for everything. Flag isn't rectangular, time zone offset is all wrong... – Robert Fraser – 2016-10-13T19:35:17.047

@BetaDecay Duck points to an C++ hello world example instead... what a waste :( – Yytsi – 2016-10-13T19:59:23.917

@RobertFraser Highest mountain in the world etc. – Beta Decay – 2016-10-14T16:34:59.783

2

bash (126 bytes)

w(){ cd /u*/sh*/z*
join -o2.2 <(egrep "$2" i*) <(cut -sf1,3 zone.*)|while read z
do TZ=$z date -d"UTC$1" +"%z %r";done|uniq;}
w 12:00PM "United States|Italy|Russia"

uses GNU date and file:/usr/share/zoneinfo/{iso3166,zone}.tab

Roman Czyborra

Posted 2016-10-13T17:42:00.110

Reputation: 604