For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > Time Conversions.. my sloppy work









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Time Conversions.. my sloppy work
Jimsu

2007-04-26, 6:58 pm

Hi.

I'm new to perl, and am in the process of changing alot of my scripts
from bourne to perl.. I run along and find some awfully sloppy ways to
do stuff.. but this seems to top them all (so far anyway, I challenge
myself to find sloppier code everyday and implement it! haha).

Basically, what happens is that from a database I get two time stamps:

Start Time: 2007-04-16 07:03:38
Finish Time: 2007-04-16 09:14:18

These times are in Pacific Time. I need to do 3 things with these
times:

1. I need to convert them to Central Time
2. I need to find the difference between those two times (2:10:40)
3. I need to compare them to make sure the finished time and start
time are in chronological order.

Now using bourne, I just use date to convert to epoch time.. and to
change the times, and bc to compare the epoch times. But in Perl, I
have to trick myself a little more. I am just curious if anyone can
look at this code and tell me if there is an obviously easier way to
do this. Am I just make this more difficult than it is. It works, so
I've accomplished the tasks, but by golly it sure does seem like a lot
of lines of code just to get there. :)

use Date::Parse;
use HTTP::Date qw(time2iso);

open (DATAFILE, "testing2.out");
open (CSVFILE, ">newout.csv");
print CSVFILE "ALARM RECEIVED,FAULT REPAIRED";
until (eof(DATAFILE)) {
$LINE = <DATAFILE>;
@LIST = split(/,/, $LINE);
$ALMSTAMP = "";
$REPSTAMP = "";
$ALMTIMECT = "N/A";
$REPTIMECT = "N/A";
if ($LIST[0] != "") {
$ALMTIME = ($LIST[0]);
$ALMSTAMP = str2time ("$ALMTIME") + 7200; #Cnvrt to
epoch & add 2 hours
$ALMTIMECT = time2iso($ALMSTAMP); #Cnvrt from epoch to
readable time
}
if ($LIST[1] != "") {
$NTFYTIME = ($LIST[1]);
$NTFYSTAMP = str2time ("$NTFYTIME") + 7200;
$NTFYTIMECT = time2iso($NTFYSTAMP);
}
if ($REPSTAMP != "" && $ALMSTAMP != "") {
$DOWNSECS = ($REPSTAMP - $ALMSTAMP);
}
if ($DOWNSECS != "") {
$HOURS = int($DOWNSECS/(60*60));
$MINS = ($DOWNSECS/60)%60;
$SECS = $DOWNSECS%60;
$DURATION = ("$HOURS:$MINS:$SECS");
}
print CSVFILE "$ALMTIMECT,$NTFYTIMECT\n";
}

close DATAFILE;
close CSVFILE;

Could this be any worse? I know there must bet a better way of doing
this. Tell me about it! :)

Jimsu

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com