Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Sorting Days of Week Using today () to today () + 6

by Cristoforo (Deacon)
on Jul 10, 2013 at 20:16 UTC ( #1043551=note: print w/ replies, xml ) Need Help??


in reply to Sorting Days of Week Using today () to today () + 6

I think you want today plus the next 6 days with the day of week for each of the days. If so, then the code below will do that.

#!/usr/bin/perl use strict; use warnings; use 5.014; use Date::Simple qw/ today /; my @day_text = qw(Sunday Monday Tuesday Wednesday Thursday Friday Satu +rday); my $dow = today->day_of_week; my @in_order = map {$_ % 7} $dow .. $dow + 6; say "localtime is: " . localtime; say "$_ $day_text[$_]" for @in_order; __END__ *** results of running the program C:\Old_Data\perlp>perl t5.pl localtime is: Wed Jul 10 16:10:33 2013 3 Wednesday 4 Thursday 5 Friday 6 Saturday 0 Sunday 1 Monday 2 Tuesday
Update: The same without need for the @day_text array. Can get the day names right from Date::Simple.
my $today = today; my @names = map { ($today + $_)->strftime("%A")} 0 .. 6; my $dow = today->day_of_week; my @in_order = map {$_ % 7} $dow .. $dow + 6; say "localtime is: " . localtime; say $in_order[$_], ' ', $names[$_] for 0 .. $#in_order;
The hash, %normal_hrs, could then be accessed like this sample code.
my $today = today; my @names = map { ($today + $_)->strftime("%A")} 0 .. 6; my $dow = $today->day_of_week; my @in_order = map {$_ % 7} $dow .. $dow + 6; my $today_dow = shift @in_order; my $today_name = shift @names; # using a hash slice my @today_info = @{ $normal_hrs{$today_dow} }{qw/ openorclosed open cl +ose /}; # write today's info to html # drop down list for my $dow (@in_order) { my $day_name = shift @names; # using a hash slice my @info = @{ $normal_hrs{$dow} }{qw/ openorclosed open close /}; # write this day's info to drop down list }
Update: For a little cleaner code, I put the contents of the 2 arrays, (@dow and @names into an array of hashes to more safely access the data below - less chance of an error.
my $today = today; my @names = map { ($today + $_)->strftime("%A")} 0 .. 6; my $dow = $today->day_of_week; my @dow = map {$_ % 7} $dow .. $dow + 6; my @data; for my $i (0 .. 6) { push @data, {dow => $dow[$i], name => $names[$i]}; } my $date = shift @data; # using a hash slice for today's info # You have access to $date->{dow} and $date->{name} my @today_info = @{ $normal_hrs{ $date->{dow} } }{qw/ openorclosed ope +n close /}; # write today's info to html # drop down list for my $date (@data) { # You have access here to $date->{dow} and $date->{name} # using a hash slice my @info = @{ $normal_hrs{$date->{dow}} }{qw/ openorclosed ope +n close /}; # write this day's info to drop down list }


Comment on Re: Sorting Days of Week Using today () to today () + 6
Select or Download Code
Replies are listed 'Best First'.
Re^2: Sorting Days of Week Using today () to today () + 6
by Cristoforo (Deacon) on Jul 11, 2013 at 13:52 UTC
    I don't think you need that hash %normal_hrs as you have all the info in the returned SQL query. So, I think this version will be simpler without the hash initialization. Also, I made a minor change to the way the dates, (day_of_week and names), were created.
    # -- connect to Voyager db my $dbh = DBI->connect( $voy_dsn,$voy_dbun,$voy_dbpass, { AutoCommit = +> 0, RaiseError => 0, PrintError => 1 } ) || die $DBI::errstr; # -- get normal hours of operations # -- yes, believe it or not there is only one row (there must be a rea +son for this setup but I can't think of one) my $sql = qq(SELECT sunday_open,sunday_openhour,sunday_closehour, monday_open,monday_openhour,monday_closehour, tuesday_open,tuesday_openhour,tuesday_closehour, wednesday_open,wednesday_openhour,wednesday_closehour, thursday_open,thursday_openhour,thursday_closehour, friday_open,friday_openhour,friday_closehour, saturday_open,saturday_openhour,saturday_closehour FROM calendar WHERE calendar_desc = '$voy_cal2use'); my $sth = $dbh->prepare( $sql ); $sth->execute; my @row = $sth->fetchrow->array; my $today = today; my @day = map $today + $_, 0 .. 6; my @data; for my $i (0 .. 6) { push @data, {dow => $day[$i]->day_of_week, name => $day[$i]->strftime("%A")}; } my $date = shift @data; # you have $date->{name} here too my $i = 3 * $date->{dow}; my @today_info = @row[$i .. $i+2]; # write today's info to html # drop down list for my $date (@data) { # You have access here to $date->{dow} and $date->{name} my $i = 3 * $date->{dow}; my @info = @row[$i .. $i+2]; # write this day's info to drop down list }

      Cristoforo,

      Thank you so much for all your help. I am going to keep working on this but I came up against my deadline and had to find another solution. I'm going to respond to my original post with what I ended up doing. I have soooo much to learn!

      Warm regards, HC

        Hans Castorp

        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; }
Re^2: Sorting Days of Week Using today () to today () + 6
by Hans Castorp (Acolyte) on Jul 11, 2013 at 12:31 UTC
    Thank you so much! I will work on this today and let you know how it goes!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043551]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2015-07-30 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls