Do-over!
OK, well, my boss decided she didn't want the drop-down menu of hours for the rest of the week, so I was left needing a simple "Today's hours are" html statement. I created a new, integrated table with all the library hours in it so the script doesn't have to loop through two tables and work with two hashes. Since I don't need the date or the day, my original problem is not addressed here--sorry about that. I hope the monks' responses will help anyone looking for that answer. Here is the script I ended up with:
#!/usr/bin/perl use strict; use warnings; use DBI; use Date::Simple qw(date today); use Time::Format qw(%time time_format %strftime %manip); use Data::Dumper; use ''::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 = today(); # -- 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') = '$today'); my $sth = $dbh->prepare( $sql ); $sth->execute; # -- fetch all data and create var to refer to my $arrayref_hrs = $sth->fetchall_arrayref( ); # -- store hours for each day of week in here my %hrs = (); # -- loop thru the data from calendar table foreach my $row ( @$arrayref_hrs ) { my ( $libdate,$openhour,$closehour ) = @$row; if ( not defined $openhour ) { $openhour = "closed" } if ( not defined $closehour ) { $closehour = "closed" } $hrs{$libdate} = { open => $openhour, close => $closehour, }; } # -- uncomment this for testing #print Data::Dumper->Dump([%hrs]); $sth->finish; $dbh->disconnect; # -- store html in here my $content; for my $key ( sort keys %hrs ) { # -- use sub to format time if ( exists $hrs{$key} ) { $hrs{$key}{open} = format_time( $hrs{$key}{open} ); $hrs{$key}{close} = format_time( $hrs{$key}{close} ); $hrs{$key}{date} = time_format( 'Month dth', $hrs{$key}{date} ); } ## end open/closed if # -- begin html code # -- write today's hours if ( $hrs{$key}{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 $hrs{$ke +y}{open} - $hrs{$key}{close}</p>); } ## end if # -- uncomment for testing print Data::Dumper->Dump([%hrs]); # -- close the html table and print all html to file open OUTFILE, ">$htmlout" or die $!; print OUTFILE $content .= qq(</tbody></table>); close OUTFILE; # -- Subs start here # -- sub format_time { my $time = shift; my $formatted_time; # -- handle if 'closed' passed (don't do anything else) if ( $time =~m/closed/ ) { $formatted_time = $time; } else { # -- Voyager system indicates morning of "same" open day as 24 + h +our, ex: 1:00AM = 2500 # -- must change to more standard time string if ( $time > 2400 ) { $formatted_time = $time - 2400; } else { $formatted_time = $time; } ## end check time over 2400 if # -- 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//; } ## end check time if # -- return formatted time string return $formatted_time; } ## end format time sub }
My next project is to write a script that will pull the normal hours and exception hours from their respective tables, and populate my new integrated table.
Thank you to MidLifeXis, ww, Cristoforo, and also tobyink, who helped me understand how to store data in a hash. The Monastery rocks!!
In reply to Re: Sorting Days of Week Using today () to today () + 6
by Hans Castorp
in thread Sorting Days of Week Using today () to today () + 6
by Hans Castorp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |