How to modify perl script to operate on localtime

rogersjr

Verified User
Joined
Mar 15, 2004
Messages
25
Location
Australia
I am using a perl script that givess details of what happenned on this day throughout history.

My page changes to the next day at 10 am each day.

Obviously I need to tell the script to update according to local time which is 10 hours ahead of GMT.

As there is a variable called localtime I assume that there is a simple way to modify the script so that it changes days at midnight and not at 10 am. If someone could tell me how to do this my hours of attempting to do so will not have been in vain.

The script is below with what I suspect to be the relevant section in bold.
#!/usr/bin/perl

# Welcome to Today In History v1.1 - June 1998
# Written and produced by [email protected] / http://www.io.com/~rga
# Archives collection by Bruce T. Goldman and reformatted for Today In History.

# By opening and configuring this for your server and application you thereby assume
# any and all responsibility for the use and outcomes of this program

# For configurations, installation, and other information:
# Please consult the <readme.txt> or <readme.doc> file packaged with Today In History

# This is the CGI start up file for Version 1.1
# You can start Version 1.1 with this file or from HTML input on your Web page
# This file displays the current month / day information for Events, Birthdays, and Deaths
# It also displays the search options to submit to the real Version 1.1 CGI file <today1S.pl>
# You can configure which archives you will display in this file

# CONFIGURATIONS

$next_file = "today1S.pl"; # the next file to process
$bkground_image = ""; # background image for the page
$bktable_image = ""; # background for the table heading
$bklogo_image = "/Documents/today/b_main.gif"; # background for the logo table
$logo_image = "/Documents/today/header_this_day.jpg"; # path / image name for logo picture
$logo_width = "754"; # image width
$logo_height = "120"; # image height
$email_address = "[email protected]"; # email address for owner of site
# used to report file opening errors

# turn on / off which archive files will be used / on = 1 / off = 0
$archiveB = "1"; # Birthdays
$archiveD = "1"; # Deaths
$archiveS = "1"; # Special Events

# history / birthday / death archive filenames
@birthday_file = ('jan-B.txt','feb-B.txt','mar-B.txt','apr-B.txt','may-B.txt','jun-B.txt','jul-B.txt','aug-B.txt','sep-B.txt','oct-B.txt','nov-B.txt','dec-B.txt');
@death_file = ('jan-D.txt','feb-D.txt','mar-D.txt','apr-D.txt','may-D.txt','jun-D.txt','jul-D.txt','aug-D.txt','sep-D.txt','oct-D.txt','nov-D.txt','dec-D.txt');
@history_file = ('jan-S.txt','feb-S.txt','mar-S.txt','apr-S.txt','may-S.txt','jun-S.txt','jul-S.txt','aug-S.txt','sep-S.txt','oct-S.txt','nov-S.txt','dec-S.txt');

# END OF CONFIGURATIONS

# get the date
@months = ('January','February','March','April','May','June','July','August','September','October','November','December');
@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
($mday,$mon,$year,$wday) = (localtime(time))[3,4,5,6];
$year += 1900;
$date = "$months[$mon] $mday ";


# get the history data file
if ( $archiveS ) {
unless (open (HXFILE, "$history_file[$mon]") ) {
print "Content-type: text/html\n\n";
print "<html><head><title>Error Opening History File</title>



</head>\n";


print "<body bgcolor=FFFFFF text=black>";
print "<center><font face=\"Arial,Helvetica\" size=2 color=black>";
print "<br><br><br>Error opening the History File <strong>$history_file[$mon]</strong><p>";
print "Program Terminated - Please contact me about this error.<p>";
print "email to <a href=\"mailto:$email_address\">$email_address</a>\n";
print "</center></body></html>";
die
}
@HXdata = <HXFILE>;
close (HXFILE);
}

