File Coverage

blib/lib/Astro/App/Satpass2/FormatTime.pm
Criterion Covered Total %
statement 63 69 91.3
branch 15 22 68.1
condition 4 9 44.4
subroutine 14 16 87.5
pod 7 7 100.0
total 103 123 83.7


line stmt bran cond sub pod time code
1             package Astro::App::Satpass2::FormatTime;
2              
3 20     20   335 use 5.008;
  20         103  
4              
5 20     20   129 use strict;
  20         50  
  20         675  
6 20     20   111 use warnings;
  20         64  
  20         1064  
7              
8 20     20   4506 use POSIX ();
  20         66825  
  20         2219  
9              
10 20     20   139 use parent qw{ Astro::App::Satpass2::Copier };
  20         36  
  20         150  
11              
12 20     20   1707 use Astro::App::Satpass2::Utils qw{ @CARP_NOT };
  20         49  
  20         2765  
13              
14             our $VERSION = '0.057';
15              
16 20     20   149 use constant ROUND_TIME => 1;
  20         3663  
  20         22685  
17              
18             my $delegate = eval {
19             require Astro::App::Satpass2::FormatTime::DateTime::Strftime;
20             require DateTime::TimeZone;
21             DateTime::TimeZone->new( name => 'local' );
22             'Astro::App::Satpass2::FormatTime::DateTime::Strftime';
23             } || do {
24             require Astro::App::Satpass2::FormatTime::POSIX::Strftime;
25             'Astro::App::Satpass2::FormatTime::POSIX::Strftime';
26             };
27              
28             sub new {
29 34     34 1 345139 my ( $class, %args ) = @_;
30 34 50       139 ref $class and $class = ref $class;
31              
32 34 100       146 __PACKAGE__ eq $class and $class = $delegate;
33              
34 34         156 my $self = {
35             round_time => ROUND_TIME,
36             };
37 34         151 bless $self, $class;
38 34         331 $self->init( %args );
39 34         158 return $self;
40             }
41              
42             sub attribute_names {
43 54     54 1 224 return ( qw{ gmt tz } );
44             }
45              
46             {
47              
48             my %skip = map { $_ => 1 } qw{ back_end };
49              
50             sub copy {
51 0     0 1 0 my ( $self, $copy ) = @_;
52 0         0 return $self->SUPER::copy( $copy, %skip );
53             }
54             }
55              
56             {
57             my $leader_re = qr{ @{[ __PACKAGE__ ]} :: }smxo;
58              
59             sub class_name_of_record {
60 2     2 1 6 my ( $self ) = @_;
61 2   33     61 ( my $name = ref $self || $self ) =~ s/ \A $leader_re //smx;
62 2         13 return $name;
63             }
64             }
65              
66             sub format_datetime { ## no critic (RequireFinalReturn)
67 0     0 1 0 my ( $self ) = @_;
68             # ->weep() throws an exception.
69 0         0 $self->weep(
70             'Method format_datetime() must be overridden' );
71             }
72              
73             {
74              
75             my %cache;
76              
77             sub format_datetime_width {
78 356     356 1 1055 my ( $self, $tplt, $gmt ) = @_;
79              
80 356 50       2568 defined $gmt
81             or $gmt = $self->gmt();
82 356 50       1129 $gmt = $gmt ? 1 : 0;
83              
84 356         989 my $class = ref $self;
85              
86             exists $cache{$class}{$tplt}[$gmt]
87 356 100       2509 and return $cache{$class}{$tplt}[$gmt];
88              
89 15         90 my ( $time, $wid ) = $self->_format_datetime_width_try( $tplt, undef,
90             $gmt, year => 2100 );
91 15         60 ( $time, $wid ) = $self->_format_datetime_width_try( $tplt, $time,
92             $gmt, month => 1 .. 12 );
93 15         69 ( $time, $wid ) = $self->_format_datetime_width_try( $tplt, $time,
94             $gmt, day => 1 .. 7 );
95 15         46 ( $time, $wid ) = $self->_format_datetime_width_try( $tplt, $time,
96             $gmt, hour => 6, 18 );
97              
98 15         89 return ( $cache{$class}{$tplt}[$gmt] = $wid );
99             }
100              
101             }
102              
103             sub _format_datetime_width_try {
104 60     60   213 my ( $self, $tplt, $time, $gmt, $name, @try ) = @_;
105 60         96 my $wid;
106             my $max_trial;
107 60         116 foreach my $trial ( @try ) {
108 330         689 $time = $self->__format_datetime_width_adjust_object(
109             $time, $name, $trial, $gmt );
110 330         670 my $size = length $self->format_datetime( $tplt, $time );
111 330 100 66     1026 defined $wid and $size <= $wid and next;
112 60         83 $wid = $size;
113 60         134 $max_trial = $trial;
114             }
115 60         142 $time = $self->__format_datetime_width_adjust_object( $time, $name,
116             $max_trial, $gmt );
117 60         191 return ( $time, $wid );
118             }
119              
120             {
121             my %valid = (
122             hour => 3600,
123             minute => 60,
124             second => 1,
125             none => undef,
126             );
127              
128             sub round_time {
129 1037     1037 1 2705 my ( $self, @arg ) = @_;
130 1037 100       2325 if ( @arg ) {
131 298         741 my $val = $arg[0];
132 298 50 33     2120 if ( defined $val && $val =~ m/ [^0-9] /smx ) {
133 0 0       0 exists $valid{$val}
134             or $self->wail( "Invalid rounding spec '$val'" );
135 0         0 $val = $valid{$val}
136             }
137 298         929 $self->{round_time} = $val;
138 298         828 return $self;
139             } else {
140 739         3726 return $self->{round_time};
141             }
142             }
143             }
144              
145             sub __round_time_value {
146 618     618   1237 my ( $self, $time ) = @_;
147 618 100       1565 ref $time
148             and return $time;
149 288 50       713 if ( my $round = $self->round_time() ) {
150 288         1879 $time = POSIX::floor( ( $time + $round / 2 ) / $round ) * $round;
151             }
152 288         983 return $time;
153             }
154              
155             __PACKAGE__->create_attribute_methods();
156              
157             1;
158              
159             __END__