20
\$\begingroup\$

Display a Digital Clock

(I see there are many clock challenges, I have tried to read them all, I'm pretty sure this is unique)

Write some code that continuously updates a digital clock displayed in the format h:m:s where h, m, and s can occupy 1 or 2 characters each. The restrictions in simple terms as @BlueEyedBeast put it, I basically want it to replace the time shown.

  • Newlines are not allowed
  • Trailing spaces are allowed
  • Other trailing characters are not allowed
  • No seconds should be missed (57 -> 59 is not allowed)
  • Numbers beginning with "0" are allowed, as long as they don't occupy more than 2 characters
  • The code must not accept any input
  • If your language can't get the current time without input, you may use for input up to 8 bytes in a standardly allowed way
  • Output must be to stdout
  • This is codegolf so the shortest answer wins!

Example

I'm working on a language named *><> (starfish) because programs like this aren't possible in ><> (just wait for file i/o) . Here's a working digital clock program in it (ungolfed). This program is written in *><>:

s":"m":"hnonon"   "ooo88888888888ooooooooooo1S

Note: Everything is identical to ><> in this except, s = second, m = minute, h = hour, S = sleep(100ms*x)

This outputs:

14:44:31

Updating every 100ms until terminated.

Disallowed Examples

The following are not allowed:

1:

14:44:3114:44:32

2:

14:44:31 14:44:32

3:

14:44:31
14:44:32

The time must remain on the first line it was outputted with no visible trailing characters. Clearing the terminal though, would be allowed as that still wouldn't have any trailing characters.

\$\endgroup\$
  • 1
    \$\begingroup\$ do we have to wait 100ms or can we just update constantly forever? \$\endgroup\$ – Blue Dec 5 '16 at 21:42
  • 1
    \$\begingroup\$ you don't have to wait, the wait is just what the example does. \$\endgroup\$ – redstarcoder Dec 5 '16 at 21:43
  • 2
    \$\begingroup\$ The challenge requirements seem too strict to me. \$\endgroup\$ – mbomb007 Dec 5 '16 at 21:50
  • 1
    \$\begingroup\$ @mbomb007 I removed "The program must be able to exit on user input that isn't a signal/interrupt" as the current answers didn't seem to follow it anyways. \$\endgroup\$ – redstarcoder Dec 5 '16 at 22:05
  • 1
    \$\begingroup\$ Can the program rely on a specific local setting of the OS? \$\endgroup\$ – raznagul Dec 6 '16 at 17:01

35 Answers 35

0
\$\begingroup\$

*><> (Starfish), 22 bytes

"   ::"s@hm@donononooo

Try it here!

Ungolfed

>s":"m":"hnonon"   "ooo1S\
\                 ;?+1iod/

Explanation

d                      push \r to the stack
 "   ::"               push "   ::" to the stack
        s              push seconds to the stack
         @             move seconds back 2
          hm           push hours and minutes to the stack
            @          move minutes back 2
             n         output a number
              o        output a character

First, we build the initial stack with d" ::"s, giving us ["\r", " ", ":", ":", seconds], building the stack this way saves two bytes by removing 4 ", and adding two @. It's important to know what the @ instruction does here, it simply changes the end of the stack in such a way so [1,2,3,4] @ [1,4,2,3]. So we run @ giving us a stack of ["\r", " ", seconds, ":", ":"]. Then we run hm@, which adds the hours, then the minutes to the end of the stack and executes @, giving us ["\r", " ", seconds, ":", ":", hours, minutes] @ ["\r", " ", seconds, ":", minutes, ":", hours]. In nononoooo, n outputs a number from the end of the stack, o outputs a byte.

Then the ><> simply loops back around to the beginning!

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

Java, 67 bytes

for(;;)System.out.print("\r"+java.time.LocalTime.now()+"\b\b\b\b");

We loop infinitely, grabbing the current time and outputting to screen. The quad backspace is for easy formatting, because otherwise we'd have to do something like this:

for(;;)System.out.print("\r" + java.time.LocalTime.now().truncatedTo(java.time.temporal.ChronoUnit.SECONDS));
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 14 bytes

CLS?TIME$EXEC.

CLS clears the screen, ? TIME$ prints the current time, and EXEC 0 runs the code that's in slot 0. It's a bit flickery, but there's nothing in the reqirements about that, so it should be fine.

Format is HH:MM:SS 24 hour time :(

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

Excel VBA, 17 Bytes

Outputs current time to cell A1

Do:[A1]=Now:Loop

Note: The above subroutine does not exit so either holding Esc to terminate the process or adding a DoEvents call to the sub above (Sub a:Do:[A1]=Now:DoEvents:Loop) to allow for hitting the stop button is recommended

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

Batch, 25 bytes (not including newline)

:k
@echo %time% && @goto k

Example output:

example output

\$\endgroup\$
  • \$\begingroup\$ I'm afraid that this is not a valid answer: The time must remain on the first line it was outputted with no visible trailing characters. Clearing the terminal though, would be allowed as that still wouldn't have any trailing characters. - see the disallowed answers section of the prompt for more information \$\endgroup\$ – Taylor Scott Aug 1 '17 at 18:46

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.