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: Intermediate:
Time::Pieces

 



perl_slave
Novice

Dec 9, 2008, 3:19 PM

Post #1 of 11 (955 views)
Time::Pieces Can't Post

I've got this particular piece of code that is not yielding proper results and I'm not quite sure why. It basically finds the time span between given dates in days.

Code
use Time::Piece; 
$addon_year = localtime->year;
$current_date = "12/21";
$stored_date = "10/21";
# Calculate the time difference
# Add on a year to timestamp
$fullcurrent_date = join "/", $addon_year,$current_date;
print "Addon year = $addon_year\n";
print "Full current date: $fullcurrent_date\n";

$format_current = Time::Piece->strptime($fullcurrent_date, "%Y/%m/%d");
print "Format current: $format_current\n";

# Find year for stored time, as it could be different and muck my calculations
&year_calc;
$fullstored_date = join "/", $mod_year,$stored_date;
print "Full stored date: $fullstored_date\n";

$format_stored = Time::Piece->strptime($fullstored_date, "%Y/%m/%d");
print "Format stored: $format_stored\n";
$newdays = $format_current - $format_stored;
print "Total days: $newdays\n";
print int($newdays->days)," days since report date.\n";
print "\n";

# And here is my year_calc subroutine
sub year_calc {
$format_current = Time::Piece->strptime("$current_date", "%m/%d");
$format_stored = Time::Piece->strptime("$stored_date", "%m/%d");
$totaldays = $format_current - $format_stored;

if (abs($totaldays) != $totaldays){
print "The number was negative, assume the year is last year.\n";
$mod_year = $addon_year - 1;
print "Year is: $mod_year\n";
}
else {
print "The number was positive, assume it was this year.\n";
$mod_year = $addon_year;
}
chomp($mod_year);
return $mod_year;

}


BUT, when I run it I get something eitherly different:

Addon year = 2008
Full current date: 2008/12/21
Format current: Sun Dec 21 00:00:00 2008
The number was positive, assume it was this year.
Full stored date: 2008/10/21
Format stored: Tue Oct 21 00:00:00 2008
Total days: -1193961600
-13819 days since report date.

I thought that maybe my strptime() was perhaps not understanding what I was giving it, so added in an additional line in the main script:

qw/$formatcurrent_date/;
qw/$formatstored_date/;

But that didn't really seem to help. Why will it not yield the proper time?


KevinR
Veteran


Dec 9, 2008, 4:33 PM

Post #2 of 11 (952 views)
Re: [perl_slave] Time::Pieces [In reply to] Can't Post

What are you expecting to see when you run your code?
-------------------------------------------------


perl_slave
Novice

Dec 9, 2008, 6:37 PM

Post #3 of 11 (949 views)
Re: [KevinR] Time::Pieces [In reply to] Can't Post

In this case I should get back 61 days according to the dates given. So it would look something like this:

Addon year = 2008
Full current date: 2008/12/21
Format current: Sun Dec 21 00:00:00 2008
The number was positive, assume it was this year.
Full stored date: 2008/10/21
Format stored: Tue Oct 21 00:00:00 2008
61 days since report date


KevinR
Veteran


Dec 9, 2008, 7:00 PM

Post #4 of 11 (948 views)
Re: [perl_slave] Time::Pieces [In reply to] Can't Post

Do this:


