| 1 | ### Change History | ||
| 2 | # 1999-03-03 Stub created. -Simon | ||
| 3 | |||
| 4 | package Data::Quantity::Time::Time; | ||
| 5 | |||
| 6 | require 5; | ||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | |||
| 10 | use vars qw( $VERSION ); | ||
| 11 | $VERSION = 0.001; | ||
| 12 | |||
| 13 | use Data::Quantity::Number::Integer '-isasubclass'; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $package = shift; | ||
| 17 | bless [ @_ ], $package; | ||
| 18 | } | ||
| 19 | |||
| 20 | # $hour = $time_q->hour; | ||
| 21 | sub hour { | ||
| 22 | my $time_q = shift; | ||
| 23 | $time_q->[0]; | ||
| 24 | } | ||
| 25 | |||
| 26 | # $hour = $time_q->minute; | ||
| 27 | sub minute { | ||
| 28 | my $time_q = shift; | ||
| 29 | $time_q->[1]; | ||
| 30 | } | ||
| 31 | |||
| 32 | # $hour = $time_q->second; | ||
| 33 | sub second { | ||
| 34 | my $time_q = shift; | ||
| 35 | $time_q->[2]; | ||
| 36 | } | ||
| 37 | |||
| 38 | use vars qw( $default_readable_format ); | ||
| 39 | $default_readable_format ||= 'hh:mm:ss'; | ||
| 40 | |||
| 41 | sub readable { | ||
| 42 | my $time_q = shift; | ||
| 43 | |||
| 44 | my $style = shift; | ||
| 45 | $style ||= $default_readable_format; | ||
| 46 | |||
| 47 | my ( $sec, $min, $hour ) = ( $time_q->[2], $time_q->[1], $time_q->[0] ); | ||
| 48 | my ( $ss, $mm, $hh ) = ( $sec, $min, $hour ); | ||
| 49 | foreach ( $ss, $mm, $hh ) { | ||
| 50 | $_ = ( '0' x ( 2 - length($_) ) ) . $_; | ||
| 51 | } | ||
| 52 | my $h = ( $hour % 12 ) || 12; | ||
| 53 | my $ampm = ( $hour > 11 ) ? 'pm' : 'am'; | ||
| 54 | my $AMPM = uc($ampm); | ||
| 55 | |||
| 56 | if ( $style eq 'hh:mm:ss' ) { | ||
| 57 | return "$hh:$mm:$ss"; | ||
| 58 | } elsif ( $style eq 'h:mmPM' ) { | ||
| 59 | return "$h:$mm$AMPM"; | ||
| 60 | } else { | ||
| 61 | croak "Unkown timestamp readable format."; | ||
| 62 | } | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | 1; |