13
\$\begingroup\$

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.

\$\endgroup\$
16
  • 2
    \$\begingroup\$ Closely related. \$\endgroup\$ Commented Dec 27, 2016 at 17:21
  • 3
    \$\begingroup\$ I 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. \$\endgroup\$ Commented Dec 27, 2016 at 18:02
  • 2
    \$\begingroup\$ Is a language specific Date/DateTime struct allowed as input? \$\endgroup\$
    – devRicher
    Commented Dec 27, 2016 at 18:35
  • 1
    \$\begingroup\$ Why do we have to support military time? It doesn't add much to the challenge. \$\endgroup\$
    – Riker
    Commented Dec 27, 2016 at 19:27
  • 2
    \$\begingroup\$ @carcusocomputing You specify that the code must handle 24 hour time, does the code have to handle AM or PM tags? \$\endgroup\$ Commented Dec 27, 2016 at 20:39

5 Answers 5

3
\$\begingroup\$

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

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

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>

\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 5, 2017 at 19:42
  • \$\begingroup\$ @carusocomputing of course you should select me. Me. ME ME ME! ... \$\endgroup\$
    – edc65
    Commented Jan 5, 2017 at 19:58
  • \$\begingroup\$ @carusocomputing or else you could select the answer that has less votes (with no reason) just to regain a balance \$\endgroup\$
    – edc65
    Commented Jan 5, 2017 at 20:00
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 5, 2017 at 20:35
  • \$\begingroup\$ Shows It is currently 🕛 with 60 minutes until 🕐. I think it should It is currently 🕛. \$\endgroup\$ Commented Jan 27, 2017 at 11:07
2
\$\begingroup\$

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>

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

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!

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

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".");
}
\$\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.