-   Programming (
-   -   How to subtract days (

HyperTrey 06-03-2008 12:49 PM

How to subtract days
I have a script that needs to run weekly ( I have it set using crontab) how I need to pull the dates for on the week. I have for loop set up and have the proper date splits and ready to go. However the trouble I am running into is the beginning of the month. (IE: june 3 - 7 is -4 and can be used)

So I need to figure out a way to look for a week only (subtract 7 day ) and not use any module or the very basic ones. This is a basic server that they don't want added things installed.

How do I solve this issue?

bigearsbilly 06-03-2008 01:08 PM

need more details ...


$ perl -e 'print "@{[scalar localtime(time - (7*86400))]}\n"'

Tue May 27 19:07:42 2008

HyperTrey 06-03-2008 01:20 PM

for example take today:


I want all the listings in the log file from 05/27/2008 to today which is 7 days because of may 31st. I have isolated the 03 and need to know how to use it to come up with the 27 and then look for those files. Simple enough however it is something I have no clue how to do.

colucix 06-03-2008 01:27 PM

In bash, using GNU date you can try something like

for n in {1..7}
  date -d "$n days ago" +"%m/%d/%Y"

I'm not sure if this is what you are looking for.

Hko 06-03-2008 01:30 PM


shell$ date --date='june 3 - 7 days'
Tue May 27 00:00:00 CEST 2008

If you need a different format, say for example: 27-05-2008, no need to use sed, awk or perl. Just specify format string to the 'date' command:


date --date='june 3 - 7 days' +%d-%m-%Y
Or (note it is OK to add a year):

date --date='june 3 2008 - 7 days' +%A
which will just say "Tuesday" since 7 days before third of june this year it will be tuesday..

Hko 06-03-2008 01:40 PM

ah.. Just now I read your second post. Combined with colucix' loop:


for d in {1..7} ; do
    date --date="06/03/2008 - $d days" +%m/%d/%Y


HyperTrey 06-03-2008 01:50 PM

forgot to mention this is in perl

HyperTrey 06-03-2008 03:04 PM

What is the way to do this using Perl with out a module.

IE: set the seven days to an Array so I can use a for loop to compare the dates for ease??

Hko 06-03-2008 03:09 PM

Hmm,... wel. The first post (bigearsbilly) was bout perl...

osor 06-03-2008 05:09 PM

What are you comparing dates to? If all you need is to compare the modification date of a file, Perl has a builtin for that (which, incidentally gives its output in units of days). It’s called “-M”.

For example, to check if a file named “foo” was modified within the last week, you could do:

print "foo modified in last week\n" if -M "foo" < 7;

HyperTrey 06-03-2008 11:54 PM

it is a log CSV file, and I am pulling all the record for a certain week and then the next week it will be pulling the data for that week so the dates will need to be in that seven week span.

chrism01 06-04-2008 12:17 AM

Well, according to this page:, the following modules are part of the Perl Core install:

# Time::Local - efficiently compute time from local and GMT time
# Time::localtime - by-name interface to Perl's built-in localtime() function
# Time::Piece - Object Oriented time objects
# Time::Piece::Seconds - a simple API to convert seconds to other date values
# Time::Seconds - a simple API to convert seconds to other date values
# Time::tm - internal object used by Time::gmtime and Time::localtime

I'm sure you can manage with those.

osor 06-04-2008 02:29 PM


Originally Posted by chrism01 (Post 3174086)
Well, according to this page:, the following modules are part of the Perl Core install

Keep in mind that some of those have only become CORE modules since 5.10, and that many sites have not yet upgraded from 5.8. Regardless, you can easily do what you want in Core Perl (similar to what bigears posted).

bigearsbilly 06-04-2008 04:22 PM

perldoc -f time
perldoc -f localtime
man strftime

well, this is simple enough...


use POSIX qw/strftime/;
local $\ = "\n";

$day = 60 * 60 * 24; # seconds in a day
$now = time();      # seconds since jan 1 1970
$now  -= 2 * $day;

print scalar localtime($now); # perl format default

# make your own format (man strftime will tell you how)
print strftime "%D", localtime $now;

frenchn00b 06-04-2008 04:56 PM

Amazing codes. Linux looks most powerful system ever for coding ;)

All times are GMT -5. The time now is 10:00 PM.