in reply to Re^3: Sorting Days of Week Using today () to today () + 6
in thread Sorting Days of Week Using today () to today () + 6
I'm afraid I've muddied up the waters with any (badly) supplied ideas. Sorry if I've only added to the confusion :-(
I'll look over your latest post.
Update: I found an extra right brace at the very end of your program that should be removed.
} ## end format time sub } <- remove this brace
Maybe the code below will help. I don't know if hash slices is the answer or not - depends on how they're used. I eliminated the %this_wk hash and the %hrs and added a subroutine, process. Its difficult to tell if this may work as I don't have a database to test it against.
#!/usr/bin/perl use strict; use warnings; use DBI; use Date::Simple qw(today); use Time::Format qw(%time time_format %strftime %manip); use Data::Dumper; use Skidmore::Utils qw(setOracleEnv); # -- call sub to set Oracle env so script can run as cronjob &setOracleEnv; # -- define outfile my $htmlout = "/www/includes/lib_hours2.html"; #connection info for webprod my ($dsn,$dbun,$dbpass) = ( 'dbi:Oracle:host=;sid=;port=','','' ); # -- define today my $today_date = today; my $end_date = $today_date + 6; # -- connect to Webprod db my $dbh = DBI->connect( $dsn,$dbun,$dbpass, { AutoCommit => 0, RaiseEr +ror => 0, PrintError => 1 } ) || die $DBI::errstr; # -- query to get hours for the upcoming week my $sql = qq(SELECT TO_CHAR(libdate,'YYYY-MM-DD'),openhour,closehour FROM hours WHERE TO_CHAR(libdate,'YYYY-MM-DD') BETWEEN '$today_date' AND '$end_da +te'); my $sth = $dbh->prepare( $sql ); $sth->execute; # -- fetch all data and create var to refer to my $arrayref_hrs = $sth->fetchall_arrayref( ); $sth->finish; $dbh->disconnect; my $today = shift @$arrayref_hrs; my $href = process($today); my $content; # -- write today's hours if ( $href->{open} eq 'closed') { $content = qq(<p class="scribsmalltext">Today the library is close +d</p>); } else { $content = qq(<p class="scribsmalltext">Today's hours are $href->{ +open} - $href->{close}</p>); } $content .= qq(<table style="height:80px;" class="scribsmalltext" widt +h="200" border="0"><tbody>); foreach my $row ( @$arrayref_hrs ) { my $href = process($row); if ( $href->{open} eq 'closed') { $content .= "<tr><td>$href->{date}</td><td>Closed</td></tr>"; + } else { $content .= "<tr><td>$href->{date}</td><td>$href->{open} - $hr +ef->{close}</td></tr>"; } ## end if } # -- close the html table and print all html to file $content .= qq(</tbody></table>); open OUTFILE, ">$htmlout" or die $!; print OUTFILE $content; close OUTFILE; # -- Subs start here # -- sub format_time { my $time = shift; # if $time > 2400, this will 'normalize' it to less than 2400 # otherwise if $time < 2400, it will remain the same my $formatted_time = $time % 2400; # -- time function (from Time module) needs time string in this fo +rmat: hh:mm:ss # -- our time string is like 800 or 2300 so we must do some format +ting to it # -- first, pad with zero if only 3 character $formatted_time = sprintf( "%04s", $formatted_time ); # -- second, add two zeros to end of time string - seconds $formatted_time = $formatted_time .'00'; # -- third, add ":" separators $formatted_time =~ s/(\d\d)(\d\d)(\d\d)/$1:$2:$3/; # -- now use time function for final formatting $formatted_time = time_format('H:mm am', "$formatted_time"); # -- for some reason "0" showing in times less than 10 even though + format for time format looks correct # -- let's remove it now $formatted_time =~ s/^0//; # -- return formatted time string return $formatted_time; } ## end format time sub sub process { my $row = shift; my %hash; my ( $libdate,$open_hr,$close_hr ) = @$row; if (defined $open_hr) { $hash{open} = format_time($open_hr); } else { $hash{open} = 'closed'; } if (defined $close_hr) { $hash{close} = format_time($close_hr); } $hash{date} = time_format( 'Day Month dth', $libdate ); return \%hash; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Sorting Days of Week Using today () to today () + 6
by Hans Castorp (Sexton) on Jul 18, 2013 at 12:48 UTC | |
|
Re^5: Sorting Days of Week Using today () to today () + 6
by Hans Castorp (Sexton) on Jul 25, 2013 at 17:58 UTC | |
by Cristoforo (Curate) on Jul 25, 2013 at 20:48 UTC |