shell计算星期几
时间:2010-04-16 来源:w1_xiao
#!/bin/sh
# SAVE THIS CODE AS dayofweek.sh
#
# Uses Zellers Congruence calculation to use a date and give
# the day of the week that date was.
#
# This function expects 1 Arguments,
# YYYYMMDD
# example:20090520
# then
# Returns a value between 0 and 6 to represent the day of the
# week where 0=Sun,1=Mon,...6=Sat
#
# e.g dayofweek.sh 20090520
#
# This formula is Year 2000 compliant.
# It is not compliant using dates previous to Oct 1752 #
YMD=$1
YEAR=`echo $YMD|cut -c0-4`
MONTH=`echo $YMD|cut -c6-7`
DAY=`echo $YMD|cut -c9-10` #echo $YEAR
#echo $MONTH
#echo $DAY # Adjust Month such that March becomes 1 month of
# year and Jan/Feb become 11/12 of previous year
# ============================================= if [ $MONTH -ge 3 ]
then
MONTH=`expr $MONTH - 2`
else
MONTH=`expr $MONTH + 10`
fi
if [ $MONTH -eq 11 ] || [ $MONTH -eq 12 ] ; then
YEAR=`expr $YEAR - 1`
fi
# ============================================== # Split YEAR into YEAR and CENTURY
# ================================ CENTURY=`expr $YEAR / 100`
YEAR=`expr $YEAR % 100`
# ================================ # Black Magic Time
# ================
#Z=(( 26*$MONTH - 2 ) / 10) + $DAY + $YEAR + ( $YEAR/4 ) + ( $CENTURY/4 ) - (2 * $CENTURY) + 77) % 7
Z=`expr \( $MONTH \* 26 - 2 \) / 10`
Z=`expr $Z + $DAY + $YEAR`
Z=`expr $Z + $YEAR / 4`
Z=`expr $Z + $CENTURY / 4`
Z=`expr $Z - $CENTURY - $CENTURY + 77`
Z=`expr $Z % 7`
if [ $Z -lt 0 ] ; then
Z=`expr $Z + 7`
fi
# ================ if [ $Z -eq 0 ] ; then
echo "星期日";
fi if [ $Z -eq 1 ] ; then
echo "星期一";
fi
if [ $Z -eq 2 ] ; then
echo "星期二";
fi
if [ $Z -eq 3 ] ; then
echo "星期三";
fi
if [ $Z -eq 4 ] ; then
echo "星期四";
fi
if [ $Z -eq 5 ] ; then
echo "星期五";
fi
if [ $Z -eq 6 ] ; then
echo "星期六"; fi
#
# Uses Zellers Congruence calculation to use a date and give
# the day of the week that date was.
#
# This function expects 1 Arguments,
# YYYYMMDD
# example:20090520
# then
# Returns a value between 0 and 6 to represent the day of the
# week where 0=Sun,1=Mon,...6=Sat
#
# e.g dayofweek.sh 20090520
#
# This formula is Year 2000 compliant.
# It is not compliant using dates previous to Oct 1752 #
YMD=$1
YEAR=`echo $YMD|cut -c0-4`
MONTH=`echo $YMD|cut -c6-7`
DAY=`echo $YMD|cut -c9-10` #echo $YEAR
#echo $MONTH
#echo $DAY # Adjust Month such that March becomes 1 month of
# year and Jan/Feb become 11/12 of previous year
# ============================================= if [ $MONTH -ge 3 ]
then
MONTH=`expr $MONTH - 2`
else
MONTH=`expr $MONTH + 10`
fi
if [ $MONTH -eq 11 ] || [ $MONTH -eq 12 ] ; then
YEAR=`expr $YEAR - 1`
fi
# ============================================== # Split YEAR into YEAR and CENTURY
# ================================ CENTURY=`expr $YEAR / 100`
YEAR=`expr $YEAR % 100`
# ================================ # Black Magic Time
# ================
#Z=(( 26*$MONTH - 2 ) / 10) + $DAY + $YEAR + ( $YEAR/4 ) + ( $CENTURY/4 ) - (2 * $CENTURY) + 77) % 7
Z=`expr \( $MONTH \* 26 - 2 \) / 10`
Z=`expr $Z + $DAY + $YEAR`
Z=`expr $Z + $YEAR / 4`
Z=`expr $Z + $CENTURY / 4`
Z=`expr $Z - $CENTURY - $CENTURY + 77`
Z=`expr $Z % 7`
if [ $Z -lt 0 ] ; then
Z=`expr $Z + 7`
fi
# ================ if [ $Z -eq 0 ] ; then
echo "星期日";
fi if [ $Z -eq 1 ] ; then
echo "星期一";
fi
if [ $Z -eq 2 ] ; then
echo "星期二";
fi
if [ $Z -eq 3 ] ; then
echo "星期三";
fi
if [ $Z -eq 4 ] ; then
echo "星期四";
fi
if [ $Z -eq 5 ] ; then
echo "星期五";
fi
if [ $Z -eq 6 ] ; then
echo "星期六"; fi
相关阅读 更多 +