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

Today the library is closed

); } else { $content = qq(

Today's hours are $hrs{$key}{open} - $hrs{$key}{close}

); } ## 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(); 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 + hour, 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 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//; } ## end check time if # -- return formatted time string return $formatted_time; } ## end format time sub }