49
\$\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\$
14
  • 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, 2018 at 13:53
  • 7
    \$\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, 2018 at 18:57
  • 3
    \$\begingroup\$ Sundays? How about failing between midnight and 1am? \$\endgroup\$ Jan 5, 2018 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\$
    – Wheat Wizard
    Jan 6, 2018 at 16:28
  • 4
    \$\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\$
    – Wheat Wizard
    Jan 6, 2018 at 18:20

52 Answers 52

1
2
2
\$\begingroup\$

Groovy, 16 bytes

1/new Date().day

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jan 6, 2018 at 14:59
  • \$\begingroup\$ Thanks! Also, thanks for editing! I was not sure which online playground to choose. Now I know. :) \$\endgroup\$ Jan 6, 2018 at 19:23
2
\$\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\$

R, 40 bytes

stopifnot(weekdays(Sys.Date(),T)!="Sun")

Try it online!

weekdays returns the weekday of the date, with an optional argument abbreviate, which shortens Sunday to Sun, saving a single byte.

stopifnot throws an error if, for each argument, not all are TRUE, and throws an error with a message indicating the first element of which isn't TRUE, so the error is Error: "Sun" is not TRUE

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

Gema, 40 characters

\A=@subst{Su=\@err\{S\}\;*=;@datime}@end

Had to specify an error message, so choose a short one: “S”.

Sample run:

bash-4.4$ faketime 2018-01-06 gema '\A=@subst{Su=\@err\{S\}\;*=;@datime}@end'

bash-4.4$ faketime 2018-01-07 gema '\A=@subst{Su=\@err\{S\}\;*=;@datime}@end'
S
\$\endgroup\$
1
\$\begingroup\$

Funky, 21 bytes

if!os.date"%w"error()

os.date"%w" returns the current day of the week in 0-6 format, where 0 is sunday. Getting the logical not of that is only true when the weekday is 0, so Sunday. Then just a basic if(a){error()} will assure that this program only errors on sunday

Try it online!

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

Perl 6,  29  21 bytes

die if now.Date.day-of-week>6

Try it

die if now/86400%7+^3

Try it

\$\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\$
4
  • 2
    \$\begingroup\$ Nice, but on non-Sunday days should output nothing. \$\endgroup\$
    – manatwork
    Jan 5, 2018 at 21:29
  • \$\begingroup\$ now it outputs nothing on non sundays. \$\endgroup\$ Jan 5, 2018 at 21:52
  • 1
    \$\begingroup\$ You can save 1 character using professorfish's tip. \$\endgroup\$
    – manatwork
    Jan 5, 2018 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, 2018 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\$
5
  • 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\$ Jan 8, 2018 at 7:58
  • \$\begingroup\$ @caird coinheringaahing - Is it allowed to have a warning like i currently have? \$\endgroup\$
    – SirTaphos
    Jan 8, 2018 at 13:04
  • \$\begingroup\$ It should be, but I would ask under the question, so that the author can decide. \$\endgroup\$ Jan 8, 2018 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, 2018 at 5:48
  • \$\begingroup\$ Yes I could, good idea, thanks! \$\endgroup\$
    – SirTaphos
    Jan 10, 2018 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\$

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

05AB1E, 54 52 bytes

žg¦¦D4÷že+•YFóåι•žf<è+žg4Öžf3‹&-ŽPjžg2£4%è++7%iõEëõ}

Try it online!

Explanation

Formula from here.

žg¦¦                                                 # Take the last two digits of the year.
    D                                                # Save for later.
     4÷                                              # Divide by 4, discarding any fraction.
       že+                                           # Add the day of the month.
          •YFóåι•                                    # Push the month's key values.
                 žf                                  # Take the month.
                   <è                                # Find the month's key value.
                     +                               # Add the month's key value.
                      žg4Ö                           # Is this year a leap year?
                          žf3‹                       # Is it January or Feburary?
                              &                      # And the results of both questions.
                               -                     # Subtract 1 for January or February of a leap year.
                                ŽPj                  # Push 6420.
                                   žg2£              # Take the first two digits of the year.
                                       4%è           # Index the thing into the list.
                                          +          # Do step 6.
                                           +         # Add the last two digits of the year.
                                            7%       # Divide by 7 and take the remainder.
                                              i  ë } # Sunday is 1, so it goes in the if.
                                              iõ ë } # Push empty string.
                                              i Eë } # For loop.
                                              i  ëõ} # If not sunday, push empty string and implicit output.
\$\endgroup\$
1
\$\begingroup\$

Pyth, 7 bytes

/1-6.d9

Try it online!

/1-6.d9
    .d9  // Current day of the week, 0 indexed on monday.
  -6     // 6 - day of week (0 if Sunday)
/1       // 1 ÷ ^
\$\endgroup\$
1
\$\begingroup\$

Japt, 7 bytes

çKe ªUí

Test it

çKe ªUí
ç           :U=0 times repeat
 K          :  Current date
  e         :  0-based day of the week
    ª       :  Logical OR with
     Uí     :  The result of running the í method on U, which doesn't exist for numbers
\$\endgroup\$
1
\$\begingroup\$

Python 3, 50 bytes

from datetime import*
1/(date.today().weekday()-6)

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\$
1
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\$
4
  • \$\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, 2018 at 17:17
  • \$\begingroup\$ Ok, but now you can remove the outer parenthesis too. \$\endgroup\$
    – manatwork
    Jan 5, 2018 at 17:21
  • \$\begingroup\$ Whoops, thats what i get for rushing \$\endgroup\$
    – Noskcaj
    Jan 5, 2018 at 17:24
  • 1
    \$\begingroup\$ END instead of BEGIN will also work \$\endgroup\$
    – mik
    Jan 6, 2018 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\$
1
2

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.