7
\$\begingroup\$

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
\$\endgroup\$
  • 3
    \$\begingroup\$ very close to this question, the difference is the "convert country name to timezone" part \$\endgroup\$ – Rod Oct 13 '16 at 17:51
  • \$\begingroup\$ @Rod Yeah, I mentioned I found that in my sandbox post, but people didn't comment on it. \$\endgroup\$ – Yodle Oct 13 '16 at 17:56
  • 1
    \$\begingroup\$ This "list of country -> timezone" - do you have a compiled version? The Wikipedia article has entries like "French Polynesia" in the section for France. \$\endgroup\$ – Jonathan Allan Oct 13 '16 at 17:59
  • \$\begingroup\$ @JonathanAllan I don't, I was going to leave it up to people as to how they wanted to set it up/format it. \$\endgroup\$ – Yodle Oct 13 '16 at 18:01
  • 1
    \$\begingroup\$ @carusocomputing See the edit, and thanks :) \$\endgroup\$ – Yodle Oct 13 '16 at 19:20
5
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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)

\$\endgroup\$
  • 4
    \$\begingroup\$ Hehe, repl.it/Dvck... Quack xD \$\endgroup\$ – Beta Decay Oct 13 '16 at 18:45
  • \$\begingroup\$ So cloooooose :) \$\endgroup\$ – Jonathan Allan Oct 13 '16 at 18:46
  • \$\begingroup\$ @Shebang Oops - yeah - I always write in Python 3 and then move over! Thanks \$\endgroup\$ – Jonathan Allan Oct 13 '16 at 18:50
  • \$\begingroup\$ 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) \$\endgroup\$ – Kade Oct 13 '16 at 18:54
  • \$\begingroup\$ And another byte by doing t=map(int,t.split(':')) and removing the int casts in the next section. \$\endgroup\$ – Kade Oct 13 '16 at 18:56
2
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.