Operating System - HP-UX
1844208 Members
2002 Online
110229 Solutions
New Discussion

Re: Retrieve week number and week day for a input date

 
SOLVED
Go to solution
Aji Thomas
Regular Advisor

Retrieve week number and week day for a input date

Hi guys,

I am writing a small shell script, and based on input date, it want to get the weeknum and weekday. One more thing, i want the week day starting from Saturday.

I have
----------------------------------------
%U Week number of the year (Sunday as the first day of the week)

%V Week number of the year (Monday as the first day of the week)

%w Weekday as a one-digit decimal number [0-6 (Sunday- Saturday)].

%W Week number of the year (Monday as the first day of the week)
---------------------------------------------

Please advice,
AJi
13 REPLIES 13
Peter Godron
Honored Contributor

Re: Retrieve week number and week day for a input date

Aji,
I would really recommend A.Clay Stephenson's caljd series of scripts at:

http://hpux.ws/merijn/caljd-2.23.sh

http://hpux.ws/merijn/caljd-2.2.pl

If you really want to work out the day of the week search for Zeller's rule.
Aji Thomas
Regular Advisor

Re: Retrieve week number and week day for a input date

Hi Peter

Please can you verify the url provided. I cant access the webpage.

As i would like to know whether there is any function in UNIX that is going to replace MOD

Thanks for the support,
AJi
Raj D.
Honored Contributor

Re: Retrieve week number and week day for a input date

HI Aji,

Check the Mirror hp-ux page of H Merjin.

http://mirrors.develooper.com/hpux/stats.html

At the bottom check for the scripts.
Hope it helps ,

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Arunvijai_4
Honored Contributor

Re: Retrieve week number and week day for a input date

Hello, Both scripts are attached.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Retrieve week number and week day for a input date

Is ACS script supporting to change first day of the week? I've checked with usage() function, nothing is relavent to change the first day of the week.

Revert when someone find it.

-Muthu
Easy to suggest when don't know about the problem!
Aji Thomas
Regular Advisor

Re: Retrieve week number and week day for a input date

Hi,

Thanks for the reply,
My purpose of the scripts is to include within my backup script to find alternate weeks.
Each week we backup up data to particular FileSystem FS, next week we backup to another FS.
I am trying to develop a script for this fuctionality based on date as input.

I thought of getting it work based on week number, but if there are 53 weeks in a year, the logic does not work.

Any idea???

AJi
Muthukumar_5
Honored Contributor

Re: Retrieve week number and week day for a input date

You may try as,

Schedule script executing with cron tab on specific time on every week.

Try to change to File system selection based on a variable or file availablity.


# touch /etc/filesystemselect
# cat > /etc/filesystemselect
FS=1
In the script,

if [[ -f /etc/filesystemselect ]]
then
. /etc/filesystemselect
if [[ ${FS} -eq 1 ]]
then
# Action with file system one
# change contents of /etc/fsselect
echo "FS=2" > /etc/filesystemselect
elif [[ ${FS} -eq 2 ]]
then
# Action with file system two
# change contents of /etc/fsselect
echo "FS=1" > /etc/filesystemselect
fi
else
echo "ERROR: Which file system to be selected"
exit 1
fi

It will do your requirement.

Note: /etc/filesystemselect must be there.

-Muthu

Easy to suggest when don't know about the problem!
Aji Thomas
Regular Advisor

Re: Retrieve week number and week day for a input date

Hi Muthu,

How will it know which week to do the switch in filesystem.

Say My week starts on SAT-FRI
Take the following example,
17th - 23rd Dec 2005
> Backup should go to /prod1 filesystem.

24th - 30th Dec 2005
> Backup should go to /prod2 filesystem.

31th Dec 2005 - 6th Jan 2006
> Backup should go to /prod1 filesystem.

Please advice,
AJi
john korterman
Honored Contributor
Solution

Re: Retrieve week number and week day for a input date

Hi Aji,

perhaps you can figure out which fs to use with the help of Stephenson's script and a bit of convention: enter the correct path to Stephenson's script in the script below and try executing that with three parameters for month, day, and year, e.g. for 31 DEC 2005:
# ./find_fs.sh 12 31 2005
which should produce
12 31 2005 should use prod1


The convention is that you cannot enter a date before Dec 23 2005.



#/usr/bin/sh

CALJD=./caljd-2.23.shi # enter path to script
CURRENT_JD=$($CALJD $1 $2 $3)
if [ "$#" != 3 ]
then
echo wrong number of parameters
exit 1
fi

# Starting point, first day is 23 dec 2005
DAY_ONE_JD=2453722

# How many days since start
PASSED_DAYS=$(( $CURRENT_JD - $DAY_ONE_JD))

# in chunks of fortnights...
let DAY_NUM=PASSED_DAYS%14
case $DAY_NUM in
0|1|2|3|4|5|6) FS=1;;
7|8|9|10|11|12|13) FS=2;;
esac
echo $1 $2 $3 should use prod$FS

regards,
John K.
it would be nice if you always got a second chance
Aji Thomas
Regular Advisor

Re: Retrieve week number and week day for a input date

Hi John,

Thanks for the best support,

Its works fine.
I forgot to mention one more thing. Every thursday we keep a weekly backup on different FSWEEK1 and FSWEEK2.

For eg
22-dec-2005 - FSWEEK1
29-dec-2005 - FSWEEK2
5-jan-2006 - FSWEEK1

and so on.

Please suggest me script to include this also.

Waiting for your expertize suggestions
AJi
john korterman
Honored Contributor

Re: Retrieve week number and week day for a input date

Hi again,

you can try the script with this extension:

#/usr/bin/sh

FULL=""
CALJD=./caljd-2.23.sh
CURRENT_JD=$($CALJD $1 $2 $3)
if [ "$#" != 3 ]
then
echo wrong number of parameters
exit 1
fi

# Starting point, first day is 23 dec 2005
DAY_ONE_JD=2453722

# How many days since start
PASSED_DAYS=$(( $CURRENT_JD - $DAY_ONE_JD))

# in chunks of fortnights...
let DAY_NUM=PASSED_DAYS%14
case $DAY_NUM in
0|1|2|3|4|5|6) FS=1;;
7|8|9|10|11|12|13) FS=2;;
esac
echo $1 $2 $3 should use prod$FS

case $DAY_NUM in
5) FULL=FSWEEK1;;
12) FULL=FSWEEK2;;
esac
if [ "${FULL}X" != "X" ]
then
# put code for full backup here
echo use full bacup on $FULL
fi


But I suggest you do a lot of testing before letting the above control your backup!

regards,
John K.
it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: Retrieve week number and week day for a input date

Hi Aji:

Here's a simple perl script that returns a three-item string denoting the day-of-the-week (1-7 where 1=Monday), the number of the week within the month (1-5) and the week-number (1-53) of the year.

#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw( Decode_Date_US Day_of_Week Week_of_Year );
die "Usage: $0 MMDDYYYY\n" unless @ARGV;
my ($year, $month, $day);
if (($year, $month, $day) = Decode_Date_US($ARGV[0])) {
print Day_of_Week ($year, $month, $day), " ",
int (($day + Day_of_Week ($year, $month, 1) - 2) / 7) + 1, " ",
scalar Week_of_Year($year, $month, $day), "\n";
}
1;

Run the script by passing a US-format (MMDDYYYY) date either with or without delimiters:

# ./daynums 01/01/2005
6 1 53

...since 01/01/2005 was a Saturday (6) in the first (1) week of January but is reckoned as the LAST (53rd) week of the preceeding year, since no Monday had yet occured in 2005.

# ./daynums 12/28/2005
3 5 52

...a Wednesday (day-3) of the fifth (5) week of the month and the 52nd week of the year.

Regards!

...JRF...
Aji Thomas
Regular Advisor

Re: Retrieve week number and week day for a input date

Solved