The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calculating sum of fields in a file dsravan Shell Programming and Scripting 4 01-03-2008 09:00 AM
Calculating quantiles in SQL figaro UNIX for Dummies Questions & Answers 3 11-20-2007 01:55 PM
Calculating the average kekanap Shell Programming and Scripting 2 01-26-2007 05:36 AM
calculating size of int apoorvasharma80 High Level Programming 10 11-01-2006 02:44 PM
How to identify week-1 and week-2 csaha Shell Programming and Scripting 1 02-16-2006 01:24 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2001
Registered User
 

Join Date: Oct 2001
Posts: 5
Question Calculating the day of the week

Hi all,

I would like to calculate the day of the week using a supplied date.

i.e. 20011012 = Day 5.

Any ideas?

Many thanks,

ligs
Forum Sponsor
  #2 (permalink)  
Old 10-15-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,462
Here is a ksh script:
Code:
#! /usr/bin/ksh

#              ja fe ma ap ma ju ju ag se oc no de
set -A lasts 0 31 28 31 30 31 30 31 31 30 31 30 31


year=$1
month=$2
day=$3

#
# Get Day of Week of Jan 1
dow1=$(cal 1 $year | sed -n '3s/. //gp')
((dow1=7-dow1))

#
#  is this a leap year?
leap=0
if ((!(year%100))); then
     ((!(year%400))) && leap=1
else
     ((!(year%4))) && leap=1
fi

#
#  set number of days in february
lasts[2]=28
((leap)) && lasts[2]=29

#
#  calculate day of year
i=0
previous=0
while ((i < month)) ; do
     ((previous=previous+lasts[i]))
     ((i=i+1))
done
((doy=previous+day))

#
#  Calculate day of week
((dow = (doy+dow1-1)%7 ))

echo dow = $dow

exit 0
  #3 (permalink)  
Old 10-15-2001
Registered User
 

Join Date: Oct 2001
Posts: 5
Thanks a lot Perderabo

I'll give it a go.


Ligs
  #4 (permalink)  
Old 10-24-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,462
I knew there was an easier way if I just just a little more clever with the math. I finally got it. Hope you're still reading, ligs, this is much faster...
Code:
#! /usr/bin/ksh
year=$1
month=$2
day=$3

#
#  Get date of last day in 1st week of month
dldw1=$(cal $month $year | sed -n '3s/. //gp')

#
#  Calculate day of week
((dow = (day+dldw1+1)%7 ))

echo dow = $dow
exit 0
  #5 (permalink)  
Old 10-27-2001
Registered User
 

Join Date: Oct 2001
Posts: 5
Hi Perderabo,

I'll try this one too.

Your last piece of code worked a treat, thanks a lot.

Ligs
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0