From cb397ea33ea59967757fe6b60dfb57aa7bfaca00 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 19 Jul 2019 10:08:38 +0200 Subject: [PATCH] DateTime: Funktionen zum Parsen von YYYY:MM:DD und YYYY:MM:DDTHH:MM:SS --- SL/Helper/DateTime.pm | 46 +++++++++++++++++++++++++++++++++++++++++++ t/helper/datetime.t | 19 +++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/SL/Helper/DateTime.pm b/SL/Helper/DateTime.pm index 831e30185..15ab95a7b 100644 --- a/SL/Helper/DateTime.pm +++ b/SL/Helper/DateTime.pm @@ -2,8 +2,12 @@ package DateTime; use strict; +use DateTime::Format::Strptime; + use SL::Util qw(_hashify); +my ($ymd_parser, $ymdhms_parser); + sub new_local { my ($class, %params) = @_; return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params); @@ -94,6 +98,37 @@ sub next_workday { return $self; } +sub from_ymd { + my ($class, $ymd_string) = @_; + + if (!$ymd_parser) { + $ymd_parser = DateTime::Format::Strptime->new( + pattern => '%Y-%m-%d', + locale => 'de_DE', + time_zone => 'local' + ); + } + + return $ymd_parser->parse_datetime($ymd_string // ''); +} + +sub from_ymdhms { + my ($class, $ymdhms_string) = @_; + + if (!$ymdhms_parser) { + $ymdhms_parser = DateTime::Format::Strptime->new( + pattern => '%Y-%m-%dT%H:%M:%S', + locale => 'de_DE', + time_zone => 'local' + ); + } + + $ymdhms_string //= ''; + $ymdhms_string =~ s{ }{T}; + + return $ymdhms_parser->parse_datetime($ymdhms_string); +} + 1; __END__ @@ -138,6 +173,17 @@ component (as opposed to L). The legacy name C is still supported. +=item C + +Parses a date string in the ISO 8601 format C and returns +an instance of L. The time is set to midnight (00:00:00). + +=item C + +Parses a date/time string in the ISO 8601 format +C (a space instead of C is also supported) and +returns an instance of L. + =item C Sets the object to the last day of object's month at midnight. Returns diff --git a/t/helper/datetime.t b/t/helper/datetime.t index afaa49e84..9d1dd03b9 100644 --- a/t/helper/datetime.t +++ b/t/helper/datetime.t @@ -1,4 +1,4 @@ -use Test::More tests => 50; +use Test::More tests => 60; use lib 't'; @@ -7,6 +7,9 @@ use Data::Dumper; use DateTime; use_ok 'SL::Helper::DateTime'; +my $local_tz = DateTime::TimeZone->new(name => 'local'); +my $mon_012345 = DateTime->new(year => 2014, month => 6, day => 23, hour => 1, minute => 23, second => 45, time_zone => $local_tz); + sub mon { DateTime->new(year => 2014, month => 6, day => 23) } sub tue { DateTime->new(year => 2014, month => 6, day => 24) } sub wed { DateTime->new(year => 2014, month => 6, day => 25) } @@ -87,3 +90,17 @@ is sun->add_businessdays(days => 1), sun->add(days => 1), "1 day after sun is mo is sat->add_businessdays(days => 1), sat->add(days => 2), "1 day after sut is mon"; is sun->add_businessdays(days => -1), sun->add(days => -2), "1 day before sun is fri"; is sat->add_businessdays(days => -1), sat->add(days => -1), "1 day before sut is fri"; + +# parsing YYYY-MM-DD formatted strings +is(DateTime->from_ymd(), undef, "no argument results in undef"); +is(DateTime->from_ymd(''), undef, "empty argument results in undef"); +is(DateTime->from_ymd('chunky bacon'), undef, "invalid argument results in undef"); +is(DateTime->from_ymd('2014-06-23'), $mon_012345->clone->truncate(to => 'day'), "2014-06-23 is parsed correctly"); +is(DateTime->from_ymd('2014-06-23')->strftime('%H%M%S'), '000000', "2014-06-23 is parsed correctly"); + +# parsing YYYY-MM-DDTHH:MM:SS formatted strings +is(DateTime->from_ymdhms(), undef, "no argument results in undef"); +is(DateTime->from_ymdhms(''), undef, "empty argument results in undef"); +is(DateTime->from_ymdhms('chunky bacon'), undef, "invalid argument results in undef"); +is(DateTime->from_ymdhms('2014-06-23T01:23:45'), $mon_012345, "2014-06-23T01:23:45 is parsed correctly"); +is(DateTime->from_ymdhms('2014-06-23 01:23:45'), $mon_012345, "2014-06-23 01:23:45 is parsed correctly"); -- 2.20.1