Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Is there a better way to print time in a HH:MM:SS format than sprintf

by gargle (Chaplain)
on Feb 03, 2006 at 19:45 UTC ( [id://527794]=note: print w/replies, xml ) Need Help??


in reply to Re: Is there a better way to print time in a HH:MM:SS format than sprintf
in thread Is there a better way to print time in a HH:MM:SS format than sprintf

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://527794]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found