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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System date and substraction

Status
Not open for further replies.

TecnoAlpha

Programmer
Aug 4, 2008
3
AE
Hello Guys,

I'm biggener to perl and i want to get the system date and do substraction from it as the below example:

Get the system date say in this format: 04-08-08

Do substraction say -2 so the result will be 02-08-08

So can you help me guys to get the right procedure to get the systm date and be able to do substraction to get the proper result.

Appreciate it.

- TecnoAlpha
 
As other will point out, what have you tried or so far and if this is school work.

You can take a look at perl module Date::Calc.

In a *nix based env
$date = `date +%d-%m-%y`;
or @myDate = localtime() and then get the necessary fields.
 
Hello max1x,

LOL =), well I'm working as a system engineer, and im trying to learn perl by my self, and practice.

I got the system date by:
my $now = localtime time;

but the issue once i tried to call Date::Calc, i got the below:

Can't locate Date/Calc.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./test2.pl line 5.
BEGIN failed--compilation aborted at ./test2.pl line 5.


Looks this API is not included in the Perl library, isn't there an easy way just to subtract from the system date !!!

Thanks.
 
If there was an easy way to do it, no-one would have bothered to write the Date::Calc module.

The system date is held as the number of seconds since 00:00:00 on Jan 1 1970. All localtime does is format it to make it more readable. So to subtract two days you'd need something like
Perl:
my $two_days_ago = localtime(time - (2 * 1440 * 60));
which is a bit clunky.

You can install Date::Calc easily using CPAN if you have root access.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi Steve, got ur point, at least i know how it works now.

I'll manage to try both, appreciate it =)

All regards.
 
If you want to subtract [tt]$nd[/tt] days from present time you just do
[tt]$ago=time-$nd*86400;[/tt]
then you normally transform this in a [tt]localtime[/tt] for displaying purposes,e.g.
[tt]print scalar localtime$ago;[/tt]

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top