#!/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, RaiseError => 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_date'); 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(

Today the library is closed

); } else { $content = qq(

Today's hours are $href->{open} - $href->{close}

); } $content .= qq(); foreach my $row ( @$arrayref_hrs ) { my $href = process($row); if ( $href->{open} eq 'closed') { $content .= ""; } else { $content .= ""; } ## end if } # -- close the html table and print all html to file $content .= qq(
$href->{date}Closed
$href->{date}$href->{open} - $href->{close}
); 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 format: hh:mm:ss # -- our time string is like 800 or 2300 so we must do some formatting 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; }