in reply to Re: localtime function
in thread localtime function

I am reading from a csv file name: dDos_flows.csv, which has whole lot of entries which I am storing them all in a array & filtering out data2. which is: startTime 1421744559358 1421744614728 1421744621032 1421724767767 Now this particular column has the time in the unix timestamp format, which I intent to convert it into standard time format & place it back into the dDos_flows.csv file.

The output I am getting: Wed Dec 31 16:00:00 1969 Thu Apr 17 13:15:58 47023 Fri Apr 18 04:38:48 47023 Fri Apr 18 06:23:52 47023 Sat Aug 31 11:36:07 47022 Thu Jan 30 16:28:48 47023 Wed Apr 16 09:25:01 47023 This is not the correct time. Besides I am getting some warning as well Use of uninitialized value $data2 in localtime at D:\ddos.pl line 20, <$fh> li ne 1. Wed Dec 31 16:00:00 1969 Use of uninitialized value $data2 in localtime at D:\ddos.pl line 20, <$fh> li ne 2. Wed Dec 31 16:00:00 1969 Argument "\x{73}\x{74}..." isn't numeric in localtime at D:\ddos.pl line 20, <$f h> line 3. Use of uninitialized value $data2 in localtime at D:\ddos.pl line 20, <$fh> li ne 22. Wed Dec 31 16:00:00 1969 Use of uninitialized value $data2 in localtime at D:\ddos.pl line 20, <$fh> li ne 23. Wed Dec 31 16:00:00 1969 Use of uninitialized value $data2 in localtime at D:\ddos.pl line 20, <$fh> li ne 24. Wed Dec 31 16:00:00 1969

Replies are listed 'Best First'.
Re^3: localtime function
by pme (Monsignor) on Jan 27, 2015 at 10:28 UTC
    These numbers are way bigger than the current timestamp what is 1422354181. The code below would solve the problem if the timestamps were valid.

    my $res = 'startTime 1421744559358 1421744614728 1421744621032 1421724 +767767'; my @res = split(' ', $res); shift @res; foreach my $t (@res) { print localtime($t) . "\n"; }
      As per pme, I made the following changes but nothing worked.
      #!/usr/bin/perl use strict; use warnings; use XML::CSV; use Text::CSV; use Time::Local; my @d; my %ha; my $file = 'dDos_flows.csv'; my $file1 = 'dDos_flows1.csv'; open(my $fh,'<:encoding(UTF-8)',$file); open(my $fh1,'>:encoding(UTF-8)',$file1); while(my $f=<$fh>) { chomp $f; my @data=split ",",$f; my $res=$data[2]; #print "$res \n"; my @res= split(' ', $res); shift @res; foreach my $t (@res) { print localtime($t) . "\n"; } #my $conv=scalar(localtime($data[2])); #print "$conv \n"; }
      output
      Use of uninitialized value $res in split at D:\ddos.pl line 21, <$fh> +line 1. Use of uninitialized value $res in split at D:\ddos.pl line 21, <$fh> +line 2. Use of uninitialized value $res in split at D:\ddos.pl line 21, <$fh> +line 22. Use of uninitialized value $res in split at D:\ddos.pl line 21, <$fh> +line 23. Use of uninitialized value $res in split at D:\ddos.pl line 21, <$fh> +line 24.
Re^3: localtime function
by Anonymous Monk on Jan 27, 2015 at 10:33 UTC

    Please use <code> tags to preserve formatting.

    localtime accepts a time value in seconds. I am guessing that your time values are in milliseconds, so you need to divide them by 1000 to get seconds. If that loss of precision is not acceptable, you may want to switch to a module to handle your time processing needs, such as DateTime.

Re^3: localtime function
by AnomalousMonk (Archbishop) on Jan 27, 2015 at 19:53 UTC