Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First and last day of the month 1

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
Date calculations are always a pain!

I'm writing some availability reports.
this_month.ksh is easy - period start is 01/$(date +%m/%y) and period end is $(date +%d/%m/%y)

However, I'm now writing last_month.ksh. Period start is relatively easy - something along the lines of
Code:
typeset -Z 2 LAST_MONTH=$(date +%m)
((LAST_MONTH -= 1))
PERIOD_START=01/$LAST_MONTH/$(date +%y)
Ok, I know I need to allow for January but that's not the problem.

Has anyone any ideas of how to generate period end? The two options I'm looking at are some sort of lookup table (a kop out!) or to use PHV's date calculatore (see FAQ822-4802) and loop backwards one day at a time untill I hit last month i.e. in pseudo code
Code:
OFFSET=24
while TRUE
do
  calc date from offset
  if month = current month - 1 end
  OFFSET += 24
done
but that's a bit clumsy and slow.

Has anyone any ideas?

Ceci n'est pas une signature
Columb Healy
 
I am unsure what defines the 'period end' in each previous month, but if it is the last day (date) of the month, then try (in Korn shell):
lastdayinfeb=$(echo $(cal 02 2007) | awk '{print $NF}')

which should produce the number 28

I hope that helps.

Mike
 
Nice one Mike, that's exactly what I want. Thanks.

Ceci n'est pas une signature
Columb Healy
 
another method, if you have gnu's version of date around...

gdate -d "${MONTH}/1/${YEAR} -1 day
 
It's solved now I know but as a variation on Mike's suggestion you can also do:

cal 02 2007 | xargs | awk '{print $NF}'

Cheers, Chris
 
and with just 2 processes:

cal 2 2007|awk '{if (NF) ld=$NF} END{print ld}'


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top