| 1 | package Weather::Com::DateTime; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | no warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | #use warnings; | ||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Time::Format; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use Time::Local; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | our $VERSION = sprintf "%d.%03d", q$Revision: 1.7 $ =~ /(\d+)/g; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | our %months = ( | |||||||||||||||||||
| 16 | 'Jan' => 0, | ||||||||||||||||||||
| 17 | 'Feb' => 1, | ||||||||||||||||||||
| 18 | 'Mar' => 2, | ||||||||||||||||||||
| 19 | 'Apr' => 3, | ||||||||||||||||||||
| 20 | 'May' => 4, | ||||||||||||||||||||
| 21 | 'Jun' => 5, | ||||||||||||||||||||
| 22 | 'Jul' => 6, | ||||||||||||||||||||
| 23 | 'Aug' => 7, | ||||||||||||||||||||
| 24 | 'Sep' => 8, | ||||||||||||||||||||
| 25 | 'Oct' => 9, | ||||||||||||||||||||
| 26 | 'Nov' => 10, | ||||||||||||||||||||
| 27 | 'Dec' => 11 | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 31 | # Constructor | ||||||||||||||||||||
| 32 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 33 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||
| 34 | 1 | my $proto = shift; | |||||||||||||||||||
| 35 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 36 | 1 | my $self = {}; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | $self->{EPOC} = timelocal(gmtime(time())); | |||||||||||||||||||
| 39 | 1 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 1 | $self->{ZONE} = shift; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | else { | ||||||||||||||||||||
| 43 | 0 | $self->{ZONE} = 0; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 1 | bless( $self, $class ); | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 1 | return $self; | |||||||||||||||||||
| 49 | } # end new() | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 52 | # very special setter methods | ||||||||||||||||||||
| 53 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 54 | # this method assumes that time is irrelevant => no conversion needed | ||||||||||||||||||||
| 55 | 0 | ▶ | sub set_date { | ||||||||||||||||||
|
Subroutine
set_date: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||
| 57 | 0 | my $datestring = shift; | |||||||||||||||||||
| 58 | 0 | my $month = substr( $datestring, 0, 3 ); | |||||||||||||||||||
| 59 | 0 | my $day = substr( $datestring, 3 ); | |||||||||||||||||||
| 60 | 0 | my @now = gmtime(); | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | my $localmidnight = undef; | |||||||||||||||||||
| 63 | 0 | eval { $localmidnight = timelocal( 0, 0, 0, $day, $months{$month}, $now[5] ); }; | |||||||||||||||||||
| 64 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | croak($@); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | my $gmtime = $localmidnight - ( $self->{ZONE} * 3600 ); | |||||||||||||||||||
| 69 | 0 | $self->{EPOC} = $gmtime; | |||||||||||||||||||
| 70 | 0 | return $self->{EPOC}; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | # this method assumes that the date is irrelevant! | ||||||||||||||||||||
| 74 | 0 | ▶ | sub set_time { | ||||||||||||||||||
|
Subroutine
set_time: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||
| 76 | 0 | my $timestring = shift; | |||||||||||||||||||
| 77 | 0 | my $colon = index( $timestring, ":" ); | |||||||||||||||||||
| 78 | 0 | my $hour = substr( $timestring, 0, $colon ); | |||||||||||||||||||
| 79 | 0 | my $minute = substr( $timestring, $colon + 1, 2 ); | |||||||||||||||||||
| 80 | 0 | my $ampm = substr( $timestring, $colon + 4 ); | |||||||||||||||||||
| 81 | 0 | ▶ | $hour += 12 if ( lc($ampm) eq "pm" ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | my @now = gmtime(); | |||||||||||||||||||
| 84 | 0 | my $localtime = undef; | |||||||||||||||||||
| 85 | 0 | eval { | |||||||||||||||||||
| 86 | 0 | $localtime = timelocal( 0, $minute, $hour, $now[3], $now[4], $now[5] ); | |||||||||||||||||||
| 87 | }; | ||||||||||||||||||||
| 88 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | croak($@); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | croak($@); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | my $gmtime = $localtime - ( $self->{ZONE} * 3600 ); | |||||||||||||||||||
| 96 | 0 | $self->{EPOC} = $gmtime; | |||||||||||||||||||
| 97 | 0 | return $self->{EPOC}; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | ▶ | sub set_lsup { | ||||||||||||||||||
|
Subroutine
set_lsup: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 101 | 1 | my $self = shift; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # this method returns epoc for gmt corresponding to | ||||||||||||||||||||
| 104 | # the provided last update value (lsup) | ||||||||||||||||||||
| 105 | 1 | my $lsup = shift; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1 | my ( $date, $time, $ampm, $zone ) = split( / /, $lsup ); | |||||||||||||||||||
| 108 | 1 | my ( $mon, $mday, $year ) = split( "/", $date ); | |||||||||||||||||||
| 109 | 1 | my ( $hour, $min ) = split( /:/, $time ); | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 1 | $year += 100; | |||||||||||||||||||
| 112 | 1 | ▶ | $hour += 12 if ( $ampm eq "PM" ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 1 | my $localtime; | |||||||||||||||||||
| 115 | 1 | eval { $localtime = timelocal( 0, $min, $hour, $mday, $mon - 1, $year ); }; | |||||||||||||||||||
| 116 | 1 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | croak($@); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 1 | my $gmtime = $localtime - ( $self->{ZONE} * 3600 ); | |||||||||||||||||||
| 121 | 1 | $self->{EPOC} = $gmtime; | |||||||||||||||||||
| 122 | 1 | return $self->{EPOC}; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 126 | # Access date and time | ||||||||||||||||||||
| 127 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 128 | # epoc is alway GMT | ||||||||||||||||||||
| 129 | 1 | ▶ | sub epoc { | ||||||||||||||||||
|
Subroutine
epoc: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 130 | 1 | my $self = shift; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 1 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | $self->{EPOC} = timelocal(gmtime(shift)); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | # we have to fake up $epoc | ||||||||||||||||||||
| 137 | # because of Time::Format behaviour, $epoc has to be in localtime | ||||||||||||||||||||
| 138 | # and to be able to provide a GMT conform epoc, we have to transform | ||||||||||||||||||||
| 139 | # this here | ||||||||||||||||||||
| 140 | 1 | return timegm(localtime($self->{EPOC})); | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 5 | ▶ | sub formatted { | ||||||||||||||||||
|
Subroutine
formatted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 144 | 5 | my $self = shift; | |||||||||||||||||||
| 145 | 5 | my $format = shift; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # Time::Format always returns localtime of the server the | ||||||||||||||||||||
| 148 | # script runs on. We have to eliminate this. | ||||||||||||||||||||
| 149 | 5 | my $localepoc = $self->{EPOC} + ( $self->{ZONE} * 3600 ); | |||||||||||||||||||
| 150 | 5 | return $time{ $format, $localepoc }; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 154 | # Access date | ||||||||||||||||||||
| 155 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 156 | 0 | ▶ | sub weekday { | ||||||||||||||||||
|
Subroutine
weekday: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 157 | 0 | my $self = shift; | |||||||||||||||||||
| 158 | 0 | return $self->formatted('Weekday'); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | ▶ | sub date { | ||||||||||||||||||
|
Subroutine
date: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||
| 163 | 0 | return $self->formatted('d. Month yyyy'); | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 1 | ▶ | sub year { | ||||||||||||||||||
|
Subroutine
year: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 167 | 1 | my $self = shift; | |||||||||||||||||||
| 168 | 1 | return $self->formatted('yyyy'); | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | ▶ | sub month { | ||||||||||||||||||
|
Subroutine
month: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||
| 173 | 0 | return $self->formatted('Month'); | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 1 | ▶ | sub mon { | ||||||||||||||||||
|
Subroutine
mon: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 177 | 1 | my $self = shift; | |||||||||||||||||||
| 178 | 1 | return $self->formatted('mm{on}'); | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 1 | ▶ | sub day { | ||||||||||||||||||
|
Subroutine
day: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 182 | 1 | my $self = shift; | |||||||||||||||||||
| 183 | 1 | return $self->formatted('dd'); | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 187 | # Access time | ||||||||||||||||||||
| 188 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 189 | 1 | ▶ | sub time { | ||||||||||||||||||
|
Subroutine
time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 190 | 1 | my $self = shift; | |||||||||||||||||||
| 191 | 1 | return $self->formatted('hh:mm'); | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 1 | ▶ | sub time_ampm { | ||||||||||||||||||
|
Subroutine
time_ampm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 195 | 1 | my $self = shift; | |||||||||||||||||||
| 196 | 1 | return $self->formatted('H:mm AM'); | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 200 | # 24hour HACK | ||||||||||||||||||||
| 201 | # This method overwrites Time::Local::timelocal | ||||||||||||||||||||
| 202 | # That is to easily workaround a problem between weather.com and | ||||||||||||||||||||
| 203 | # the original 'timelocal' function: 'timelocal' only accepts hours | ||||||||||||||||||||
| 204 | # from 0 to 23, but weather.com works from 0 to 24... | ||||||||||||||||||||
| 205 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 206 | 2 | ▶ | sub timelocal { | ||||||||||||||||||
|
Subroutine
timelocal: called CC=3 SCAR=12.6
| |||||||||||||||||||||
| 207 | 2 | my ( $sec, $min, $hour, $mday, $mon, $year ) = @_; | |||||||||||||||||||
| 208 | 2 | my $twentyforhack = 0; | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 2 | ▶ | if ( $hour == 24 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | 0 | $hour = 23; | |||||||||||||||||||
| 212 | 0 | $twentyforhack = 1; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 2 | my $epoc = &Time::Local::timelocal( 0, $min, $hour, $mday, $mon, $year ); | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 2 | ▶ | if ($twentyforhack) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | 0 | $epoc += 3600; | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 2 | return $epoc; | |||||||||||||||||||
| 222 | } | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | 2 | 1; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | __END__ | ||||||||||||||||||||