What time is it in unicode?

12

3

The challenge is simplistic, given an input time as a string in any one of the following formats:

hh, hh:mm or hh:mm:ss with 0 ≤ hh ≤ 23, 0 ≤ mm ≤ 59 and 0 ≤ ss ≤ 59.

Output what time it currently is using the following symbols:

AA  LABEL FOR CHARACTER     CODE POINT   HEXADECIMAL
==  ====================    ==========   ===========
  Clock Face 01 Oclock    128336       0x1F550  
  Clock Face 02 Oclock    128337       0x1F551  
  Clock Face 03 Oclock    128338       0x1F552  
  Clock Face 04 Oclock    128339       0x1F553  
  Clock Face 05 Oclock    128340       0x1F554  
  Clock Face 06 Oclock    128341       0x1F555  
  Clock Face 07 Oclock    128342       0x1F556  
  Clock Face 08 Oclock    128343       0x1F557  
  Clock Face 09 Oclock    128344       0x1F558  
  Clock Face 10 Oclock    128345       0x1F559  
  Clock Face 11 Oclock    128346       0x1F55A  
  Clock Face 12 Oclock    128347       0x1F55B  

In the following format:

It is currently {Clock Face 1} with {mm} minutes and {ss} seconds until {Clock Face 2}.

Examples (Including all fringe cases):

Case with only hours...

f("12") = "It is currently ."

Case with hours and minutes...

f("12:30") = "It is currently  with 30 minutes until ."

Case with only hours, but has minutes included as 00...

f("12:00") = "It is currently ."

Case with hours, minutes and seconds...

f("12:30:30") = "It is currently  with 29 minutes and 30 seconds until ."

Case with hours and minutes, but has seconds included as 00...

f("12:30:00") = "It is currently  with 30 minutes until ."

Case with hours and minutes, with less than a minute until the next hour...

f("12:59:59") = "It is currently  with 1 seconds until ."

You do not have to change from plural to singular.


Case with hours and minutes, with 1 minute to the next hour...

f("12:59") = "It is currently  with 1 minutes until ."

You do not have to change from plural to singular.


Case using military time (yes you must handle this)...

f("23:30:30") = "It is currently  with 29 minutes and 30 seconds until ."

Invalid cases...

f("PPCG") = This cannot occur, you are guaranteed a valid format by the definition of the problem.
f(66:66:66) = This cannot occur, you are guaranteed valid numbers by the definition of the problem.
f(24:60:60) = This cannot occur, you are guaranteed valid numbers by the definition of the problem.

You do not have to conform to any style of output for invalid cases, errors are fine.


Overall the challenge is rather simplistic, but seemed to be dynamic enough to be fun in my opinion. The shortest code here is the winner as there isn't much variable aspect to the code other than length.

Magic Octopus Urn

Posted 2016-12-27T17:18:14.993

Reputation: 19 422

1Closely related. – Martin Ender – 2016-12-27T17:21:34.690

Should this be a whole program or is a function/lambda enough? – devRicher – 2016-12-27T17:50:12.407

2I think that instead of 0 < hh < 24, 0 < mm < 60 and 0 < ss < 60, you meant 0 ≤ hh ≤ 23, 0 ≤ mm ≤ 59 and 0 ≤ ss ≤ 59. – Erik the Outgolfer – 2016-12-27T18:02:08.177

Assume UTF-8, do the clock emojis count full byte count, or none, or one byte? – devRicher – 2016-12-27T18:16:32.813

@devRicher http://meta.codegolf.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet?answertab=votes#tab-top also, use byte counting utilities like http://bytesizematters.com/ to determine the answer to the second question. The clock, if used directly in your code, counts for 4 bytes.

– Magic Octopus Urn – 2016-12-27T18:19:28.797

2Is a language specific Date/DateTime struct allowed as input? – devRicher – 2016-12-27T18:35:03.137

@devRicher No, the input is a string, exactly as described. – Magic Octopus Urn – 2016-12-27T18:43:26.970

Why do we have to support military time? It doesn't add much to the challenge. – Rɪᴋᴇʀ – 2016-12-27T19:27:43.513

@EasterlyIrk hh%12 isn't hard to support either. – Magic Octopus Urn – 2016-12-27T19:32:13.763

