45
\$\begingroup\$

Challenge

You must output the current time continuously (until cancelled by an interrupt), once every second, by any of the following means:

  • It must be in 24-hour or AM/PM format.
    • If it is the former, it must be spaced out with colons (i.e. 15:47:36).
    • If it is the latter, it must be spaced out with colons and have the AM/PM following (i.e. 3:47:36 PM)
  • It may be pulled from the internet.
  • It may be the system time.
  • It must output any naturally accessible form of output which supports text that you choose.
  • Output may have extra information aside of the time in it, but you must guarantee one, and only one, output of time per second.
  • The continuous output must be a second apart - if you choose to wait until the second changes between outputs, that is fine. If you wait a second between each output, that is perfectly acceptable, despite the eventual loss of accuracy.

Since this is a catalog, languages created after this challenge are allowed to compete. Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language. Other than that, all the standard rules of must be obeyed. Submissions in most languages will be scored in bytes in an appropriate preexisting encoding (usually UTF-8).

Catalog

The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=65020,OVERRIDE_USER=44713;function answersUrl(e){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"//api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
25
  • 1
    \$\begingroup\$ @user81655 Yes, it can be any form of interrupt - don't worry about coding that bit. \$\endgroup\$ Nov 27, 2015 at 23:01
  • 1
    \$\begingroup\$ @Vi. meta.codegolf.stackexchange.com/questions/6961/… \$\endgroup\$
    – user45941
    Nov 28, 2015 at 6:21
  • 1
    \$\begingroup\$ @NBZ Yes, that's fine. \$\endgroup\$ Jan 5, 2016 at 22:36
  • 1
    \$\begingroup\$ Warning: All sleep 1 based answer break rule 5: you must guarantee one, and only one, output of time per second. !! \$\endgroup\$ May 17, 2016 at 23:03
  • 1
    \$\begingroup\$ F. Hauri: How so? Rule 6 says "If you wait a second between each output, that is fine as well." \$\endgroup\$
    – YetiCGN
    Aug 26, 2016 at 22:03

94 Answers 94

1 2 3
4
0
\$\begingroup\$

VBA, 50 Bytes

Anonymous VBE immediate window function that takes no input and outputs to the VBE immdiate window

Do:DoEvents:?Now:Application.Wait 1/864E2+Now:Loop
\$\endgroup\$
0
\$\begingroup\$

Phooey, 39 bytes

[1@_1~t/1000@/60@/60%24[_1%60$i":"&]#1]

Try it online!

It's not pretty. It prints the time followed by another colon, with no newline in between.

In order to see the output you probably need to use stdbuf -i0 -o0 -e0 to set it to unbuffered, as I don't print newlines. The output is always in UTC time.

Explanation:

[1            // While tape value is not 1 (it will either be 0 the first time or -1 the second)
  @_1         // Push sentinel of negative 1
  ~t          // tape = millis since epoch
  /1000       // Divide tape by 1000 to get seconds
  @           // Push tape value to save the seconds
  /60         // Divide by 60 to get minutes
  @           // Push tape to save minutes
  /60         // Divide by 60 to get hours
  %24         // Modulo by 24 to hour of the day
              // Stack: -1, seconds, minutes
              // Tape: hours
  [_1         // While tape is not -1 sentinel
     %60      //   modulo tape by 60 (redundant for hours)
     $i       //   print as number
     ":"      //   print separator
     &        //   Pop to tape
  ]           // Loop if tape == tape (nop condition)
  #1          // sleep 1 second
]             // End loop

Note: 2:58 PM EST = 18:58 UTC

Excuse the low quality, the attachment feature has a stupid requirement of 2 MB which is not easy for GIFs to reach...

enter image description here

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

x86-16 DOS .COM executable (FreeDOS 1.3), 40 bytes

00000000: b4 2c cd 21 38 f3 74 f8 88 f3 88 e8 e8 07 00 91  .,.!8.t.........
00000010: e8 03 00 88 d8 56 d4 0a 05 30 30 86 c4 cd 29 88  .....V...00...).
00000020: e0 cd 29 b0 3a cd 29 c3                          ..).:.).

Assembly source code:

    ; NASM syntax
    [org 0x100]
start:
    ; Note: The first second may be a little off due to synchronizing to the time
.loop:
    ; Int 21:2C: get time
    ; Return:
    ;    CH = hour
    ;    CL = minute
    ;    DH = second
    ;    DL = 1/100 seconds
    mov   ah, 0x2C
    int   0x21
    ; If seconds is the same as before, try again.
    cmp   bl, dh
    jz    .loop
.loop.end:
    ; Save last seconds
    mov   bl, dh
    ; Print hours
    mov   al, ch
    call  print
    ; Print minutes
    ; mov al, cl
    xchg  ax, cx
    call  print
    ; print seconds
    mov al, bl
    ; Assume SI = 0x0100, push that as a return address
    ; and fall through to loop
    push  si

    ; Converts AL to 2 digit ASCII and prints, followed by :
print:
    ; AL = AL % 10
    ; AH = AL / 10
    aam   10
    ; Convert to ASCII
    add   ax, '00' ; 0x3030
    ; Byteswap to get tens in AL, save ones in AH
    xchg  al, ah
    ; Print tens with int 29h
    int   0x29
    ; Then ones
    mov   al, ah
    int   0x29
    ; Then colon
    mov   al, ':'
    int   0x29
    ; Return from function
    ret

Like my other answer it isn't pretty but it does the job. It prints a : between each time.

Output on FreeDOS 1.3 on QEMU Output on FreeDOS 1.3 on QEMU

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

HP‑41C series, 4 Bytes

This requires an HP‑41CX or an HP‑41C/CV with a time module plugged in.

CLKT    2 Bytes  select time-only display for CLOCK (default)
CLOCK   2 Bytes  displays clock, refreshes every second

NB: CLOCK is effectively OFF if the system flag 49 (“low battery”) is set.

\$\endgroup\$
1 2 3
4

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.