I can have a large if/else condition for each 30 minutes but I'm looking for more math and Unicode based solution.

Here are clock emojis: πŸ•πŸ•‘πŸ•’πŸ•“πŸ•”πŸ••πŸ•–πŸ•—πŸ•˜πŸ•™πŸ•šπŸ•›πŸ•œπŸ•πŸ•žπŸ•ŸπŸ• πŸ•‘πŸ•’πŸ•£πŸ•€πŸ•₯πŸ•¦πŸ•§. If you lack proper rendering support, you can see them below (they're not in the same order and they may look different to what you see) or at the Unicode chart, page 4. They correspond to Unicode codepoints U+1F550 (CLOCK FACE ONE OCLOCK) through U+1F567 (CLOCK FACE TWELVE-THIRTY).

Your challenge is to write a program that outputs to STDOUT the closest clock face emoji to the current system time (AM and PM should be treated the same). For example if the time t is [5:15 < t < 5:45), you would display the 5:30 emoji πŸ• .

This is code-golf, so shortest code in bytes wins. You may want to include a way to easily specify the time to test your code.

Edge test cases

Time   Output  Written
11:48  πŸ•›       12:00
3:15   πŸ•ž       3:30
9:45   πŸ•™       10:00

enter image description here

  • 7
    \$\begingroup\$ This question isn't currently on-topic here but it's probably an easy fix. You need an objective win condition, but if you tag the question code-golf the winner will be whoever writes the shortest program. It's also a bit under specified, but you could say "a script that outputs the clock emoji closest to the current time when run" and that should be enough. Also, while not technically required, I'd recommend that you allow submissions in other languages. (Someone will probably do one in Bash anyway.) \$\endgroup\$ Dec 10, 2014 at 20:07
  • \$\begingroup\$ Windows Command prompt doesn't support emojis :( \$\endgroup\$
    – stokastic
    Dec 10, 2014 at 20:48
  • 2
    \$\begingroup\$ I've edited your post to attempt to have it meet our standards by clarifying the rules and making it code golf. Feel free to edit it if I misinterpreted anything. \$\endgroup\$ Dec 11, 2014 at 3:44
  • 3
    \$\begingroup\$ How should xx:15 and xx:45 be rounded? \$\endgroup\$ Dec 11, 2014 at 8:35
  • 1
    \$\begingroup\$ @PeterTaylor As seen in the test cases, they should be rounded up (i.e. 1:45 => 2:00) \$\endgroup\$ Dec 11, 2014 at 12:53

5 Answers 5


Ruby β€” 61 54 bytes

Python 3 β€” 79 77 75 74 bytes

edit: Turns out this is much shorter in Ruby, because Time doesn't need to be imported and integer division is the default.



  • Time.now.to_i: Seconds since epoch.
  • /900: Combine time into 15-minute segments.
  • -3: -4 because characters start at 1 o'clock, +1 so that rounding is done to nearest half hour, rather than downwards.
  • /2: Combine into half-hour segments.
  • %24: Get current half-hour segment out of 12 hours.
  • a/2 + a%2*12: Account for the fact that whole-hour characters come in a block before half-hour characters.

Local time version, 69 bytes:


Original Python 3 version:

import time

Prints the current time UTC. Python 2's chr only accepts 0-255, and unichr only 0-65535. In Python 3, / is always floating-point division and // is always integer division.

  • \$\begingroup\$ You can cut the parens from 12*(a%2) if you do it in the other order: a%2*12. \$\endgroup\$
    – xnor
    Dec 12, 2014 at 5:11

Bash + Coreutils, 60 bytes

date +%I\ 60*%M+45-30/24%%2+2~C*+C8335+0PP|dc|iconv -f ucs-4

Emojis render fine on the OSX terminal, but for Linux (Ubuntu 14.04) you'll have to sudo apt-get install ttf-ancient-fonts

For testing different times, insert -d $time into the date command. e.g for testing 12:59:

date -d 12:59 +%I\ 60*%M+45-30/24%%2+2~C*+C8335+0PP|dc|iconv -f ucs-4

JavaScript – 137 117 107 102 95

This is going to get easily beaten, but I'll just try my best here.

EDIT 1: Now prints in UTC like the Python answer to save 20 bytes.

EDIT 2: Changed new Date().getTime() to Date.now() to save 10 bytes.

EDIT 3: Changed Math.round(x) to ~~(x+.5) to save 5 bytes.

EDIT 4: Removed unnecessary ( and &1023) left from some old development version where I did a more universal UTF-16 encoding to save 7 bytes.


JavaScript does allow for emojis, but in UTF-16 which encodes them as two characters. Luckily, we can simply define them as two characters; the first character is also constant (0xD83D), while the other changes.

Alternative, prints in local time, 137 132 125:

a=new Date();d=~~(a.getHours()%12*2+a.getMinutes()/30+.5);d+=d<2?24:0;alert(String.fromCharCode(55357,56655+(d%2?23+d:d)/2));

  • \$\begingroup\$ Isn't (d%2?23+d:d) the same as d+d%2*23? \$\endgroup\$
    – Neil
    Feb 27, 2017 at 15:31

CJam, 50 bytes


The algorithm is simple, round the time to nearest 30 minutes, count how many 30 minutes have passed since 12 AM/PM, if the number is odd, then add 24 to it, floor divide by 2 and increment the initial unicode by that many code points to get the right clock face.

Also, make sure that you start with 1 AM/PM instead of 12 so take an offset there.

For testing purposes, use the following code:


Where the 3:H; part is the current hour and the 45:M; part is the current minute of the hour. Simply replace 3 and 45 with the desired value.

Try it online here

Not all unicode characters might print correctly on the online version depending upon your browser and OS and installed fonts. But for me, all print fine on Firefox with Windows 7.

  • \$\begingroup\$ @user23013 Nice!. I think you should post that as a new answer as I did not do any golfing in that :) \$\endgroup\$
    – Optimizer
    Dec 19, 2014 at 14:08

CJam, 31 bytes


You can change the time in the following program to test it:

"πŸ•")[2014 12 19 22 14 1 901 5 -28800000]5<60b675+30/24%2mdC*++

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.