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

I have a small and silly question...

The following code

my ($years,$month,$day) = Today(); my $date = Date_to_Text_Long( $years,$month,$day );

gives me the day in a nice long format. While

my ($hours,$minutes,$seconds) = Now(); my $time = sprintf("%02d:%02d:%02d",$hours,$minutes,$seconds);

gives me the time.

I searched the Date::Calc for a better way to format the time, something resembling Date_to_Text_Long( $y, $m, $d) , but couldn't find it.

What do the monks use? A quick sprintf or do they know of something on CPAN that formats the time in a HH:MM:SS format?

--
if ( 1 ) { $postman->ring() for (1..2); }

Replies are listed 'Best First'.
Re: Is there a better way to print time in a HH:MM:SS format than sprintf
by tirwhan (Abbot) on Feb 03, 2006 at 08:14 UTC

    You could use DateTime:

    use DateTime; my $dt=DateTime->now(); print $dt->hms();

    Dogma is stupid.

      That's the one I'm looking for, thanks a million!

      How did I miss out on DateTime I don't know. I probably did too much java, it rots the brain you know ;)

      --
      if ( 1 ) { $postman->ring() for (1..2); }

      update: removed the antipattern ref($proto) and replaced by a simple shift, see ref($proto) - just say no! for more

      My little experiment with unit testing, mod_perl, and business and even transfer objects...

      It's actually a bit silly to use a transfer object just to pass around a date and time.

      A major gain however is the fact that you can access the data in a uniform way:

      $timeTO->getDate(); $timeTO->getTime();

      are both a little easier on the eyes (my view) than using a hash or even an index in an array

      tmp/OO/BO/TimeBO.pm

      package TimeBO; # BO to tell the time use warnings; use strict; use Carp; # We need the day of Today and represent it in a long format # Today() gives $year, $month and $day in a list # Date_to_Text_long gives p.e. Thursday, February 2nd 2006 use DateTime; # Because we want to pass the time around use OO::TO::TimeTO; sub new { my $class = shift; my $self = { TIMETO => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure,$class); return $closure; } # a public accessor to set DATE sub setDate { my $closure = $_[0]; my $date = DateTime->now(); my $timeTO = TimeTO->new(); $timeTO->setDate( $date->ymd() ); $timeTO->setTime( $date->hms() ); setTimeTO( $closure, $timeTO ); } # a private setter for TIMETO sub setTimeTO { caller(0) eq __PACKAGE__ || confess "setTimeTO is a private method +"; &{ $_[0] }("TIMETO", @_[1..$#_]); } # a public getter for TIMETO sub getTimeTO { &{ $_[0] }("TIMETO"); } 1;

      tmp/OO/BO/TimeBO.t

      #!/usr/bin/perl use strict; use warnings; # change the lib path to import OO::BO::TimeBO and OO::TO::TimeTO use lib qw( /home/gargle/public_html/cgi/ ); use Test::More 'no_plan'; use DateTime; # We need the TimeBO and TimeTO classes use OO::BO::TimeBO; use OO::TO::TimeTO; my $timeBO = TimeBO->new(); is( ref $timeBO, "TimeBO", "A TimeBO object"); $timeBO->setDate(); my $timeTO = $timeBO->getTimeTO(); is( ref $timeTO, "TimeTO", "A TimeTO object"); # the output of Today is $year, $month, $day, which is exactly what # Date_to_Text_Long needs my $today = DateTime->now(); is( $timeTO->getDate() , $today->ymd(), "It's $today"); is( $timeTO->getTime() , $today->hms(), "It's $today");

      tmp/OO/TO/TimeTO.pm

      package TimeTO; # TO to pass the time around use warnings; use strict; use Carp; # constructor sub new { my $class = shift; my $self = { DATE => undef, TIME => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure,$class); return $closure; } # a public accessor to set DATE sub setDate { &{ $_[0] }("DATE", @_[1..$#_] ) } # a public getter to get DATE sub getDate { &{ $_[0] }("DATE") } # a public accessor to set TIME sub setTime { &{ $_[0] }("TIME", @_[1..$#_] ) } # a public getter to get TIME sub getTime { &{ $_[0] }("TIME") } 1;

      tmp/OO/TO/TimeTO.t

      #!/usr/bin/perl use strict; use warnings; # change the lib path to import OO::TO::TimeTO use lib qw( /home/gargle/public_html/cgi/ ); use Test::More 'no_plan'; use DateTime; use OO::TO::TimeTO; my $timeTO = TimeTO->new(); is( ref $timeTO, "TimeTO", "A TimeTO object"); # the output of Today is $year, $month, $day, which is exactly what # Date_to_Text_Long needs my $today = DateTime->now(); $timeTO->setDate( $today->ymd() ); is( $timeTO->getDate(), $today->ymd(), "it's $today"); $timeTO->setTime( $today->hms() ); is( $timeTO->getTime(), $today->hms(), "it's $today");

      tmp/Time.pl

      #!/usr/bin/perl -wT use strict; use warnings; use CGI; # we add the BO's and TO's to the path use lib qw( /home/gargle/public_html/cgi/ ); # classes needed use OO::BO::TimeBO; use OO::TO::TimeTO; my $page = CGI->new(); my $timeBO = TimeBO->new(); $timeBO->setDate(); my $timeTO = $timeBO->getTimeTO(); print $page->header( "text/html" ), $page->start_html(-title => "My first mod_perl cgi", -bgcolor => "#ffffcc", ), $page->h1( "The current day is" ), $page->p( $timeTO->getDate() ), $page->h1( "The current time is" ), $page->p( $timeTO->getTime() ), $page->end_html();
      --
      if ( 1 ) { $postman->ring() for (1..2); }
Re: Is there a better way to print time in a HH:MM:SS format than sprintf
by ikegami (Patriarch) on Feb 03, 2006 at 07:04 UTC
    There's strftime in core module POSIX. It takes a format string and the output of gmtime or localtime as input.

      I thought about using strftime but it would give me the same 'problem': provide a format string

      I was looking actually for a module that would show HH:MM:SS by default.

      --
      if ( 1 ) { $postman->ring() for (1..2); }

        Well, the POSIX::strftime way is shorter than printf:

        use POSIX; print strftime("%H:%M:%S", localtime);
        since time (i.e. Now) is the default for localtime. And if you wrap this into a sub, its usage is even shorter:
        sub TimeHMS { my $time = scalar(@_) ? shift(@_) : time; return strftime( "%H:%M:%S", localtime($time) ); } # TimeHMS print TimeHMS();

        Best regards,
        perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Is there a better way to print time in a HH:MM:SS format than sprintf
by spiritway (Vicar) on Feb 03, 2006 at 07:16 UTC

    I usually grab all the time-related variables and then format them according to my needs.

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime (ti +me); my $ts = sprintf("%02d:%02d:%2d" $hour, $min, $sec);

    This way, they're available if I need some other form.