@JamesHolderness updated with answers, if the quantity of the time is 0, you drop it. You do not have to change plural versions to singular versions. – Magic Octopus Urn – 2016-12-27T20:11:03.730

Case 12:00:01 should be with 59 minutes and 59 seconds until ... Correct? – edc65 – 2016-12-27T20:21:52.110

2@carcusocomputing You specify that the code must handle 24 hour time, does the code have to handle AM or PM tags? – Taylor Scott – 2016-12-27T20:39:56.157

If the language only supports UCS-2, would it be OK to output the 2 characters that would produce the clock symbol if they were UTF-16? – 12Me21 – 2017-01-27T20:33:30.487

@12Me21 if it's a limitation of the language you may, but that limitation must be explained in depth. – Magic Octopus Urn – 2017-01-27T20:36:44.133

May we, if need be, take the input as a string in brackets ([HH:MM:SS]) rather than in double quotes ("HH:MM:SS")? – R. Kap – 2017-01-29T10:07:03.250

@R.Kap yeah sure. – Magic Octopus Urn – 2017-01-30T13:43:09.767

Answers

3

JavaScript (ES6), 201

t=>(T=h=>String.fromCodePoint(128336+h%12),[h,m,s]=t.match(/\d+/g),'It is currently '+T(h-=~10)+((m-=-!!-s)?` with ${60-m?60-m+' minutes'+(-s?' and ':''):''}${-s?60-s+' seconds':''} until `+T(-~h):''))

Less golfed

t=>(
  T=h=>String.fromCodePoint(128336+h%12),
  [h,m,s]=t.match(/\d+/g),
  'It is currently '+T(h-=~10)
   +( 
      // if seconds is not 0, increment m else just convert to number
      // have to use '- -' to force conversion to number 
     (m -= - !!-s) 
-s?++m:m)
      ? ` with ${60-m ? 60-m+' minutes'+(-s?' and ':''):''}${-s?60-s+' seconds':''} until `+T(-~h)
      : ''
    )
)

Test

F=
t=>(T=h=>String.fromCodePoint(128336+h%12),[h,m,s]=t.match(/\d+/g),'It is currently '+T(h-=~10)+((m-=-!!-s)?` with ${60-m?60-m+' minutes'+(-s?' and ':''):''}${-s?60-s+' seconds':''} until `+T(-~h):'')
)


var tid=0

function getTime(t)
{
  var a=t.match(/\d+/g)
  if (a) {
    var [h,m,s]=a
    h|=0, s|=0, m|=0
    if(h>=0 & h<24 & m>=0 & m<60 & s>=0 & s<60) 
      return [h,m,s]
  }
  return null
}      

function update()
{
  clearTimeout(tid)
  var t=I.value, a=getTime(t)
  if (a) {
    O.textContent = F(t)
    tid = setTimeout(again,5000)
  }
  else {
    O.textContent = 'invalid ' + t
  }
}
function again()
{      
  var t=I.value, a=getTime(t)
  if (a) {
    var [h,m,s]=a
    ++s>59?(s=0,++m>59?(m=0,++h):0):0
    h%=24
    s<10?s='0'+s:0
    m<10?m='0'+m:0
    t = h+(-m-s?':'+m+(-s?':'+s:''):'')
    I.value = t
    O.textContent=F(t)
    tid = setTimeout(again, 1000)
  }
}

update()
#O { font-size:16px }
Time <input id=I value='12' oninput='update()' onkeydown='update()'>
(modify current time as you wish - but use valid values)
<pre id=O></pre>

edc65

Posted 2016-12-27T17:18:14.993

Reputation: 31 086

I don't know how to select a winner for this, you both posted within 2 minutes of each other with 201 bytes in the same language. – Magic Octopus Urn – 2017-01-05T19:42:21.397

@carusocomputing of course you should select me. Me. ME ME ME! ... – edc65 – 2017-01-05T19:58:49.793

@carusocomputing or else you could select the answer that has less votes (with no reason) just to regain a balance – edc65 – 2017-01-05T20:00:09.857

Or you could replace the word String in your code with "" an empty string to save yourself 4 bytes :). Ah, crap, he can too though. – Magic Octopus Urn – 2017-01-05T20:35:11.923

Shows It is currently with 60 minutes until. I think it should It is currently . – Roman Gräf – 2017-01-27T11:07:48.357

