File: 01conversions.t

package info (click to toggle)
libdatetime-format-datemanip-perl 0.04-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 183; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,681 bytes parent folder | download
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
use strict;

use Test::More;

use DateTime::Format::DateManip;
use DateTime;
use Date::Manip;


my $dfdm = "DateTime::Format::DateManip";

## Set the timezone for Date::Manip and DateTime
my $dm_tz = "EST";
my $dt_tz = "US/Eastern";

# Setup Date::Manip manually so we can force the TZ to beat a config
# file setting
Date_Init("setdate=now,$dm_tz");

## Date::Manip to DateTime
my @dm_to_dt_tests = 
    (["March 23, 2003" =>
      DateTime->new(year => 2003, month => 3, day => 23, time_zone => $dt_tz) ],
     ["March 23, 2003 12:00 EST" =>
      DateTime->new(year => 2003, month => 3, day => 23,
		    hour => 12,   time_zone => $dt_tz) ],
     );

## DateTime to Date::Manip
# Redefine the DT timezone to make sure we get the conversion right
$dt_tz = "US/Pacific";
my @dt_to_dm_tests = 
    ([DateTime->new(year => 2003, month => 3, day => 23, time_zone => $dt_tz) =>
      '2003032303:00:00'],
     [DateTime->new(year => 2003, month => 3, day => 23,
		    hour => 12,   time_zone => $dt_tz) =>
      '2003032315:00:00'],
     );

## Date::Manip Delta to DateTime::Duration
my $dur1 = DateTime::Duration->new(years => 3, months => 2);
$dur1->subtract(hours => 3, minutes => 57, seconds => 2);

my @dm_to_dt_dur_tests =
    (["3 years 2 months -4 hours +3mn -2 second",
      $dur1],
     );

## DateTime::Duration to Date::Manip Delta
my @dt_to_dm_dur_tests =
    ([$dur1,
      ParseDateDelta("3 years 2 months -4 hours +3mn -2 second")],
     );

# Work out how many tests there are
plan tests => @dm_to_dt_tests     + @dt_to_dm_tests     +
              @dm_to_dt_dur_tests + @dt_to_dm_dur_tests ;

foreach my $t (@dm_to_dt_tests) {
    my ($f, $dt) = @$t;

    my $res = $dfdm->parse_datetime($f);

    my $d1 = $dt->strftime("%FT%T.%9N %Z\n");
    my $d2 = defined $res ? $res->strftime("%FT%T.%9N %Z\n") : 'undef';

    is($d2, $d1, "Parse Date '$f'");
}

foreach my $t (@dt_to_dm_tests) {
    my ($dt, $dm) = @$t;
    my $res = $dfdm->format_datetime($dt);
    is($res, $dm, "Format Date '".$dt->datetime."'");
}

foreach my $t (@dm_to_dt_dur_tests) {
    my ($dm, $dt) = @$t;
    my $res = $dfdm->parse_duration($dm);

    my $d1 = format_dur($dt);
    my $d2 = format_dur($res);

    is($d2, $d1, "Parse Duration '$d1'");
}

foreach my $t (@dt_to_dm_dur_tests) {
    my ($dt, $dm) = @$t;

    my $res = $dfdm->format_duration($dt);
    my $d2 = format_dur($dt);

    is($res, $dm, "Format Duration '$d2'");
}


sub format_dur {
    my $dur = shift;
    return undef unless defined $dur;

    my %deltas = $dur->deltas();
    my @args = ();
    foreach my $k (qw( months days minutes seconds )) {
	push @args, "$k=$deltas{$k}";
    }

    return join ":", @args;
}