Commit | Line | Data |
3fea05b9 |
1 | package DateTime; |
2 | |
3 | use strict; |
4 | |
5 | use DateTime::LeapSecond; |
6 | |
7 | |
8 | sub _normalize_tai_seconds |
9 | { |
10 | return if grep { $_ == INFINITY() || $_ == NEG_INFINITY() } @_[1,2]; |
11 | |
12 | # This must be after checking for infinity, because it breaks in |
13 | # presence of use integer ! |
14 | use integer; |
15 | |
16 | my $adj; |
17 | |
18 | if ( $_[2] < 0 ) |
19 | { |
20 | $adj = ( $_[2] - 86399 ) / 86400; |
21 | } |
22 | else |
23 | { |
24 | $adj = $_[2] / 86400; |
25 | } |
26 | |
27 | $_[1] += $adj; |
28 | |
29 | $_[2] -= $adj * 86400; |
30 | } |
31 | |
32 | sub _normalize_leap_seconds |
33 | { |
34 | # args: 0 => days, 1 => seconds |
35 | my $delta_days; |
36 | |
37 | use integer; |
38 | |
39 | # rough adjust - can adjust many days |
40 | if ( $_[2] < 0 ) |
41 | { |
42 | $delta_days = ($_[2] - 86399) / 86400; |
43 | } |
44 | else |
45 | { |
46 | $delta_days = $_[2] / 86400; |
47 | } |
48 | |
49 | my $new_day = $_[1] + $delta_days; |
50 | my $delta_seconds = ( 86400 * $delta_days ) + |
51 | DateTime::LeapSecond::leap_seconds( $new_day ) - |
52 | DateTime::LeapSecond::leap_seconds( $_[1] ); |
53 | |
54 | $_[2] -= $delta_seconds; |
55 | $_[1] = $new_day; |
56 | |
57 | # fine adjust - up to 1 day |
58 | my $day_length = DateTime::LeapSecond::day_length( $new_day ); |
59 | if ( $_[2] >= $day_length ) |
60 | { |
61 | $_[2] -= $day_length; |
62 | $_[1]++; |
63 | } |
64 | elsif ( $_[2] < 0 ) |
65 | { |
66 | $day_length = DateTime::LeapSecond::day_length( $new_day - 1 ); |
67 | $_[2] += $day_length; |
68 | $_[1]--; |
69 | } |
70 | } |
71 | |
72 | 1; |