Solved

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

Posted on 2003-11-14
20
730 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
abstract class with all non abstract mentods 6 79
zeroMAx challenge 20 111
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 103
Increment column based of a FK 8 31
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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