changeset 409:f7eeafbd33da ref20150831

sync
author cin
date Sun, 13 Sep 2015 19:30:49 +0300
parents 5c80e33f1218
children 9335cf010b23
files lib/IMPL/lang.pm
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
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]);