| 1 | package DT; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings 'FATAL'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp qw(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Sub::Install; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use DateTime::Format::ISO8601; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use parent 'DateTime::Moonpig'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | our $VERSION = '0.4.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | use overload | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | '==' => \&_dt_int_eq, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | '!=' => \&_dt_int_ne, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | '<=>' => \&_dt_int_cmp, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | '<' => \&_dt_int_lt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | '<=' => \&_dt_int_le, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | '>' => \&_dt_int_gt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | '>=' => \&_dt_int_ge, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 'eq' => \&_dt_str_eq, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 'ne' => \&_dt_str_ne, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 'cmp' => \&_dt_str_cmp, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 'lt' => \&_dt_str_lt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 'le' => \&_dt_str_le, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 'gt' => \&_dt_str_gt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | 'ge' => \&_dt_str_ge; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | my $HAVE_PG; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 47 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 47 | my $dt; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 47 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Most probably Unix time, will croak if not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 47 | ▶ | if ( looks_like_number $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 31 | $dt = $class->SUPER::new(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | elsif ( not ref $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # May be ISO8601(ish) format used by PostgreSQL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | $dt = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | $HAVE_PG || ($HAVE_PG = require DateTime::Format::Pg); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | DateTime::Format::Pg->parse_datetime($_[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # May be a real ISO8601 format date/time | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | $dt = eval { DateTime::Format::ISO8601->parse_datetime($_[0]) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | if not $dt; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # This will croak | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 47 | ▶ | $dt = DateTime->new(@_) unless $dt; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # Rebless into DT so our methods are called instead of DateTime | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 46 | return bless $dt, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | sub unix_time { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | my ($dt) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unix_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | return $dt->epoch; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | sub pg_timestamp_notz { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my ($dt) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pg_timestamp_notz: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return DateTime::Format::Pg->format_timestamp_without_time_zone($dt); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | sub pg_timestamp_tz { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my ($dt) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pg_timestamp_tz: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return DateTime::Format::Pg->format_timestamptz($dt); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | for my $method (qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | add_duration subtract_duration | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | truncate | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | set set_time_zone set_year set_month set_day | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | set_hour set_minute set_second set_nanosecond | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | )) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | Sub::Install::install_sub({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 14 | ▶ | my $dt = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 14 | my $copy = $dt->clone; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 14 | bless $copy, 'DateTime'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 14 | $copy->$method(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 14 | bless $copy, ref $dt; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 14 | return $copy; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 60 | as => $method, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | sub TO_JSON { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return "$self"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | ############## PRIVATE METHODS BELOW ############## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | sub _promote { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 28 | ▶ | my $side_b = $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_promote: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # Deliberately not catching errors here | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 28 | ▶ | $side_b = DT->new($side_b) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $side_b and $side_b->isa("DateTime")
MC/DC0%0/2
ref $side_b and $side_b->isa("DateTime")
ref $side_b
$side_b->isa("DateTime")
Truth table33%1/3
A and B
A= ref $side_bB= $side_b->isa("DateTime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | if not ref($side_b) or not $side_b->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 28 | return ($_[0], $side_b, $_[2]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | sub _dt_int_eq { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 6 | ▶ | return undef unless defined $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_eq: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | my ($side_a, $side_b) = _promote(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | return DateTime::compare($side_a, $side_b) == 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | sub _dt_int_cmp { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 12 | ▶ | return undef unless defined $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_cmp: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 10 | my ($side_a, $side_b, $flip) = _promote(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 10 | ▶ | return $flip ? DateTime::compare($side_b, $side_a) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | : DateTime::compare($side_a, $side_b) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub _dt_int_ne { !defined($_[1]) ? undef : !_dt_int_eq(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_ne: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | ▶ | sub _dt_int_lt { !defined($_[1]) ? undef : _dt_int_cmp(@_) < 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_lt: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | ▶ | sub _dt_int_le { !defined($_[1]) ? undef : _dt_int_cmp(@_) <= 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_le: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | sub _dt_int_gt { !defined($_[1]) ? undef : _dt_int_cmp(@_) > 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_gt: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 4 | ▶ | sub _dt_int_ge { !defined($_[1]) ? undef : _dt_int_cmp(@_) >= 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_int_ge: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | sub _dt_str_eq { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | ▶ | return undef unless defined $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_eq: called CC=4 SCAR=14.3
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 4 | my ($side_a, $side_b) = _promote(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | ▶ | return ("$side_a" || '') eq ("$side_b" || ''); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
"$side_a" || ''
"$side_b" || ''
MC/DC0%0/1
"$side_a" || ''
"$side_a"
MC/DC0%0/1
"$side_b" || ''
"$side_b"
Truth table50%1/2
A
A= "$side_a"
Truth table50%1/2
A
A= "$side_b"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | sub _dt_str_cmp { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 12 | ▶ | return undef unless defined $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_cmp: called CC=9 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 10 | my ($side_a, $side_b) = _promote(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 10 | ▶ | $side_b = DT->new($side_b) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $side_b and $side_b->isa("DateTime")
MC/DC0%0/2
ref $side_b and $side_b->isa("DateTime")
ref $side_b
$side_b->isa("DateTime")
Truth table33%1/3
A and B
A= ref $side_bB= $side_b->isa("DateTime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | if not ref($side_b) or not $side_b->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 10 | ▶ | return $_[2] ? ("$side_b" || '') cmp ("$side_a" || '') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
"$side_b" || ''
"$side_a" || ''
"$side_a" || ''
"$side_b" || ''
MC/DC0%0/1
"$side_b" || ''
"$side_b"
MC/DC0%0/1
"$side_a" || ''
"$side_a"
MC/DC0%0/1
"$side_a" || ''
"$side_a"
MC/DC0%0/1
"$side_b" || ''
"$side_b"
Truth table50%1/2
A
A= "$side_b"
Truth table50%1/2
A
A= "$side_a"
Truth table50%1/2
A
A= "$side_a"
Truth table50%1/2
A
A= "$side_b"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | : ("$side_a" || '') cmp ("$side_b" || '') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 4 | ▶ | sub _dt_str_ne { !defined($_[1]) ? undef : !_dt_str_eq(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_ne: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 4 | ▶ | sub _dt_str_lt { !defined($_[1]) ? undef : _dt_str_cmp(@_) < 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_lt: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 4 | ▶ | sub _dt_str_le { !defined($_[1]) ? undef : _dt_str_cmp(@_) <= 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_le: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 4 | ▶ | sub _dt_str_gt { !defined($_[1]) ? undef : _dt_str_cmp(@_) > 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_gt: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | sub _dt_str_ge { !defined($_[1]) ? undef : _dt_str_cmp(@_) >= 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dt_str_ge: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =begin readme text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | DT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | == | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | =end readme | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | =for readme stop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | DT - DateTime wrapper that tries hard to DWYM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | use DT; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | my $dt_now = DT->new(time); # Just works | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | my $dt_fh = DT->new('2018-02-06T15:45:00-0500'); # Just works | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | my ($pg_time_str) = $pg_dbh->selectrow_array("SELECT now();") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | my $dt_pg = DT->new($pg_time_str); # Also just works | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | say "Wowza!" if $dt_now < time + 1; # Unexpectedly, works too! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | my $timestamp_notz = $dt_pg->pg_timestamp_notz; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | my $timestamp_tz = $dt->pg->pg_timestamp_tz; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | =for readme continue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | DT is a very simple and thin wrapper over DateTime::Moonpig, which | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | in turn is a wrapper over DateTime. DateTime::Moonpig brings immutability | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | and saner operator overloading at the cost of cartoonish name but also | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | lacks date/time parsing capabilities that are badly needed all the time. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | There is a myriad of helpful modules on CPAN but oh all that typing! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | Consider: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | use DateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | my $dt = DateTime->from_epoch(epoch => time); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | use DateTime::Format::Pg; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | my $dt = DateTime::Format::Pg->parse_datetime($timestamp_from_postgres); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | use DateTime::Format::ISO8601; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | my $dt = DateTime::Format::ISO8601->parse_datetime($iso_datetime); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | Versus: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | use DT; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | my $dt_unix = DT->new(time); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | my $dt_pg = DT->new($timestamp_from_postgres); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | my $dt_iso = DT->new($iso_datetime); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | DT constructor will try to Do What You Mean, and if it cannot it will | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | fall back to default DateTime constructor. Simple. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | =for readme stop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | =head1 IMMUTABILITY AND DATE MATH | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | One thing that L<DateTime::Moonpig> authors get right is data immutability: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | any operations on a L<DateTime> object should not mutate original object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | as this leads to a multitude of potential prioblems. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | However the solution presented in L<DateTime::Moonpig> is to throw an exception | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | when a mutator method is called, which is far from Doing What I Mean. Even more, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | with C<add_duration> and C<subtract_duration> methods rendered effectively unusable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | the only way to handle date arithmetic suggested is by adding or subtracting | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | the number of seconds from the date which semantically is not the same as adding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | or subtracting days/months/etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | A more reasonable approach is to clone the date object, perform the mutation | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | on the copy and return the new object. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | =head1 DATE COMPARISON | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | Yet another pretty annoying L<DateTime> quirk is comparison operator overloading. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | In my humble opinion it is not very unreasonable to expect a sophisticated date module | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | to automatically grok something like this and Just Work without throwing an exception, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | or requiring a metric ton of boilerplate: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | if ( $dt < time ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | As a side effect of added operator overload C<DT> also has saner semantics for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | comparing with C<undef> values: the result of course is C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | The following mutator methods are overridden to return a new C<DT> object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | instead of performing operations on the original object: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | C<add>, C<add_duration>, C<subtract>, C<subtract_duration>, C<truncate>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | C<set>, C<set_time_zone>, C<set_year>, C<set_month>, C<set_day>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | C<set_hour>, C<set_minute>, C<set_second>, C<set_nanosecond> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | Note that C<set_locale> and C<set_formatter> are not overridden. These methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | do not affect the actual date/time value so are safe to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | DT also adds a few useful methods: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | =item C<unix_time> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | A synonym for C<epoch>. No special magic, just easier to remember. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | =item C<pg_timestamp_notz> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | Format $dt object into a string suitable for PostgreSQL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | C<TIMESTAMP WITHOUT TIME ZONE> type column. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | =item C<pg_timestamp_tz> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | Format $dt object into a string suitable for PostgreSQL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | C<TIMESTAMP WITH TIME ZONE> type column. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =for readme continue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =head1 INSTALLATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | To install this module type the following: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | perl Makefile.PL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | make && make test && make install | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | =for readme stop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | =for readme continue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | L<DateTime::Moonpig> is the parent class for C<DT>. L<DateTime::Format::ISO8601> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | is required for parsing ISO8601 date/time formats. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | PostgreSQL related methods are optional and depend on L<DateTime::Format::Pg> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | being installed. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =for readme stop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | =head1 REPORTING BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | No doubt there are some. Please post an issue on GitHub (see below) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | if you find something. Pull requests are also welcome. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | GitHub repository: https://github.com/nohuhu/DT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | =for readme continue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | Copyright (c) 2018 by Alex Tokarev E<lt>nohuhu@cpan.orgE<gt>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | the same terms as Perl itself. See L<"perlartistic">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | =for readme stop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||