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

sync
author cin
date Sun, 13 Sep 2015 19:30:49 +0300
parents 5c80e33f1218
children 30e8c6a74937
line wrap: on
line diff
--- 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]);