CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Date/Time Calculation

 



mauto
User

Sep 3, 2002, 8:23 AM

Post #1 of 13 (1934 views)
Date/Time Calculation Can't Post

Is it possible to take the date (09/03/2002) and minus 3 days from it to derive (08/31/2002). Is there a date function that will allow me to do this ?


fashimpaur
User / Moderator

Sep 3, 2002, 9:43 AM

Post #2 of 13 (1932 views)
Re: [mauto] Date/Time Calculation [In reply to] Can't Post

Mauto,

See the module Date::Calc. It should do what you need. Particularly the method Add_Delta_YMD().

It is available from CPAN and also from ActiveState.

Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


davorg
Thaumaturge / Moderator

Sep 4, 2002, 1:03 AM

Post #3 of 13 (1927 views)
Re: [mauto] Date/Time Calculation [In reply to] Can't Post

I recommend the Time::Piece module for this kind of problem.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mauto
User

Sep 4, 2002, 2:00 AM

Post #4 of 13 (1925 views)
Re: [davorg] Date/Time Calculation [In reply to] Can't Post

I am trying the following code but get an error: can't locate time piece in time/piece.pm @INC


Code
use Time::Piece; 

my $t = localtime;
print "Time is $t\n";
print "Year is ", $t->year, "\n";


I take it this is due to the fact that I need to I install the time piece module. How do I do this ?


davorg
Thaumaturge / Moderator

Sep 4, 2002, 2:06 AM

Post #5 of 13 (1924 views)
Re: [mauto] Date/Time Calculation [In reply to] Can't Post


In Reply To
I take it this is due to the fact that I need to I install the time piece module. How do I do this ?


The same way that you install any other module from CPAN.

Download it from the URL I gave you and then follow the instructions in the README (or in perldoc perlmodinstall).

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


(This post was edited by davorg on Sep 4, 2002, 2:07 AM)


Paul
Enthusiast

Sep 4, 2002, 2:26 AM

Post #6 of 13 (1921 views)
Re: [davorg] Date/Time Calculation [In reply to] Can't Post

If he doesn't know how to install a perl module I doubt he's a sys admin and so is unlikely to have the permissions to install via ssh/telnet. I think it will be a case of uploading the files manually, locally.


mauto
User

Sep 4, 2002, 2:43 AM

Post #7 of 13 (1919 views)
Re: [davorg] Date/Time Calculation [In reply to] Can't Post

I have downloaded the file and unpacked it. The following files were uncompressed:

changes
makefile.pl
manifest
manifest.skip
piece.pm
piece.xs
readme
seconds.pm

I am running Active State Perl on Win2000. I'm not sure if I need to compile the perl module or do I just copy the files into c:\perl\lib directory ?


davorg
Thaumaturge / Moderator

Sep 4, 2002, 3:35 AM

Post #8 of 13 (1915 views)
Re: [mauto] Date/Time Calculation [In reply to] Can't Post

The perlmodinstall document that I linked to before has a section entitled "#If you're running ActivePerl (Win95/98/2K/NT/XP, Linux, Solaris)" which you'll probably find interesting.

I know very little about ActivePerl, but I know it has its own Perl Package Manager (ppm) which makes it easy to install CPAN modules. Try typing:


Code
ppm


and then


Code
install Time-Piece


Your Activestate distribution will have come with its own documentation which will be far more useful to you than my half-remembered memories :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


(This post was edited by davorg on Sep 4, 2002, 6:39 AM)


mauto
User

Sep 4, 2002, 5:57 AM

Post #9 of 13 (1907 views)
Re: [davorg] Date/Time Calculation [In reply to] Can't Post

Managed to work out how to install the Time-Piece module. Just run the following from a dos shell:

ppm install http://www.activestate.com/PPMPackages/5.6plus/Time-Piece.ppd

However, I cannot seem to work out how to do the date calculation. I am obtaining the date as follows:


Code
    use Time::Piece; 

my $t = localtime;
print $t->mdy("/"), "\n";


Which returns:

Code
09/04/2002


But how do I minus 4 days from this to get 08/31/2002 ?


fashimpaur
User / Moderator

Sep 4, 2002, 6:28 AM

Post #10 of 13 (1904 views)
Re: [mauto] Date/Time Calculation [In reply to] Can't Post

Try using Date::Calc::Add_Delta_YMD(). Time::Piece is primarily to get portions
of the localtime() function and simple comparisons. Date::Calc has a method
Add_Delta_YMD() where in it is used in the following manner:


Code
   
use Date::Calc qw( Add_Delta_YMD Today Date_to_Text );

($thisyear, $thismonth, $thisday) = Today();
($year, $month, $day) = Add_Delta_YMD($thisyear, $thismonth, $thisday, 0, 0, -3);

$threedaysago = Date_to_Text($year, $month, $day);
print $threedaysago;


Hope that helps. I made the suggestion earlier but you seemed to think that
Time::Piece was a better solution and disregarded this. You can also do a
PPM install of Date-Calc from activestate to install this PM as well.

Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


davorg
Thaumaturge / Moderator

Sep 4, 2002, 6:38 AM

Post #11 of 13 (1903 views)
Re: [mauto] Date/Time Calculation [In reply to] Can't Post

Did you look at the documentation for Time::Piece? Did you see the section entitled Date Calculations?

Code
#!/usr/bin/perl -w 

use strict;
use Time::Piece;
use Time::Seconds;

my $t = localtime;

$t -= 4 * ONE_DAY;

print $t->mdy("/"), "\n";


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Sep 4, 2002, 11:14 AM

Post #12 of 13 (1897 views)
Re: [davorg] Date/Time Calculation [In reply to] Can't Post

You guys like to make things difficult....minus 4 days:


Code
my $t = localtime(time - 345600);



davorg
Thaumaturge / Moderator

Sep 5, 2002, 2:12 AM

Post #13 of 13 (1888 views)
Re: [RedRum] Date/Time Calculation [In reply to] Can't Post

Well, that's true. But I think that part of the way thru this discussion we've dropped one of the original requirements - which is that we need to deal with abitrary dates in a format like '08/30/2002'. With Time::Piece, that change is simple.

Code
#!/usr/bin/perl -w 

use strict;
use Time::Piece;
use Time::Seconds;

my $t = Time::Piece->strptime('09/03/2002', '%m/%d/%Y');

$t -= 4 * ONE_DAY;

print $t->mdy("/"), "\n";


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives