25
\$\begingroup\$

Task

Write a program or function that takes as input a time in UTC, and outputs the equivalent time in 5 different cities around the world. The cities displayed are up to the programmer, but the time must be different in each city.

To prevent golfers from using overly obscure city names, the city names used must appear on this list: World Cities*, which corresponds to the list of cities and times found here.

As usual, Standard loopholes are forbidden, including fetching data from an external source.

*The times provided in the pastebin use daylight time and assume that the time is 0:30 UTC.

Example

Given as input 12:30AM, your output might be:

Athens: 3:30AM
London: 1:30AM
New York: 8:30PM
Seoul: 9:30AM
Beijing: 8:30AM

Input

You may take input in any reasonable format. You can choose to take the time as 24-hour time, or 12-hour time with AM and PM. Specify the input format in your answer.

Output

Output may also be in either 12-hour or 24-hour time. The city-time pairs may be in any order and any reasonable format, so long as it is possible to associate each city with the corresponding time.

Daylight Savings Time can safely be ignored (ie. you can either use the offsets from the pastebin above, or you can assume that all times are in daylight time, or alternatively all in standard time)

Scoring

This is . Happy golfing!

\$\endgroup\$
14
  • \$\begingroup\$ What is the format of the pastebin file? For instance, what does "London, 01:30" mean? (If DST are ignored, London should be UTC+0.) \$\endgroup\$
    – Arnauld
    Commented May 15, 2020 at 18:06
  • \$\begingroup\$ @Arnauld Ah yes, the pastebin times use daylight time and are in reference to UTC 0:30. I'll specify that in the question \$\endgroup\$ Commented May 15, 2020 at 18:09
  • \$\begingroup\$ So, should we ignore the times in this file and just use it as a list of allowed cities? \$\endgroup\$
    – Arnauld
    Commented May 15, 2020 at 18:12
  • \$\begingroup\$ @Arnauld Yes, you can ignore the times in that file. They might be useful to calculate the offset from UTC for a given city though \$\endgroup\$ Commented May 15, 2020 at 18:14
  • 1
    \$\begingroup\$ kyiv lima rome oslo suva doha is what everyone will do, just making it simpler. \$\endgroup\$
    – Wezl
    Commented May 15, 2020 at 18:20

20 Answers 20

26
\$\begingroup\$

Python 2, 80 79 77 bytes

-1 byte thanks to Surculose Sputum.

24-hour time, input as hours, minutes.

h,m=input();i=h
exec"print'ALCADcaamucgimbroraaasoni'[i-h::5],i%24,m;i+=1;"*5

Try it online!

Example output for 0, 30 (12:30AM):

Accra 0 30
Lagos 1 30
Cairo 2 30
Amman 3 30
Dubai 4 30
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 79 bytes \$\endgroup\$ Commented May 15, 2020 at 18:51
  • \$\begingroup\$ @SurculoseSputum thanks a lot. For some reason I thought I need an additional UTC+5 city. \$\endgroup\$
    – ovs
    Commented May 16, 2020 at 9:51
  • 1
    \$\begingroup\$ 72 bytes by moving cities to input('...') prompt, albeit at the cost of readability. AccraLagosRomeKyivDubai can save another two. \$\endgroup\$
    – Duncan
    Commented May 16, 2020 at 15:32
  • \$\begingroup\$ @Duncan thanks for the suggestion, but I feel like this will be too similar to the other Python answer. \$\endgroup\$
    – ovs
    Commented May 16, 2020 at 16:46
16
\$\begingroup\$

Python 2, 71 65 bytes

Save 2 bytes using @dingle dooper's printing through input() trick!

t=input('Accra Lagos Rome Kyiv Dubai ')
exec"print t%24,;t+=1;"*5

Try it online!

Takes input as a complex number (e.g 01:30 is inputted as \$1+30i\$), and prints out the list of cities, then the corresponding times. Uses 24-hour time.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The spaces could be left out, saving 5 bytes, and the comma in print t%24, can also be left out for another 1 byte \$\endgroup\$
    – Duncan
    Commented May 16, 2020 at 15:41
  • \$\begingroup\$ @Duncan thanks! It seems like most answer does have separator between city names and times, so I'll keep the spaces for now. \$\endgroup\$ Commented May 17, 2020 at 1:55
