Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue converting epoch time to standard convention in Perl
    primarykey
    data
    text
    <p>In Perl I am trying to convert a variable containing millisecond epoch time variable <code>$adjSendTime</code> to standard convention using the following code:</p> <pre><code>$timeLabel = sprintf("%02d:%02d:%02d", (($adjSendTime/3600000) % 24), (($adjSendTime/60000) % 60), $currentSecond); </code></pre> <p>The issue is that whenever it has reach the 59th second the minute portion of the time will be on minute higher than it should be. The output looks something like</p> <pre><code>11:58:57 11:58:58 11:59:59 11:59:00 11:59:01 </code></pre> <p>The calculation for <code>$adjSendTime</code> is as follows:</p> <pre><code># Determine number of hours between local time and UTC. # This code is needed to compare timestamps in local time with those in epoch time. # This code only works in time zones with current time less than UTC time. @timeArray = gmtime(); $utcHour = $timeArray[2]; @timeArray = localtime(); $localHour = $timeArray[2]; # calculate number of milliseconds between current time and UTC time $utcShift = ($utcHour - $localHour + 24) % 24; $utcShift = $utcShift*60*60*1000; ... if ($field[$i] =~ /^\[29997]=/) { $sendTimestamp = $field[$i]; $sendTimestamp =~ s/.*=(\d*).*/$1/; # convert send time to local time. $adjSendTime = $sendTimestamp - $utcShift; } </code></pre> <p>The calculation for <code>$currentSecond</code> is in two different parts of the code. The first piece occurs the first time through the loop when <code>$FIRST = 1;</code>. <code>$FIRST</code> is never reset to 1 again after this if statement is executed. </p> <pre><code>$second = ($adjSendTime/1000) % 60; if ($FIRST) { $currentSecond = $second; $prevSeqId = $seqId; $FIRST = 0; } </code></pre> <p>and in the subroutine <code>resetCounters</code> where every value that is calculated in the script is reinitialize to 0. This subroutine is called is called at the start of every new second in the input log file.</p> <pre><code>sub resetCounters # ----------------------------------------------------------- # resets all metrics counters # ----------------------------------------------------------- { $tps = 0; $mps = 0; $batch = 0; $maxBatch = 0; $avgBatch = 0; $latency = 0; $latencySum = 0; $maxLatency = 0; $avgLatency = 0; $overThreshold = 0; $percentOver = 0; $zeroLatencyCount = 0; $currentSecond = $second; @latencies = (); } </code></pre> <p>Can anyone help me figure out why Perl would be doing this because when I find the remainder via long hand division I do not have this issue? I realize that I could use datetime to possibly find the hour and minute for this calculation, but I would still be interested in determining what is wrong with my calculation so that I can hopefully avoid issues like this in the future when using <code>%</code> in Perl.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload