We Had A Question Once Which Only Failed On Sundays

39

2

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

Jo King

Posted 2018-01-05T12:19:23.073

Reputation: 38 234

Sandbox Post – Jo King – 2018-01-05T12:21:04.237

Can the output be on STDOUT, as long it is generated by an error? – Rod – 2018-01-05T13:26:27.903

@Rod Yes you can – Jo King – 2018-01-05T13:28:47.597

2By "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 – 2018-01-05T13:53:16.167

@Bromind It has to fail at any time on Sunday – Jo King – 2018-01-05T14:03:54.050

Sad :'( I wanted to do something like (time-time_of_first_sunday_after_epoch)%nb_of_second_per_week – Bromind – 2018-01-05T14:08:29.623

If we use a function instead of a full program, may the function return something (which we don't use whatsoever)? To perhaps explain it better: In Java I have to assign a value when I use divide, so instead of ()->{1/sundayValue;} I'll have to use ()->{int x=1/sundayValue;}. This is a lambda with no parameter and no return-type. However, if I change this to a lambda with a return-type integer, I could do ()->1/sundayValue to shorten it. I don't use the output, and it still errors on division by 0, but I'm not sure if this is allowed? – Kevin Cruijssen – 2018-01-05T15:20:15.163

@KevinCruijssen Sure. A function may return a value – Jo King – 2018-01-05T15:36:57.153

5This 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 – 2018-01-05T18:57:53.570

I was confused after seeing some of the answers to this. Does the program/function itself need to produce some output which you are considering an error, or does the logic to determine if something is output need to be triggered by some error in the code? I had assumed the former, but others seem to be assuming the latter. With my interpretation, Dennis' answer could be shortened by 2 characters by removing the back-ticks, otherwise they are necessary. – Aaron – 2018-01-05T19:09:31.163

3

Sundays? How about failing between midnight and 1am?

– Draco18s no longer trusts SE – 2018-01-05T20:25:38.637

1It's not clear from your question what constitutes an error. I'm voting to close until this is remedied. – Post Rock Garf Hunter – 2018-01-06T16:28:05.960

@WheatWizard Better? – Jo King – 2018-01-06T18:18:17.573

3Not 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 – 2018-01-06T18:20:39.253

Answers

22

Bash + coreutils, 15 14 bytes

`date|grep Su`

Try it online!

Dennis

Posted 2018-01-05T12:19:23.073

Reputation: 196 637

18

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

or try it online!

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

axiac

Posted 2018-01-05T12:19:23.073

Reputation: 749

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 – 2018-01-06T12:55:08.560

@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 – 2018-01-06T18:29:10.253

1Quoting 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 – 2018-01-06T18:48:31.853

@IsmaelMiguel the paragraph you are quoting was added to the question less than one hour ago. – axiac – 2018-01-06T18:58:26.980

1Quoting 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 – 2018-01-07T00:49:48.987

9

PHP, 15 bytes

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

Assumes default settings.

Output on Sundays

Fatal error: Undefined constant 'n' on line 1

Try it online!

primo

Posted 2018-01-05T12:19:23.073

Reputation: 30 891

2Notice that this answer requires PHP 5.3+. An alternative could be ||\n instead of ?:\n. – Ismael Miguel – 2018-01-06T13:03:06.753

8

Java 8, 69 43 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.

Kevin Cruijssen

Posted 2018-01-05T12:19:23.073

Reputation: 67 575

1v->{int i=1/new java.util.Date().getDay();} (43 bytes). – Olivier Grégoire – 2018-01-05T13:33:52.803

@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 – 2018-01-05T13:42:45.307

Yup, usually the older classes have all the functionality in shorter names. ;-) – Olivier Grégoire – 2018-01-05T13:49:34.470

1

Is this valid for 34 bytes? Try it online!

– Neil – 2018-01-05T14:58:22.023

@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 – 2018-01-09T11:07:45.190

7

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.

Mr. Xcoder

Posted 2018-01-05T12:19:23.073

Reputation: 39 774

1@Rod Why not "Su"in time.ctime()>q (I was editing with this one)? – Mr. Xcoder – 2018-01-05T12:42:01.403

I can't quite grasp it. How does it work? – pacholik – 2018-01-05T13:00:01.120

@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 – 2018-01-05T13:01:48.950

