View | Details | Raw Unified | Return to bug 12669
Collapse All | Expand All

(-)a/Koha/DateUtils.pm (-6 / +28 lines)
Lines 53-61 to the system preferences. If the date string is empty DateTime->now is returned Link Here
53
53
54
sub dt_from_string {
54
sub dt_from_string {
55
    my ( $date_string, $date_format, $tz ) = @_;
55
    my ( $date_string, $date_format, $tz ) = @_;
56
    if ( !$tz ) {
56
57
        $tz = C4::Context->tz;
57
    my $dt;
58
    }
58
    $tz ||= C4::Context->tz;
59
    if ( !$date_format ) {
59
    if ( !$date_format ) {
60
        $date_format = C4::Context->preference('dateformat');
60
        $date_format = C4::Context->preference('dateformat');
61
    }
61
    }
Lines 84-93 s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; Link Here
84
                $date_string =~ s/00T/01T/;
84
                $date_string =~ s/00T/01T/;
85
            }
85
            }
86
        }
86
        }
87
        return DateTime::Format::DateParse->parse_datetime( $date_string,
87
        if (   $date_string =~ m|00:00:00$|
88
            $tz->name() );
88
            or $date_string =~ m|^\d{4}-\d{2}-\d{2}$|
89
            or $date_string =~ m|^\d{4}/\d{2}/\d{2}$| )
90
        {
91
            $tz = DateTime::TimeZone->new( name => 'floating' );
92
        }
93
        else {
94
            $tz = C4::Context->tz;
95
        }
96
97
        $dt = eval {
98
            DateTime::Format::DateParse->parse_datetime( $date_string,
99
                $tz->name() );
100
        };
101
        if ( $@ ) {
102
            $tz = DateTime::TimeZone->new( name => 'floating' );
103
            $dt = eval {
104
                DateTime::Format::DateParse->parse_datetime( $date_string,
105
                    $tz->name() );
106
            };
107
        }
108
    } else {
109
        $dt = DateTime->now( time_zone => $tz );
89
    }
110
    }
90
    return DateTime->now( time_zone => $tz );
111
112
    return $dt;
91
113
92
}
114
}
93
115
(-)a/Koha/Template/Plugin/KohaDates.pm (-2 / +1 lines)
Lines 30-37 sub filter { Link Here
30
    return "" unless $text;
30
    return "" unless $text;
31
    $config->{with_hours} //= 0;
31
    $config->{with_hours} //= 0;
32
32
33
    my $tz = DateTime::TimeZone->new(name => 'floating') unless $config->{with_hours};
33
    my $dt = dt_from_string( $text, 'iso' );
34
    my $dt = dt_from_string( $text, 'iso', $tz );
35
34
36
    return $config->{as_due_date} ?
35
    return $config->{as_due_date} ?
37
        output_pref({ dt => $dt, as_due_date => 1 }) :
36
        output_pref({ dt => $dt, as_due_date => 1 }) :
(-)a/t/DateUtils.t (-4 / +22 lines)
Lines 1-11 Link Here
1
use strict;
1
use Modern::Perl;
2
use warnings;
3
use 5.010;
4
use DateTime;
2
use DateTime;
5
use DateTime::TimeZone;
3
use DateTime::TimeZone;
6
4
7
use C4::Context;
5
use C4::Context;
8
use Test::More tests => 34;
6
use Test::More tests => 36;
9
use Test::MockModule;
7
use Test::MockModule;
10
8
11
BEGIN { use_ok('Koha::DateUtils'); }
9
BEGIN { use_ok('Koha::DateUtils'); }
Lines 145-147 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '2 Link Here
145
cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
143
cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
146
$date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
144
$date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
147
cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
145
cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
146
147
$module_context->mock(
148
    'tz',
149
    sub {
150
        return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
151
    }
152
);
153
154
$dt = dt_from_string('1979-04-01');
155
isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
156
157
$module_context->mock(
158
    'tz',
159
    sub {
160
        return DateTime::TimeZone->new( name => 'Europe/Paris' );
161
    }
162
);
163
164
$dt = dt_from_string('2014-03-30 02:00:00');
165
isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
(-)a/t/db_dependent/Koha_template_plugin_KohaDates.t (-6 / +25 lines)
Lines 1-16 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
#
3
2
4
use strict;
3
use Modern::Perl;
5
use warnings;
6
use C4::Context;
4
use C4::Context;
7
use C4::Dates;
5
use C4::Dates;
8
use Test::More tests => 5;
6
use Test::More tests => 7;
7
use Test::MockModule;
9
8
10
BEGIN {
9
BEGIN {
11
        use_ok('Koha::Template::Plugin::KohaDates');
10
        use_ok('Koha::Template::Plugin::KohaDates');
12
}
11
}
13
12
13
my $module_context = new Test::MockModule('C4::Context');
14
14
my $date = "1973-05-21";
15
my $date = "1973-05-21";
15
my $context = C4::Context->new();
16
my $context = C4::Context->new();
16
my $dateobj = C4::Dates->new();
17
my $dateobj = C4::Dates->new();
Lines 18-24 my $dateobj = C4::Dates->new(); Link Here
18
my $filter = Koha::Template::Plugin::KohaDates->new();
19
my $filter = Koha::Template::Plugin::KohaDates->new();
19
ok ($filter, "new()");
20
ok ($filter, "new()");
20
21
21
22
$context->set_preference( "dateformat", 'iso' );
22
$context->set_preference( "dateformat", 'iso' );
23
$context->clear_syspref_cache();
23
$context->clear_syspref_cache();
24
$dateobj->reset_prefformat;
24
$dateobj->reset_prefformat;
Lines 40-42 $dateobj->reset_prefformat; Link Here
40
40
41
$filtered_date = $filter->filter($date);
41
$filtered_date = $filter->filter($date);
42
is ($filtered_date,'21/05/1973', "metric conversion") or diag ("metric conversion fails $filtered_date");
42
is ($filtered_date,'21/05/1973', "metric conversion") or diag ("metric conversion fails $filtered_date");
43
- 
43
44
$module_context->mock(
45
    'tz',
46
    sub {
47
        return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
48
    }
49
);
50
51
$filtered_date = $filter->filter('1979-04-01');
52
is( $filtered_date, '01/04/1979', 'us: dt_from_string should return the valid date if a DST is given' );
53
54
$module_context->mock(
55
    'tz',
56
    sub {
57
        return DateTime::TimeZone->new( name => 'Europe/Paris' );
58
    }
59
);
60
61
$filtered_date = $filter->filter('2014-03-30 02:00:00');
62
is( $filtered_date, '30/03/2014', 'us: dt_from_string should return a DateTime object if a DST is given' );

Return to bug 12669