6
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ Possible dupe? From reading the article it looks like doing a minor amount of arithmetic on the output to that challenge. Perhaps if you described the intricacies of Swatch Internet Time within the post, it would be clearer whether or not this is a duplicate. That said, welcome to PPCG, I hope your first question goes well, but note we have a sandbox where challenges can be posted for review. \$\endgroup\$ – FryAmTheEggman Jul 10 '16 at 23:59
  • 1
    \$\begingroup\$ SE policy is that I should not need to visit external sites to understand and answer your question and the same goes for challenges. I don't know what SIT is, and when the thing hosting your link blows up, neither will anyone else. \$\endgroup\$ – cat Jul 11 '16 at 1:29
  • 2
    \$\begingroup\$ Some more sample inputs and outputs would be nice. This challenge might have been better were it steeped in the Sandbox \$\endgroup\$ – cat Jul 11 '16 at 1:29
  • 1
    \$\begingroup\$ What sort of precision is required? Minute, hour, second ... for example if it is second, I need a factor of 0.69444 while for minutes 0.694 is sufficient (assuming a floored int as output) \$\endgroup\$ – MickyT Jul 11 '16 at 2:25
  • 2
    \$\begingroup\$ @R.Kap I agree with your points a) and b), but it's obvious that if SIT is based on UTC+1 and it's currently 23:47 UTC(+0), it's 00:47 UTC+1, which agrees with Andrea's example. Hopefully we can get the question clarified, because I don't think it's a duplicate of the Current Time question. \$\endgroup\$ – beaker Jul 11 '16 at 20:12

11 Answers 11

5
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ I'm pretty sure supresssing notices is actually the default for PHP golfing on this site \$\endgroup\$ – SuperJedi224 Jul 16 '16 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\$ – Andrea Jul 17 '16 at 15:09
4
\$\begingroup\$

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

\$\endgroup\$
  • \$\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 Jul 11 '16 at 12:09
  • \$\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+3600 to (time(NULL)+3600)%86400. \$\endgroup\$ – Andrea Jul 13 '16 at 20:50
  • \$\begingroup\$ Also, you could save space by omitting the two includes, and replacing the NULL with 0. \$\endgroup\$ – Andrea Jul 13 '16 at 20:51
  • \$\begingroup\$ Hmm, doesn't %03.f round up? That would give inaccurate results. \$\endgroup\$ – Andrea Jul 16 '16 at 0:22
  • \$\begingroup\$ @Andrea Thank you. Edited. \$\endgroup\$ – veganaiZe Jul 24 '16 at 22:03
2
\$\begingroup\$

PHP, 48 bytes

<?=sprintf("%03d",((time()+3600)%86400)/86.4|0);

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 |0 as a short way to truncate a float to an integer.

\$\endgroup\$
2
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • \$\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\$ – Andrea Jul 13 '16 at 20:45
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

q/k (21 bytes)

7h$1e3*(.z.n+0D01)%1D
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Interesting! Though, it shouldn't be necessary to get this from stdin, given Python knows the current time, right? \$\endgroup\$ – Andrea Jul 15 '16 at 23:52
  • \$\begingroup\$ @Andrea It is fixed and works perfectly now. \$\endgroup\$ – R. Kap Jul 16 '16 at 8:50
  • \$\begingroup\$ Hmm, intriguing. Might it be shorter using UNIX time? \$\endgroup\$ – Andrea Jul 17 '16 at 15:11
1
\$\begingroup\$

Javascript, 83 bytes

a=(((36e5+(+new Date()))%864e5)/864e2).toFixed(),alert("00".slice(Math.log10(a))+a)
\$\endgroup\$
0
\$\begingroup\$

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 :)

Try it online!

\$\endgroup\$
  • \$\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 Jul 18 '16 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 Jul 18 '16 at 3:17
  • \$\begingroup\$ Since this adds 3600 after doing the modulo, won't it return the wrong result after 23:00 UTC? \$\endgroup\$ – Andrea Sep 5 '16 at 18:53
  • \$\begingroup\$ @Andrea Maybe indeed. Did you try it after 23h? \$\endgroup\$ – aloisdg Sep 8 '16 at 8:11
  • \$\begingroup\$ @aloisdg I haven't tried your code yet, but it should be clear this will happen without running it. \$\endgroup\$ – Andrea Sep 9 '16 at 3:33
0
\$\begingroup\$

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
\$\endgroup\$

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.