# get the historical birthday data file
if ( $archiveB ) {
unless (open (BDYFILE, "$birthday_file[$mon]") ) {
print "Content-type: text/html\n\n";
print "<html><head><title>Error Opening Birthday File</title></head>";
print "<body bgcolor=FFFFFF text=black>";
print "<center><font face=\"Arial,Helvetica\" size=2 color=black>";
print "<br><br><br>Error opening the Historical Birthdays File <strong>$birthday_file[$mon]</strong><p>";
print "Program Terminated - Please contact me about this error.<p>";
print "email to <a href=\"mailto:$email_address\">$email_address</a>\n";
print "</center></body></html>";
die
}
@BDYdata = <BDYFILE>;
close (BDYFILE);
}

# get the historical deaths data file
if ( $archiveD ) {
unless (open (DTHFILE, "$death_file[$mon]") ) {
print "Content-type: text/html\n\n";
print "<html><head><title>Error Opening Death File</title></head>";
print "<body bgcolor=FFFFFF text=black>";
print "<center><font face=\"Arial,Helvetica\" size=2 color=black>";
print "<br><br><br>Error opening the Historical Deaths File <strong>$death_file[$mon]</strong><p>";
print "Program Terminated - Please contact me about this error.<p>";
print "email to <a href=\"mailto:$email_address\">$email_address</a>\n";
print "</center></body></html>";
die
}
@DTHdata = <DTHFILE>;
close (DTHFILE);
}

## HTML Output
print "Content-type: text/html\n\n";
print "<html><head><title>Cerberus on this Day In History - $date</title>

<!-- http://www.brothercake.com/dropdown/ -->
<script language=\"javascript\" src=\"../resources/sniffer.js\"></script>
<script language=\"javascript1.2\" src=\"../resources/custom.js\"></script>
<script language=\"javascript1.2\" src=\"../resources/style.js\"></script>




</head>\n";
print "<body background=\"$bkground_image\" bgcolor=C3CADD text=black><BR>";

print "<FORM method=POST action=\"$next_file\">\n";
print "<table border=0 background=\"$bklogo_image\" cellpadding=0 cellspacing=0 width=100%><tr><td>\n";
print "<IMG border=0 SRC=\"$logo_image\" width=$logo_width ";
print " height=$logo_height alt=\"Today In History v1.1\"><br>";

print "<blockquote>";
print "<font face=\"Arial,Helvetica\" size=2><strong>Search: </strong>";
print "<select name=\"themonth\">\n";
print "<option>All Months<option>January<option>February<option>March<option>April";
print "<option>May<option>June<option>July<option>August<option>September";
print "<option>October<option>November<option>December\n</select>&nbsp";

print "<select name=\"theday\">\n";
print "<option>All Days<option>1<option>2<option>3<option>4<option>5<option>6";
print "<option>7<option>8<option>9<option>10<option>11<option>12<option>13";
print "<option>14<option>15<option>16<option>17<option>18<option>19<option>20";
print "<option>21<option>22<option>23<option>24<option>25<option>26<option>27";
print "<option>28<option>29<option>30<option>31\n</select>&nbsp\n";

print "<font face=\"Arial,Helvetica\" size=3>";
print "<input type=text name=\"theyear\" value=\"All Years\" size=7>&nbsp";

print "<font face=\"Arial,Helvetica\" size=2><input type=submit value=\"Go\">";

print" Database contains - April only.";
print "</blockquote>";

print "</td></tr></table>\n";
print "</FORM>";

print "<font face=\"Arial,Helvetica\" size=2 color=black>";
print "<blockquote><CENTER><font size=4 color=blue> <strong>Cerberus on this Day In History for $date.</font></CENTER>";
print "</strong></blockquote><p>";
if ( $archiveS + $archiveB + $archiveD == 0 ) {
print "<blockquote>All archives are disabled from displaying.</blockquote>";
}