If this is false, the >q part isn't evaluated at all — that's the thing – why not? – pacholik – 2018-01-05T13:03:08.220

1

@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 – 2018-01-05T13:07:51.597

3@pacholik Note that Python chains boolean operators, so that is equivalent to ("Su" in time.ctime()) and (time.ctime() > q). – user202729 – 2018-01-05T13:14:53.660

You imported everything from datetime, so you can use now() instead of datetime.now(), right? – OldBunny2800 – 2018-01-05T14:44:48.180

@OldBunny2800 Nope, because the method is datetime.datetime.now(), not datetime.now(). – Mr. Xcoder – 2018-01-05T14:52:51.037

Ah, makes sense. – OldBunny2800 – 2018-01-05T14:53:41.130

6

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

Rod

Posted 2018-01-05T12:19:23.073

Reputation: 17 588

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 – 2018-01-05T13:37:18.057

But you are not allowed to output to STDOUT unless it is Sunday, and you do output to STDOUT. – Mr. Xcoder – 2018-01-05T13:41:01.943

@Mr.Xcoder is correct. On non-Sundays nothing should be outputted – Jo King – 2018-01-05T13:42:26.720

@JoKing I totally skipped this rule, fixed now – Rod – 2018-01-05T13:44:30.467

6

Haskell + Data.Dates, 55 bytes

import Data.Dates
succ.dateWeekDay<$>getCurrentDateTime

Try it online!

This uses the fact that Sunday is the last day of the week. dateWeekDay returns the day of the week as a WeekDay type, which is simply defined as

data WeekDay = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday

WeekDay is an instance of Enum, thus we can use succ and pred to get the successor or predecessor of a weekday, e.g. succ Monday yields Tuesday.

However, Sunday is the last enum entry, so calling succ Sunday results in the following error:

