38
\$\begingroup\$

Inspired by We had a unit test once which only failed on Sundays, write a program or function that does nothing but throw an error when it is Sunday, and exit gracefully on any other day.

Rules:

  • No using input or showing output through the usual IO methods, except to print to STDERR or your language's equivalent. You are allowed to print to STDOUT if it's a by-product of your error.
  • A function may return a value on non-Sundays as long as it doesn't print anything
  • Your program may use a Sunday from any timezone, or the local timezone, as long as it is consistent.
  • An error is a something that makes the program terminate abnormally, such as a divide by zero error or using an uninitialised variable. This means that if any code were to be added after the part that errors, it would not be executed on Sunday.
    • You can also use statements that manually create an error, equivalent to Python’s raise.
    • This includes runtime errors, syntax errors and errors while compiling (good luck with that!)
  • On an error there must be some sign that distinguishes it from having no error
  • This is , so the shortest bytecount in each language wins!

I'll have to wait til Sunday to check the answers ;)

\$\endgroup\$
  • 2
    \$\begingroup\$ By "write a program or function that does nothing but throw an error on Sunday, and exit gracefully on any other day", do you mean that whenever it is run on sunday it should fail, or do you mean that there should be at least one possibility it fails a sunday. To make it clearer, if it fails only on sunday at 2pm, but not on sunday 3pm, is it fine ? \$\endgroup\$ – Bromind Jan 5 '18 at 13:53
  • 5
    \$\begingroup\$ This would have been even better if Saturday had been used. You could have called it "Saturday Night Error" and even worked in some adjusted song lyrics to the question. \$\endgroup\$ – Aaron Jan 5 '18 at 18:57
  • 3
    \$\begingroup\$ Sundays? How about failing between midnight and 1am? \$\endgroup\$ – Draco18s Jan 5 '18 at 20:25
  • 1
    \$\begingroup\$ It's not clear from your question what constitutes an error. I'm voting to close until this is remedied. \$\endgroup\$ – Sriotchilism O'Zaic Jan 6 '18 at 16:28
  • 3
    \$\begingroup\$ Not really ... How can we distinguish the output of an error from regular output. Is something like print "error" an error? The added paragraph doesn't really clarify anything. \$\endgroup\$ – Sriotchilism O'Zaic Jan 6 '18 at 18:20

44 Answers 44

1
\$\begingroup\$

Julia 0.6, 32 bytes

Thanks to @Dennis for pointing out < saves a byte over !=.

@assert Dates.dayofweek(now())<7

Try it online!

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

Pure bash BASH (interactive mode + no coreutils required), 17 20 19 bytes

PS1='`((1/\D{%w}))&&:`'

Now only 19 bytes thanks to manatwork's comment below.

Bonus, if you put it in your bashrc it fails every sunday you log in :-) not just when you run it on sundays!

\$\endgroup\$
  • 2
    \$\begingroup\$ Nice, but on non-Sunday days should output nothing. \$\endgroup\$ – manatwork Jan 5 '18 at 21:29
  • \$\begingroup\$ now it outputs nothing on non sundays. \$\endgroup\$ – Ahmed Masud Jan 5 '18 at 21:52
  • 1
    \$\begingroup\$ You can save 1 character using professorfish's tip. \$\endgroup\$ – manatwork Jan 5 '18 at 22:16
  • 1
    \$\begingroup\$ You should mark this as Bash REPL, as this won't fail in a Bash script/full program. \$\endgroup\$ – Dennis Jan 5 '18 at 23:42
1
\$\begingroup\$

Excel, 77 30 bytes

Yes, vastly more golfable.

=IF(WEEKDAY(TODAY())=1,1/0,"")

Simply checks if it's Sunday, and if so, finds the quickest way I know of to error. If not Sunday, returns "",the closest Excel has to not returning anything

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

C# (.NET Core), 39 43 41 bytes

_=>{if(1/(int)DateTime.Now.DayOfWeek<0);}

Try it online!

