+ return if $date_string and $date_string =~ m|^0000-0|;
+
+ $tz = C4::Context->tz unless $tz;;
+
+ return DateTime->now( time_zone => $tz ) unless $date_string;
+
+ $date_format = C4::Context->preference('dateformat') unless $date_format;
+
+ if ( ref($date_string) eq 'DateTime' ) { # already a dt return it
+ return $date_string;
+ }
+
+ my $regex;
+
+ # The fallback format is sql/iso
+ my $fallback_re = qr|
+ (?<year>\d{4})
+ -
+ (?<month>\d{2})
+ -
+ (?<day>\d{2})
+ |xms;
+
+ if ( $date_format eq 'metric' ) {
+ # metric format is "dd/mm/yyyy[ hh:mm:ss]"
+ $regex = qr|
+ (?<day>\d{2})
+ /
+ (?<month>\d{2})
+ /
+ (?<year>\d{4})
+ |xms;
+ }
+ elsif ( $date_format eq 'dmydot' ) {
+ # dmydot format is "dd.mm.yyyy[ hh:mm:ss]"
+ $regex = qr|
+ (?<day>\d{2})
+ .
+ (?<month>\d{2})
+ .
+ (?<year>\d{4})
+ |xms;
+ }
+ elsif ( $date_format eq 'us' ) {
+ # us format is "mm/dd/yyyy[ hh:mm:ss]"
+ $regex = qr|
+ (?<month>\d{2})
+ /
+ (?<day>\d{2})
+ /
+ (?<year>\d{4})
+ |xms;
+ }
+ elsif ( $date_format eq 'rfc3339' ) {
+ $regex = qr/
+ (?<year>\d{4})
+ -
+ (?<month>\d{2})
+ -
+ (?<day>\d{2})
+ ([Tt\s])
+ (?<hour>\d{2})
+ :
+ (?<minute>\d{2})
+ :
+ (?<second>\d{2})
+ (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
+ /xms;
+ }
+ elsif ( $date_format eq 'iso' or $date_format eq 'sql' ) {
+ # iso or sql format are yyyy-dd-mm[ hh:mm:ss]"
+ $regex = $fallback_re;
+ }
+ else {
+ die "Invalid dateformat parameter ($date_format)";
+ }
+
+ # Add the faculative time part [hh:mm[:ss]]
+ my $time_re .= qr|
+ (
+ \s*
+ (?<hour>\d{2})
+ :
+ (?<minute>\d{2})
+ (
+ :
+ (?<second>\d{2})
+ )?
+ )?
+ |xms;
+ $regex .= $time_re;
+ $fallback_re .= $time_re;
+
+ my %dt_params;
+ if ( $date_string =~ $regex ) {
+ %dt_params = (
+ year => $+{year},
+ month => $+{month},
+ day => $+{day},
+ hour => $+{hour},
+ minute => $+{minute},
+ second => $+{second},
+ );
+ } elsif ( $date_string =~ $fallback_re ) {
+ %dt_params = (
+ year => $+{year},
+ month => $+{month},
+ day => $+{day},
+ hour => $+{hour},
+ minute => $+{minute},
+ second => $+{second},
+ );
+ }
+ else {
+ die "The given date ($date_string) does not match the date format ($date_format)";
+ }
+
+ # system allows the 0th of the month
+ $dt_params{day} = '01' if $dt_params{day} eq '00';
+
+ # Set default hh:mm:ss to 00:00:00
+ $dt_params{hour} = 00 unless defined $dt_params{hour};
+ $dt_params{minute} = 00 unless defined $dt_params{minute};
+ $dt_params{second} = 00 unless defined $dt_params{second};
+
+ my $dt = eval {
+ DateTime->new(
+ %dt_params,
+ # No TZ for dates 'infinite' => see bug 13242
+ ( $dt_params{year} < 9999 ? ( time_zone => $tz->name ) : () ),
+ );
+ };
+ if ($@) {
+ $tz = DateTime::TimeZone->new( name => 'floating' );
+ $dt = DateTime->new(
+ %dt_params,
+ # No TZ for dates 'infinite' => see bug 13242
+ ( $dt_params{year} < 9999 ? ( time_zone => $tz->name ) : () ),
+ );
+ }
+ return $dt;