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.
- Q1: Is the right solution?
- Q2: Is this the most efficient way to deal with this requirement?
- Q3: The code I have written to achieve this (see next page) seems way too long and convoluted and I'm sure that there must be a better way, can anyone improve/refine/streamline this code any further?
Notes pertaining to the code segment over the page:
"$this_day" is the current day in "human" form based on the system clock (i.e. "Monday", "Tuesday"...);
"$time" is the current system time in epoch seconds (from Perl's "time" function);
"$this_record2" contains a time-stamp that is the epoch time of the record's creation;
"$record_day" contains the day in "human" form on which the record was created;
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];
}
}
}
In theory, there is no difference between theory and practise. But in practise, there is.
Jonathan M. Hollin
Digital-Word.com