File: 17quantize.t

package info (click to toggle)
libdatetime-set-perl 0.3400-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 344 kB
  • ctags: 98
  • sloc: perl: 3,002; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/perl -w

use strict;

use Test::More;
plan tests => 1;

use DateTime::Set;
use DateTime::SpanSet;

my $months = DateTime::Set->from_recurrence(
    next => sub {
            $_[0]->truncate( to => 'month' )
                 ->add( months => 1 );
        } );

my $spanset = DateTime::SpanSet->from_spans(
    spans => [
        DateTime::Span->from_datetimes(
                start => DateTime->new( year => 2000, month => 3, day => 15 ),
                before => DateTime->new( year => 2001 ),
           ),
        DateTime::Span->from_datetimes(
                start => DateTime->new( year => 2004 ),
                end => DateTime->new( year => 2005 ),
           ),
      ] );

# quantize to months

my $month_set = $spanset->map(
    sub {
        ( $_->intersection( $months ),
          $months->current( $_->min ) )
     }
  )->start_set;

is ( "" . $month_set->{set},
     "2000-03-01T00:00:00,2000-04-01T00:00:00,2000-05-01T00:00:00,".
     "2000-06-01T00:00:00,2000-07-01T00:00:00,2000-08-01T00:00:00,".
     "2000-09-01T00:00:00,2000-10-01T00:00:00,2000-11-01T00:00:00,".
     "2000-12-01T00:00:00,2004-01-01T00:00:00,2004-02-01T00:00:00,".
     "2004-03-01T00:00:00,2004-04-01T00:00:00,2004-05-01T00:00:00,".
     "2004-06-01T00:00:00,2004-07-01T00:00:00,2004-08-01T00:00:00,".
     "2004-09-01T00:00:00,2004-10-01T00:00:00,2004-11-01T00:00:00,".
     "2004-12-01T00:00:00,2005-01-01T00:00:00" ,
     "spanset was quantized to a set"
);