date less 7 days...help? (329 Views)
Reply
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 1 of 9 (329 Views)
Accepted Solution

date less 7 days...help?

Hi,

I have to create a script that will name a file with the date - less 7 days. So it should look like the following:

file.20070219 -> file.20070212

I guess this would get very tricky at the beginning of a month as the day and month would change.
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 2 of 9 (329 Views)

Re: date less 7 days...help?

No, it's actually pretty easy to get such a
string. For example:

alp $ write sys$output f$element( 0, " ", f$cvtime( "today-7-")) - "-" - "-"
20070212

Or, for three weeks, to cross a month
boundary:

alp $ write sys$output f$element( 0, " ", f$cvtime( "today-21-")) - "-" - "-"
20070129

Oops. Wrong operating system. Never mind.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 9 (329 Views)

Re: date less 7 days...help?

Hi:

Pass the yyyymmdd you need to this Perl snippet. As written, you will be returned the date seven days ago:

# perl -MDate::Calc=Add_Delta_Days -wle '($y,$m,$d)=unpack "A4A2A2",$ARGV[0];($ny,$nm,$nd)=Add_Delta_Days($y,$m,$d,-7);printf "%4d%02d%02d\n",$ny,$nm,$nd' 20070219

Regards!

...JRF...
Honored Contributor
Bill Hassell
Posts: 14,227
Registered: ‎05-29-2000
Message 4 of 9 (329 Views)

Re: date less 7 days...help?

Yeah, it gets tricky since dates have so many exceptions. Clay has created a great script to handle exactly this type of situation (attached).
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 9 (329 Views)

Re: date less 7 days...help?

For almost any date question the answer is caljd.sh (or caljd.sl):

DTMINUS7=$(caljd.sh -y -s $(caljd.sh -p 7))
echo "7 days ago was ${DTMINUS7}"


Invoke as caljd.sh -u for full usage and many examples.
If it ain't broke, I can fix that.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 6 of 9 (329 Views)

Re: date less 7 days...help?


Never invent your own wheel / date-math

Never even invent your own usage of that math!

It has all been done before. Just google.
I'm sur ethat in the minutes past someone will have replied with Clay's 'date hammer.
If not, goolge for: caljd.sh

Here are two PERL code snippets I might use though. They both figure out the time in seconds, then subtract 7 days worth of seconds:


---- show date math.pl ---
use Time::Local;
my $day = 86400;
($y,$mo,$d) = unpack 'a4a2a2', shift;
$new_time = timelocal(0,0,0,$d,$mo - 1,$y - 1900) - 7*$day;
($s,$m,$h,$d,$m,$y)=localtime($new_time);
printf "%04d%02d%02d\n", $y+1900, $m+1, $d;



--------------------- rename_to_week_ago.pl ----

use Time::Local;
my $day = 86400;
$_ = shift or die "Please provide a file to rename";
if (/(.*)\.(\d{4})(\d\d)(\d\d)$/) {
$new_time = timelocal(0,0,0,$4,$3 - 1,$2 - 1900) - 7*$day;
($s,$m,$h,$d,$m,$y)=localtime($new_time);
$new_name = sprintf "%s.%04d%02d%02d\n", $1, $y+1900, $m+1, $d;
rename $_, $new_name;
} else {
die "filespec not in xxx.yyyymmdd format";
}



Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 9 (329 Views)

Re: date less 7 days...help?

Hi (again):

By the way, given your filename format, simply use the shell parameter substitution to snip off the date to convert:

# FILE=file.20070219;DATE=`echo ${FILE##*.}`

...and then (from my first post) pass the ${DATE} thus derived to the Perl script as its argument:

# perl -MDate::Calc=Add_Delta_Days -wle '($y,$m,$d)=unpack "A4A2A2",$ARGV[0];($ny,$nm,$nd)=Add_Delta_Days($y,$m,$d,-7);printf "%4d%02d%02d\n",$ny,$nm,$nd' ${DATE}

Regards!

...JRF...

Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 8 of 9 (329 Views)

Re: date less 7 days...help?

Coolmar,
get yourself gnu date !
date -d '1 week ago'

For other examples see:
http://www.faqs.org/faqs/hp/hpux-faq/section-168.html
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 9 of 9 (329 Views)

Re: date less 7 days...help?

Thanks everyone!
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.