if ( $archiveS ) {
print "<blockquote>";
print "<table background=\"$bktable_image\" border=0 cellspacing=0 cellpadding=4 width=90%><tr>";
print "<td><strong><HR color=black><font color=red>Events that occured, or were reported on $date ...</font><HR color=black></strong></td></tr></table>";
print "</blockquote>";

# insert history information
print "<ul>";

# split lines in the history file
foreach $HXeach (@HXdata) {
@HXeachline = split (/::/,$HXeach);

$theyear = substr ($HXeachline[0], 0, 4);
$theday = substr ($HXeachline[0], 6,);

if ($theday == $mday) {
print "<li><strong>$theyear</strong> - $HXeachline[1]";
}
}

print "</ul><p>";
}

if ( $archiveB ) {
print "<blockquote>";
print "<table background=\"$bktable_image\" border=0 cellspacing=0 cellpadding=4 width=90%><tr>";



print "<td><strong><HR color=black><font color=blue>Cerberus Crew Members Born On $date ...</font><HR color=black></strong></td></tr></table>";
print "</blockquote><p> ";

# insert historical birthdays
print "<ul>";

# split lines in the history file
foreach $BDYeach (@BDYdata) {
@BDYeachline = split (/::/,$BDYeach);

$theyear = substr ($BDYeachline[0], 0, 4);
$theday = substr ($BDYeachline[0], 6,);

if ($theday == $mday) {
print "<li><strong>$theyear</strong> - $BDYeachline[1]";
}
}

print "</ul><p>";
}

if ( $archiveD ) {
print "<blockquote>";
print "<table background=\"$bktable_image\" border=0 cellspacing=0 cellpadding=4 width=90%><tr>";
print "<td><strong><HR color=black><font color=green>Cerberus Crew Members who died on $date ...</font><HR color=black></strong></td></tr></table>";
print "</blockquote>";

# insert death information
print "<ul>";

# split lines in the history file
foreach $DTHeach (@DTHdata) {
@DTHeachline = split (/::/,$DTHeach);

$theyear = substr ($DTHeachline[0], 0, 4);
$theday = substr ($DTHeachline[0], 6,);

if ($theday == $mday) {
print "<li><strong>$theyear</strong> - $DTHeachline[1]";
}
}

print "</ul>";
}

# bottom of page
print "<br><br><center><font face=\"Arial,Helvetica\" size=2 color=black>";
print "Explore the InterNet - Create the InterNet<font size=1><br>";
print "Today In History written and produced by <a href=\"http://www.io.com/~rga\"><i>RGA.</i></a><br>";
print "Archives by John Rogers<br>";
print "Email to <a href=\"mailto:rga\@io.com\">rga\@io.com</a></center>";

print "<script language=\"javascript1.2\" src=\"../resources/menu.js\"></script>";

print "</body></html>\n";
 
Hello,

I'm a bit confused because there is no reference to any hours in this script.. only days. If you want the script to run at midnight your time, then just set the cron to run it then ....

However, if I'm missing something.. in general "time" is just an integer holding the number of seconds since 1970 .. so if you want to adjust the timezone by 10 hours, you can try to change it to (not sure about perl.. but that's how I'd do it in c/c++):
Code:
($mday,$mon,$year,$wday) = (localtime(time[b]+(60*60*10))[/b]))[3,4,5,6];
John
 
I think John meant:
Code:
($mday,$mon,$year,$wday) = (localtime(time+60*60*10))[3,4,5,6];
 
Actually the script changes to the next day at 11.30 pm instead of midnight. If I change the formula that calculates local time from

= (localtime(time+60*60*10))[3,4,5,6];

to

= (localtime(time+60*60*9.5))[3,4,5,6];

Then the correction does not occur at midnight.

Is it possible that the script does not respond to .5 hours? or am I writing it incorrectly?


The Relevant Page
 
Hello,

Time needs to be an integer, meaning it cannot have decimals. It's measured in seconds, so 9.5 hours is 570 minutes, so just use:

= (localtime(time+60*570))[3,4,5,6];

John
 
Back
Top