Unix Shell - HOW TO deal with DATES

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > HOW TO deal with DATES





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author HOW TO deal with DATES
klebergreco@yahoo.com.br

2006-10-25, 1:27 pm

Hi All ,

I have one date with format

01-SEP-06

How i can get only the month , i mean SEP??

I need it to pass and discover how many days the month have.

MONTH=SEP

case $MONTH in

[JAN-MAR-APR-MAY-JUL-AUG-OCT-DEC])MONTH31;;
[APR-JUN-SEP-NOV]) MONTH30;;
[FEB]) MONTH28;;

MONTH31() {
do something
}

MONTH30() {

}

MONTH28() {
}

Anyone can help me or another suggestion how to do that?

Thank you!!

Ed Morton

2006-10-25, 1:27 pm

klebergreco@yahoo.com.br wrote:
> Hi All ,
>
> I have one date with format
>
> 01-SEP-06
>
> How i can get only the month , i mean SEP??
>
> I need it to pass and discover how many days the month have.
>
> MONTH=SEP
>
> case $MONTH in
>
> [JAN-MAR-APR-MAY-JUL-AUG-OCT-DEC])MONTH31;;
> [APR-JUN-SEP-NOV]) MONTH30;;
> [FEB]) MONTH28;;


date="01-SEP-06"
month="${date#*-}"
month="${month%-*}"

case $month in
APR|JUN|SEP|NOV ) MONTH30 ;;
FEB ) MONTH28 ;;
* ) MONTH31 ;;
esac

Regards,

Ed.
Chris F.A. Johnson

2006-10-25, 1:27 pm

On 2006-10-25, klebergreco@yahoo.com.br wrote:
> Hi All ,
>
> I have one date with format
>
> 01-SEP-06
>
> How i can get only the month , i mean SEP??


split_date()
{
## Assign defaults when no variable names are given on the command line
sd_1=${2:-SD_YEAR}
sd_2=${3:-SD_MONTH}
sd_3=${4:-SD_DAY}

oldIFS=$IFS ## save current value of field separator
IFS="-/. $TAB$NL" ## new value allows date to be supplied in other formats
set -- $1 ## place the date into the positional parameters
IFS=$oldIFS ## restore IFS
[ $# -lt 3 ] && return 1 ## The date must have 3 fields

## Remove leading zeroes and assign to variables
eval "$sd_1=\"${1#0}\" $sd_2=\"${2#0}\" $sd_3=\"${3#0}\""
}

## Example of usage:
date=01-SEP-06
split_date DAY MONTH YEAR

> I need it to pass and discover how many days the month have.
>
> MONTH=SEP
>
> case $MONTH in
>
> [JAN-MAR-APR-MAY-JUL-AUG-OCT-DEC])MONTH31;;
> [APR-JUN-SEP-NOV]) MONTH30;;
> [FEB]) MONTH28;;


case $MONTH in
JAN|MAR|APR|MAY|JUL|AUG|OCT|DEC]) MONTH31 ;;
APR|JUN|SEP|NOV) MONTH30 ;;
FEB) MONTH28 ;;
esac

> MONTH31() {
> do something
> }
>
> MONTH30() {
>
> }
>
> MONTH28() {
> }
>
> Anyone can help me or another suggestion how to do that?


There is a chapter on dates in my book that contains the following
functions:

split_date - Divide a Date into Day, Month, and Year
is_leap_year - Is There an Extra Day This Year?
days_in_month - How Many Days Hath September?
date2julian - Calculate the Julian Day Number...
julian2date - ...and Convert It Back to Year, Month, and Day
dateshift - Add or Subtract a Number of Days
diffdate - Find the Number of Days Between Two Dates
day_of_week - Find the Day of the Week for Any Date
display_date - Show a Date in Text Format
parse_date - Decipher Various Forms of Date String
valid_date - Where Was I on November 31st?

That chapter is available on line at
<http://cfaj.freeshell.org/shell/ssr...ting-Game.shtml>.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com