use base 'Exporter';
use version; our $VERSION = qv('1.0.0');
-our @EXPORT = (qw( dt_from_string output_pref));
+our @EXPORT = (qw( dt_from_string output_pref format_sqldatetime));
=head1 DateUtils
return;
}
+=head2 format_sqldatetime
+
+$string = format_sqldatetime( $string_as_returned_from_db );
+
+a convenience routine for calling dt_from_string and formatting the result
+with output_pref as it is a frequent activity in scripts
+
+=cut
+
+sub format_sqldatetime {
+ my $str = shift;
+ my $force_pref = shift; # if testing we want to override Context
+ if ( defined $str && $str =~ m/^\d{4}-\d{2}-\d{2}/ ) {
+ my $dt = dt_from_string( $str, 'sql' );
+ $dt->truncate( to => 'minutes' );
+ return output_pref( $dt, $force_pref );
+ }
+ return q{};
+}
+
1;
use DateTime::TimeZone;
use C4::Context;
-use Test::More tests => 19; # last test to print
+use Test::More tests => 21; # last test to print
BEGIN { use_ok('Koha::DateUtils'); }
# Return undef if passed mysql 0 dates
$dt0 = dt_from_string( '0000-00-00', 'iso' );
is( $dt0, undef, "undefined returned for 0 iso date" );
+
+my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric' );
+cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
+
+$formatted = format_sqldatetime( undef, 'metric' );
+cmp_ok( $formatted, 'eq', q{},
+ 'format_sqldatetime formats undef as empty string' );