• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • Last Modified:

Shell Utility/Unix Command that returns day of date (Mon, Tue) when given any date.

I need a Shell Utility/Unix Command that returns day of date (Mon, Tue) when given any date.

Eg: let's day the command that I need is called dayofdate
$ dayofdate 1/1/2002
Tue
$


Any idea ?

Thanks.
0
wooi
Asked:
wooi
  • 11
  • 3
  • 3
  • +3
1 Solution
 
mbekkerCommented:
You could try command 'cal' or the following c-source:

#include <time.h>

main(int argc, char **argv)
{
  time_t  timepointer;
  struct tm *tm;

  tm = localtime((time_t *)0);    /* Init tm structure for mktime() */
  tm->tm_mday=atoi(argv[1]);      /* day-number */
  tm->tm_mon=atoi(argv[2])-1;     /* month-number (0-11) */
  tm->tm_year=atoi(argv[3])-1900; /* year-number (year - 1900) */

  timepointer=mktime(tm);         /* calculate # of seconds */
  printf("%3.3s\n",ctime(&timepointer));
}

Have fun!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rishiskCommented:
#!/bin/sh
if ( test $# -lt 3 )
then
        echo "Usage $0 year month day"
        exit 1
fi

year=$1
month=`expr $2 - 1`
day=$3

if ( test $month -lt 3 )
then
        year=`expr $year - 1`
fi

case $month in
        1) x=0;;
        2) x=3;;
        3) x=2;;
        4) x=5;;
        5) x=0;;
        6) x=3;;
        7) x=5;;
        8) x=1;;
        9) x=4;;
        10) x=6;;
        11) x=2;;
        12) x=4;;
esac

val=`expr \( $year \+ \( $year \/ 4 \) - \( $year \/ 400 \) \+ $x \+ $day \) % 7 `
case $val in
        0) echo "Sunday";;
        1) echo "Monday";;
        2) echo "Tuesday";;
        3) echo "Wednesday";;
        4) echo "Thursday";;
        5) echo "Friday";;
        6) echo "Saturday";;
esac
0
 
guynumber5764Commented:
date +%a

You can feed it a date if you need to...

date -d11/2/2003 +%a
0
 
wooiAuthor Commented:
rishisk,

Yours run into
expr: syntax error
when run with
2004 01 01

bash-2.03$ ./xxx.sh 2004 01 01
expr: syntax error


Why ?

0
 
wooiAuthor Commented:
rishisk,

Yours run into
expr: syntax error
when run with
2004 01 01

bash-2.03$ ./xxx.sh 2004 01 01
expr: syntax error


Why ?

0
 
wooiAuthor Commented:
bash-2.03$ ./xxx.sh 2004 07 18
Monday


2004 07 18 is a Sunday and not a Monday.  A bug ?
0
 
wooiAuthor Commented:
rishisk,

bash-2.03$ ./xxx.sh 2004 07 18
Monday


2004 07 18 is a Sunday and not a Monday.  A bug ?
0
 
wooiAuthor Commented:
guynumber5764,

I am using Solaris Unix.  There's no -d option for the date command.

bash-2.03$ date -d11/2/2003 +%a
date: illegal option -- d
date: illegal option -- 1
date: illegal option -- 1
date: illegal option -- /
date: illegal option -- 2
date: illegal option -- /
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 0
date: illegal option -- 3
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
bash-2.03$
bash-2.03$ date  +%a
Mon
bash-2.03$
0
 
guynumber5764Commented:
Oh well,
rishisk's soln will work once you sort out the off-by-one bug.
0
 
wooiAuthor Commented:
shivsa,

Thanks for the link... very informative and detailed and helpful... however...

I have read through the accepted answer by yuzh... but can't get the correct answer.

Simple... let's say I want the script to display Wed when I key in 01 Jan 2003.  The Julian format for 01 Jan 2003 is 2003001.
2003001 % 7 is 0.  So it is telling me that it is a Friday... "Friday" is wrong.

By the way, yuzh gave the mapping as :
TimeJulianDay mod 7 returns:
1=Sat, 2=Sun, 3=Mon, 4=Tues, 5=Wed, 6=Thurs, 0=Fri.

How ?
Thanks.
0
 
wooiAuthor Commented:
I have increased points from 125 to 200.
Thanks.
0
 
wooiAuthor Commented:
HamdyHassan
Thanks... but I am not looking for a perl solution.

mbekker
Thanks... but I am not looking for a C solution too.

I need a pure shell programming solution.

0
 
mbekkerCommented:
Wooi,

The following works for a kornshell:

# First calculate Julian Day Number from calendar date
day=$1;  month=$2;  year=$3
tmpmonth=$((12 * year + month - 3))
tmpyear=$((tmpmonth / 12))
juliandate=$(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear + \
  tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))

case $(( $juliandate % 7 + 1)) in
   1) echo "Mon" ;;
   2) echo "Tue" ;;
   3) echo "Wed" ;;
   4) echo "Thu" ;;
   5) echo "Fri" ;;
   6) echo "Sat" ;;
   7) echo "Sun" ;;
esac

I've got the calculating part from:

# Date calculations using POSIX shell
# Tapani Tarvainen July 1998, February 2001 (POSIXified)
# This code is in the public domain.

Good luck!
0
 
wooiAuthor Commented:
mbekker,

Your kronshell script works fine ... but I have to put in #!/bin/ksh as the top.  On other shells it does not work.  See my tests :

------------------------
with #!/bin/ksh

Result :
bash-2.03$ ./weekday.sh 18 09 2004
Saturday
bash-2.03$
-------------------------

-------------------------
#!/bin/sh

Result :
bash-2.03$ ./weekday.sh 18 09 2004
./weekday.sh: syntax error at line 6: `tmpmonth=$' unexpected
bash-2.03$
-------------------------

I think this is a small problem.  

Is there a limitation for this script ?  I mean something like "only works for year 1970 - 2099..."

Can you tell me where is the URL for the original code in the public domain ?  I have not been to the "public domain" before.  Thanks.

Will do more tests...
0
 
guynumber5764Commented:
When something is "in the public domain" that means that it is not under copyright and can be used freely.  For example:  "Each time Mickey Mouse has been about to enter the public domain, the US congress has extended the length of the copyright protection term."

I haven't tested rishisk's solution but it looks like a very minor bug that you could easily fix by changing the case statement.
0
 
wooiAuthor Commented:
I wish to close this question (and give points of course)... but there are still some questions unanswered above :

Is there a limitation for this script ?  I mean something like "only works for year 1970 - 2099..."



0
 
wooiAuthor Commented:
mbekker,

Can you lead me to the source of the source code that you posted ?

Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 11
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now