7
\$\begingroup\$

Bash + Core utilities, 79 78 72 bytes

for c in Amman Dubai Kabul Dhaka Seoul;{ TZ=Asia/$c date +$c\ %R -d$1Z;}

Try it online!

Input is passed as an argument, and output is on stdout. Both are in 24-hour time.

\$\endgroup\$
6
\$\begingroup\$

Google Sheets, 82 77 80 bytes

Saved 5 bytes by finding a shorter list of city names that are all off by 1 hour.
Added 3 bytes thanks to Surculose Sputum pointing out that I was referencing the wrong list.

=ArrayFormula(Split("Lagos,Rome,Kyiv,Dubai,Kabul",",")&Mod(A1+Column(A:E),24)&B1

Sheets will automatically add the trailing parentheses. Hours are input in A1 and minutes in B1.

Split(~) gives us the list of city names.
A1+Column(A:E) iterates the input time by ones.
Mod(~,24) accounts for when the clock rolls to the next day.
Split(~)&Mod(~)&B1 tacks on the minutes.
ArrayFormula(~) makes the whole thing work on arrays instead of just the first value.

enter image description here

There aren't any delimiters between the city name and the time because pretty costs bytes. It's fairly easy to distinguish between the end of the name and the start of the time, though, since none of the city names have numbers in them.

\$\endgroup\$
2
  • \$\begingroup\$ @SurculoseSputum Could you be more specific? All 5 listed appear in OP's list of allowed cities. \$\endgroup\$ Commented May 18, 2020 at 16:01
  • 1
    \$\begingroup\$ @SurculoseSputum My apologies. I had gotten the two lists confused in my copies. I have corrected the city names now. Thank you. \$\endgroup\$ Commented May 18, 2020 at 17:40
4
\$\begingroup\$

Befunge-93, 238 bytes

&&\" arccA",,,,,,:.\:.\" nammA",,,,,,3+:345*+`!v v    <
>    v v          `+*543:+3,,,,,,"Dhaka "\.:\.:_#<83*-^
^-*38>#_         :.\:.\" luoeS",,,,,,3+:345*+`!v v    <
>    v v          `+*543:+3 ,,,,,"Suva " \.:\.:_#<83*-^
^-*38>#_:.\:.@

Try it online!

Reads the hour and minutes from the input. For each city, except the first, it adds 3 to the hour, then checks whether it exceeds 23. If it does, it makes a small loop to subtract 24. The rest is just printing strings, moving the PC around and swapping the hours/minute to/from the top of the stack.

\$\endgroup\$
2
  • \$\begingroup\$ You can take the input in reverse order, removing the need for the initial swap: tio.run/##S0pNK81LT/3/… \$\endgroup\$
    – ovs
    Commented May 16, 2020 at 16:49
  • \$\begingroup\$ And the subtract 24-part for Amman can be moved to the front: tio.run/##S0pNK81LT/3/…. The same may be possible for Suva, but this is a little bit trickier \$\endgroup\$
    – ovs
    Commented May 16, 2020 at 16:57
4
\$\begingroup\$

05AB1E, 25 bytes

Thanks to Kevin Cruijssen for shaving off 7 bytes.

”Accra‡¸«ÄçªâÈ”#ε¹N+24%²»

Try it online!

05AB1E, 32 bytes

Just another port of Surculose Sputum's answer. It produces quite an ugly output, due to joining by newlines.

”Accra Lagos«Ä KyivâÈ”#ε¹N+24%²»

Try it online!

Explanation (for both)

”...”           Compressed string with the cities
     #          Space-split
      ε         Map:
                    Implicit current city pushed
       ¹            First input:
        N+              Add by the iteration counter
          24%       Modulo by 24
             ²      Append the second input
              »     Join the whole stack by newlines.
\$\endgroup\$
2
  • \$\begingroup\$ -2 byte using a compressed string instead of dictionary string: .•Eān³ûζǝ₃δb¶«Ív¢L•™ \$\endgroup\$ Commented May 18, 2020 at 6:51
  • \$\begingroup\$ 25 bytes by using London and Baghdad instead of Lagos and Kyiv, which are part of the dictionary. \$\endgroup\$ Commented May 18, 2020 at 6:58
3
\$\begingroup\$

q, 43 40 bytes

{`Accra`Cork`Rome`Doha`Dubai!x+60*til 5}