Code
sub year_calc {  
$format_current = Time::Piece->strptime("$current_date", "%m/%d");
$format_stored = Time::Piece->strptime("$stored_date", "%m/%d");
print "<$format_current> <$format_stored>\n";


and see what those values are. It looks like you are trying to subtract formatted strings, which I doubt is the correct way to find the difference between the dates.
-------------------------------------------------


perl_slave
Novice

Dec 10, 2008, 7:31 AM

Post #5 of 11 (938 views)
Re: [KevinR] Time::Pieces [In reply to] Can't Post


Code
<Mon Dec 21 00:00:00 1970> <Wed Oct 21 00:00:00 1970>


That's the output I get, the reason its 1970 is because no year was specified, but for that particular check it is not necessary.

As far as subtracting formatted strings - this works just fine:

Code
#!/usr/bin/perl 
use Time::Piece;
$current = Time::Piece->strptime("2008/12/10", "%Y/%m/%d");
$stored = Time::Piece->strptime("2008/12/3", "%Y/%m/%d");
$diff = $current - $stored;
print int($diff->days), " days since $stored\n";


Resulting in -

Code
7 days since Wed Dec  3 00:00:00 2008


I wonder if it has something to do with the way I'm adding the year. Because if I leave off the year in my original script it runs just fine. Perhaps the

$addon_year = localtime->year;

is in a different format originally (i.e its not a straight up string) and I'm passing it to pass it off as a string and it just wasn't designed that way. I'm going to try a few more things and if not going to look for an alternative means to accomplish my goal.


(This post was edited by perl_slave on Dec 10, 2008, 7:33 AM)


KevinR
Veteran


Dec 10, 2008, 11:09 AM

Post #6 of 11 (933 views)
Re: [perl_slave] Time::Pieces [In reply to] Can't Post

I don't see how this code could work:


Code
#!/usr/bin/perl  
use Time::Piece;
$current = Time::Piece->strptime("2008/12/10", "%Y/%m/%d");
$stored = Time::Piece->strptime("2008/12/3", "%Y/%m/%d");
$diff = $current - $stored;
print int($diff->days), " days since $stored\n";


Maybe you have left some of the code out of your post?

You can't subtract strings with perl, perl will give them a value of 0 (zero) when used in numeric context. The Time::Piece module has no 'days' method listed and $diff does not appear to be an object of any class but the return value of the numeric expression.

Or it could be I just do not know a thing about the Time::Piece module so my comments don't apply.

Edit....

OK, after a quick review of the module I see that it has some unusual ways of creating objects so the subtraction of the dates appears to create a Time::Piece object, which in this case is $diff. But I see no "days" method listed in the module.
-------------------------------------------------


FishMonger
Veteran / Moderator

Dec 10, 2008, 11:29 AM

Post #7 of 11 (929 views)
Re: [perl_slave] Time::Pieces [In reply to] Can't Post

Why go through so much effort? Why not use one of these functions from the Data::Calc module?

Delta_Days
Delta_DHMS
Delta_YMD
Delta_YMDHMS


KevinR
Veteran


Dec 10, 2008, 12:20 PM

Post #8 of 11 (927 views)
Re: [FishMonger] Time::Pieces [In reply to] Can't Post


In Reply To
Why go through so much effort? Why not use one of these functions from the Data::Calc module?

Delta_Days
Delta_DHMS
Delta_YMD
Delta_YMDHMS


I agree
-------------------------------------------------


perl_slave
Novice

Dec 10, 2008, 12:52 PM

Post #9 of 11 (923 views)
Re: [KevinR] Time::Pieces [In reply to] Can't Post


In Reply To

In Reply To
Why go through so much effort? Why not use one of these functions from the Data::Calc module?

Delta_Days
Delta_DHMS
Delta_YMD
Delta_YMDHMS


I agree


That's what I did. I ended up scrapping it and using DateTime module to find it. Here is basically how it looks now:

Code
#!/usr/bin/perl 
use Time::Piece;
use Date::Calc qw(Delta_DHMS);

# One is my stored date, and two is the report date

$year1 = localtime->year;
$month1 = 12;
$day1 = 3;
$year2 = 2008;
$month2 = 12;
$day2 = 10;
$hours1 = 11;
$minutes1 = 33;
$hours2 = 9;
$minutes2 = 56;
my $seconds1;
my $seconds2;
($days,$hours,$minutes,$seconds) = Delta_DHMS( $year1, $month1, $day1, $hours1, $minutes1, $seconds1, $year2, $month2, $day2, $hours2, $minutes2, $seconds2);
print "Days: $days\n";
print "Hours: $hours\n";
print "Minutes: $minutes\n";


Works excellent.


FishMonger
Veteran / Moderator

Dec 10, 2008, 2:10 PM

Post #10 of 11 (920 views)
Re: [perl_slave] Time::Pieces [In reply to] Can't Post

Your script is missing following 2 very important items which should be in every Perl script you write.

use strict;
use warnings;

And for readability/maintainability, you should try to keep your line lengths to 80 chars or less or as close as reasonably possible.


Code
my ($days,$hours,$minutes,$seconds) = Delta_DHMS( $year1, $month1, $day1, $hours1, $minutes1, $seconds1, 
$year2, $month2, $day2, $hours2, $minutes2, $seconds2
);



perl_slave
Novice

Dec 10, 2008, 2:40 PM

Post #11 of 11 (916 views)
Re: [FishMonger] Time::Pieces [In reply to] Can't Post

Yeah that was just a crappy demo. Rest assured it is a habit I'm trying to get into.
Smile


(This post was edited by perl_slave on Dec 10, 2008, 2:40 PM)

 
 


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

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