comparison lib/IMPL/lang.pm @ 409:f7eeafbd33da ref20150831

sync
author cin
date Sun, 13 Sep 2015 19:30:49 +0300
parents 5c80e33f1218
children 30e8c6a74937
comparison
equal deleted inserted replaced
408:5c80e33f1218 409:f7eeafbd33da
7 use IMPL::clone qw(clone); 7 use IMPL::clone qw(clone);
8 use Scalar::Util qw(blessed); 8 use Scalar::Util qw(blessed);
9 use POSIX; 9 use POSIX;
10 use Carp qw(carp); 10 use Carp qw(carp);
11 11
12 our @EXPORT = qw(&is &isclass &typeof &coarsen); 12 our @EXPORT = qw(&is &isclass &typeof &coarsen &coarsen_dt);
13 our %EXPORT_TAGS = ( 13 our %EXPORT_TAGS = (
14 base => [ 14 base => [
15 qw( 15 qw(
16 &is 16 &is
17 &clone 17 &clone
138 sub coarsen { 138 sub coarsen {
139 my ( $value, $resolution ) = @_; 139 my ( $value, $resolution ) = @_;
140 return $resolution ? ceil( $value / $resolution ) * $resolution : $value; 140 return $resolution ? ceil( $value / $resolution ) * $resolution : $value;
141 } 141 }
142 142
143 # datetime is DateTime object
144 # resolution is DateTime::Duration object, the resulting time will be aligned to it
145 sub coarsen_dt {
146 my ( $datetime, $resolution ) = @_;
147
148 return $datetime unless $resolution;
149
150 my $date = $datetime->clone()->truncate( to => "day" );
151
152 return $date->add(
153 minutes => coarsen(
154 $datetime->subtract_datetime($date)->in_units('minutes'),
155 $resolution->in_units('minutes')
156 )
157 );
158 }
159
143 sub equals { 160 sub equals {
144 if (defined $_[0]) { 161 if (defined $_[0]) {
145 return 0 if (not defined $_[1]); 162 return 0 if (not defined $_[1]);
146 163
147 return $_[0] == $_[1]; 164 return $_[0] == $_[1];