Operating System - Linux
1748054 Members
4769 Online
108758 Solutions
New Discussion юеВ

Re: date less 7 days...help?

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

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.
8 REPLIES 8
Steven Schweda
Honored Contributor

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.
James R. Ferguson
Acclaimed Contributor

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...
Bill Hassell
Honored Contributor

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).


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor
Solution

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.
Hein van den Heuvel
Honored Contributor

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";
}



James R. Ferguson
Acclaimed Contributor

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...

Peter Godron
Honored Contributor

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
Coolmar
Esteemed Contributor

Re: date less 7 days...help?

Thanks everyone!