File: 15time_zone.t

package info (click to toggle)
libdatetime-set-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 336 kB
  • sloc: perl: 2,972; makefile: 2
file content (141 lines) | stat: -rw-r--r-- 4,433 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/perl -w

use strict;

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

use DateTime;
use DateTime::Set;

#======================================================================
# TIME ZONE TESTS
#====================================================================== 

my $t1 = new DateTime( year => '2001', month => '11', day => '22' );
my $t2 = new DateTime( year => '2002', month => '11', day => '22' );
my $s1 = DateTime::Set->from_datetimes( dates => [ $t1, $t2 ] );


my $s2 = $s1->set_time_zone( 'Asia/Taipei' );

is( $s2->min->datetime, '2001-11-22T00:00:00', 
    'got 2001-11-22T00:00:00 - min' );

is( $s2->min->time_zone->name, 'Asia/Taipei', 
    'got time zone name from set' );

my $span1 = DateTime::Span->from_datetimes( start => $t1, end => $t2 );
$span1->set_time_zone( 'America/Sao_Paulo' );
my $span2 = $span1->clone;

$span1->set_time_zone( 'Asia/Taipei' );

is( $span1->start->datetime, '2001-11-22T10:00:00',
    'got 2001-11-22T10:00:00 - min' );
is( $span1->end->datetime, '2002-11-22T10:00:00',
    'got 2002-11-22T10:00:00 - max' );

# check for immutability
is( $span2->start->datetime, '2001-11-22T00:00:00',
    'got 2001-11-22T00:00:00 - min' );
is( $span2->end->datetime, '2002-11-22T00:00:00',
    'got 2002-11-22T00:00:00 - max' );

# recurrence
{
my $months = DateTime::Set->from_recurrence(
                 recurrence => sub {
                     my $tz = $_[0]->time_zone;
                     $_[0]->set_time_zone( 'floating' );
                     $_[0]->truncate( to => 'month' )->add( months => 1 );
                     $_[0]->set_time_zone( $tz );
                     $_[0];
                 }
             )
             ->set_time_zone( 'Asia/Taipei' );

my $str = $months->next( $t1 )->datetime . ' ' .
          $months->next( $t1 )->time_zone_long_name;

my $original = $t1->datetime . ' ' .
               $t1->time_zone_long_name;

is( $str, '2001-12-01T00:00:00 Asia/Taipei', 'recurrence with time zone' );
is( $original, '2001-11-22T00:00:00 floating', 'does not mutate arg' );


{
  my $str;

  my $dt_floating = new DateTime( 
      year => 2001, month => 11, day => 1
  );
  my $dt_with_tz  = $dt_floating->clone->set_time_zone( 'America/Sao_Paulo' );

  my $set_floating = DateTime::Set->from_recurrence(
       recurrence => sub {
                     my $tz = $_[0]->time_zone;
                     $_[0]->set_time_zone( 'floating' );
                     $_[0]->truncate( to => 'month' )->add( months => 1 );
                     $_[0]->set_time_zone( $tz );
                     $_[0];
                  }
  );
  my $set_with_tz = $set_floating->clone->set_time_zone( 'Asia/Taipei' );

  # tests with the "next" method

  # floating set => floating dt
      is( $set_floating->next( $dt_floating )->
          strftime( "%FT%H:%M:%S %{time_zone_long_name}"),
          '2001-12-01T00:00:00 floating',
          'recurrence without time zone, arg without time zone' );
  # tz set => floating dt
      is( $set_with_tz->next( $dt_floating )->
          strftime( "%FT%H:%M:%S %{time_zone_long_name}"),
          '2001-12-01T00:00:00 Asia/Taipei',
          'recurrence with time zone, arg without time zone' );
  # floating set => tz dt
      is( $set_floating->next( $dt_with_tz )->
          strftime( "%FT%H:%M:%S %{time_zone_long_name}"),
          '2001-12-01T00:00:00 America/Sao_Paulo',
          'recurrence with time zone, arg without time zone' );

  # TODO: {
  #  local $TODO = "Time zone settings do not backtrack";
  # bug reported by Tim Mueller-Seydlitz

  # tz set => tz dt
      is( $set_with_tz->next( $dt_with_tz )->
          strftime( "%FT%H:%M:%S %{time_zone_long_name}"),
          # = '2001-12-01T00:00:00 Asia/Taipei',
          '2001-11-30T14:00:00 America/Sao_Paulo',
          'recurrence with time zone, arg with time zone' );
  # } 

    # TODO: limit set_floating with a start=>dt_floating;
    #       ask for next( dt_with_tz_before_start ) 
    #       and next( dt_with_another_tz_before_start )
    #       and next( dt_floating_before_start )
    #       and check for caching problems

}


# set locale, add duration
is ( $months->clone->add( days => 1 )->
              next( $t1 )->
              strftime( "%a" ), 'Sun', 
     'default locale' );

is ( $months->clone->add( days => 1 )->
              set( locale => 'en_US' )->
              next( $t1 )->
              strftime( "%a" ), 
     'Sun', 
     'new locale' );
}

1;