<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Perl - calculate time difference in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612240#M104403</link>
    <description>I recommend a time tested tool, created by A. Clay, our fearless point leader.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://hpux.ws/merijn/caljd-2.2.pl" target="_blank"&gt;http://hpux.ws/merijn/caljd-2.2.pl&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;SEP</description>
    <pubDate>Thu, 25 Aug 2005 15:27:40 GMT</pubDate>
    <dc:creator>Steven E. Protter</dc:creator>
    <dc:date>2005-08-25T15:27:40Z</dc:date>
    <item>
      <title>Perl - calculate time difference</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612239#M104402</link>
      <description>I have to find the time difference between 2 date time of the format 08252005193248 and 08252005193249. The last 6 digits value is that of the time.&lt;BR /&gt;&lt;BR /&gt;I am doing the following:&lt;BR /&gt;&lt;BR /&gt;my $time1 = substr($data[3],9,6);&lt;BR /&gt;my $time2 = substr($data[4],9,6);&lt;BR /&gt;&lt;BR /&gt;my $timediff = $time2 - $time1;&lt;BR /&gt;&lt;BR /&gt;if($timediff &amp;lt; 0)&lt;BR /&gt;{&lt;BR /&gt;     $timediff += 60*60*24;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;But still I am getting time difference in negative.&lt;BR /&gt;&lt;BR /&gt;Please help.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Anand&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Aug 2005 15:25:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612239#M104402</guid>
      <dc:creator>Anand_30</dc:creator>
      <dc:date>2005-08-25T15:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Perl - calculate time difference</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612240#M104403</link>
      <description>I recommend a time tested tool, created by A. Clay, our fearless point leader.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://hpux.ws/merijn/caljd-2.2.pl" target="_blank"&gt;http://hpux.ws/merijn/caljd-2.2.pl&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;SEP</description>
      <pubDate>Thu, 25 Aug 2005 15:27:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612240#M104403</guid>
      <dc:creator>Steven E. Protter</dc:creator>
      <dc:date>2005-08-25T15:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Perl - calculate time difference</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612241#M104404</link>
      <description>First of all, substr in Perl unlike awk is zero-based rather than one-based but a much better answer is to convert the entire string to epoch seconds so that differences over days can be calculated.&lt;BR /&gt;#!/usr/bin/perl -w&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use Time::Local;&lt;BR /&gt;&lt;BR /&gt;my $t1 = "08252005194248";&lt;BR /&gt;my $t2 = "08252005194249";&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;sub to_seconds&lt;BR /&gt;{&lt;BR /&gt;  use integer;&lt;BR /&gt;  my $x = $_[0];&lt;BR /&gt;&lt;BR /&gt;  my $mo = substr($x,0,2);&lt;BR /&gt;  my $day = substr($x,2,2);&lt;BR /&gt;  my $year = substr($x,4,4);&lt;BR /&gt;  my $hour = substr($x,8,2);&lt;BR /&gt;  my $minute = substr($x,10,2);&lt;BR /&gt;  my $second  = substr($x,12,2);&lt;BR /&gt;&lt;BR /&gt;  my $t = timelocal($second,$minute,$hour,$day,$mo - 1,$year - 1900);&lt;BR /&gt;  return($t);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;my $diff = to_seconds($t2) - to_seconds($t1);&lt;BR /&gt;printf("Diff = %d seconds\n",$diff);&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Aug 2005 15:43:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612241#M104404</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2005-08-25T15:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: Perl - calculate time difference</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612242#M104405</link>
      <description>Hi Anand:&lt;BR /&gt;&lt;BR /&gt;Are you running with 'use warnings' or at least '-w' ???&lt;BR /&gt;&lt;BR /&gt;Looking at your code in isolation, this would lead to a negative computation if you data[4] wasn't defined.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 25 Aug 2005 15:52:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612242#M104405</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2005-08-25T15:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Perl - calculate time difference</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612243#M104406</link>
      <description>Like Clay's solution, but using UNPACK to break down the input strings&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;---------&lt;BR /&gt;use Time::Local;&lt;BR /&gt;&lt;BR /&gt;sub seconds {&lt;BR /&gt;($mo,$d,$y,$h,$mi,$s) = unpack 'a2a2a4a2a2a2', $_[0];&lt;BR /&gt;return timelocal($s,$mi,$h,$d,$mo - 1,$y - 1900);&lt;BR /&gt;}&lt;BR /&gt;$t1 = seconds (shift @ARGV);&lt;BR /&gt;$t2 = seconds (shift @ARGV);&lt;BR /&gt;&lt;BR /&gt;print $t1 - $t2;&lt;BR /&gt;------&lt;BR /&gt;$ perl tmp.p 08252005194248 08252005184248&lt;BR /&gt;3600&lt;BR /&gt;$ perl tmp.p 08252005194248 08242005194248&lt;BR /&gt;86400&lt;BR /&gt;&lt;BR /&gt;Hein.</description>
      <pubDate>Thu, 25 Aug 2005 20:36:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-calculate-time-difference/m-p/3612243#M104406</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2005-08-25T20:36:05Z</dc:date>
    </item>
  </channel>
</rss>

