SUMMARY: date command usage

From: Tom Linden <tom_at_kednos.com>
Date: Mon, 25 Feb 2002 06:40:28 -0800

RE: date command usageAs many pointed out a more careful reading of the man
page would have made clear
the each descriptor needs to be preced by a %

However a very useful response is reproduced here.

-----Original Message-----
From: Smith, Lawrie [mailto:Lawrie.Smith_at_capita.co.uk]
Sent: Monday, February 25, 2002 6:31 AM
To: 'Tom Linden'
Subject: RE: date command usage


#===========================================================================
========
s_timestamp() # (c) R. H. Reepe. Returns string
"YYYY-MM-DD HH:MM:SS"
#===========================================================================
========
{
        date "+%Y-%h-%d %H:%M:%S "
}

#===================================================================================
s_now() # (c) R. H. Reepe. Returns string "YYYYMMDDHHMMSS"
#===================================================================================
{
        date +%Y%m%d%H%M%S
}

#===================================================================================
s_date() # (c) R. H. Reepe. Returns string "YYYYMMDD"
#===================================================================================
{
        date +%Y%m%d
}

#===================================================================================
s_time() # (c) R. H. Reepe. Returns string "HHMMSS"
#===================================================================================
{
        date +%H%M%S
}

#===================================================================================
s_interval() # (c) R. H. Reepe. Returns time difference "HHMMSS"
#===================================================================================
# Arg_1 = start_time (Format - see s_time)
# Arg_2 = stop_time (Format - see s_time)
{
        _hour_1=`echo "$1" | cut -c1-2`
        _mins_1=`echo "$1" | cut -c3-4`
        _secs_1=`echo "$1" | cut -c5-6`
        _hour_2=`echo "$2" | cut -c1-2`
        _mins_2=`echo "$2" | cut -c3-4`
        _secs_2=`echo "$2" | cut -c5-6`
        _secs_3=`expr $_secs_2 - $_secs_1`
        if [ $_secs_3 -lt 0 ]
        then
                _secs_3=`expr $_secs_3 + 60`
                _mins_1=`expr $_mins_1 + 1`
        fi
        _mins_3=`expr $_mins_2 - $_mins_1`
        if [ $_mins_3 -lt 0 ]
        then
                _mins_3=`expr $_mins_3 + 60`
                _hour_1=`expr $_hour_1 + 1`
        fi
        _hour_3=`expr $_hour_2 - $_hour_1`
        for _num in $_hour_3 $_mins_3 $_secs_3
        do
                if [ $_num -lt 10 ]
                then
                        /usr/5bin/echo "0$_num\c"
                else
                        /usr/5bin/echo "$_num\c"
                fi
        done
        echo ""
}

#===================================================================================
s_month_length() # (c) R. H. Reepe. Returns number of days in month
#===================================================================================
# Arg_1 = MONTH_NUMBER
{
        if [ $1 -lt 1 ] || [ $1 -gt 12 ]
        then
                echo "Function:s_month_length(): [$1] is not between 1 and 12"
                exit
        fi
        _lengths="312831303130313130313031"
        _cut2=`expr $1 + $1`
        _cut1=`expr $_cut2 - 1`
        echo "$_lengths" | cut -c$_cut1-$_cut2
}

#===================================================================================
s_back_date() # (c) R. H. Reepe. Returns back dated date strings
#===================================================================================
# Arg_1 = DAYS_PREVIOUS
{
        _date_d=`date +%d`
        _date_m=`date +%m`
        _date_y=`date +%y`

        _days=$1

        #-------------------------------------
        # Days Back Size Test #
        #-------------------------------------
        if [ $_days
 -lt $_date_d ]
        then
                _date_d=`expr $_date_d - $_days`
        else
                _days=`expr $_days - $_date_d`
                _date_m=`expr $_date_m - 1`
                _month_length=`s__month_length $_date_m`
                while [ $_days -gt $_month_length ]
                do
                        _days=`expr $_days - $_month_length`
                        _date_m=`expr $_date_m - 1`
                        _month_length=`s__month_length $_date_m`
                done
                _date_d=`expr $_month_length - $_days`
        fi

        #-------------------------------------
        # Date String Padding #
        #-------------------------------------
        if [ $_date_d -lt 10 ]
        then
                _date_d=`expr $_date_d + 0`
                _date_d="0"$_date_d
        fi
        if [ $_date_m -lt 10 ]
        then
                _date_m=`expr $_date_m + 0`
                _date_m="0"$_date_m
        fi
        echo $_date_y $_date_m $_date_d
}



Kind regards

Lawrie Smith
Capita Technical Services
West Malling

email: lawrie.smith_at_capita.co.uk
Tel: 01732 877266
Received on Mon Feb 25 2002 - 14:44:58 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:43 NZDT