Thanks to @caird coinheringaahing and @Jo King

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to the site! You can remove the leading newline in your answer to save one byte. Also, it seems as though this is a snippet, rather than a program or a function. If this is the case, you can prepend a _=> to make it a valid submission. \$\endgroup\$ – caird coinheringaahing Jan 8 '18 at 7:58
  • \$\begingroup\$ @caird coinheringaahing - Is it allowed to have a warning like i currently have? \$\endgroup\$ – SirTaphos Jan 8 '18 at 13:04
  • \$\begingroup\$ It should be, but I would ask under the question, so that the author can decide. \$\endgroup\$ – caird coinheringaahing Jan 8 '18 at 16:08
  • \$\begingroup\$ A warning is fine. Could you also use <0 instead of ==-1 to save 2 bytes? \$\endgroup\$ – Jo King Jan 10 '18 at 5:48
  • \$\begingroup\$ Yes I could, good idea, thanks! \$\endgroup\$ – SirTaphos Jan 10 '18 at 9:52
1
\$\begingroup\$

Postgresql, 32

Not sure if you need to add ; at the end for valid answer

SELECT 1/EXTRACT(DOW FROM now())
\$\endgroup\$
1
\$\begingroup\$

Zsh, 15 bytes

${(%):-%(w._.)}

Try it online!

Prompt sequences can be really crazy sometimes...

${(%):-%(w._.)}
${(%)         }   # expand as prompt sequence
     :-           # ${var:-fallback}, but without the var
       %( . .)    # Prompt ternary
         w        # If DoW matches given number (implied 0, which is Sunday)
          ._      # Then substitute _
            .     # Else substitute nothing

Since _ is not a command, it fails on Sunday.

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

SmileBASIC, 20 bytes

DTREAD OUT,,,W
W=W/W

DTREAD outputs the current year, month, day, and day of the week. Sunday is 0.

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

Java 8, 34 bytes

Returns 1 on Mondays, 0 on other days, and throws an ArithmeticException on Sundays

v->1/new java.util.Date().getDay()

Try it online!

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

Batch, 88 bytes

@for /f "skip=1" %%d in ('wmic path win32_localtime get dayofweek')do @set/a1/%%d&exit/b

Tries to divide by zero on Sunday.

Unfortunately neither date nor powershell date work on my PC to give me the day of the week.

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

PowerShell, 24 20 Bytes

1/(date).DayOfWeek>1

Sunday triggers divide by zero same as most of the other solution here. Problem is that on other days that a double gets returned so redirect to nowhere useful trumps that output.

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

Swift 4, 78 bytes

import Foundation;if(Calendar.current.component(.day,from:Date()))==7{exit(1)}

Try it online!

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

AWK, 27 25 23 21 bytes

END{1/strftime("%w")}

Try it online!

Saved 4 bytes thanks to manatwork

Saved 2 bytes thanks to mik

\$\endgroup\$
  • \$\begingroup\$ But strftime() also handles “%w The day of the week as a decimal, range 0 to 6, Sunday being 0.”, according to man 3 strftime. \$\endgroup\$ – manatwork Jan 5 '18 at 17:17
  • \$\begingroup\$ Ok, but now you can remove the outer parenthesis too. \$\endgroup\$ – manatwork Jan 5 '18 at 17:21
  • \$\begingroup\$ Whoops, thats what i get for rushing \$\endgroup\$ – Noskcaj Jan 5 '18 at 17:24
  • \$\begingroup\$ END instead of BEGIN will also work \$\endgroup\$ – mik Jan 6 '18 at 15:30
0
\$\begingroup\$

Octave, 23 22 bytes

(1:6)(weekday(now)-1);

Try it online!

This will try to access an element in 1:6. When the day is Sunday it will try to access the element (1-1)=(0) which will result in an error as MATLAB is 1-indexed.


Original for 23.

assert(weekday(now)~=1)

Try it online!

assert(...) will throw an error when the condition is false. weekday(now) returns the current day of the week where 1 = Sunday. Put the two together, the code will throw an error only on sundays when the condition becomes 1~=1.

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

Clojure, 43 bytes

#(and(=(.getDay(java.util.Date.))7)(/ 1 0))

Try it online!

Uses the fact that and doesn't evaluate the second argument unless necessary. I originally thought I could get away with using the Ratio literal 1/0 to save two bytes, but that unfortunately causes exceptions immediately. It must try to reduce the Ratio right away or something.

(defn sunday-fail []
  (and (= (.getDay (Date.)) 7)
       (/ 1 0)))
\$\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.