in reply to Re: Converting a date to a string and back
in thread Converting a date to a string and back

Yes, it's with localtime
use 5.10.1; use strict; use Time::Local; use Time::localtime; use IO::File; my $ti = localtime; my $tf; my ($logFile, $logFileH); my $basePath = "c:\\las\\dump"; my $processName = "dummy process"; my $startState = "enabled"; my $deaths = 4; my $diff; my $processNameOut; my $startStateOut; my $deathsOut; my $deathFile = "$basePath\\itrsDeaths.dat"; my $deathFileH = new IO::File(">$deathFile") or logit("Can't open fi +le: $deathFile for output.\n"); printf $deathFileH ("%-35s%-9s %-5d %s\n", $processName, $startState +, $deaths,$ti); if ( -f "$deathFile") { open(INIFILE,'<',"$deathFile") or logit( "Could not open death fil +e $deathFile: $!"); my @records = <INIFILE>; foreach my $record (@records) { my ($processNameOut, $startStateOut, $deathsOut, $ti) = $record +=~ /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)?/; } close INIFILE; } else { open(INIFILE,'>',"$deathFile") or logit( "Could not create death f +ile $deathFile: $!"); close INIFILE; } sleep(2); $tf = localtime; $diff = getTimeDifferenceInSeconds($ti,$tf); say "The time difference in seconds is $diff"; sub getTimeDifferenceInSeconds { my ($t1,$t2) = @_; my $sec1 = $t1->sec; my $sec2 = $t2->sec; my $min1 = $t1->min; my $min2 = $t2->min; my $hour1 = $t1->hour; my $hour2 = $t2->hour; my $mday1 = $t1->mday; my $mday2 = $t2->mday; my $month1 = $t1->mon; my $month2 = $t2->mon; my $year1 = $t1->year; my $year2 = $t2->year; my $diff; my $es1 = timegm($sec1,$min1,$hour1,$mday1,$month1-1,$year1); my $es2 = timegm($sec2,$min2,$hour2,$mday2,$month2-1,$year2); $diff = $es2-$es1; return $diff; } sub logit { print @_; # print $logFileH @_ if ($ENV{ENABLE_ITRS_LOGGING}); }

Replies are listed 'Best First'.
Re^3: Converting a date to a string and back
by hippo (Archbishop) on Jul 13, 2016 at 13:02 UTC

    Thanks for this code - that makes it all a good deal clearer. So, if all you are after is the time difference in seconds, why not just use epoch times throughout? You could even save both the epoch time and the localtime-as-string in the file if you want the file times to be easily understood by a non-machine reader.

      Using an epoch time is a good idea. I need localtime for other things (log file suffices), so I will keep that around as you suggested.

      The epoch time in the file could be confusing to the support staff. I guess I could tell them to ignore it.

      Thanks for your help!