Check if a date is a Sunday

Solved
dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   -  
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   -
Hello,

I am in the process of creating a script under Unix that schedules a series of tasks (an at in a for loop).
That's all good; I can manage the dates, the changes of days, months, and years.
No problem there.
I schedule one task per day regardless of the conditions.

What I would like to do (though it's optional) is to add an additional task if the day is a Sunday (since the client is closed, we can put in more tasks without any issues).

So I checked the man page for date, and I found the %a which returns the day of the week.
Cool...
Except that I don't want to know what day it is today.
I want to know what day it will be tomorrow, the day after tomorrow, etc...
In fact, I want to know what day it will be on 121125 or 121215
Is there a simple one-line command that can return that?
I have in variables: $year, $month, $day, $hour, $minute (each two digits; it’s not the current timestamp, but the one I will use for my at command, in the future).
Note that I am on SCO Unix, an old system whose last update dates back to 2003; the bash is a bit specific.

If there's none, or if the command requires extensive scripting, just forget it; it's not vital (unless it amuses you).

Configuration: Anthill inside
SCO/Unix 5.07 /bin/sh

--
Stop failing the Turing test!

5 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    calculate the day of a date with ncal
    $ set 25 12 2012 ; ncal $2 $3 | awk -v d=$1 '{for(n=2; n<=NF;n++)if($n == d){print $1; exit }}' Tu $ set 14 11 2012 ; ncal $2 $3 | awk -v d=$1 '{for(n=2; n<=NF;n++)if($n == d){print $1; exit }}' We $ set 25 12 2012 ; ncal $2 $3 | awk -v d=$1 '{for(n=2; n<=NF;n++)if($n == d){print $1; exit }}' Tu $ set 6 1 2013 ; ncal $2 $3 | awk -v d=$1 '{for(n=2; n<=NF;n++)if($n == d){print $1; exit }}' Su $ 
    1
    1. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
       
      under debian (but it’s useless to me, I need it under unix):
      root@LECBIARRITZ:/home/sesadm# man ncal
      Reformatting ncal(1), please wait...
      root@LECBIARRITZ:/home/sesadm# ncal
      November 2012
      Mo 5 12 19 26
      Tu 6 13 20 27
      We 7 14 21 28
      Th 1 8 15 22 29
      Fr 2 9 16 23 30
      Sa 3 10 17 24
      Su 4 11 18 25

      Under unix:
      # man ncal
      man: ncal not found
      # ncal
      ncal: not found

      Thanks anyway, I didn’t know about ncal
      0
  2. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    hello
    does the date command in SCO have these options?
    $ date +%a -d tomorrow Tue $ date +%a -d "now+4 days" Fri $ date +%a -d "12/25/2012" Tue 
    0
    1. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
       
      For the first two, I know it's not the case, it's Linux (it was a revolution for our scripters when we switched to Linux)
      For the last one, it's not in the man, so it seems unlikely.

      # date +%a -d "12/25/2012"
      Usage: date [-u] [+format]
      date [-u] [-t [[CC]YYMMDDhhmm[.SS] | MMDDhhmm[YY] ]
      # date +%a -d 122512
      Usage: date [-u] [+format]
      date [-u] [-t [[CC]YYMMDDhhmm[.SS] | MMDDhhmm[YY] ]
      # date +%a -d "122512"
      Usage: date [-u] [+format]
      date [-u] [-t [[CC]YYMMDDhhmm[.SS] | MMDDhhmm[YY] ]

      After checking, I do have this -d on a Debian (in the man), but not on SCO Unix.
      Thanks anyway.
      0
  3. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
     
    wacky options that I'd rather avoid.

    store the current date in a variable.
    Modify the current date while my script is running, so I can use date +%a on the now
    reset the current date at the end of the script.
    (technically, it should work)

    Less wacky option but a bit complicated for the added value

    I get the day of the week in decimal with %u when the script launches
    I increment the value as I increment the day of the month. and when I reach +6, I make my case, and reset it to 0/1
    written like this, it doesn't sound too bad, I’ll keep it on hand for lack of better.
    --
    Stop failing the Turing test!
    0
    1. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
       
      another trick, to find out what day of the week it will be in n days.
      I get the date with %u
      I add n
      and do a modulo 7

      Not useful in my case, since I loop by incrementing the days anyway, and I need to check at each increment, so the method above is better.
      0
  4. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
     
    Dubcek: thank you very much, but I think it's dead for me in this case, I think I'm going to resort to scripting and reinvent the wheel.
    However, it helps others, and you have more than answered the question, it's not your fault that I'm working on a system that's from the last century.
    I'm marking it as resolved because you provided two good answers for others.
    If you come up with any other ideas, feel free to send them, you never know.

    --
    Stop failing the Turing test!
    0
  5. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    ```html with cal
    $ set 1 1 2013 ; cal $2 $3 | awk -v d=$1 '/^ / {gsub(" ", "xx ")} NR==2 {split($0,a);next} {for(n=1; n<=NF;n++)if($n == d){print a[n]; exit }}' Tu $ 
    ```
    0
    1. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
       
      I say: "Sir " !!
      it's working.
      You lost me at
      '/^ /'

      I'm trying to figure it out myself, and I'll get back to you if I need explanations.
      0
    2. dna.factory Posted messages 19915 Registration date   Status Moderator Last intervention   1 621
       
      ok, I generally understood
      the part split($0,a);next snip print a[n] is a bit blurry, but I get the general idea.
      it's still a brute force thing...
      0
    3. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
       
      The `gsub` is used to have 7 fields on each line, in order to later use `a[n]`, where `n` is the field number and `a` contains the days:
      $ cal 12 2012| awk '/^ / {gsub(" ", "xx ")} {print}' xx December 2012 Su Mo Tu We Th Fr Sa xx xx xx xx xx xx 1 2 3 4 5 6 7 8 ...
      We could also replace `gsub` with:
      BEGIN{FIELDWIDTHS="3 3 3 3 3 3 3"}
      FIELDWIDTHS is a gawk option, unknown to old awk.
      0