Quantity/Time/Timestamp.pm

filter: Statementstmt 30.3% 27 / 89 Branchbran 2.9% 1 / 34 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 11 totaltotal 22.2% 36 / 162 CC 21 SCAR 47.8
CC
21
Cov
22%
readable
272.0
get_udt
6.0
date
6.0
CRAP 119.5
↑ Summary Quantity/Time/Date.pm »
1 ### Change History
2 # 2001-05-03 Added 'mm/dd/yy' -Ed
3 # 2001-03-06 Added even more readable formats. -Piglet
4 # 2000-12-16 Added more readable formats. -Simon
5 # 2000-12-01 Added 1900 to year in readable. -Piglet
6 # 1999-02-21 Created. -Simon
7  
8 package Data::Quantity::Time::Timestamp;
9  
10 1 require 5;
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::ParseDate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 $VERSION = 0.001;
17  
18 1 ▶use Data::Quantity::Number::Integer '-isasubclass';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # undef = Data::Quantity::Time::Time->scale();
21 0 ▶sub scale {
Subroutine scale: not called CC=1 SCAR=6.9
Pod undocumented
22 0 return 'Second';
23 }
24  
25 # $date = Data::Quantity::Time::Time->current();
26 0 ▶sub current {
Subroutine current: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my $class = shift;
28 0 my $date = $class->new_instance;
29 0 $date->set_udt( time() );
30 0 return $date;
31 }
32  
33 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
34 0 return 'temporal', 'absolute';
35 }
36  
37 # $seconds_since_1970 = $moment_q->get_udt;
38 0 ▶sub get_udt {
Subroutine get_udt: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my $moment_q = shift;
40 0 ▶ my $udt = $moment_q->value or return;
Branch: 1 branch
BranchTrueFalseTotal
my $udt = $moment_q->value or return 0 0 0
41 0 return $udt;
42 }
43  
44 # $moment_q->set_udt( $seconds_since_1970 );
45 0 ▶sub set_udt {
Subroutine set_udt: not called CC=1 SCAR=6.9
Pod undocumented
46 0 my $moment_q = shift;
47 0 my $udt = shift;
48 0 $moment_q->value( $udt );
49 }
50  
51 1 ▶use vars qw( $default_readable_format );
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶$default_readable_format ||= 'year/mon/mday hh:mm:ss';
Condition50%1/2
$Data::Quantity::Time::Timestamp::default_readable_format ||= "year/mon/mday hh:mm:ss"
l!l
01
MC/DC0%0/1
$Data::Quantity::Time::Timestamp::default_readable_format ||= "year/mon/mday hh:mm:ss"
$Data::Quantity::Time::Timestamp::default_readable_format
Truth table50%1/2
A
A= $Data::Quantity::Time::Timestamp::default_readable_format
Aresult
11
00
53  
54 0 ▶sub readable {
Subroutine readable: not called CC=16 SCAR=56.1
Pod undocumented
55 0 my $moment_q = shift;
56 0 ▶ my $udt = $moment_q->value or return;
Branch: 1 branch
BranchTrueFalseTotal
my $udt = $moment_q->value or return 0 0 0
57  
58 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( $udt );
59 0 $mon += 1;
60 0 $year += 1900;
61
62 0 my $style = shift;
63 0 ▶ $style ||= $default_readable_format;
Condition0%0/3
$style ||= $Data::Quantity::Time::Timestamp::default_readable_format
l!l&&r!l&&!r
000
MC/DC0%0/2
$style ||= $Data::Quantity::Time::Timestamp::default_readable_format
$style $Data::Quantity::Time::Timestamp::default_readable_format
Truth table0%0/3
A ||= B
A= $styleB= $Data::Quantity::Time::Timestamp::default_readable_format
ABresult
1X1
011
000
64
65 # zero-padding
66 0 my ( $ss, $mm, $hh ) = ( $sec, $min, $hour );
67 0 my ( $monmon, $dayday ) = ( $mon, $mday );
68 0 foreach ( $ss, $mm, $hh, $monmon, $dayday ) {
69 0 $_ = ( '0' x ( 2 - length($_) ) ) . $_;
70 }
71 0 my($yy) = ( $year =~ /(\d\d)$/ );
72  
73 0 ▶ my $ampm = ( $hour > 11 ) ? 'pm' : 'am';
Branch: 1 branch
BranchTrueFalseTotal
$hour > 11 ? : 0 0 0
74 0 my $AMPM = uc($ampm);
75 0 ▶ my $h = ( $hour % 12 ) || 12;
Condition0%0/2
$hour % 12 || 12
l!l
00
MC/DC0%0/1
$hour % 12 || 12
$hour % 12
Truth table0%0/2
A
A= $hour % 12
Aresult
11
00
76
77 0 ▶ if ( $style eq 'year/mon/mday hh:mm:ss' ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($style eq 'year/mon/mday hh:mm:ss') { } 0 0 0
elsif ($style eq 'year/mon/mday hh:mm') { } 0 0 0
elsif ($style eq 'year/mon/mday') { } 0 0 0
elsif ($style eq 'yy/mm/dd hh:mm') { } 0 0 0
elsif ($style eq 'yy/mm/dd') { } 0 0 0
elsif ($style eq 'mm/dd/yy') { } 0 0 0
elsif ($style eq 'yyyy-mm-dd') { } 0 0 0
elsif ($style eq 'yyyy-mm-dd:hh:mm') { } 0 0 0
elsif ($style =~ /^DATE: ?(.*?) TIME: ?(.*)$/) { } 0 0 0
elsif ($style eq 'h:mmPM') { } 0 0 0
78 0 return "$year/$mon/$mday $hh:$mm:$ss";
79 } elsif ( $style eq 'year/mon/mday hh:mm' ) {
80 0 return "$year/$mon/$mday $hh:$mm";
81 } elsif ( $style eq 'year/mon/mday' ) {
82 0 return "$year/$mon/$mday";
83 } elsif ( $style eq 'yy/mm/dd hh:mm' ) {
84 0 return "$yy/$monmon/$dayday $hh:$mm";
85 } elsif ( $style eq 'yy/mm/dd' ) {
86 0 return "$yy/$monmon/$dayday";
87 } elsif ( $style eq 'mm/dd/yy' ) {
88 0 return "$monmon/$dayday/$yy";
89 } elsif ( $style eq 'yyyy-mm-dd' ) {
90 0 return "$year-$monmon-$dayday";
91 } elsif ( $style eq 'yyyy-mm-dd:hh:mm' ) {
92 0 return "$year-$monmon-$dayday:$hh:$mm";
93 } elsif ( $style =~ /^DATE: ?(.*?) TIME: ?(.*)$/ ) {
94 0 my ($d_fmt, $t_fmt) = ($1, $2);
95 0 my $date = Data::Quantity::Time::Date->new("$year-$mon-$mday");
96 0 return $date->readable( $d_fmt ) . " $h:$mm$AMPM";
97 } elsif ( $style eq 'h:mmPM' ) {
98 0 return "$h:$mm$AMPM";
99 } else {
100 0 croak "Unknown timestamp readable format.";
101 }
102 }
103  
104 0 ▶sub date {
Subroutine date: not called CC=2 SCAR=17.9
Pod undocumented
105 0 my $moment_q = shift;
106 0 ▶ my $udt = $moment_q->value or return;
Branch: 1 branch
BranchTrueFalseTotal
my $udt = $moment_q->value or return 0 0 0
107 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( $udt );
108 0 $mon += 1;
109 0 $year += 1900;
110 0 require Data::Quantity::Time::Date;
111 0 Data::Quantity::Time::Date->new("$year-$mon-$mday");
112 }
113  
114 0 ▶sub time {
Subroutine time: not called CC=2 SCAR=17.9
Pod undocumented
115 0 my $moment_q = shift;
116 0 ▶ my $udt = $moment_q->value or return;
Branch: 1 branch
BranchTrueFalseTotal
my $udt = $moment_q->value or return 0 0 0
117 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( $udt );
118 0 require Data::Quantity::Time::Time;
119 0 Data::Quantity::Time::Time->new($hour, $min, $sec);
120 }
121  
122 # $value = Data::Quantity::Time::Date->readable_value($number)
123 # $value = Data::Quantity::Time::Date->readable_value($number, $style)
124 0 ▶sub readable_value {
Subroutine readable_value: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my $class_or_item = shift;
126 0 my $value = shift;
127 0 $class_or_item->new($value)->readable(@_);
128 }
129  
130 # $quantity->init( $n_val );
131 2 ▶sub init {
Subroutine init: called CC=2 SCAR=7.2
Pod undocumented
132 2 my $num_q = shift;
133
134 2 my $n_val = shift;
135 2 my $numerals = $num_q->numeric_value( $n_val );
136 2 ▶ if ( defined $numerals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $numerals) { } 2 0 2
137 2 $num_q->value( $numerals );
138 } else {
139 0 $num_q->set_from_string( $n_val );
140 }
141 }
142  
143 # $date->set_from_string( $value );
144 0 ▶sub set_from_string {
Subroutine set_from_string: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my ($date, $value) = @_;
146
147 0 my $udt = Time::ParseDate::parsedate($value, 'DATE_REQUIRED' => 1);
148 0 ▶ if ($udt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($udt) { } 0 0 0
149 0 $date->set_udt($udt);
150 } else {
151 0 $date->value( 0 );
152 }
153 }
154  
155 1 1;
↑ Summary Quantity/Time/Date.pm »