Output the current time of day as Swatch Internet Time.
Specifically, output a three-digit (zero-padded) number of ".beats" (1000ths of a day) which represent the current time of day in the Swatch Internet Time time zone of UTC+01:00 ("Biel Meantime").
For example, if the current time in UTC is 23:47, then it is 00:47 in UTC+1 ("Biel Meantime"), and the output would be 032.
Examples:
UTC Current Time -> SIT Output
23:47 -> 032
23:00 -> 000
11:00 -> 500
14:37 -> 651
The program should produce this output then immediately exit.
Output is to standard output (or equivalent). Must be a complete, runnable program, not a function. The program takes no input; it outputs the current time.
18 Answers 18
PHP, 12 bytes
<?=@date(B);
But it can only be so short because PHP has built-in Swatch Internet Time support. So this answer isn't much fun.
<?= exits HTML mode and evaluates an expression, then echoes it.
@ silences an error.
date() outputs the current time, formatting it with a given format string.
B is an undefined constant. In PHP, if you reference a constant that doesn't exist, you get back a string containing its name, and it also produces a "notice"-level error. Here, the @ suppresses that error. B is the date() format code for Swatch Internet Time.
; terminates the expression.
If we assume PHP is being run with the default error-reporting settings, where "notices" are silenced, we could skip the @, and it would only be 11 bytes.
-
3\$\begingroup\$ I'm pretty sure supresssing notices is actually the default for PHP golfing on this site \$\endgroup\$SuperJedi224– SuperJedi2242016年07月16日 03:12:07 +00:00Commented Jul 16, 2016 at 3:12
-
\$\begingroup\$ @SuperJedi224 It's PHP's default configuration, too, but I have it set to show notices on my machine, so... \$\endgroup\$mystery– mystery2016年07月17日 15:09:54 +00:00Commented Jul 17, 2016 at 15:09
C, 56 bytes
main(){printf("%03d",(int)((time(0)+3600)%86400/86.4));}
Explanation:
- %03d - tells printf to zero-pad up to 3 digits.
- time(NULL)+3600 - gets amount of seconds (UTC) elapsed since epoch and adds an hour to it (UTC+1).
- %86400 - divides epoch by the amount of seconds in a 24hr day and gets the remainder (representing seconds elapsed, so far, "today").
- /86.4 - divide remaining seconds by 86.4 to get the ".beat" count since midnight (UTC+1).
Compile (MSVC):
C:> cl swatch.c
Compile (GCC):
$ gcc swatch.c
-
\$\begingroup\$ For future reference: you should post the golfed version of the code, and, at your option, an ungolfed version or test program below. \$\endgroup\$cat– cat2016年07月11日 12:09:29 +00:00Commented Jul 11, 2016 at 12:09
-
1\$\begingroup\$ This contains a bug. It doesn't do a modulo after correcting the time-zone, it does it before, which means between 23:00–0:00 UTC (i.e. 0:00–1:00 UTC+1), it incorrectly produces a four-digit number. You could fix this by changing
time(NULL)%86400+3600to(time(NULL)+3600)%86400. \$\endgroup\$mystery– mystery2016年07月13日 20:50:33 +00:00Commented Jul 13, 2016 at 20:50 -
\$\begingroup\$ Also, you could save space by omitting the two includes, and replacing the
NULLwith0. \$\endgroup\$mystery– mystery2016年07月13日 20:51:28 +00:00Commented Jul 13, 2016 at 20:51 -
\$\begingroup\$ Hmm, doesn't
%03.fround up? That would give inaccurate results. \$\endgroup\$mystery– mystery2016年07月16日 00:22:36 +00:00Commented Jul 16, 2016 at 0:22 -
1\$\begingroup\$ @Andrea Thank you. Edited. \$\endgroup\$veganaiZe– veganaiZe2016年07月24日 22:03:45 +00:00Commented Jul 24, 2016 at 22:03
PHP, (削除) 48 (削除ここまで) 46 bytes
<?=sprintf("%03d",((time()+3600)%86400)/86.4);
I have another PHP answer above, but this one avoids using PHP's built-in Swatch Internet Time support, so it's more interesting, if longer.
This is largely self-explanatory if you're familiar with UNIX time and sprintf, (削除) though note I'm using (actually I realised this is unnecessary in PHP, oops!)|0 as a short way to truncate a float to an integer (削除ここまで)
Java 8, 143 bytes
import java.time.*;interface A{static void main(String[]a){System.out.format("%03.0f",LocalTime.now(ZoneId.of("UT+1")).toSecondOfDay()/86.4);}}
this uses Java 8's java.time package to get current time, convert it to UTC+1, and then get the number of seconds. At the end it divides by the number of 1000s of seconds in a day, which turns out to be 86.4.
Funny how the function that actually calculates the time is only about a third of the overall program size.
05AB1E, (削除) 29 (削除ここまで) 21 bytes
ža>60©%®*žb+4*1440÷Dg3s-Å0š ̃J
After compressing integers thanks to @KevinCruijssen (-8 bytes) :
ža>60©%®*žb+4*Ž5¦÷4+¦
Explanation:
ža>60©%®*žb+4*1440÷Dg3s-Å0š ̃J
ža push current hours in UT
> increment
60© push 60 and save in register c
% hours mod 60
®* push from register c and Multiply
žb+ add current minutes
4* multiply by 1000
1440÷ Integer division with 1440
D Duplicate
g Length
3s- 3-length
Å0 create list of a 0s
š Prepand
̃J Flat and joon
I didn't find a better idea for prepanding Zeros, so if anyone got a better idea I'll be glad to know :)
-
\$\begingroup\$ The output of this is not three padded digits, no? \$\endgroup\$mystery– mystery2020年07月30日 12:49:52 +00:00Commented Jul 30, 2020 at 12:49
-
1\$\begingroup\$ I've edited my answer \$\endgroup\$SomoKRoceS– SomoKRoceS2020年07月30日 13:44:26 +00:00Commented Jul 30, 2020 at 13:44
-
1\$\begingroup\$ Yes :) it prints the current swatch time, I pushed the current hours in UTC and Incremented it (mod 24). \$\endgroup\$SomoKRoceS– SomoKRoceS2020年07月30日 21:52:47 +00:00Commented Jul 30, 2020 at 21:52
-
2\$\begingroup\$ The "tio.run" output I was looking at was outdated and just so happened to be exactly an hour behind! Clicking the big ▶️ button re-ran it and I see your code is correct, sorry! \$\endgroup\$mystery– mystery2020年07月31日 09:00:09 +00:00Commented Jul 31, 2020 at 9:00
-
1\$\begingroup\$
1440can be compressed toŽ5¦(see section How to compress large integers? in this 05AB1E tip of mine). As for theDg3s-Å0š ̃J, this can be golfed to4+¦(add 1000, remove the first character). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年10月21日 12:44:12 +00:00Commented Oct 21, 2020 at 12:44
Javascript, (削除) 53 (削除ここまで) (削除) 52 (削除ここまで) 48 bytes
alert((new Date/864e5%1+1/24+'0000').slice(2,5))
Explanation:
new Date/864e5%1 is the fraction of a day in UTC. +1/24 adds 1/24th of a day (1 hour), moving it to UTC+1.
"0000" is concatenated with this, turning it into a string. Then the 3rd through 5th characters are taken. The reason "0000" is concatenated instead of an empty string is for the cases in which there are fewer than 3 decimal places:
"0.XXX(any number of digits here)0000"Swatch time is XXX"0.XX0000"Swatch time is XX0"0.X0000"Swatch time is X00"00000"Swatch time is 000
(the first character is 1 instead of 0 from 23:00 UTC to 24:00 UTC, but the first character is ignored.)
JavaScript, 98 bytes
d=new Date();t=;console.log(Math.floor((360*d.getHours()+60*d.getMinutes()+d.getSeconds())/86.4));
Definitely could be optimized, I had some problems with the Date object so I'm looking into shorter ways to do that.
-
\$\begingroup\$ Hmm. You could probably be more efficient by using the UNIX timestamp feature (
Date.prototype.getTime), instead of summing the hours, minutes and seconds. Also, though I didn't make this clear before, I think the output should be zero-padded. \$\endgroup\$mystery– mystery2016年07月13日 20:45:53 +00:00Commented Jul 13, 2016 at 20:45
Octave, 64 bytes
t=gmtime(time);sprintf("%03.f",(mod(++t.hour,24)*60+t.min)/1.44)
Uses veganaiZe's printf formatting.
I'm having a bit of difficulty with the time that ideone is returning, so here's a sample run with the time struct returned by gmtime for reference. I'll look around and see if I can get any of the other online Octave compilers to give me proper time.
t =
scalar structure containing the fields:
usec = 528182
sec = 17
min = 24
hour = 21
mday = 15
mon = 6
year = 116
wday = 5
yday = 196
isdst = 0
zone = UTC
ans = 933
q/k (21 bytes)
7h1ドルe3*(.z.n+0D01)%1D
Python 2.7, (削除) 131 (削除ここまで) (削除) 128 (削除ここまで) 121 bytes:
from datetime import*;S=lambda f:int(datetime.utcnow().strftime(f));print'%03.0f'%(((S('%H')+1%24)*3600+S('%M')*60)/86.4)
A full program that outputs the Swatch Internet Time.
Simply uses Python's built in datetime module to first get the UTC+0 time in hours and minutes using datetime.utfnow().strftime('%H') and datetime.utfnow().strftime('%M'), respectively. Then, the time is converted into UTC+1 by adding 1 to the hours and then modding the sum by 24 to ensure the result is in the 24-hour range. Finally, the hour is turned into its equivalent in seconds, which is added to the minute's equivalent in seconds, and the resulting sum is divided by 86.4, as there are 86.4 seconds or 1 min. 24 sec. in 1 ".beat", after which, using string formatting, the quotient is rounded to the nearest integer and padded with zeroes until the length is 3.
However, I am not the one to stop here. In the above solution, I used a more direct method to convert the time to UTC+1. However, wanted to add a bit of a bigger challenge for myself and implement this using only Python's built in time module, which apparently does not have any built-in method that I know of to convert local time into UTC+0 time. So now, without further ado, here is the perfectly working version using only the time module, currently standing at 125 bytes:
from time import*;I=lambda g:int(strftime(g));print'%03.0f'%((((I('%H')+1-daylight)%24*3600+timezone)%86400+I('%M')*60)/86.4)
This can output the correct Swatch Internet Time for any and all time zones, and basically does pretty much everything the same as in the first solution, except this time converts the local time into UTC+1 by first adding 1 to the hour, and then subtracting 1 if daylight-savings time is currently, locally observed, or 0 otherwise. Then, this difference is modded by 24 to ensure that the result stays within the 24 hour range, after which it is multiplied by 3600 for conversion into seconds. This product is then added to the result from the built-in timezone method, which returns the local offset from UTC+0. After this, you finally have your hours in UTC+1. This then continues on from here as in the first solution.
-
\$\begingroup\$ Interesting! Though, it shouldn't be necessary to get this from stdin, given Python knows the current time, right? \$\endgroup\$mystery– mystery2016年07月15日 23:52:12 +00:00Commented Jul 15, 2016 at 23:52
-
\$\begingroup\$ @Andrea It is fixed and works perfectly now. \$\endgroup\$R. Kap– R. Kap2016年07月16日 08:50:03 +00:00Commented Jul 16, 2016 at 8:50
-
\$\begingroup\$ Hmm, intriguing. Might it be shorter using UNIX time? \$\endgroup\$mystery– mystery2016年07月17日 15:11:45 +00:00Commented Jul 17, 2016 at 15:11
Javascript, 83 bytes
a=(((36e5+(+new Date()))%864e5)/864e2).toFixed(),alert("00".slice(Math.log10(a))+a)
CJam, 34 bytes
Update: I shouldn't code hungry. My previous answer was shorter, but didn't cast to int or left-pad. These are now fixed, with +8 bytes to left-pad (probably improvable), +1 to int cast, and -2 to optimizations. My old comment no longer applies.
r':/~\~)24md60*@~+1.44/i"%03d"e%o;
Explanation:
r read input
':/ split on :
~ unwrap array
\~ evaluate hour to numbers
) increment hour
24md modulo 24
60* multiply by 60
@~ switch to minute and eval to num
+ add hour*60 and minute
1.44/ divide by 1.44 to get SIT
i cast to int
"%03d"e% format as 3 leading 0s
o; output and discard spare 0 from modulo
-
\$\begingroup\$ I suppose the first three digits are correct, so maybe it's a valid entry in that way (I never said a newline was required...) but it's not supposed to have a decimal point or any fractional digits. \$\endgroup\$mystery– mystery2020年07月26日 21:49:21 +00:00Commented Jul 26, 2020 at 21:49
-
\$\begingroup\$ Oops, I missed that no decimal requirement. I have other optimizations I thought of in the last half-hour, so I'll just add a cast to int along with those. \$\endgroup\$Ethan Chapman– Ethan Chapman2020年07月26日 22:18:03 +00:00Commented Jul 26, 2020 at 22:18
-
\$\begingroup\$ @Andrea I missed the leading 0s requirement, so that's fixed as well. There may be a better way but I'm not very familiar with that part of CJam. \$\endgroup\$Ethan Chapman– Ethan Chapman2020年07月26日 22:36:47 +00:00Commented Jul 26, 2020 at 22:36
Japt, (削除) 27 23 19 (削除ここまで) 15 bytes
Kj z86400 s t3n
Javascript port.
I hope I've done it correctly.
-4 bytes from Shaggy.
-4 more bytes from Shaggy.
-4 more more bytes from Shaggy.
-
\$\begingroup\$ I'm not entirely sure I've understood the challenge but this definitely isn't right; you need a space before the
/and thet2,3should be at the end, with a space before it, givingKj +36e5 /864e5%1+"0000" t2,3. And, from there, you can get down to 23 bytes with36e5+Kj)/864e5%1 x4 ¤¯3. When I figure the challenge out, I'll see if any more can be saved. \$\endgroup\$Shaggy– Shaggy2020年10月20日 11:41:00 +00:00Commented Oct 20, 2020 at 11:41 -
1\$\begingroup\$ OK, think I've figured it out and you should be able to get down to at least 21 bytes by: claiming locale dependency (i.e., a UTC+1 timezone), getting the current hour, multiplying by 60, adding the minutes, floor dividing by 1.44, converting to a string and then left padding with
0s. \$\endgroup\$Shaggy– Shaggy2020年10月20日 15:00:41 +00:00Commented Oct 20, 2020 at 15:00 -
1\$\begingroup\$ Oh, and welcome to Japt :) \$\endgroup\$Shaggy– Shaggy2020年10月20日 15:01:27 +00:00Commented Oct 20, 2020 at 15:01
-
\$\begingroup\$ Actually, feck that, just knock the first 5 bytes off the current version for 18 bytes. Dunno why I was trying to overcomplicate it! \$\endgroup\$Shaggy– Shaggy2020年10月20日 15:35:59 +00:00Commented Oct 20, 2020 at 15:35
-
\$\begingroup\$ @Shaggy wait, knocking that off is to claim locale dependency, correct? \$\endgroup\$Razetime– Razetime2020年10月20日 15:37:00 +00:00Commented Oct 20, 2020 at 15:37
-
\$\begingroup\$ This seems to be rounded differently than PHP's
date("B");but I guess it's close enough? (I don't know what the PHP code does, I'm guessing this is a ceil versus floor difference, or your constants aren't precise enough) \$\endgroup\$mystery– mystery2020年10月22日 20:33:09 +00:00Commented Oct 22, 2020 at 20:33 -
1\$\begingroup\$ @Andrea the difference is probably that I'm using only the hour and minute specifiers, whereas I'm guessing the php implementation uses a more precise value. My approach is rounded down to the minute, as per all the test cases. \$\endgroup\$Jitse– Jitse2020年10月22日 21:56:39 +00:00Commented Oct 22, 2020 at 21:56
-
1\$\begingroup\$ Fair enough! Swatch Internet Time is not very precise so I won't be pedantic over precision if it's only off by ±1. \$\endgroup\$mystery– mystery2020年10月23日 21:42:09 +00:00Commented Oct 23, 2020 at 21:42
FreePascal, 110 byte
Speaking of brain‐damaged time formats, Delphi invented a time format tDateTime using floating‐point numbers.
| integral part | radix point | fractional part |
|---|---|---|
45398 |
. |
5 |
| number of complete days elapsed since 1899‐12‐30 | fractional part of day since midnight (here noon) |
uses
sysUtils,dateUtils;begin
writeLn(format('%3.3U',[trunc(frac(localTimeToUniversal(time)+1/24)*1E3)]))end.
The time function returns the fraction of the day since midnight.
This value is subject to local time, so localTimeToUniversal converts it to UTC.
Then you add oneHour (= 1/24) to obtain UTC+1.
Since this may cause the value equal to or exceed 1.0, frac discards any integral part.
Multiply that by 1000 and trunc for an integer value.
Finally format emits a zero‐padded value.
C#, 112 bytes
class P{static void M(){System.Console.Write((int)((DateTime.UtcNow.TimeOfDay.TotalSeconds%86400+3600)/86.4));}}
C#, 68 bytes
()=>(int)((DateTime.UtcNow.TimeOfDay.TotalSeconds%86400+3600)/86.4);
A simple port of @veganaiZe's code. Thanks to him :)
-
1\$\begingroup\$ Is this a "complete and runnable program"? I don't know much C#, but this looks like a lambda than a full program. \$\endgroup\$user55852– user558522016年07月18日 00:19:11 +00:00Commented Jul 18, 2016 at 0:19
-
\$\begingroup\$ @YakovLipkovich Lambda are allowed on PPCG. Well I saw that here, in this specific question, they aren't. I asked why. \$\endgroup\$aloisdg– aloisdg2016年07月18日 03:17:24 +00:00Commented Jul 18, 2016 at 3:17
-
\$\begingroup\$ Since this adds
3600after doing the modulo, won't it return the wrong result after 23:00 UTC? \$\endgroup\$mystery– mystery2016年09月05日 18:53:54 +00:00Commented Sep 5, 2016 at 18:53 -
\$\begingroup\$ @Andrea Maybe indeed. Did you try it after 23h? \$\endgroup\$aloisdg– aloisdg2016年09月08日 08:11:09 +00:00Commented Sep 8, 2016 at 8:11
-
\$\begingroup\$ @aloisdg I haven't tried your code yet, but it should be clear this will happen without running it. \$\endgroup\$mystery– mystery2016年09月09日 03:33:31 +00:00Commented Sep 9, 2016 at 3:33
Common Lisp (Lispworks), 144 bytes
(defun f(s)(let*((d(split-sequence":"s))(h(parse-integer(first d)))(m(parse-integer(second d))))(round(/(+(*(mod(1+ h) 24)3600)(* m 60))86.4))))
ungolfed:
(defun f (s)
(let* ((d (split-sequence ":" s))
(h (parse-integer (first d)))
(m (parse-integer (second d))))
(round
(/
(+
(*
(mod (1+ h) 24)
3600)
(* m 60))
86.4))))
usage:
CL-USER 2759 > (f "23:47")
33
-0.36111111111111427
date()or equivalent here, but PHP is an exception. \$\endgroup\$