Solved

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

Posted on 2003-11-14
20
731 Views
Last Modified: 2013-12-26
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
Comment
Question by:wooi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 3
  • 3
  • +3
20 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 9748343
0
 
LVL 1

Expert Comment

by:mbekker
ID: 9748754
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
 
LVL 24

Expert Comment

by:shivsa
ID: 9749969
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!

 

Expert Comment

by:rishisk
ID: 9751489
#!/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
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9757912
date +%a

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

date -d11/2/2003 +%a
0
 

Author Comment

by:wooi
ID: 9761716
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
 

Author Comment

by:wooi
ID: 9761723
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
 

Author Comment

by:wooi
ID: 9761740
bash-2.03$ ./xxx.sh 2004 07 18
Monday


2004 07 18 is a Sunday and not a Monday.  A bug ?
0
 

Author Comment

by:wooi
ID: 9761741
rishisk,

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


2004 07 18 is a Sunday and not a Monday.  A bug ?
0
 

Author Comment

by:wooi
ID: 9761787
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
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9761813
Oh well,
rishisk's soln will work once you sort out the off-by-one bug.
0
 

Author Comment

by:wooi
ID: 9762156
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
 

Author Comment

by:wooi
ID: 9762212
I have increased points from 125 to 200.
Thanks.
0
 

Author Comment

by:wooi
ID: 9762351
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
 
LVL 1

Accepted Solution

by:
mbekker earned 200 total points
ID: 9762615
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
 

Author Comment

by:wooi
ID: 9775902
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
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9775974
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
 

Author Comment

by:wooi
ID: 9835401
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
 

Author Comment

by:wooi
ID: 9835778
mbekker,

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

Thanks
0
 
LVL 1

Expert Comment

by:mbekker
ID: 9836129
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 102
Importing Special Characters in Dynamics GP Through Integration Manager 3 89
sumHeights2  challenge 7 130
wordmultiple challenge 12 156
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question