PDA

View Full Version : session-retrieved hash nudging the date


bazz
05-31-2008, 02:55 AM
Hi,

I have a hash stored in a session, which pairs up a date with a price and, it does it correctly.

when I retrieve it, and loop the the key value pair, it shows they are matched ok.

However, when I convert the iso date to a timestamp and then, to a readable date, it changes the dates from 14th, 15th 16th, 17th, to 15th, 16th 17th and 18th. This, obviously, is wrong so I need to fix it.


ok, so I have found that the higlighted code caused the problem. Why does it shunt the dates up by one?


my $bedroom_id=$session->param('bedroom_id');
#print qq( bedroom_id = $bedroom_id ); #ok

my %dates_and_prices = % {$session->param($bedroom_id) };
use Date::Parse;

foreach my $iso_date (sort keys %dates_and_prices)
{
print "$iso_date: $dates_and_prices{$iso_date}<br />"; #ok

my $timestamp = build_timestamp($iso_date);
my $finished_date = date_conversion($timestamp);
my $price_to_two_decimal_places = sprintf("%.2f", $dates_and_prices{$iso_date});


print qq(
<tr><td>$finished_date</td><td>$price_to_two_decimal_places </td></tr>
);

}


sub build_timestamp {
my $date = shift;
use Date::Parse;
# parse and convert date to unix time for start_date
my $timestamp = str2time($date);
$timestamp += 86400;
return $timestamp;
}





sub date_conversion {
my $timestamp = shift;

return strftime( "%a %d %b %Y", localtime($timestamp) );

}


and this is what is stored in the session:



'0000000001' => {
'2008-05-15' => '98',
'2008-05-16' => '93',
'2008-05-14' => '98',
'2008-05-17' => '98'
},




bazz

FishMonger
05-31-2008, 03:11 AM
I think you're jumping through too many hoops for the date formatting.

Are you using the date_conversion() sub elsewhere in the script? If not, then it would make more sense to add the strftime() call to the build_timestamp() sub.

I'm in the middle of something else, but will take a look at this in a little while.

FishMonger
05-31-2008, 03:13 AM
This may help to understand part of the problem.

http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/a1d255b1a09a8877/635ba69fa4092b06?hl=en#635ba69fa4092b06

FishMonger
05-31-2008, 11:20 PM
See if this does what you need. Part of it was borrowed from the usenet postings that I linked to in my prior post.

use POSIX qw/strftime mktime/;
use Date::Parse;

foreach my $iso_date (sort keys %dates_and_prices)
{
print "$iso_date: $dates_and_prices{$iso_date}<br />"; #ok

my $finished_date = date_conversion($iso_date);
my $price_to_two_decimal_places = sprintf("%.2f", $dates_and_prices{$iso_date});


print qq(
<tr><td>$finished_date</td><td>$price_to_two_decimal_places </td></tr>
);

}

sub date_conversion {

my $timestamp = str2time($_[0]);
my @date = localtime($timestamp);

$date[3]++; $date[8] = -1; # add 1 day to the date

$timestamp = mktime(@date);

my %suffix;
$suffix{$_} = 'st' for ('01', 21, 31);
$suffix{$_} = 'nd' for ('02', 22);
$suffix{$_} = 'rd' for ('03', 23);
$suffix{$_} = 'th' for ('04'..'20', 24..30);

my $date = strftime( "%a %d %b %Y", localtime( $timestamp ) );
$date =~ s/(\d+)/$1$suffix{$1}/;
return $date;

}