No Tio unfortunately for q

q has built in time types that you can perform arithmetic with, so simple enough

Example:
q){`Accra`Cork`Rome`Doha`Dubai!x+60*til 5}00:30
Accra| 00:30
Cork | 01:30
Rome | 02:30
Doha | 03:30
Dubai| 04:30
\$\endgroup\$
3
  • 1
    \$\begingroup\$ If you can change, try using Doha (timeanddate.com/worldclock/qatar/doha) instead of Ammon. \$\endgroup\$ Commented May 18, 2020 at 14:13
  • 1
    \$\begingroup\$ Thanks Ismael! Switched from Lagos to Cork and Cairo to Rome aswell \$\endgroup\$
    – Thaufeki
    Commented May 19, 2020 at 2:20
  • 1
    \$\begingroup\$ in k4 this is down to 37 bytes {AccraCorkRomeDoha`Dubai!x+60*!5} \$\endgroup\$
    – Thaufeki
    Commented May 19, 2020 at 2:23
3
\$\begingroup\$

Vyxal, 26 bytes

5ɾ+₆+24%$S+`∴∷₇›†↵ƈ…±↑`5/Y

Try It Online!

Takes in <hour> <minute>, outputs an interleaved list of <time>|<city>.

My first Vyxal answer... it took me an embarrassingly long time to figure out how to properly deal with input.

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 92 bytes

i;f(h,m){for(i=0;i<5;)printf("%s %d:%d;","AccraLagosRome.Kyiv.Dubai"+6*i++,(h+i)%24,m);}

Try it online!

Port of Bash, didn't check map

C (gcc), 94 bytes

i;f(h,m){for(i=0;i<15;i+=3)printf("%s %d:%d;","AccraAmmanDhakaSeoulSuva"+2*i,(h+i)%24,m);}

Try it online!

Port of Python solution, didn't check map

\$\endgroup\$
2
\$\begingroup\$

Jelly, 23 bytes

+Ɱ5%24;€⁹“F=ẓŒMdƲEṢ$Ṛ.»

A full program accepting two arguments, hours then minutes, which prints in an ugly format. Uses Algiers Amsterdam Amman Dubai Islamabad since they are the alphabetically earliest allowed cities with DST offsets of 1-5 hours from UTC.

Try it online!

How?

+Ɱ5%24;€⁹“F=ẓŒMdƲEṢ$Ṛ.» - Main Link: hour, H; minute M
 Ɱ5                     - map across [1..5] with:
+                       -   addittion (to H)
   %24                  - modulo by 24
        ⁹               - chain's right argument, M
      ;€                - concatenate to each
         “F=ẓŒMdƲEṢ$Ṛ.» - compressed string "Algiers Amsterdam Amman Dubai Islamabad"
                        -   (this new niladic chain forces the previous result to be printed)
                        - implicit print
\$\endgroup\$
2
\$\begingroup\$

Bash, 118 \$\cdots\$ 76 75 bytes

Saved a byte thanks to Duncan!!!
Saved a whopping 18 39 41 42 bytes thanks to David G.!!!

for d in Accra Lagos Rome Kyiv Dubai;do date -d$1Z+$((i++))hour +$d%R;done&

Try it online!

Input passed as command line argument.
Outputs to stdout.
Uses 24-hour time format.

\$\endgroup\$
15
  • \$\begingroup\$ ("Accra""Lagos""Rome""Kyiv""Dubai") saves four bytes at the cost of readability. Printing without a space saves another byte. \$\endgroup\$
    – Duncan
    Commented May 16, 2020 at 15:55
  • \$\begingroup\$ @Duncan Unfortunately, the first one just merges all the cities into one big string. But, although it's not as pretty, the second abides by the rules (I think) so nice one - thanks! :-) \$\endgroup\$
    – Noodle9
    Commented May 17, 2020 at 9:28
  • \$\begingroup\$ c=(Accra Lagos Rome Kyiv Dubai) saves ten bytes. Shells only need quotes if something is special. So you can save five more by remove all the " and ' on the second line, and putting a \ before the space in the -d option. Save one more by removing the space between the -d and its parameter. (101) \$\endgroup\$
    – David G.
    Commented May 17, 2020 at 20:57
  • \$\begingroup\$ Changing %H:%M to %R can save 3 btyes. Might be specific to the version of date, but works on TIO. \$\endgroup\$
    – David G.
    Commented May 17, 2020 at 21:13
  • \$\begingroup\$ @DavidG. Yes, I do know that (use bash a lot) but totally missed it. Didn't know about%R though. Really nice golfs - thanks! :D \$\endgroup\$
    – Noodle9
    Commented May 17, 2020 at 22:25
1
\$\begingroup\$

Charcoal, 38 bytes

E⪪”↶+∧e-|GT⊘"↘w⌊⎇*↔JAN·”⁵⪫⟦ι﹪⁺Iθκ²⁴η⟧ 

Try it online! Link is to verbose version of code. Port of @SurculoseSputum's Python answer. Takes input as separate hours and minutes and uses the 24 hour clock. Explanation:

  ...               Compressed string of five cities
 ⪪   ⁵              Split into substrings of length 5
E                   Map over each substring
        ι           Current substring
           Iθ       Input hours as a number
          ⁺  κ      Plus current index
         ﹪    ²⁴    Modulo literal 24
                η   Input minutes
      ⪫⟦         ⟧  Joined with spaces
                    Implicitly print on separate lines
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 98 bytes

((h,m)=>['Accra','Lagos','Rome','Kyiv','Dubai'].map((e,i)=>console.log(e+' '+(h+i)%24+':'+m+';')))

Try it online!

Get the original idea from the C and so the Python guys.

\$\endgroup\$
1
  • \$\begingroup\$ You can save 2 bytes by changing (e,i)=>console.log(e+' '+(h+i)%24+':'+m+';') into (e,i)=>console.log`${e} ${(h+i)%24}:${m};` . You can save 1 more byte by changing ['Accra','Lagos','Rome','Kyiv','Dubai'] into 'Accra0Lagos0Rome0Kyiv0Dubai'.split(0). If you don't mind, you can save 1 byte by changing (h,m)=> into m=>h=>, requiring you to call it as (m=>h=>[...])(30)(0). \$\endgroup\$ Commented May 19, 2020 at 8:31
1
\$\begingroup\$

PHP 5.4+, 96 bytes

Yes, it's a fat boy :(
But was fun to write it!

foreach([Accra,Lagos,Cairo,Doha,Dubai]as$k=>$v)echo"\n$v ",date(H_i,strtotime("$argn {$k}hour"));

Run this with php -r (produces warnings).

The input format is flexible, as long as it can be interpreted by strtotime().
You can input hours in 12-hour or 24-hour formats, being displayed in 24-hour format.

The cities chosen were picked because they are NOT in DST at the time of this answer: https://www.timeanddate.com/worldclock/?sort=2

\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 81 bytes

(h,m)=>'Accra-Lagos-Rome-Kyiv-Dubai'.split`-`.map(e=>console.log(e,h++%24+':'+m))

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Wow - first time I seen this syntax of avoiding parentheses on the split function. Can you provide some reference? \$\endgroup\$ Commented May 18, 2020 at 15:33
  • \$\begingroup\$ I think this way you can call any function with one parameter. \$\endgroup\$ Commented May 18, 2020 at 15:42
  • \$\begingroup\$ @AnArrayOfFunctions The syntax used in the .split is allowed when you have a template string (the string with the accent character) as the only argument, which allows you to remove the parenthesis. However, using a number, in this case, removes the need for the template string. As I've suggested to another answer, using 'Accra0Lagos0Rome0Kyiv0Dubai'.split(0) has the same exact size, but is easier to read and understand what's going on. \$\endgroup\$ Commented May 19, 2020 at 8:47