@RomanGräf fixed, thanks for noticing – edc65 – 2017-01-27T11:33:21.800

3

Befunge, 256 250 bytes

>&~85+`v
8~-&"<"_00v`+5
0v%\-&"<<"_
v>:00p!!-"<"%10p65++:66+%0" yltnerruc si tI">:#,_$"Hu 5x"2*,3*,+,2*+,10g00g+
_".",@,".",+*2,+,*3,*2"x5 uH"%+66+1$_,#!>#:<v0
" litnu  htiw ",,,,,,10g:>#v_$"sdnoces"00g.^>
_>,,,,,,,>" dna ">,,,,,00^ >."setunim"00g!#^

Try it online!

The results are encoded as utf-8, since that works best with TIO, but if you're testing locally, you may need to adjust your system's default code page to see the clock faces correctly. Otherwise just redirect the output to a file and open that in a utf-8 compatible editor.

Explanation

The first three lines read the hours minutes and seconds from stdin, checking for EOF or a linefeed after each value, and substituting zeros for the componenents that are missing from the input. On line four, we adjust the minute value if the seconds are non-zero, convert the hour value into the range 0 to 11 (to match the appropriate unicode character for each hour), and write out the initial part of the output, including the the first clock face.

It's at this point that we need to follow different branches depending on what components are non-zero. The first test, at the start of line five, just checks if both minutes and seconds are zero. If so, we write out a final . and exit. Otherwise lines six and seven deal with the remaining cases - writing out the appropriate text and values, before the paths all combine again on line five to write out the final clock face (executing right to left).

James Holderness

Posted 2016-12-27T17:18:14.993

Reputation: 8 298

2

JavaScript (ES6), 201 bytes

(i,[h,m,s]=i.split`:`,c=n=>String.fromCodePoint(128336+(n+11)%12))=>`It is currently ${c(+h)}${m|s?` with ${(m=59+!+s-m)?m+` minutes`:``}${+s&&m?` and `:``}${+s?60-s+` seconds`:``} until `+c(-~h):``}.`

226 bytes if you take plurals into account:

f=
(i,[h,m,s]=i.split`:`,c=n=>String.fromCodePoint(128336+(n+11)%12))=>`It is currently ${c(+h)}${m|s?` with ${(m=59+!+s-m)?m+` minute`+(m-1?`s`:``):``}${+s&&m?` and `:``}${+s?60-s+` second`+(59-s?`s`:``):``} until `+c(-~h):``}.`
<input oninput=o.textContent=f(this.value)><div id=o>

Neil

Posted 2016-12-27T17:18:14.993

Reputation: 95 035

1

PowerShell, 250 243 bytes

$h,$m,$s=$args-split':'
$f={[char]::ConvertFromUtf32(128336+(11+$args[0])%12)}
$u=(60-$s)%60
$v=(59-$m+!$u)%60
"It is currently $(&$f $h;"with $(("$v minutes"|?{$v}),("$u seconds"|?{$u})-match'.'-join' and ') until $(&$f (1+$h))"|?{$u-or$v})."

Try it online!

briantist

Posted 2016-12-27T17:18:14.993

Reputation: 3 110

1

C, 241 bytes

Writes UTF-8 to stdout.

#define p printf(
c(v,h){p"%s \xf0\x9f\x95%c",v,144+h%12);}f(t){int h=0,m=0,s=0;sscanf(t,"%d:%d:%d",&h,&m,&s);m=(59-m+!s)%60;c("It is currently",h-1);m&&p" with %d minutes",m);s&&p" %s %d seconds",m?"and":"with",60-s);m|s&&c(" to",h);p".");}

Try it online!

Code with whitespace:

#define p printf(

c(v, h) {
    p"%s \xf0\x9f\x95%c", v, 144 + h % 12);
}

f(t) {
    int h = 0, m = 0, s = 0;

    sscanf(t, "%d:%d:%d", &h, &m, &s);
    c("It is currently", h - 1);
    m = (59 - m + !s) % 60;
    m && p" with %d minutes", m);
    s && p" %s %d seconds", m ? "and" : "with", 60 - s);
    m | s && c(" to", h);
    p".");
}

nwellnhof

Posted 2016-12-27T17:18:14.993

Reputation: 10 037