25
\$\begingroup\$

I'd like you to build me a clock that displays time in this format:

18 ----------
19 --------------------------------------------------

This displays '18:10'. The current hour and the next hour are shown at the front of the line, followed by a space and a number of dashes: on the first line the number of minutes that have passed in this hour, and the second line shows how many more minutes to go in this hour.

To clarify

  • The clock should display the system's time. If fetching the time from another source is more convenient, that's fine too. It may not be supplied as input.
  • At 18:00, the top line is just 18 (Trailing spaces allowed but not required)
  • At 18:59, the bottom line is 19 -
  • The hours < 10 are either pre-padded with a 0 (01 -----) or right-aligned ( 1 -----). A left-aligned single digit is not allowed, not even if the dashes start at the right place (1 ----- is invalid).
  • The clock should display the hours in a 24h-format.
  • Although it's called the 24h format, there is not actually a 24 on it. During the 23rd hour, the second line starts with 00 or 0.
  • The display needs to be updated at least once a minute, but that doesn't have to happen at exactly 00 seconds. You may update more frequently / continuously if that is more convenient, but the result must of course still be legible - not one smear all over the screen.

Input

None.

Output

  • As described above. Trailing spaces to pad the clock to 60-ish positions is allowed on both lines, a trailing newline is also allowed.
  • The screen needs to be cleared when displaying the next minute: either with a clear-screen command or by adding no less than 30 newlines.

Additional rules

\$\endgroup\$
  • \$\begingroup\$ may we have two spaces between the number and the dashes? \$\endgroup\$ – Adám Jan 17 '17 at 16:48
  • 6
    \$\begingroup\$ "updated once a minute" -- Can it be updated more often? \$\endgroup\$ – smls Jan 17 '17 at 16:49
  • 1
    \$\begingroup\$ @smls Yes, you may update as often as you like. I'll change the specs to 'at least once a minute'. \$\endgroup\$ – steenbergh Jan 17 '17 at 17:52
  • 1
    \$\begingroup\$ @KritixiLithos That would break alignment with two-digit hours (9, 10 or 23, 00), so no, not allowed. \$\endgroup\$ – steenbergh Jan 17 '17 at 19:04
  • 1
    \$\begingroup\$ After 23, is the next hour 24 or 0? \$\endgroup\$ – Kritixi Lithos Jan 18 '17 at 15:43

35 Answers 35

1
\$\begingroup\$

Python3.5 104 127 125 bytes.

from time import* while[sleep(9)]:h,m=localtime()[3:5];print("{:02d} {}".format(h,m*'-'),end='\n'*30)

Riffing off Gurupad Mamadapur's answer above.

edit: xnor is right. This isn't a valid answer. - This misses the second line completely.

edit2: Instead, to at least have an answer that works:

from time import* while[sleep(9)]:h,m=localtime()[3:5];print("{:02d} {}\n{:02d} {}".format(h,m*'-',h+1,(60-m)*'-'),end='\n'*30)

edit3: Saved two bytes by moving the \n*30 to the format instead of the end='\n'*30

from time import*
while[sleep(9)]:h,m=localtime()[3:5];print("{}{:02d} {}\n{:02d} {}".format('\n'*30,h,m*'-',h+1,(60-m)*'-'))
\$\endgroup\$
  • \$\begingroup\$ I don't think this does the right thing. The next hour needs to be printed with a dash for each remaining minute to it. \$\endgroup\$ – xnor Jan 18 '17 at 1:45
1
\$\begingroup\$

C#, 192 185 176 172 169 bytes

Saved 3 bytes thanks to @nurchi

using System;()=>{for(;;){Console.Clear();var t=DateTime.Now;int h=t.Hour,m=t.Minute;Console.Write($"{h:00} {new string('-',m)}\n{++h%24:00} {new string('-',60-m)}");}};

Uses C# 6 features which I don't have access to so cannot test it properly yet.

Full version showing how it works and is called including a Thread.Sleep so the input can actually be seen. Note that this version shows the use of Console.Writes overload to use String.Format and doesn't use string interpolation like the golfed version above.

using System;

class P
{
    static void Main()
    {
        Action a = () =>
        {
            for (;;)
            {
                Console.Clear();

                var t = DateTime.Now;
                int h = t.Hour,m=t.Minute;

                Console.Write("{0:00} {1}\n{2:00} {3}", h, new string('-', m), ++h % 24, new string('-', 60 - m));

                System.Threading.Thread.Sleep(500);
            }
        };

        a();
    }
}
\$\endgroup\$
  • \$\begingroup\$ Your line does not include the Sleep (which is included in the full code), so the output will flicker. \$\endgroup\$ – nurchi Jan 26 '17 at 0:13
  • \$\begingroup\$ @nurchi the sleep is in the full code to view the output easier, as far as I am aware a smooth clock is not needed \$\endgroup\$ – TheLethalCoder Jan 26 '17 at 0:52
  • \$\begingroup\$ TheLethalCoder, you are right if the output doesn't flicker too much (it has to be legible). And I just tested it, it barely flickers, but this may depend on the system. My answer below is 176 chars with Sleep (but I rely on the using System.Threading;. I do like your code though. To further shorten it, replace your while(1>0){...} with for(;;){...} \$\endgroup\$ – nurchi Jan 26 '17 at 1:04
1
\$\begingroup\$

Guile, 146, 143 bytes

(while(sleep 9)(let*((t(gmtime(current-time)))(h(tm:hour t))(m(tm:min t)))(format #t "~d ~v,,'-t~%~d ~v,,'-t~%" h m(modulo(1+ h)24)(- 60 m))))

Shaved off three bytes thanks to ceilingcat.

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

Bash + Coreutils, 114 bytes

while sleep 9; do date +'%k %_M'|{ read h m;printf %02d:%${m}s\n$((h+1)):%$((60-m))s\n" $h|tr " :"  "- "; }; done
\$\endgroup\$
0
\$\begingroup\$

tcl, 164

while 1 {scan [clock f [clock se] -f %T] %d:%d h m
puts "[format %2s $h] [string repe - $m]\n[format %2s [expr ($h+1)%24]] [string repe - [expr 60-$m]]"
after 9999}

demo

\$\endgroup\$
  • \$\begingroup\$ This code can be golfed quite a bit. [This](goo.gl/zU9n2W ) is only 165 bytes. Also, it doesn't clear the screen (or add 30 newlines) between frames. \$\endgroup\$ – steenbergh Jan 24 '17 at 6:33
  • \$\begingroup\$ @steenbergh When I execute the code of your link I get errors. \$\endgroup\$ – sergiol Feb 4 '17 at 16:06
  • \$\begingroup\$ Oh, now I see what happened. That environment just kept running your original code instead of running my modifications. Sorry about that... \$\endgroup\$ – steenbergh Feb 4 '17 at 18:50

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.