1
\$\begingroup\$

JavaScript, 65 bytes

Input f(h)(m). Outputs an array of arrays: [[City, mm, ss], ...].

h=>m=>`Accra
Lagos
Rome
Kyiv
Dubai`.split`
`.map(c=>[c,h++%24,m])

Try it online!


JavaScript, 66 bytes

Prettier. Outputs a string with City,mm,ss separated by newlines.

h=>m=>`Accra
Lagos
Rome
Kyiv
Dubai`.replace(/.+/g,M=>[M,h++%24,m])

Try it online!


JavaScript, 64 bytes

Uglier. Outputs a string without delimiters: Citymm,ssCitymm,ss....

h=>m=>`Accra Lagos Rome Kyiv Dubai `.replace(/ /g,_=>[h++%24,m])

Try it online!

\$\endgroup\$
0
\$\begingroup\$

W, 33 bytes

Just realized that my language is such a bad language. It doesn't even support dumping the stack...

☻|▓■°u:"≡∟◘Σ≡☺╖ⁿ╩]┼ε╝╣╗à_∙▬Cô\K←û

Uncompressed:

"3@374Ry<+o;<r>YwI+eA78P`5a(c0[+24mc1[{M

Explanation

"3@374Ry<+o;<r>YwI+eA78P`                % Compressed string containing the cities
                         5             M % In the range 1..5:
                          a              %    The current counter,
                           (             %    Decremented,
                            c0[+         %    Is added to the hours
                                24m      %    And modulo'd by 24
                                   c1[   %    Push the minutes
                                      {  %    Pair the two items
\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 119 bytes

Input: hh:mm
Output: h:mm

t=>String.Concat("Accra,Lagos,Rome,Kyiv,Dubai".Split(',').Select((c,i)=>c+$":{(int.Parse(t[0..2])+i)%24}:{t[3..5]}\n"))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 548 bytes

Golfed vesrion, try it online!

use strict;use warnings;my %time_offsets = ('Athens' => 3,'Beijing' => 8,'London' => 1,'New York' => -4,'Seoul' => 9);my $input = '12:30 AM';my ($hour, $minute, $ampm) = $input =~ /^(\d{1,2}):(\d{2}) (AM|PM)$/;if ($ampm eq 'PM' && $hour != 12) {$hour += 12;} elsif ($ampm eq 'AM' && $hour == 12) {$hour = 0;}my %city_times;while (my ($city, $offset) = each %time_offsets) {my $local_hour = ($hour + $offset) % 24;$city_times{$city} = sprintf("%02d:%02d", $local_hour, $minute);}while (my ($city, $time) = each %city_times) {print "$city: $time\n";}

Ungolfed vesrion

use strict;
use warnings;

my %time_offsets = (
    'Athens' => 3,
    'Beijing' => 8,
    'London' => 1,
    'New York' => -4,
    'Seoul' => 9,
);

my $input = '12:30 AM';  # Replace with input from user

# Extract hour, minute, and AM/PM from input
my ($hour, $minute, $ampm) = $input =~ /^(\d{1,2}):(\d{2}) (AM|PM)$/;

# Convert 12-hour time to 24-hour time
if ($ampm eq 'PM' && $hour != 12) {
    $hour += 12;
} elsif ($ampm eq 'AM' && $hour == 12) {
    $hour = 0;
}

# Calculate time in each city
my %city_times;
while (my ($city, $offset) = each %time_offsets) {
    my $local_hour = ($hour + $offset) % 24;
    $city_times{$city} = sprintf("%02d:%02d", $local_hour, $minute);
}

# Output city times
while (my ($city, $time) = each %city_times) {
    print "$city: $time\n";
}

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 123 bytes

Input can be in 12 or 24 hour format i.e. hh:mm OR hh:mm AM/PM

param($i)0..4|%{"{0}: {1}"-f("Accra","Lagos","Cairo","Amman","Dubai")[$_],([datetime]$i).AddHours($_).ToString("hh:mm tt")}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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