rsodhia has asked for the wisdom of the Perl Monks concerning the following question:

I have just started using perl, the first thing is on reading unixtimestamp values from file & converting them into standard format. I used localtime(). but with no luck
#!/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' or die $!; my $file1 = 'dDos_flows1.csv' or die $!; 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]; my @conv=scaler(localtime($data[2])); print "$conv[2]\n"; }
$data2 has below output: startTime 1421744559358 1421744614728 1421744621032 1421724767767

Replies are listed 'Best First'.
Re: localtime function
by Anonymous Monk on Jan 27, 2015 at 08:14 UTC

    Please wrap your code in <code> tags. Have a look at The Perl Monks Guide to the Monastery and How do I post a question effectively?

    However, I do see a couple of things: The or die $! should be on the opens, not on the variable assignment, and it's scalar not "scaler". You should be getting an error message to that effect - please also always post any error messages you are getting here.

    Your problem seems to be coming from the assignment that looks like it's my @conv=scalar(localtime($data[2])); because scalar only returns one value, which you are assigning to an array @conv, so @conv is now an array with only one element, $conv[0], but then you access $conv[2] which does not exist. So either access $conv[0] instead of $conv[2], or change @conv to $conv and only work with $conv after that.

Re: localtime function
by jellisii2 (Hermit) on Jan 27, 2015 at 12:52 UTC
    Do these timestamps include milliseconds? The extra 3 digits would resolve that.
    Also, I know that some Microsoft stuff has a weird time definition that is something like "Milliseconds from Midnight Jan 1 year 0".
      In this case the print line may look like this in my comment above:
      print localtime(int($t/1000)) . ' (' . sprintf("%03d", ($t % 1000)) . + "ms)\n";

        Here's my Input file: sourceIP geographic startTime endTime sourcePort 192.168.0.123 Other 1.42E+12 1.42E+12 61077 115.239.228.15 Asia.China 1.42E+12 1.42E+12 47308 74.125.206.188 NorthAmerica.UnitedStates 1.42E+12 1.42E+12 5228 92.51.156.102 Europe.Germany 1.42E+12 1.42E+12 5938 1.1.1.1 Oceania.Australia 1.42E+12 1.42E+12 N/A 201.48.158.12 SouthAmerica.Brazil 1.42E+12 1.42E+12 34290 64.233.167.188 NorthAmerica.UnitedStates 1.42E+12 1.42E+12 5228 50.87.144.182 NorthAmerica.UnitedStates 1.42E+12 1.42E+12 2096 180.222.178.177 Asia.Japan 1.42E+12 1.42E+12 48264 54.161.79.163 NorthAmerica.UnitedStates 1.42E+12 1.42E+12 8576 1.1.1.2 Oceania.Australia 1.42E+12 1.42E+12 123 172.16.16.100 Other 1.42E+12 1.42E+12 138

        THe whole input is getting into array while I want the data part only to play with leaving the header fields as it is.

Re: localtime function
by Anonymous Monk on Jan 27, 2015 at 09:43 UTC

    By the way, what is "TEXT::CSV"? I only know of Text::CSV - does your code compile and run as you've shown it?

    You may also want to have a look at the Basic debugging checklist.

      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

        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"; }

        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: localtime function
by pme (Monsignor) on Jan 27, 2015 at 08:51 UTC
    What is your input file format?
      csv

        Please see How do I post a question effectively?: The best and usually fastest way to get an answer is to post as much of the following as possible: code that complies, a description of what the code is supposed to do (and possibly a description of what it's currently doing instead), a small but representative sample of the input data, the expected output for that input data, the current (presumably incorrect) output, and any error messages you're getting (copy/pasted exactly).

        Anything about the columns content?