dt_from_string
output_pref
format_sqldatetime
+ flatpickr_date_format
);
}
return q{};
}
+=head2 flatpickr_date_format
+
+$date_format = flatpickr_date_format( $koha_date_format );
+
+Converts Koha's date format to Flatpickr's. E.g. 'us' returns 'm/d/Y'.
+
+If no argument is given, the dateformat preference is assumed.
+
+Returns undef if format is unknown.
+
+=cut
+
+sub flatpickr_date_format {
+ my $arg = shift // C4::Context->preference('dateformat');
+ return {
+ us => 'm/d/Y',
+ metric => 'd/m/Y',
+ dmydot => 'd.m.Y',
+ iso => 'Y-m-d',
+ }->{$arg};
+}
+
1;
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
-use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::DateUtils qw( dt_from_string output_pref flatpickr_date_format );
my $builder = sub {
my ( $params ) = @_;
my $function_name = $params->{id};
my $date = output_pref({ dt => dt_from_string, dateonly => 1 });
-
- my $dateformat_pref = C4::Context->preference('dateformat');
- my $dateformat =
- $dateformat_pref eq 'us' ? 'm/d/Y'
- : $dateformat_pref eq 'metric' ? 'd/m/Y'
- : $dateformat_pref eq 'dmydot' ? 'd.m.Y'
- : 'Y-m-d';
+ my $dateformat = flatpickr_date_format();
my $res = <<END_OF_JS;
<script>
use C4::Context;
-use Test::More tests => 82;
+use Test::More tests => 83;
use Test::MockModule;
use Test::Warn;
use Time::HiRes qw/ gettimeofday /;
use Try::Tiny;
-use Koha::DateUtils qw( dt_from_string output_pref format_sqldatetime );
+use Koha::DateUtils qw( dt_from_string output_pref format_sqldatetime flatpickr_date_format );
use t::lib::Mocks;
is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' );
};
+subtest 'flatpickr_date_format' => sub {
+ plan tests => 4;
+
+ t::lib::Mocks::mock_preference('dateformat', 'iso');
+ is( flatpickr_date_format(), 'Y-m-d', 'check fallback to pref' );
+ is( flatpickr_date_format(q{}), undef, 'empty string' );
+ is( flatpickr_date_format('metric'), 'd/m/Y', 'check valid arg' );
+ is( flatpickr_date_format('zz'), undef, 'wrong arg' );
+};
+
# End of tests