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

Hi, I am new to Perl. I've got a task to convert a given time (e.g, 6 minutes 30 secs or 24 secs 50 millisecs) completely in seconds. Can you please let me know the approach to do this?

I've worked a bit with unix shell scripting. The problem here I see is that we've environmental variables for time in unix/perl so we cannot declare a variable and store a time into it (time in this specific format: 3min 55s, 4s 230ms ).

I've this "time" printed in the logs which I need to display in "seconds". Now, I can do it in another way like extracting this particular time as a string from the logs (string for example: processing completed in 7s 155ms.) and then operate on it. What I wanted to know is that is there any perl function to simplify this process.

Replies are listed 'Best First'.
Re: Convert time into seconds
by Random_Walk (Prior) on Apr 23, 2013 at 14:45 UTC

    I would multiply the minutes by 60, divide the milliseconds by 1000 and add the resulting two values to the seconds you were given.

    Of course to extract the minutes, seconds, ms you may want to use split or some regular expression

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      Thank you all and thanks Random_Walk for providing the simple solution for this. I loved writing my first simple perl script. :)

      Here is the script I created taking a string from the log (though hard coded).

      Suggestions are welcome.

        I am glad you enjoyed it. Perl coding is fun. I do have a couple of suggestions

        suggestion 1: use strict and warnings. These will alert you to dangerous coding practices. They are a great way to learn to write solid code.

        #!/usr/bin/perl use strict; use warnings;

        :-)

        OK here is a version of your code tidied up a bit, compiling under strict and not throwing warnings

        #!/usr/bin/perl use strict; use warnings; # switch one in for testing each case. # # my $statement = "File extraction sucessfully completed in 8s 180ms"; my $statement = "File extraction sucessfully completed in 3min 8s"; # my $statement = "File extraction sucessfully completed in too much t +ime"; my @values = split(/\s+/, $statement); # split on any number/sort of w +hitespace my @time; $time[0] = pop @values; $time[1] = pop @values; # or use and array slice # my @time = @values[-2,-1]; my $seconds = 0; for my $val (@time) { if ($val =~ s/min$//) { $seconds = $seconds + $val * 60; } elsif ($val =~ s/ms$//) { $seconds = $seconds + $val / 1000; } elsif ($val =~ s/s$//) { $seconds = $seconds + $val; } else { print "Warning, strange value in expected time field: $val\n"; exit; } } print "Total time taken: $seconds seconds \n"

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!
Re: Convert time into seconds
by blue_cowdawg (Monsignor) on Apr 23, 2013 at 14:29 UTC
        Hi, I am new to Perl. I've got a task to convert a given time (e.g, 6 minutes 30 secs or 24 secs 50 millisecs) completely in seconds.

    Welcome to the Monastery Lucifer.
    The problem with your question is what I refer to as "problem definition." What format is your input being resented as (eg. 00:00:00.000 would be one way).

    While you are at it check out Understanding and Using Perl Monks so that you can effectively ask questions that yield useful answers.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Convert time into seconds
by hardburn (Abbot) on Apr 23, 2013 at 14:26 UTC

    What have you tried?


    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Convert time into seconds
by bioinformatics (Friar) on Apr 23, 2013 at 18:57 UTC

    I would start here: http://perldoc.perl.org/functions/time.html. Are you timing a specific program? If you are parsing logs, then you can read in the values and use a regular expression to pull out the corresponding numbers since you would know the output format. You can divide or multiply as necessary at that point.

    The question is: are you trying to parse logs or time a specific script/program/function directly with Perl? We need to know what the input and output are to provide advice :).

    Bioinformatics
Re: Convert time into seconds
by sureshepuri (Initiate) on Apr 25, 2013 at 07:20 UTC
    #!/usr/bin/perl use strict; use warnings; use Time::ParseDate; my $date1=`date`; my $ElpsedTime=parsedate($date1); chomp $ElpsedTime; print "seconds: $ElpsedTime\n"; $ElpsedTime=$ElpsedTime/60; print "Minutes:$ElpsedTime\n"; -- Please install ParseDate package.
      Thanks!