fail_on_sunday.hs: succ{WeekDay}: tried to take `succ' of last tag in enumeration
CallStack (from HasCallStack):
  error, called at .\Data\Dates.hs:56:34 in dates-0.2.2.1-6YwCvjmBci55IfacFLnAPe:Data.Dates

Edit 1: Thanks to nimi for -3 bytes!
Edit 2: -11 bytes now that functions are allowed.


Full program: 88 81 74 69 66 bytes

import Data.Dates
main=pure$!succ.dateWeekDay<$>getCurrentDateTime

Try it online!

pure is needed to lift the resulting WeekDay back into the IO Monad. However, Haskell sees that the value is not output in any way by the program, so lazy as it is, the expression is not evaluated, so even on Sundays the program would not fail. This is why $! is needed, which forces the evaluation even if Haskell would normally not evaluate the expression.


Previous approach with Data.Time: 127 124 bytes

import Data.Time.Clock
import Data.Time.Calendar.WeekDate
c(_,_,d)|d<7=d
main=getCurrentTime>>=(pure$!).c.toWeekDate.utctDay

Try it online! These are some impressive imports. Change d<7 to e.g. d/=5 to test failure on a Friday. Fails with the following exception: Non-exhaustive patterns in function c.

Laikoni

Posted 2018-01-05T12:19:23.073

Reputation: 23 676

1main=pure$!succ.dateWeekDay<$>getCurrentDateTime. And, as functions are allowed, you can drop the main=. – nimi – 2018-01-05T15:01:26.030

@nimi Thanks! I'm not sure about the function though, because of the No using input or showing output through the usual IO methods rule. As far as I see, using a function would result in an output for non-Sundays, even though it is wrapped in an IO-action. – Laikoni – 2018-01-05T15:42:44.720

Maybe you're right, but on the other hand a full program has an exit code, which is also a standard method. – nimi – 2018-01-05T15:46:57.287

... the challenge rules now allow functions to return values on non-Sundays as long as they don't print. – nimi – 2018-01-05T15:51:46.000

@nimi Thanks for the notice. – Laikoni – 2018-01-06T20:12:46.520

5

JavaScript, 23 Bytes

Date().slice(1)>'um'&&k

Full program.

The variable k must not be defined.

JavaScript, 20 bytes by Rick Hitchcock

/Su/.test(Date())&&k

JavaScript, 19 bytes by apsillers

Date().match`Su`&&k

l4m2

Posted 2018-01-05T12:19:23.073

Reputation: 5 985

>

  • You forgot to include the _=>; without it this is a snippet which are not allowed by default. 2) This outputs false on every other day when it shouldn't output anything.
  • < – Shaggy – 2018-01-05T12:58:13.970

    6>

  • It's a full program, which is allowed by the OP. 2) If you run it as a program, there's no output
  • < – l4m2 – 2018-01-05T13:00:15.637

    Nice solution. Also 23 Bytes: Date().indexOf("Su")||k – Steven Palinkas – 2018-01-05T15:42:36.803

    3/Su/.test(Date())&&k for 20 bytes. – Rick Hitchcock – 2018-01-05T15:58:45.200

    1new Date version 25 bytes (new Date/864e5%7|0)-3||p – l4m2 – 2018-01-05T16:37:13.533

    @l4m2 ah, someone ported my C version :) – Alnitak – 2018-01-05T16:45:39.427

    3The solution by @RickHitchcock can be made one shorter by template-tag execution on match instead: Date().match`Su`&&k – apsillers – 2018-01-05T20:53:48.913

    5

    VBA / VBScript, 22 20 bytes

    Saved 2 bytes thanks to Taylor Scott.

    a=1/(Weekday(Now)-1)
    

    This should be run in the Immediate Window. Weekday() returns 1 (Sunday) through 7 (Saturday) so this creates a divide by zero error on Sunday. Otherwise, no output.

    Error Message

    Engineer Toast

    Posted 2018-01-05T12:19:23.073

    Reputation: 5 769

    You beat me to an answer by an hour - 19 Bytes: ?1/(Weekday(Now)-1) – Taylor Scott – 2018-01-05T15:23:27.027

    1@TaylorScott I forgot that Now is valid without the () in VBA but I can't use print because I think all output is disallowed unless it errors out. No using input or showing output through the usual IO methods, except to print to STDERR. Still, saved 2 bytes. – Engineer Toast – 2018-01-05T15:40:33.203

    1I had just written these exact 20 bytes as a VBScript solution, and then I thought I'd look to see if there was an existing VB-style language already submitted and here it is. So, this works for VBScript and probably other VB-style languages as well. – None – 2018-01-08T16:51:57.877

    @EngineerToast I think you should mark this as a polyglot with VBScript – Taylor Scott – 2018-01-11T14:25:14.083

    @TaylorScott I haven't done that before. Did I do it right? – Engineer Toast – 2018-01-11T15:32:24.470

    @EngineerToast as far as I know yes :P but for the future, if you end up with more then will fit in a single line its standard to mark it as Polyglot, n Bytes and then indicate what languages it works with below – Taylor Scott – 2018-01-11T15:36:04.247

    5

    05AB1E, 45 44 bytes

    As 05AB1E doesn't have a built in for getting the day of the week, I've used Zeller's Rule to calculate it.

    Prints a newline to stderr in case of a Sunday (observable in the debug view on TIO)

    žežf11+14%Ì13*5÷žgžf3‹-т%D4÷žgт÷©4÷®·(O7%i.ǝ
    

    Try it online!

    Explanation

    The general formula used is
    DoW = d + [(13*(m+1))/5] + y + [y/4] + [c/4] - 2*c
    Where DoW=day of week, d=day, m=month, y=last 2 digits of year, c=century and and expression in brackets ([]) is rounded down.

    Each month used in the formula correspond to a number, where Jan=13,Feb=14,Mar=3,...,Dec=12
    As we have the current month in the more common format Jan=1,...,Dec=12 we convert the month using the formula
    m = (m0 + 11) % 14 + 1

    As a biproduct of March being the first month, January and February belong to the previous year, so the calculation for determining y becomes
    y = (year - (m0 < 3)) % 100

    The final value for DoW we get is an int where 0=Sat,1=Sun,...,6=Fri.
    Now we can explicitly throw an error if the result is true.

    Emigna

    Posted 2018-01-05T12:19:23.073

    Reputation: 50 798

    1105AB1E losing to Java? Everything I know is a lie – Kamil Drakari – 2018-01-05T16:40:17.650

    4

    Ruby, 15 bytes

    1/Time.now.wday
    

    wday will return 0 on Sunday causing a ZeroDivisionError: divided by 0 error. For example: 1/Time.new(2018,1,7).wday.

    Biketire

    Posted 2018-01-05T12:19:23.073

    Reputation: 200

    4

    Perl 5, 13 bytes

    1/(gmtime)[6]
    

    Try it online!

    Ported @biketire's answerj

    removed 3 bytes with @mik's reminder

    Xcali

    Posted 2018-01-05T12:19:23.073

    Reputation: 7 671

    1gmtime instead of localtime will also meet the rules, and is 3 bytes shorter – mik – 2018-01-06T15:28:24.683

    3

    jq, 42 characters

    (39 characters code + 3 characters command line option)

    now|strftime("%w")|strptime("%d")|empty
    

    Just trying a different approach here: parse week day number (0..6) as month day number (1..31).

    Sample run:

    bash-4.4$ TZ=UTC faketime 2018-01-06 jq -n 'now|strftime("%w")|strptime("%d")|empty'
    
    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!

    manatwork

    Posted 2018-01-05T12:19:23.073

    Reputation: 17 865

    3

    C, 35, 34 27 bytes

    f(n){n/=time(0)/86400%7^3;}
    

    -7 bytes with thanks to @MartinEnder and @Dennis

    Try it online!

    Alnitak

    Posted 2018-01-05T12:19:23.073

    Reputation: 124

    3Welcome to PPCG! – Martin Ender – 2018-01-05T16:28:47.567

    3

    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!

    Gregor Thomas

    Posted 2018-01-05T12:19:23.073

    Reputation: 271

    >6 is a byte shorter. – Giuseppe – 2018-01-08T01:19:16.463

    3

    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.

    JohnRC

    Posted 2018-01-05T12:19:23.073

    Reputation: 141

    2Welcome to PPCG! In general, an explanation and a link to an online compiler/interpreter is appreciated. – FantaC – 2018-01-07T21:51:13.953

    @tfbninja - ok updating – JohnRC – 2018-01-07T21:51:51.747

    and, if necessary, instructions for how to use, e.g. function call or variable – FantaC – 2018-01-07T21:53:06.750

    Nice golfing, was just about to post this myself after seeing the other answer! – Greedo – 2018-01-08T00:12:16.433

    2

    C# (.NET Core), 55 54 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

    LiefdeWen

    Posted 2018-01-05T12:19:23.073

    Reputation: 3 381

    154 bytes? – Shaggy – 2018-01-05T12:56:29.267

    3Do 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 – 2018-01-05T13:18:57.130

    3

    @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 – 2018-01-05T13:38:19.413

    1

    40 bytes: ()=>1/(int)System.DateTime.Now.DayOfWeek. Try it here.

    – Ayb4btu – 2018-01-08T23:52:58.517

    2

    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!

    Steadybox

    Posted 2018-01-05T12:19:23.073

    Reputation: 15 798

    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 – 2018-01-08T05:59:25.880

    @KenY-N Thanks! – Steadybox – 2018-01-08T11:05:05.157

    2

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

    Bromind

    Posted 2018-01-05T12:19:23.073

    Reputation: 289

    1Welcome to PPCG! – Laikoni – 2018-01-05T14:46:46.777

    1

    Is it possible to make this code work on Try it online?

    – Laikoni – 2018-01-05T14:52:45.587

    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 – 2018-01-05T15:04:57.577

    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 – 2018-01-05T15:08:37.087

    2

    SAS, 36 bytes

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

    J_Lard

    Posted 2018-01-05T12:19:23.073

    Reputation: 351

    2

    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.

    Timtech

    Posted 2018-01-05T12:19:23.073

    Reputation: 12 038

    2

    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)
    

    Luis Mendo

    Posted 2018-01-05T12:19:23.073

    Reputation: 87 464

    2

    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.

    tkg

    Posted 2018-01-05T12:19:23.073

    Reputation: 21

    3Welcome to PPCG! – Martin Ender – 2018-01-05T16:29:19.913

    2

    Groovy, 16 bytes

    1/new Date().day
    

    Try it online!

    RandomOfAmbr

    Posted 2018-01-05T12:19:23.073

    Reputation: 21

    Welcome to PPCG! – Martin Ender – 2018-01-06T14:59:10.757

    Thanks! Also, thanks for editing! I was not sure which online playground to choose. Now I know. :) – RandomOfAmbr – 2018-01-06T19:23:27.457

    1

    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

    Giuseppe

    Posted 2018-01-05T12:19:23.073

    Reputation: 21 077

    Got it down to 31 with a slightly different approach

    – Gregor Thomas – 2018-01-07T20:55:17.263

    1

    APL (Dyalog), 23 bytes

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

    Try it online!

    Uriel

    Posted 2018-01-05T12:19:23.073

    Reputation: 11 708

    1

    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
    

    manatwork

    Posted 2018-01-05T12:19:23.073

    Reputation: 17 865

    1

    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!

    ATaco

    Posted 2018-01-05T12:19:23.073

    Reputation: 7 898

    1

    Julia 0.6, 32 bytes

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

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

    Try it online!

    gggg

    Posted 2018-01-05T12:19:23.073

    Reputation: 1 715

    1

    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.

    Rɪᴋᴇʀ

    Posted 2018-01-05T12:19:23.073

    Reputation: 7 410

    1

    Perl 6,  29  21 bytes

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

    Try it

    die if now/86400%7+^3
    

    Try it

    Brad Gilbert b2gills

    Posted 2018-01-05T12:19:23.073

    Reputation: 12 713

    1

    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!

    Ahmed Masud

    Posted 2018-01-05T12:19:23.073

    Reputation: 111

    2Nice, but on non-Sunday days should output nothing. – manatwork – 2018-01-05T21:29:26.753

    now it outputs nothing on non sundays. – Ahmed Masud – 2018-01-05T21:52:26.490

    1

    You can save 1 character using professorfish's tip.

    – manatwork – 2018-01-05T22:16:28.713

    1You should mark this as Bash REPL, as this won't fail in a Bash script/full program. – Dennis – 2018-01-05T23:42:29.297

    1

    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

    Scott

    Posted 2018-01-05T12:19:23.073

    Reputation: 171

    1

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

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

    Try it online!

    Thanks to @caird coinheringaahing and @Jo King

    SirTaphos

    Posted 2018-01-05T12:19:23.073

    Reputation: 11

    2Welcome 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. – caird coinheringaahing – 2018-01-08T07:58:30.930

    @caird coinheringaahing - Is it allowed to have a warning like i currently have? – SirTaphos – 2018-01-08T13:04:30.873

    It should be, but I would ask under the question, so that the author can decide. – caird coinheringaahing – 2018-01-08T16:08:05.090

    A warning is fine. Could you also use <0 instead of ==-1 to save 2 bytes? – Jo King – 2018-01-10T05:48:52.973

    Yes I could, good idea, thanks! – SirTaphos – 2018-01-10T09:52:03.787

    1

    Postgresql, 32

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

    SELECT 1/EXTRACT(DOW FROM now())
    

    dwana

    Posted 2018-01-05T12:19:23.073

    Reputation: 531

    1

    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.

    GammaFunction

    Posted 2018-01-05T12:19:23.073

    Reputation: 2 838

    1

    SmileBASIC, 20 bytes

    DTREAD OUT,,,W
    W=W/W
    

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

    12Me21

    Posted 2018-01-05T12:19:23.073

    Reputation: 6 110

    1

    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!

    Benjamin Urquhart

    Posted 2018-01-05T12:19:23.073

    Reputation: 1 262

    0

    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.

    Neil

    Posted 2018-01-05T12:19:23.073

    Reputation: 95 035

    0

    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.

    Matt

    Posted 2018-01-05T12:19:23.073

    Reputation: 1 075

    0

    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

    Noskcaj

    Posted 2018-01-05T12:19:23.073

    Reputation: 421

    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. – manatwork – 2018-01-05T17:17:58.853

    Ok, but now you can remove the outer parenthesis too. – manatwork – 2018-01-05T17:21:57.753

    Whoops, thats what i get for rushing – Noskcaj – 2018-01-05T17:24:40.973

    END instead of BEGIN will also work – mik – 2018-01-06T15:30:26.937

    0

    Swift 4, 78 bytes

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

    Try it online!

    Zeke Snider

    Posted 2018-01-05T12:19:23.073

    Reputation: 121

    70 bytes – Herman L – 2018-01-09T06:54:47.020

    0

    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.

    Tom Carpenter

    Posted 2018-01-05T12:19:23.073

    Reputation: 3 990

    0

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

    Carcigenicate

    Posted 2018-01-05T12:19:23.073

    Reputation: 3 295