Re: Playing with date.. (202 Views)
Valued Contributor
Posts: 287
Registered: ‎04-26-2006
Message 1 of 6 (202 Views)

# Playing with date..

Hi Experts,
I have some requirements with date. Based on TODAY and YESTERDAY, need to calculate the date variables. Which will be passed as an argument to the database.

If today is Thursday Oct 25, 2007:

Requirements.
Value Maestro parm and meaning

TODAY = today ( today's date )
YESTERDAY = Yesterday .

\$YESTERDAY < 1 DAY = day before yesterday
\$YESTERDAY < 2 days = 2 days before yesterday (3 days ago)
\$YESTERDAY < 6 DAYS = 6 days before yesterday
\$YESTERDAY < 7 DAYS = 7 days before yesterday
\$YESTERDAY < 20 DAYS = 20 days before yesterday
\$YESTERDAY < 30 DAYS = 30 days before yesterday
\$YESTERDAY < 60 DAYS = 60 days before yesterday
\$YESTERDAY < 90 DAYS = 90 days before yesterday
\$YESTERDAY < NEAREST FIRSTDAY = the nearest first day of month before yesterday
\$TODAY < 1 MONTH < NEAREST FIRSTDAY = the nearest first day of the month before 1 month ago
\$YESTERDAY < 6 MONTH < NEAREST FIRSTDAY = the nearest first day of the month before 6 months ago
\$YESTERDAY < 12 MONTH < NEAREST FIRSTDAY = the nearest first day of the month before 1 year ago
\$YESTERDAY < 24 MONTH < NEAREST FIRSTDAY = the nearest first day of the month before 2 years ago
\$TODAY < NEAREST LASTDAY = the nearest last day of the month before today
\$TODAY < 1 MONTH < NEAREST LASTDAY = the nearest last day of the month before 1 month ago
\$TODAY < 1 MONTH > NEAREST LASTDAY = the nearest last day of the month after 1 month ago
\$YESTERDAY > 14 DAYS = 14 days forward from yesterday

Is there any better way to write a script or function to archive this.
Regards,
Sridhara.
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 6 (202 Views)

# Re: Playing with date..

Hi:

Use Perl;

To obtain dates in the past or the future, you can simply do:

# perl -MPOSIX -le '\$days=shift or die;print strftime "%m/%d/%Y",localtime(time-\$days*86400)' 1

10/30/2007

...or yesterday (one day ago).

The 'strftime' formatting directives can be found in the standard manpages so you can craft your output according to your taste.

The above code can also be made to calculate future dates:

# perl -MPOSIX -le '\$days=shift or die;print strftime "%m/%d/%Y",localtime(time-\$days*86400)' -- -1

11/01/2007

THe double dash says that no options follow and thus the '-1' is treated as the argument we want.

You will find the Perl 'Date::Calc' module quite useful for more complicated calculations.

Regards!

...JRF...

Pass the
Honored Contributor
Posts: 6,593
Registered: ‎05-19-2003
Message 3 of 6 (202 Views)

# Re: Playing with date..

Check out (google) the caljd tool (shell or perl) often referred to in this forum. It can do many (all?) of the above request, but uses a different 'language'.
Is the above specification a fixed given, or can you use something caljd uses?

If i were to have to solve a problem like stated i would grab PERL and check out Time::Timelocal and localtime (sp) between them you can create and work with an array of time elements (month-day, month).
That make it easy to calculate the time corresponding with the first of the month, the firs of last month and so on. Switch to the time in seconds to subtrace days (24*60*60 = 86400 seconds / day)

It's work, but not unpleasant.
Cheers,
Hein.
Trusted Contributor
Posts: 303
Registered: ‎06-27-2006
Message 4 of 6 (202 Views)

# Re: Playing with date..

Hello Sridhara,

Find the attached solution.

Rgds
-NKG-
Acclaimed Contributor
Posts: 26,195
Registered: ‎03-06-2006
Message 5 of 6 (202 Views)

# Re: Playing with date..

>NKG: Find the attached solution.

To go back more than one day, get_one_day_before_specified_date must be called N times.
Some slight improvements can be made:
if [ \$intmonth -eq 01 ]; then
intmonth=12
Not needed since checked in "then" block.

intmonth=`expr \$intmonth - 1`
Can be done with: (( intmonth -= 1 ))

\${AWK} 'NF != 0{ last = \$0 }; END{ print last }' | \${AWK} '{ print \$NF }'
Can be done with one awk:
\${AWK} 'NF != 0 { last = \$NF }; END{ print last }'
Highlighted
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 6 (202 Views)

# Re: Playing with date..

Hi (again):

If you have found a solution, please assign points and close this thread. Thanks!

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.