$date_string =~ s/00T/01T/;
}
}
- return DateTime::Format::DateParse->parse_datetime( $date_string, $tz->name() );
+ return DateTime::Format::DateParse->parse_datetime( $date_string,
+ $tz->name() );
}
return DateTime->now( time_zone => $tz );
}
+=head2 output_pref
+
+$date_string = output_pref($dt, [$format] );
+
+Returns a string containing the time & date formatted as per the C4::Context setting
+
+A second parameter allows overriding of the syspref value. This is for testing only
+In usage use the DateTime objects own methods for non standard formatting
+
+=cut
+
sub output_pref {
- my $dt = shift;
- my $pref = C4::Context->preference('dateformat');
+ my $dt = shift;
+ my $force_pref = shift; # if testing we want to override Context
+ my $pref =
+ defined $force_pref ? $force_pref : C4::Context->preference('dateformat');
given ($pref) {
when (/^iso/) {
return $dt->strftime('%Y-%m-%d %H:%M');
--- /dev/null
+use strict;
+use warnings;
+use 5.010;
+
+use C4::Context;
+use Test::More tests => 7; # last test to print
+
+BEGIN { use_ok('Koha::DateUtils'); }
+
+my $tz = C4::Context->tz;
+
+isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
+
+my $testdate_iso = '2011-06-16'; # Bloomsday 2011
+my $dt = dt_from_string( $testdate_iso, 'iso' );
+
+isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
+
+cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
+
+$dt->set_hour(12);
+$dt->set_minute(0);
+
+my $date_string = output_pref( $dt, 'iso' );
+cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
+
+$date_string = output_pref( $dt, 'us' );
+cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
+
+# metric should return the French Revolutionary Calendar Really
+$date_string = output_pref( $dt, 'metric' );
+cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';