Return the clock emoji closest to the current time when run

20

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

Mohsen

Posted 2014-12-10T20:02:23.667

Reputation: 677

7This 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 [tag: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.) – undergroundmonorail – 2014-12-10T20:07:39.373

Windows Command prompt doesn't support emojis :( – stokastic – 2014-12-10T20:48:43.153

@undergroundmonorail Fair point. Updated – Mohsen – 2014-12-11T00:08:07.237

2I'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. – NinjaBearMonkey – 2014-12-11T03:44:48.113

What if the languages I am writing in does not print the emoji clock face at all, but instead, a box with the hex code in it ? – Optimizer – 2014-12-11T08:30:13.657

3How should xx:15 and xx:45 be rounded? – Peter Taylor – 2014-12-11T08:35:27.843

1@PeterTaylor As seen in the test cases, they should be rounded up (i.e. 1:45 => 2:00) – NinjaBearMonkey – 2014-12-11T12:53:41.103

@Optimizer As long as it's the same character as the emoji, how it looks doesn't matter. – NinjaBearMonkey – 2014-12-11T12:59:36.883

Who even has a programming environment that enables these to be printed and seen? – feersum – 2014-12-11T18:11:58.560

I have a font that supports N:00 but not N:30, lol. – feersum – 2014-12-11T18:13:46.630

@feersum I have that on both my phone and my PC. Seems to be common. – PurkkaKoodari – 2014-12-11T18:28:26.337

Is JavaScript alert or console.log allowed instead of stdout? – PurkkaKoodari – 2014-12-11T18:33:23.110

@Pietu1998 Yes, alert is allowed as it is javascript's closest alternative. – NinjaBearMonkey – 2014-12-11T20:04:48.487

Answers

9

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.

a=(Time.now.to_i/900-3)/2%24
puts""<<128336+a/2+a%2*12

Arithmetic:

  • 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:

n=Time.now
a=((n.to_i+n.gmtoff)/900-3)/2%24
puts""<<128336+a/2+a%2*12

Original Python 3 version:

import time
a=int((time.time()/900-3)/2%24)
print(chr(128336+a//2+a%2*12))

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.

colevk

Posted 2014-12-10T20:02:23.667

Reputation: 225

You can cut the parens from 12*(a%2) if you do it in the other order: a%2*12. – xnor – 2014-12-12T05:11:19.490

7

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

Digital Trauma

Posted 2014-12-10T20:02:23.667

Reputation: 64 644

5

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.

d=~~(Date.now()/18e5+.5)%24;d+=d<2?24:0;alert(String.fromCharCode(55357,56655+(d%2?23+d:d)/2));

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));

PurkkaKoodari

Posted 2014-12-10T20:02:23.667

Reputation: 16 699

Isn't (d%2?23+d:d) the same as d+d%2*23? – Neil – 2017-02-27T15:31:47.297

4

CJam, 50 bytes

"":i~et3=C%60*et4=+30d/mo2m24+24%_2%24*+2/+]:c

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:

3:H;45:M;"":i~HC%60*M+30d/mo2m48+48%_2%24*+2/+]:c

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.

Optimizer

Posted 2014-12-10T20:02:23.667

Reputation: 25 836

@user23013 Nice!. I think you should post that as a new answer as I did not do any golfing in that :) – Optimizer – 2014-12-19T14:08:42.183

3

CJam, 31 bytes

"")et5<60b675+30/24%2mdC*++

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*++

jimmy23013

Posted 2014-12-10T20:02:23.667

Reputation: 34 042