DarkBlue has asked for the wisdom of the Perl Monks concerning the following question:
I am in the process of writing a "Press Release" CGI application in Perl for a client's website.
The database is of a high volume with new press-releases being issued on a daily basis (Mon - Fri). As such, they have requested that the script offer a means by which the user can select a time frame and press releases issued within that time frame are displayed.
I have dealt with "Today's", "This Month's", "Most Recent" (displayed 20 to a page) and all seem to be working fine.
My client also wants a "This Week's" option - but, it is to be based on a calendar working week, not "display anything published within the last seven days" (which would have been easy). That is, if today is Monday, display only those articles published today, if Tuesday - display today's and yesterday's, if Wednesday - display today's, Monday's and Tuesday's... etc. If today is Sunday, display everything published between the Monday and Friday immediately prior to today.
My solution to this can be described as follows:
If today is Monday then display only those documents published on a Monday with an epoch time less than 604,800 seconds (one day) difference from the current epoch time;
If today is Tuesday then display only those documents published on either a Monday or a Tuesday with an epoch time less than (604,800 * 2) difference from the current epoch time; and so on.
Notes pertaining to the code segment over the page:
A hash called "display_index" is being populated with the reference key and creation time (in epoch seconds) of each matching record.
This whole chunk of code is being executed recursively within a while loop ("while (($key, $value) = each %index) {") as the program evaluates each record in the database (a tied hash ("%index") to a GDBM database).
if ($sort_method eq "week") { $record_day = $this_record[0]; $record_day =~ s/(\w*?) .*/$1/so; if ($this_day eq "Monday") { if (($time - $this_record[2]) <= 604800) and ($record_day eq $this_day +)) { $display_index{$key} = $this_record[2]; } } if ($this_day eq "Tuesday") { if (($time - $this_record[2]) <= (604800 * 2)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Wednesday") { if (($time - $this_record[2]) <= (604800 * 3)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Thursday") { if (($time - $this_record[2]) <= (604800 * 4)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday") or ($record_day eq "Thursday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Friday") { if (($time - $this_record[2]) <= (604800 * 5)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday") or ($record_day eq "Thursday") or ($record_day eq "Fri +day")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Saturday") { if (($time - $this_record[2]) <= (604800 * 6)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday") or ($record_day eq "Thursday") or ($record_day eq "Fri +day") or ($record_day eq "Saturday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Sunday") { if (($time - $this_record[2]) <= (604800 * 7)) { $display_index{$key} = $this_record[2]; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: There's More Than One Way To Do It
by merlyn (Sage) on Apr 04, 2001 at 19:13 UTC | |
|
Re: There's More Than One Way To Do It
by davorg (Chancellor) on Apr 04, 2001 at 19:22 UTC | |
|
(boo) TIMTOWDI - munging dates and times
by boo_radley (Parson) on Apr 04, 2001 at 19:16 UTC | |
|
Re: There's More Than One Way To Do It
by arturo (Vicar) on Apr 04, 2001 at 19:29 UTC | |
|
Re: There's More Than One Way To Do It
by DarkBlue (Sexton) on Apr 04, 2001 at 19:30 UTC |