# We Had A Question Once Which Only Failed On Sundays

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

• 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 ? – Bromind Jan 5 '18 at 13:53
• 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. – Aaron Jan 5 '18 at 18:57
• Sundays? How about failing between midnight and 1am? – Draco18s no longer trusts SE Jan 5 '18 at 20:25
• It's not clear from your question what constitutes an error. I'm voting to close until this is remedied. – Post Rock Garf Hunter Jan 6 '18 at 16:28
• 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. – Post Rock Garf Hunter Jan 6 '18 at 18:20

# Bash + coreutils, 15 14 bytes

date|grep Su


Try it online!

# PHP 7, 12 bytes

1%date("w");


On PHP 7 it throws an exception of type DivisionByZero on Sundays. The same happens if it is interpreted using HHVM.

On PHP 5 it displays a warning (on stderr) on Sundays:

PHP Warning:  Division by zero in Command line code on line 1


On any PHP version, it doesn't display anything on the other days of the week.

Run using the CLI:

php -r '1%date("w");'


Two more bytes can be squeezed by stripping the quotes (1%date(w);) but this triggers a notice (that can be suppressed by properly set error_reporting = E_ALL & ~E_NOTICE in php.ini).

• I believe you must specify (in the title) that this answer is only and only for PHP7+ and for HHVM, since PHP5.6 and lower exit without any problem. Warnings aren't errors and don't stop the execution of the code. If you do 1%date("w");echo "Alive!", it will stop in PHP7+ and HHVM, but not in all the other versions since PHP 4. – Ismael Miguel Jan 6 '18 at 12:55
• @IsmaelMiguel the question classifies any printing to Standard Error as an error, so a warning's good enough in this case. – Please stop being evil Jan 6 '18 at 18:29
• Quoting the question: "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. ". This doesn't happen with a warning. – Ismael Miguel Jan 6 '18 at 18:48
• @IsmaelMiguel the paragraph you are quoting was added to the question less than one hour ago. – axiac Jan 6 '18 at 18:58
• Quoting an older version, the one that was on at the time of the comment that I wrote: "[...] write a program or function that does nothing but throw an error when it is Sunday, and exit gracefully on any other day.". A warning is against this line because PHP will exit gracefully. An error would be a fatal error or a syntax error. Not a warning for dividing by 0. PHP 5.6 and older aren't valid for this challenge. – Ismael Miguel Jan 7 '18 at 0:49

# PHP, 15 bytes

<?@date(w)?:\n;


Assumes default settings.

Output on Sundays

Fatal error: Undefined constant 'n' on line 1


Try it online!

• Notice that this answer requires PHP 5.3+. An alternative could be ||\n instead of ?:\n. – Ismael Miguel Jan 6 '18 at 13:03

# Java 8, 6943 34 bytes

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


-26 bytes thanks to @OlivierGrégoire.
-9 bytes thanks to @Neil.

Explanation:

Try it here.

• v->{...} (unused Void null parameter) is one byte shorter than ()->{...} (no parameter).
• new java.util.Date().getDay() will return 0-6 for Sunday-Saturday, so 1/... will give an java.lang.ArithmeticException: / by zero error if the value is 0, which only happens on Sundays.
• – Olivier Grégoire Jan 5 '18 at 13:33
• @OlivierGrégoire Ah, java.util.Date() does have a method to get the day of the week.. And it's even 0 for Sunday.. Not sure how I missed that. :S – Kevin Cruijssen Jan 5 '18 at 13:42
• Yup, usually the older classes have all the functionality in shorter names. ;-) – Olivier Grégoire Jan 5 '18 at 13:49
• Is this valid for 34 bytes? Try it online! – Neil Jan 5 '18 at 14:58
• @Neil it wasn't valid at the time you posted but became valid less than an hour later. ;-) It was my first idea, though... – Olivier Grégoire Jan 9 '18 at 11:07

# Python 3, 33 bytes

import time
"Su"in time.ctime()>q


Try it online!

# Python 3, 50 bytes

from datetime import*
datetime.now().weekday()>5>q


Try it online!

Saved ~3 bytes thanks to Rod.

• @Rod Why not "Su"in time.ctime()>q (I was editing with this one)? – Mr. Xcoder Jan 5 '18 at 12:42
• I can't quite grasp it. How does it work? – pacholik Jan 5 '18 at 13:00
• @pacholik "Su"in time.ctime() checks if the current day is Sunday. If this is false, the >q part isn't evaluated at all and everything exits smoothly. But if that is true, then the second part of the inequality is evaluated, and since q is not defined, this would throw a NameError. – Mr. Xcoder Jan 5 '18 at 13:01
• @pacholik Edit: I don't think I am wrong. For efficiency purposes, if the first part is falsy, then Python doesn't even bother to evaluate the last part. I'll have to wait until Sunday to test this, though. (I think) Demonstration. – Mr. Xcoder Jan 5 '18 at 13:07
• @pacholik Note that Python chains boolean operators, so that is equivalent to ("Su" in time.ctime()) and (time.ctime() > q). – user202729 Jan 5 '18 at 13:14

# Pyth, 8 7 bytes

 l-6.d9


Try it online!

Explanation

    .d9 # Get the current day of week (0 = Monday, 6 = Sunday)
-6    # Subtract 6 from the day
l      # Try to calculate the log base 2 of the result of the previous operation raising a "ValueError: math domain error" on sundays
# there is an extra space at the start, to supress the output on the other days

• This is still invalid. Your output (1.0 is not generated by the error). The OP said explicitly that they don’t allow that. You can fix that by prepending a single space to your code, though – Mr. Xcoder Jan 5 '18 at 13:37
• But you are not allowed to output to STDOUT unless it is Sunday, and you do output to STDOUT. – Mr. Xcoder Jan 5 '18 at 13:41
• @Mr.Xcoder is correct. On non-Sundays nothing should be outputted – Jo King Jan 5 '18 at 13:42
• @JoKing I totally skipped this rule, fixed now – Rod Jan 5 '18 at 13:44

# Haskell + Data.Dates, 55 bytes

import Data.Dates

### Previous approach with Data.Time: 127 124 bytes

import Data.Time.Clock
import Data.Time.Calendar.WeekDate
c(_,_,d)|d<7=d

bash-4.4$TZ=UTC faketime 2018-01-07 jq -n 'now|strftime("%w")|strptime("%d")|empty' jq: error (at <unknown>): date "0" does not match format "%d"  Note that jq only handles UTC dates. Try it online! ## C, 35, 34 27 bytes f(n){n/=time(0)/86400%7^3;}  -7 bytes with thanks to @MartinEnder and @Dennis Try it online! • Welcome to PPCG! – Martin Ender Jan 5 '18 at 16:28 ## VBA 18 bytes This relies on the inbuilt function date() returning a day number that remainders 1 if divided by 7, so may be OS and/or CPU specific. a=1/(date mod 7-1)  It runs in the VBA project Immediate window. • Welcome to PPCG! In general, an explanation and a link to an online compiler/interpreter is appreciated. – FantaC Jan 7 '18 at 21:51 • @tfbninja - ok updating – JohnRC Jan 7 '18 at 21:51 • and, if necessary, instructions for how to use, e.g. function call or variable – FantaC Jan 7 '18 at 21:53 • Nice golfing, was just about to post this myself after seeing the other answer! – Greedo Jan 8 '18 at 0:12 # R, 31 bytes 30 bytes if(format(Sys.Date(),'%u')>6)a  Try it online! No output on non-Sundays, Error: object 'a' not found on Sundays. format(Sys.Date(),'%u') was the shortest way I could find to get weekday, it outputs a character-class number for day of week, with 7 for Sundays. We can compare to a numeric 7, and if true attempt to use an undefined object. Saved a byte thanks to Giuseppe! • >6 is a byte shorter. – Giuseppe Jan 8 '18 at 1:19 ## Ocaml, 46 bytes open Unix let()=1/(gmtime(time())).tm_wday;()  and in the ocaml REPL, we can achieve better by removing the let and the final :(): $ open Unix;;1/(gmtime(time())).tm_wday;;<CR>


which is 41 bytes (incuding 1 byte for the carriage return).

• Welcome to PPCG! – Laikoni Jan 5 '18 at 14:46
• Is it possible to make this code work on Try it online? – Laikoni Jan 5 '18 at 14:52
• Mmh... the compile command is ocamlopt unix.cmxa <file>, I don't know how to give compile option on tio. I'll investigate this evening – Bromind Jan 5 '18 at 15:04
• The tio uses a (simili) REPL interpreter, so you should use the 2nd possibility. However, it doesn't seem to have the Unix library (or don't allow access to it, for any reason) – Bromind Jan 5 '18 at 15:08

