49
\$\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\$
  • \$\begingroup\$ Does "until cancelled by an interrupt" include closing the program or does it need a mechanism to stop the cycle coded into it? \$\endgroup\$ – user81655 Nov 27 '15 at 23:00
  • 1
    \$\begingroup\$ @Vi. meta.codegolf.stackexchange.com/questions/6961/… \$\endgroup\$ – Mego Nov 28 '15 at 6:21
  • 1
    \$\begingroup\$ Warning: All sleep 1 based answer break rule 5: you must guarantee one, and only one, output of time per second. !! \$\endgroup\$ – F. Hauri May 17 '16 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 '16 at 22:03
  • 1
    \$\begingroup\$ @Jasen No, but the meaning of "that is fine" implies that the wait option is still valid, regardless of the eventual time loss. I have clarified this for you. \$\endgroup\$ – Addison Crump Jan 7 '17 at 19:07

89 Answers 89

55
\$\begingroup\$

Minecraft 1.8.7, 7 + 8 = 15 blytes (bytes + blocks)

Only one command block involved:

xp 1 @p

Output goes to the client console like so:

times

As part of the normal output.

This is the system:

the system

xp gives a specified amount of experience to a specified player with the syntax xp <amount> <player>. I'm pretty sure this is the smallest command that has singular output that I can get, now.

\$\endgroup\$
  • 3
    \$\begingroup\$ Don't say "19 bytes" because you didn't measure your answer size in bytes. \$\endgroup\$ – feersum Nov 28 '15 at 0:00
  • 6
    \$\begingroup\$ @feersum There's a reason for me saying bytes, trust me. This is the closest thing to a consensus for MC as of now. \$\endgroup\$ – Addison Crump Nov 28 '15 at 0:04
  • 4
    \$\begingroup\$ You can use the deprecated number codes can't you? \$\endgroup\$ – Conor O'Brien Nov 28 '15 at 2:07
  • 4
    \$\begingroup\$ Use this scoring system if you want, 19 whatevers, but it isn't 19 bytes, because there is no previously defined encoding that allows you to store this solution in a 19-byte file. \$\endgroup\$ – feersum Nov 28 '15 at 5:47
  • 1
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ I'd have to wrap it in JSON, I believe. Unless I used 1.6, but that's practically another language. \$\endgroup\$ – Addison Crump Nov 28 '15 at 11:00
17
\$\begingroup\$

JavaScript, 32 bytes

setInterval("alert(Date())",1e3)

Uses the fact that setInterval evaluates code. This is not recommended, but when was that a concern in code golf?

Date() returns the current time and date in a format like this (may vary per system).

 Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
\$\endgroup\$
  • \$\begingroup\$ Would setInterval(alert,1e3,Date()) work instead? \$\endgroup\$ – ETHproductions Dec 9 '15 at 18:24
  • \$\begingroup\$ @ETHproductions It always shows the same time for me \$\endgroup\$ – ev3commander Dec 9 '15 at 21:28
  • \$\begingroup\$ @ETHproductions in your example Date() will only be evaluated once when you create the interval \$\endgroup\$ – MMM Dec 14 '15 at 13:00
15
\$\begingroup\$

sh (+ watch), 11 bytes

Script:

watch -n1 .

(no trailing newline)

Output:

Every 1,0s: . (SPACES) Sat Nov 28 19:07:51 2015

The amount of spaces depends on the terminal width.

Tested on Debian8 and NetBSD7.

\$\endgroup\$
  • \$\begingroup\$ Right tool for the job, +1 \$\endgroup\$ – Addison Crump Nov 28 '15 at 18:13
  • \$\begingroup\$ Wait, does this count as a programming language? Can you give me a link to an interpreter? \$\endgroup\$ – Addison Crump Nov 28 '15 at 18:34
  • \$\begingroup\$ @VoteToClose watch is a coreutil. The real language is bash/sh. \$\endgroup\$ – Doorknob Nov 28 '15 at 18:35
  • \$\begingroup\$ So... shouldn't this be Bash (distribution), not watch? @Doorknob \$\endgroup\$ – Addison Crump Nov 28 '15 at 18:37
  • 1
    \$\begingroup\$ Hmm You have to use -p switch to not break the rule you must guarantee one, and only one, output of time per second.!! \$\endgroup\$ – F. Hauri May 17 '16 at 23:00
