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

Simple question:

Is there a module which will take a number of seconds and format it as e.g. "7 hours, 15 minues and 1 second"?

I do this myself right now, but it's kinda ugly.

thanks,

Replies are listed 'Best First'.
Re: "natural language" time formatting?
by Mr. Muskrat (Canon) on Jul 15, 2003 at 21:07 UTC

    Time::Duration looks like it fits the bill nicely.

    #!/usr/bin/perl -w use strict; use Time::Duration; my $seconds = 163546; print duration_exact($seconds),"\n"; __DATA__ 1 day, 21 hours, 25 minutes, and 46 seconds

      Thanks, worked beautifully - just what I needed and nothing more.
Re: "natural language" time formatting?
by davorg (Chancellor) on Jul 16, 2003 at 11:21 UTC

    Date/Time support in Perl has undergone a huge transformation (for the better) in recent months. You should look at the modules that are available from the Date/Time Project.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: "natural language" time formatting?
by fredopalus (Friar) on Jul 15, 2003 at 20:59 UTC
Re: "natural language" time formatting?
by Limbic~Region (Chancellor) on Jul 16, 2003 at 00:43 UTC
    glwtta,
    I seriously recommend you use one of the modules already recommended. I was interested in the problem, so I rolled my own purely for learning purposes. I wouldn't recommend you actually use this code (using AUTOLOAD, no POD, etc) - but you might be able to learn from it. I am not sure how far along "the path" you are. Learning POD is on my "to-do" list, so I did a bad job of documenting the methods by using each one of them once in the test script. You can overide the number of seconds in a month or year. I used 30 days for month and 365 days for a year.
  • my $object = Seconds2English->new(1000000, 'month' => 2_678_400);

    Cheers - L~R

    Update: If you are interested, I will update this later with POD and give better examples as well as make sure that it works with all kinds of test data - just let me know.

Re: "natural language" time formatting?
by wufnik (Friar) on Jul 16, 2003 at 11:14 UTC
    hola

    imho Date::Manip by Sullivan Beck has all you need.

    here is how you would do it there:

    my $scs = shift; my $dlt = ParseDateDelta($scs . " seconds"); $nlt = Delta_Format($dlt,0,"%dv days,%hv hours,%mv mins & %sv secs"); print "$scs = $nlt\n";
    i think it is fair to say that Date::Manip is the most complete of the date modules, and can do all sorts of conversions for you (including adding business time secs). it compiles from the box on win32 or unix. it's downside: a trifle slow.

    hope that helps,

    ...wufnik

    -- in the world of the mules there are no rules --