1use Test::More;
2use Time::Piece;
3
4# Skip if doing a regular install
5# These are mostly for reverse parsing tests, not required for installation
6plan skip_all => "Reverse parsing not required for installation"
7  unless ( $ENV{AUTOMATED_TESTING} );
8
9my $t = gmtime(1373371631);    # 2013-07-09T12:07:11
10
11#locale should be undef
12is( $t->_locale, undef );
13&Time::Piece::_default_locale();
14
15ok( $t->_locale );
16
17#use localized names
18cmp_ok( $t->monname,   'eq', &Time::Piece::_locale()->{mon}[ $t->_mon ] );
19cmp_ok( $t->month,     'eq', &Time::Piece::_locale()->{mon}[ $t->_mon ] );
20cmp_ok( $t->fullmonth, 'eq', &Time::Piece::_locale()->{month}[ $t->_mon ] );
21
22#use localized names
23cmp_ok( $t->wdayname, 'eq', &Time::Piece::_locale()->{wday}[ $t->_wday ] );
24cmp_ok( $t->day,      'eq', &Time::Piece::_locale()->{wday}[ $t->_wday ] );
25cmp_ok( $t->fullday,  'eq', &Time::Piece::_locale()->{weekday}[ $t->_wday ] );
26
27my @frdays = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );
28$t->day_list(@frdays);
29cmp_ok( $t->day,     'eq', &Time::Piece::_locale()->{wday}[ $t->_wday ] );
30cmp_ok( $t->fullday, 'eq', &Time::Piece::_locale()->{weekday}[ $t->_wday ] );
31
32
33#load local locale
34Time::Piece->use_locale();
35
36#test reverse parsing
37sub check_parsed
38{
39    my ( $t, $parsed, $t_str, $strp_format ) = @_;
40
41    cmp_ok( $parsed->epoch, '==', $t->epoch,
42        "Epochs match for $t_str with $strp_format" );
43    cmp_ok(
44        $parsed->strftime($strp_format),
45        'eq',
46        $t->strftime($strp_format),
47        "Outputs formatted with $strp_format match"
48    );
49    cmp_ok( $parsed->strftime(), 'eq', $t->strftime(),
50        'Outputs formatted as default match' );
51}
52
53my @dates = (
54    '%Y-%m-%d %H:%M:%S',
55    '%Y-%m-%d %T',
56    '%A, %e %B %Y at %H:%M:%S',
57    '%a, %e %b %Y at %r',
58    '%s',
59    '%c',
60    '%F %T',
61
62#TODO
63#    '%u %U %Y %T',                    #%U,W,V currently skipped inside strptime
64#    '%w %W %y %T',
65#    '%A, %e %B %Y at %I:%M:%S %p',    #%I and %p can be locale dependant
66    '%x %X',    #hard coded to American localization
67);
68
69for my $time (
70    time(),        # Now, whenever that might be
71    1451606400,    # 2016-01-01 00:00
72    1451649600,    # 2016-01-01 12:00
73  )
74{
75    my $t = gmtime($time);
76    for my $strp_format (@dates) {
77
78        my $t_str = $t->strftime($strp_format);
79        my $parsed;
80      SKIP: {
81            eval { $parsed = $t->strptime( $t_str, $strp_format ); };
82            skip "gmtime strptime parse failed", 3 if $@;
83            check_parsed( $t, $parsed, $t_str, $strp_format );
84        }
85
86    }
87
88}
89
90for my $time (
91    time(),        # Now, whenever that might be
92    1451606400,    # 2016-01-01 00:00
93    1451649600,    # 2016-01-01 12:00
94  )
95{
96    my $t = localtime($time);
97    for my $strp_format (@dates) {
98
99        my $t_str = $t->strftime($strp_format);
100        my $parsed;
101      SKIP: {
102            eval { $parsed = $t->strptime( $t_str, $strp_format ); };
103            skip "localtime strptime parse failed", 3 if $@;
104            check_parsed( $t, $parsed, $t_str, $strp_format );
105        }
106
107    }
108
109}
110
111done_testing(154);
112