# HG changeset patch # User cin # Date 1442161849 -10800 # Node ID f7eeafbd33da47232b240f20438cadf3211ebb05 # Parent 5c80e33f1218d7f560b0b7ae07b401e97ee3589c sync diff -r 5c80e33f1218 -r f7eeafbd33da lib/IMPL/lang.pm --- a/lib/IMPL/lang.pm Mon Sep 07 01:35:25 2015 +0300 +++ b/lib/IMPL/lang.pm Sun Sep 13 19:30:49 2015 +0300 @@ -9,7 +9,7 @@ use POSIX; use Carp qw(carp); -our @EXPORT = qw(&is &isclass &typeof &coarsen); +our @EXPORT = qw(&is &isclass &typeof &coarsen &coarsen_dt); our %EXPORT_TAGS = ( base => [ qw( @@ -140,6 +140,23 @@ return $resolution ? ceil( $value / $resolution ) * $resolution : $value; } +# datetime is DateTime object +# resolution is DateTime::Duration object, the resulting time will be aligned to it +sub coarsen_dt { + my ( $datetime, $resolution ) = @_; + + return $datetime unless $resolution; + + my $date = $datetime->clone()->truncate( to => "day" ); + + return $date->add( + minutes => coarsen( + $datetime->subtract_datetime($date)->in_units('minutes'), + $resolution->in_units('minutes') + ) + ); +} + sub equals { if (defined $_[0]) { return 0 if (not defined $_[1]);