## SAS, 36 bytes

%put %eval(1/(1-%index(&sysday,Su)))


# TI-Basic 84+, 23 bytes

getDate
0/(1-dayOfWk(Ans(1),Ans(2),Ans(3


Needs date & time commands, which are 84+ and higher only.

# MATL, 12 bytes

vZ'8XOs309>)


The error produced on Sundays is:

• Running on Octave:

MATL run-time error: The following Octave error refers to statement number 9:  )
---
array(1): out of bound 0

• Running on Matlab:

MATL run-time error: The following MATLAB error refers to statement number 9:  )
---
Index exceeds matrix dimensions


To invert behaviour (error on any day except on Sundays), add ~ after >.

Try it Online!

### Explanation

This exploits the fact that

• indexing into an empty array with the logical index false is valid (and the result is an empty array, which produces no output); whereas

• indexing with true causes an error because the array lacks a first entry.

Commented code:

v       % Concatenate stack. Gives empty array
Z'      % Push current date and time as a number
8XO     % Convert to date string with format 8: gives 'Mon', 'Tue' etc
s       % Sum of ASCII codes. Gives 310 for 'Sun', and less for others
309>    % Greater than 309? Gives true for 'Sun', false for others
)       % Index into the empty array
% Implicit display. Empty arrays are not displayed (not even newline)


# Q, 20 Bytes

if[1=.z.d mod 7;'e]


.z.d returns the current date. mod does the modulo of the current date, which returns an int. If the date is a sunday, .z.d mod 7 returns 1. If 1=1, (on sunday), and error is raised using the ' operator For brevity the error is just the e character.

• Welcome to PPCG! – Martin Ender Jan 5 '18 at 16:29

# C# (.NET Core), 5554 48 bytes

Try it online!

Saved 1 byte thanks to Shaggy

Saved 5 byte thanks to Emigna

Saved 1 byte thanks to Kevin Cruijssen

_=>{var k=1/(int)System.DateTime.Now.DayOfWeek;}


Lucky that Sunday is indexed 0 in enum or else it would've needed to be (System.DayOfWeek)7

• – Shaggy Jan 5 '18 at 12:56
• Do you get using System for free in C#? If so I think you could do ()=>{var k=1/(int)DateTime.Now.DayOfWeek;} for 42. Otherwise 49 with the explicit System. – Emigna Jan 5 '18 at 13:18
• @Emigna beat me to it; ()=>{var k=1/(int)System.DateTime.Now.DayOfWeek;} is shorter. And you can save one more byte by using an empty unused parameter instead of no parameter (i.e. v->{...} instead of ()->{...}) – Kevin Cruijssen Jan 5 '18 at 13:38
• 40 bytes: ()=>1/(int)System.DateTime.Now.DayOfWeek. Try it here. – Ayb4btu Jan 8 '18 at 23:52

# Groovy, 16 bytes

1/new Date().day


Try it online!

• Welcome to PPCG! – Martin Ender Jan 6 '18 at 14:59
• Thanks! Also, thanks for editing! I was not sure which online playground to choose. Now I know. :) – RandomOfAmbr Jan 6 '18 at 19:23

# C,  68  55 bytes

Thanks to @Ken Y-N for saving 13 bytes!

#import<time.h>
f(n){time(&n);n/=gmtime(&n)->tm_wday;;}


Try it online!

• Drop the intermediate d to get n/=gmtime(&n)->tm_wday; for 54 bytes (but I'm not sure I like all these compiler warnings...) – Ken Y-N Jan 8 '18 at 5:59
• @KenY-N Thanks! – Steadybox Jan 8 '18 at 11:05

# 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

# APL (Dyalog), 23 bytes

⎕CY'dfns'
o←÷7|⌊days⎕TS


Try it online!

# 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


# 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!

# Perl 6,  29  21 bytes

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


Try it

die if now/86400%7+^3


Try it

# Julia 0.5, 28 bytes

Dates.dayofweek(now())<7||~-
`

Try it online!

This does not work with 0.6, but it does with 0.4.