15
\$\begingroup\$

Snap, 8 7 6 blocks


(Yes, I changed it in MS Paint because I was too lazy to make another screenshot. So what? At least it's readable.)
click the script to run
24-hour clock.

\$\endgroup\$
  • 2
    \$\begingroup\$ I always love graphical programming languages. :D +1 \$\endgroup\$ – Addison Crump Nov 28 '15 at 14:27
  • \$\begingroup\$ oh hey ev3! I know you from Snap/Scratch forums :P why do you need Snap instead of Scratch, exactly? \$\endgroup\$ – anOKsquirrel Nov 28 '15 at 14:43
  • 2
    \$\begingroup\$ @anOKsquirrel Because I don't need to nest all those join blocks. \$\endgroup\$ – ev3commander Nov 28 '15 at 14:45
  • \$\begingroup\$ Shouldn't that read 9:21:08? \$\endgroup\$ – Luke Nov 30 '15 at 14:06
  • \$\begingroup\$ @Luke As shown by the TI-BASIC answer, the time without the leading zero in an output is fine. It is human readable and clear what the time is. \$\endgroup\$ – Addison Crump Nov 30 '15 at 19:48
11
\$\begingroup\$

Python 2, 47 bytes

from time import*
while[sleep(1)]:print ctime()

No online link because ideone times out (huehuehue) before printing anything.

Thanks to @xsot for the while[sleep(1)] trick and the ctime trick.

Prints out the current date and time like so: Fri Nov 27 21:23:02 2015

\$\endgroup\$
  • 2
    \$\begingroup\$ /me claps slowly at the pun. Very funny Mego. ;D \$\endgroup\$ – Addison Crump Nov 28 '15 at 0:15
  • \$\begingroup\$ I've posted your solution for Python 3 \$\endgroup\$ – jfs Nov 30 '15 at 19:45
  • \$\begingroup\$ sleep 1 based answer break rule 5: you must guarantee one, and only one, output of time per second. \$\endgroup\$ – F. Hauri May 17 '16 at 23:01
  • \$\begingroup\$ @F.Hauri sleep 1 causes the current thread to sleep for exactly one second. It complies with the rules. \$\endgroup\$ – Mego May 18 '16 at 0:35
  • 1
    \$\begingroup\$ @F.Hauri Considering that the challenge author himself uses the equivalent of sleep(1) in both his AppleScript solution and his Vitsy+bash solution, it's clearly acceptable. \$\endgroup\$ – Mego May 18 '16 at 6:38
9
\$\begingroup\$

Perl, Command Line, 24 bytes

sleep(say)while$_=gmtime

This must be run from the command line, as perl -E'sleep(say)while$_=gmtime' (on windows, use double quotes instead). The date will be output along with the time, which seems to be allowed.


Perl, 25 bytes

sleep print$/.gmtime;do$0

In a scalar context, gmtime will return a string similar to Sat Nov 28 10:23:05 2015. The result from print, always 1, is used as the parameter for sleep. do$0 is used to execute the script again, after the timer has finished.

\$\endgroup\$
  • 2
    \$\begingroup\$ As evidenced by @VoteToClose's AppleScript solution and comment, you do not need to extract the time. \$\endgroup\$ – Mego Nov 28 '15 at 3:36
  • 1
    \$\begingroup\$ Another 24-byte solution: {sleep say~~gmtime;redo}. \$\endgroup\$ – Denis Ibaev May 17 '16 at 20:14
  • \$\begingroup\$ sleep 1 based answer break rule 5: you must guarantee one, and only one, output of time per second. \$\endgroup\$ – F. Hauri May 17 '16 at 23:05
  • \$\begingroup\$ @F.Hauri the rule was added after this post was made. \$\endgroup\$ – primo May 18 '16 at 0:29
9
\$\begingroup\$

Javascript ES6, 43 39 bytes

setInterval(_=>console.log(Date()),1e3)

Works with my current time settings (which have not been changed for some time, thank you), at least.

4 bytes saved by Conor O'Brien.

\$\endgroup\$
  • \$\begingroup\$ alert is shorter? \$\endgroup\$ – Maltysen Nov 27 '15 at 23:09
  • 8
    \$\begingroup\$ @Maltysen I think in many browsers you have to close one alert window in order to see the next one. \$\endgroup\$ – flawr Nov 27 '15 at 23:10
  • \$\begingroup\$ @Maltysen I tried it, messes up the interval somehow \$\endgroup\$ – SuperJedi224 Nov 27 '15 at 23:11
  • 1
    \$\begingroup\$ You don't need .toTimeString() in Chrome and FF. \$\endgroup\$ – Bob Nov 30 '15 at 6:45
  • 1
    \$\begingroup\$ I think you can replace new Date() with Date() \$\endgroup\$ – Conor O'Brien Dec 13 '15 at 22:39
8
\$\begingroup\$

Arcyóu, 27 bytes

(@ t(pn(zz 1)(p(st %H:%M:%S

I had to kludge together two new functions for this challenge, zz and st.

pn: Exactly like Lisp's progn.

zz: Direct link to Python's time.sleep.

st: Direct link to Python's time.strftime.

Normally, quotes would be necessary around the format string, but since there are no spaces, it's parsed as a symbol. The interpreter evaluates undefined symbols as themselves, so we get a string.

\$\endgroup\$
  • 1
    \$\begingroup\$ I would up vote, but I don't have any votes left. +1 ;c And nice updates. :D \$\endgroup\$ – Addison Crump Nov 28 '15 at 20:54
7
\$\begingroup\$

Bash, 51 31 24 21 20 bytes

Thanks to @quartata for some very helpful comments. Thank you @Dennis for clarifications and for chopping off even more bytes. Thank you @VoteToClose for clarifying the rules (which apparently I am bad at reading) further reducing the bytes.

date displays the the full date with a 24 hour clock. sleep 1 sleeps for a second. exec $0 loops the script infinitely.

date;sleep 1;exec $0
\$\endgroup\$
  • \$\begingroup\$ You don't need the shebang. \$\endgroup\$ – a spaghetto Nov 28 '15 at 2:52
  • \$\begingroup\$ Also, I think you can replace the true with the sleep 1 since sleep returns a 1. \$\endgroup\$ – a spaghetto Nov 28 '15 at 2:52
  • \$\begingroup\$ date +%T;sleep 1;exec $0 is a bit shorter. @quartata Nitpicking: sleep has exit code zero, which is why while continues. \$\endgroup\$ – Dennis Nov 28 '15 at 3:47
  • 3
    \$\begingroup\$ Oh I'm an idiot \$\endgroup\$ – a spaghetto Nov 28 '15 at 3:50
  • 2
    \$\begingroup\$ You don't need the space after date, so this is only 20 bytes. \$\endgroup\$ – ThisSuitIsBlackNot Nov 30 '15 at 1:46
6
\$\begingroup\$

Befunge 98, 53 bytes

 v
v>"EMIT"4(>H.8,':,M.8,':,S:.8,d,
>:S-    !!k^

Notes:

  1. This program does not zero-pad the numbers it prints.
  2. This program prints a space and then a backspace character after every number, as the . command prints an implicit space at the end.
  3. The size of the stack in this program grows every second, and thus it will eventually run out of memory.
  4. This program will overwrite the previous time when it prints a new one. To make it print each time on a new line, change the d on the first line to an a
\$\endgroup\$
  • \$\begingroup\$ Why is the "TIME" string necessary? Is there some way it's grabbing it? (I didn't know about this) \$\endgroup\$ – Addison Crump Nov 27 '15 at 23:38
  • \$\begingroup\$ The "EMIT"4( at the beginning of the program is telling the interpreter to load the TIME fingerprint, which enables the H,M, and S instructions. (No, it doesn't emit the number 4). \$\endgroup\$ – pppery Nov 27 '15 at 23:39
  • \$\begingroup\$ That is awesome. I never even thought that many esolangs would do this, +1. \$\endgroup\$ – Addison Crump Nov 27 '15 at 23:41
  • \$\begingroup\$ @VoteToClose Befunge 98 even has an instruction to execute an arbritrary program popped from the stack. \$\endgroup\$ – pppery Nov 27 '15 at 23:42
  • 1
    \$\begingroup\$ runs off to learn this language Excuse me... \$\endgroup\$ – Addison Crump Nov 27 '15 at 23:42
6
\$\begingroup\$

mIRC 7.49 20 16 Bytes

/timer 0 1 $time
\$\endgroup\$
  • \$\begingroup\$ Where can we test this? \$\endgroup\$ – Addison Crump May 31 '16 at 12:56
  • \$\begingroup\$ You have to download mIRC it's a shareware IRC client. You can download it from: mirc.com also since the answer above makes use of say you will need to connect to a server and then join a channel. other wise you would use echo instead of say. mIRC has it's own scripting language mSL(mIRC scripting language) \$\endgroup\$ – Do0msDay May 31 '16 at 14:01
5
\$\begingroup\$

AutoHotkey, 50 bytes

x::Send,% a!=A_Sec? A_Hour ":" A_Min ":" a:=A_Sec:

Notes:

  1. Requires you to hold x for continuous output.
  2. Remove your finger from x to interrupt and end the output.
  3. Output is in 24-hour format.
\$\endgroup\$
  • 2
    \$\begingroup\$ You could, for a few extra bytes, put that in a tooltip so it doesn't require the user to hold x. \$\endgroup\$ – Michelfrancis Bustillos May 18 '16 at 11:58
5
\$\begingroup\$

C (on Linux x64), 179 177 175 168 159 bytes

#include<time.h>
#include<sys/time.h>
main(){struct timeval a;char b[9];for(;;){gettimeofday(&a,0);strftime(b,9,"%T",localtime(&a.tv_sec));puts(b);sleep(1);}}

Ungolfed:

#include <time.h>
#include <sys/time.h>
main(){
  struct timeval a;
  char b[9];
  for(;;){
    gettimeofday(&a, 0);
    strftime(b, 9, "%T",localtime(&a.tv_sec));
    puts(b);
    sleep(1);
  }
}

Only tested on, and likely only functions on, linux x64 (x32 might work, but other platforms probably won't).

The main difference between this program and the other posted C program is the use of linux-exclusive function calls, which, while terrible practice for real software, saves quite a few bytes...if you ignore all the compiler warnings.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Unless I'm mistaken, I believe you can remove the space between #include and <...>. Also, can you use 1e6 instead of 1000000? \$\endgroup\$ – Addison Crump Nov 28 '15 at 14:33
  • \$\begingroup\$ @VoteToClose You were right about the spaces in the #includes. It doesn't look like usleep() accepts 1e6, though - based on how fast it starts spitting out timestamps it just interprets it as 1. \$\endgroup\$ – Gamerdog Nov 28 '15 at 14:37
  • \$\begingroup\$ ¯\_(ツ)_/¯ I don't really know C, either. I'll let the professionals at that. Welcome again, have an up vote. :D \$\endgroup\$ – Addison Crump Nov 28 '15 at 14:39
  • \$\begingroup\$ Ah, another thing - you don't have to cut the string for just the time, so you can just output the entire date time, provided it has the time string required in it. \$\endgroup\$ – Addison Crump Nov 28 '15 at 14:42
  • 2
    \$\begingroup\$ You can use %T as the format spec in strftime. \$\endgroup\$ – Mego Nov 29 '15 at 2:41
5
\$\begingroup\$

Jolf, 14 8 7 bytes

Crossed out 14 is a striked 1? Naw, it will never catch on ;)

Try it here! Click run, do not click on anything else ^_^ the page is highly... explosive. Yes. Fixed output system with update.

TaD#`~2

(I added some constants, and ~1 to ~4 are powers of 10.)

Explanation

setInterval("alert(Date())",1000);
     T         a      D#   ` ~2
\$\endgroup\$
  • 4
    \$\begingroup\$ Someone should create YAJSGV (Yet Another JavaScript Golfing Variant). \$\endgroup\$ – intrepidcoder Nov 28 '15 at 3:43
  • \$\begingroup\$ This seems to print a lot more than just the time. \$\endgroup\$ – Dennis Nov 28 '15 at 3:45
  • 1
    \$\begingroup\$ @Dennis I assumed that was fine as per other solutions \$\endgroup\$ – Conor O'Brien Nov 28 '15 at 3:48
  • \$\begingroup\$ My bad, I had only read yours and the Bash answer. \$\endgroup\$ – Dennis Nov 28 '15 at 3:48
  • \$\begingroup\$ @Dennis Not a problem ^_^ \$\endgroup\$ – Conor O'Brien Nov 28 '15 at 3:48
5
\$\begingroup\$

Dyalog APL, 27 18 16 bytes

':',¨⎕TS⋄→≢⎕DL 1

Try it online!

⎕TS Y M D h m s t
':',¨ prepend : to each
new statement
⎕DL 1 wait a second and return actual waited time; 1.0something seconds
tally the actual waited time, giving 1
go to line (1 = this line)

\$\endgroup\$
4
\$\begingroup\$

Perl 6, 29 bytes

The right way to do this:

Supply.interval(1).tap: -> $ {
  say join ':',.hour,.minute,.whole-second given DateTime.now
}
await Promise.new; # halt this thread indefinitely
22:21:58
22:21:59
22:22:0
22:22:1
22:22:2
…

The golfed version

loop {sleep say join ':',now.polymod(1,60,60,24)[3…1]} # 56 bytes
3:59:26
3:59:27
3:59:28
3:59:29
…

Since the output doesn't have to be restricted to just the time, I can make it quite a bit shorter.

loop {sleep say DateTime.now} # 29 bytes
2015-11-27T22:18:10-06:00
2015-11-27T22:18:11-06:00
2015-11-27T22:18:12-06:00
2015-11-27T22:18:13-06:00
…
\$\endgroup\$
4
\$\begingroup\$

R, 38 bytes

repeat{Sys.sleep(1);print(Sys.time())}

This outputs the current time in the following format:

[1] "2015-11-28 07:34:01 CET"
\$\endgroup\$
4
\$\begingroup\$

Powershell, 19 bytes

for(){date;sleep 1}
\$\endgroup\$
  • \$\begingroup\$ As it's since been pointed out that output merely needs to include the hh:mm:ss you can reduce to for(){date;sleep 1} \$\endgroup\$ – steve Nov 28 '15 at 23:25
4
\$\begingroup\$

PHP, 37 bytes

<?=date('G:i:s');header('refresh:1');

Outputs the formatted server time and sets the page to refresh every second.

Of course, it depends on your internet connection and how fast-repsonding your server is :)

Demo

\$\endgroup\$
  • \$\begingroup\$ Nope, no refreshing : Warning: Cannot modify header information - headers already sent \$\endgroup\$ – Martijn Nov 30 '15 at 14:20
  • \$\begingroup\$ @Martijn I have no idea what you can mess with, because it works for me :) \$\endgroup\$ – nicael Nov 30 '15 at 14:40
  • \$\begingroup\$ I'll remove the downvote as benefit of the doubt, but this works exactly once for me. Tried on two different regular servers. \$\endgroup\$ – Martijn Nov 30 '15 at 14:42
  • 2
    \$\begingroup\$ Depends on whether you have output buffering turned on. \$\endgroup\$ – Andrea Jul 11 '16 at 0:10
  • \$\begingroup\$ <?=header('refresh:1').date('G:i:s'); should work with either output bffering setting. \$\endgroup\$ – Jasen Jan 7 '17 at 11:17
4
\$\begingroup\$

VBA, 69 Bytes

Getting the Time is easy, now() Only outputting every 1 second.... MUCH more verbose.

This is the "Right" way of waiting 1 second in VBA. 78 Bytes

Sub a():Debug.Print Now():Application.Wait Now()+TimeValue("0:0:1"):a:End Sub

Or if you want to Cut some Corners and only being right 86% of the time is good enough 63 Bytes
adding one SigFig take you to 95% accurate and a third will get you to 99.36%

Sub a():Debug.Print Now():Application.Wait Now()+1e-5:a:End Sub

If you want to get the above method to 100% then you need 69 Bytes (1/86400)

Sub a():Debug.Print Now():Application.Wait Now()+(1/86400):a:End Sub

All of these methods would stumble on a leap second Beacuse they do not wait for 1 second, But wait untill 1 second. At midnight when the clocks fall back an hour this clock will stop for 1 hour and then pick up where it left off.

VBA does allow for the Sleep Method but your byte couter is Ruined. 97 Bytes

Declare Sub Sleep Lib "kernel32" (ByVal k as Long)
Sub a():Debug.Print Now():Sleep(1e3):a:End Sub
\$\endgroup\$
3
\$\begingroup\$

JavaScript, 47 38 35 bytes

for(p=1;t=Date();p=t)t!=p&&alert(t)

Explanation

Continuously checks if the time has changed then alerts if it has.

for(
  p=1;
  t=Date();
  p=t
)
  t!=p
    &&alert(t)
\$\endgroup\$
  • \$\begingroup\$ You don't have to just get the time string. ;D As long as it's in there somewhere. \$\endgroup\$ – Addison Crump Nov 28 '15 at 0:49
  • \$\begingroup\$ @VoteToClose Oh true! \$\endgroup\$ – user81655 Nov 28 '15 at 0:54
  • \$\begingroup\$ Don't think you need the +"", as without new, Date() returns a string. \$\endgroup\$ – ETHproductions Nov 28 '15 at 1:25
  • \$\begingroup\$ @ETHproductions Ah, yes that was left there from when it used new. Thanks for catching that! \$\endgroup\$ – user81655 Nov 28 '15 at 1:29
3
\$\begingroup\$

AppleScript, 51 35 bytes

repeat
log current date
delay 1
end

Pretty dang obvious. Prints the current date, which contains the time, delays a second, then continues.

\$\endgroup\$
  • 1
    \$\begingroup\$ So wait, is it allowed to print out the current date as well as the time? \$\endgroup\$ – a spaghetto Nov 28 '15 at 3:31
  • \$\begingroup\$ @quartata Yes, any concatenation with the time, as long as the time is continuously output and guaranteed to be output, is fine. \$\endgroup\$ – Addison Crump Nov 28 '15 at 10:59
  • \$\begingroup\$ delay 1 does what? pause for 1000000000 ns? \$\endgroup\$ – Jasen Jan 7 '17 at 11:19
  • \$\begingroup\$ @Jasen That's correct. \$\endgroup\$ – Addison Crump Jan 7 '17 at 12:22
3
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 13 chars / 25 bytes

Ĥ⇀ôᶁ⬮+⬬),1𝕜)

Try it here(Firefox only).

This is surprisingly readable.

\$\endgroup\$
  • 4
    \$\begingroup\$ When you say "surprisingly readable", you know you're dealing with an amusing language. +1 \$\endgroup\$ – Addison Crump Nov 29 '15 at 0:47
3
\$\begingroup\$

Dyalog APL, 36 bytes

Not very short this time.

{⎕←1↓∊'⊂:⊃,ZI2'⎕FMT 3↑3↓⎕TS⋄∇⎕DL 1}1

This outputs 24-hour time, i.e.:

14:37:44
14:37:45
14:37:46
...

Explanation:

  • {...}1: define a function and call it (the argument is ignored, but we need the function in order to call it recursively)
    • ⎕TS: a system function that returns the current date and time, in the format year - month - day - hour - minute - second - millisecond. (⎕TS = timestamp)
    • 3↑3↓: drop the first 3 items (i.e. year - month - day) and then take the first 3 items that are left (hour - minute - second).
    • '⊂:⊃,ZI2'⎕FMT: format each number as a two-digit integer (I2) with leading zeroes (Z), prefixed by a colon (⊂:⊃). (This results in a matrix.)
    • : Get all the elements in the matrix, in row order. (This gives a vector, in this case a string.)
    • 1↓: drop the first character (which is an extra :)
    • ⎕←: output it
    • ⎕DL 1: then wait one second (⎕DL = delay)
    • : call the function recursively
\$\endgroup\$
  • \$\begingroup\$ {⎕←1↓∊':',¨⍕¨3↑3↓⎕TS⋄∇⎕DL 1}1 for 28 bytes, is enough as per OP. \$\endgroup\$ – Adám Dec 10 '15 at 1:25
  • \$\begingroup\$ The new rules allow garbage output and random spaces, so you can get rid of a lot. \$\endgroup\$ – Adám Jan 5 '16 at 22:51
3
\$\begingroup\$

QBasic, 18 bytes

?TIME$
SLEEP 1
RUN
\$\endgroup\$
3
\$\begingroup\$

Haskell, 98 96 85 bytes

import GHC.Conc
import Data.Time
m@main=getCurrentTime>>=print>>threadDelay(10^6)>>m

Alternate version using do notation:

main = do
  time <- getCurrentTime
  print time
  threadDelay 1000000
  main

Gets the current time with getCurrentTime from the Data.Time library, then pipes it into print, waits 1,000,000 microseconds (1 second) and calls itself.

Thanks to nimi and Mauris!

\$\endgroup\$
  • \$\begingroup\$ m@main=...>>m is always shorter than main=...>>main \$\endgroup\$ – Lynn Nov 29 '15 at 15:59
  • \$\begingroup\$ Also, since threadDelay is GHC-specific anyway, you could import it from GHC.Conc \$\endgroup\$ – Lynn Nov 29 '15 at 16:01
3
\$\begingroup\$

Batch - 34 40 bytes

I love built-in variables:

:A                    //Set label A
echo %time%           //Print the time in 24 hour format
timeout 1    //Wait 1 second (thanks DavidPostill)
goto A                //Jump back to A and repeat

There definitely needs to be some sleep command in Batch anytime soon.

\$\endgroup\$
  • \$\begingroup\$ No builtin sleep but there is timeout. You can replace the ping ... with timeout 1. Also the clock isn't very accurate when using ping. It's better with timeout ;) \$\endgroup\$ – DavidPostill Nov 29 '15 at 20:36
  • \$\begingroup\$ @DavidPostill thanks for the tip, I never knew there was such a command in Windows. :) \$\endgroup\$ – GiantTree Nov 29 '15 at 20:39
  • \$\begingroup\$ @GiantTree You're very welcome ;) \$\endgroup\$ – DavidPostill Nov 29 '15 at 20:40
  • \$\begingroup\$ Golf: remove :A and replace goto A with %0 \$\endgroup\$ – stevefestl May 20 '17 at 15:15
3
\$\begingroup\$

Python 3, 99 Bytes

Apologies if it formats the code weird, doing this from an ipad. (dont ask). I know this code is big considering some of the other answers, but I didn't see a python one yet so I wanted to add it in.

import time
import datetime
while(True):
 print(datetime.datetime.now().time())
 time.sleep(1)  
\$\endgroup\$
  • \$\begingroup\$ You can replace True with 1, and if you put the print and sleep lines on the same line as the while (separated with a semicolon) it saves you the whitespace. \$\endgroup\$ – undergroundmonorail Dec 9 '15 at 13:40
  • \$\begingroup\$ @undergroundmonorail ok thanks for the True/1 tip. I'm not used to that since Java Dosent do that. I didn't know that Python had semicolons either. I'll look into that \$\endgroup\$ – Ashwin Gupta Dec 9 '15 at 15:02
3
\$\begingroup\$

Java, 300 166 137 125 124 bytes

Nearly More than halved thanks to VoteToClose, Paülo Ebermann and janschweizer!

interface A{static void main(String[]a)throws Exception{for(;;Thread.sleep(1000))System.out.println(new java.util.Date());}}

Ungolfed:

interface A {
    static void main(String[] a) throws Exception {
        for (;; Thread.sleep(1000)) System.out.println(new java.util.Date());
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Simplify your imports: java.util.* instead of the two java.util imports, java.text.* for the same reasoning. Use for(;;) for infinite loops. Use String[]a in the main declaration. Use interface A{static void main(..., as this will shorten it even more. You can use Exception over InterruptedException, and you can replace ex with e. You can also remove all text inside the catch, as this does nothing anyways. \$\endgroup\$ – Addison Crump Nov 28 '15 at 14:51
  • 1
    \$\begingroup\$ Yeah, the interface thing is a little wacky - I have no idea why Java would let that be a thing, considering that interfaces are specifically designed to be unrunnable. \$\endgroup\$ – Addison Crump Nov 28 '15 at 14:56
  • 1
    \$\begingroup\$ Two things: you need to change the time string to "HH:mm:ss" - as of now, you're formatting "Hour:Month:Second". Also, since you're using a loop, you can get rid of the DateFormat and Calendar variables declarations all together. You replace the contents of the for loop with System.out.println(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));try{Thread.sleep(1000);}catch(Exception e){}. \$\endgroup\$ – Addison Crump Nov 28 '15 at 15:40
  • 1
    \$\begingroup\$ All of the things I just suggested, wrapped up: import java.util.*;import java.text.*;interface A{static void main(String[]a){for(;;){System.out.println(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));try{Thread.sleep(1000);}catch(Exception e){}}}} \$\endgroup\$ – Addison Crump Nov 28 '15 at 15:42
  • 1
    \$\begingroup\$ throws Exception is shorter than a try/catch \$\endgroup\$ – phase Nov 29 '15 at 7:21
3
\$\begingroup\$

Perl, 99 81 75 51 40 36 29 27 bytes

sleep(print gmtime.$/);do$0
\$\endgroup\$
  • 1
    \$\begingroup\$ You can shorten it by using gmtime instead of localtime. \$\endgroup\$ – Brad Gilbert b2gills Nov 28 '15 at 3:11
  • 2
    \$\begingroup\$ Also printf"%02d:%02d:%02d\n",(gmtime)[2,1,0] \$\endgroup\$ – Brad Gilbert b2gills Nov 28 '15 at 3:18
  • 1
    \$\begingroup\$ gmtime."\n" will force a scalar context. \$\endgroup\$ – primo Nov 28 '15 at 15:12
  • 1
    \$\begingroup\$ You should almost never need to do ."\n" in golf: use .$/ to save 2 bytes. At the very least, use a literal newline instead of \n to save 1 byte. \$\endgroup\$ – ThisSuitIsBlackNot Dec 1 '15 at 19:40

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.