Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision cb397ea3

Von Moritz Bunkus vor mehr als 5 Jahren hinzugefügt

  • ID cb397ea33ea59967757fe6b60dfb57aa7bfaca00
  • Vorgänger d83769ec
  • Nachfolger 79c0cb3e

DateTime: Funktionen zum Parsen von YYYY:MM:DD und YYYY:MM:DDTHH:MM:SS

Unterschiede anzeigen:

SL/Helper/DateTime.pm
2 2

  
3 3
use strict;
4 4

  
5
use DateTime::Format::Strptime;
6

  
5 7
use SL::Util qw(_hashify);
6 8

  
9
my ($ymd_parser, $ymdhms_parser);
10

  
7 11
sub new_local {
8 12
  my ($class, %params) = @_;
9 13
  return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params);
......
94 98
  return $self;
95 99
}
96 100

  
101
sub from_ymd {
102
  my ($class, $ymd_string) = @_;
103

  
104
  if (!$ymd_parser) {
105
    $ymd_parser = DateTime::Format::Strptime->new(
106
      pattern   => '%Y-%m-%d',
107
      locale    => 'de_DE',
108
      time_zone => 'local'
109
    );
110
  }
111

  
112
  return $ymd_parser->parse_datetime($ymd_string // '');
113
}
114

  
115
sub from_ymdhms {
116
  my ($class, $ymdhms_string) = @_;
117

  
118
  if (!$ymdhms_parser) {
119
    $ymdhms_parser = DateTime::Format::Strptime->new(
120
      pattern   => '%Y-%m-%dT%H:%M:%S',
121
      locale    => 'de_DE',
122
      time_zone => 'local'
123
    );
124
  }
125

  
126
  $ymdhms_string //= '';
127
  $ymdhms_string   =~ s{ }{T};
128

  
129
  return $ymdhms_parser->parse_datetime($ymdhms_string);
130
}
131

  
97 132
1;
98 133

  
99 134
__END__
......
138 173

  
139 174
The legacy name C<from_lxoffice> is still supported.
140 175

  
176
=item C<from_ymd $string>
177

  
178
Parses a date string in the ISO 8601 format C<YYYY-MM-DD> and returns
179
an instance of L<DateTime>. The time is set to midnight (00:00:00).
180

  
181
=item C<from_ymdhms $string>
182

  
183
Parses a date/time string in the ISO 8601 format
184
C<YYYY-MM-DDTHH:MM:SS> (a space instead of C<T> is also supported) and
185
returns an instance of L<DateTime>.
186

  
141 187
=item C<end_of_month>
142 188

  
143 189
Sets the object to the last day of object's month at midnight. Returns
t/helper/datetime.t
1
use Test::More tests => 50;
1
use Test::More tests => 60;
2 2

  
3 3
use lib 't';
4 4

  
......
7 7
use DateTime;
8 8
use_ok 'SL::Helper::DateTime';
9 9

  
10
my $local_tz   = DateTime::TimeZone->new(name => 'local');
11
my $mon_012345 = DateTime->new(year => 2014, month => 6, day => 23, hour => 1, minute => 23, second => 45, time_zone => $local_tz);
12

  
10 13
sub mon { DateTime->new(year => 2014, month => 6, day => 23) }
11 14
sub tue { DateTime->new(year => 2014, month => 6, day => 24) }
12 15
sub wed { DateTime->new(year => 2014, month => 6, day => 25) }
......
87 90
is sat->add_businessdays(days => 1), sat->add(days => 2), "1 day after sut is mon";
88 91
is sun->add_businessdays(days => -1), sun->add(days => -2), "1 day before sun is fri";
89 92
is sat->add_businessdays(days => -1), sat->add(days => -1), "1 day before sut is fri";
93

  
94
# parsing YYYY-MM-DD formatted strings
95
is(DateTime->from_ymd(),                                 undef,                                     "no argument results in undef");
96
is(DateTime->from_ymd(''),                               undef,                                     "empty argument results in undef");
97
is(DateTime->from_ymd('chunky bacon'),                   undef,                                     "invalid argument results in undef");
98
is(DateTime->from_ymd('2014-06-23'),                     $mon_012345->clone->truncate(to => 'day'), "2014-06-23 is parsed correctly");
99
is(DateTime->from_ymd('2014-06-23')->strftime('%H%M%S'), '000000',                                  "2014-06-23 is parsed correctly");
100

  
101
# parsing YYYY-MM-DDTHH:MM:SS formatted strings
102
is(DateTime->from_ymdhms(),                      undef,       "no argument results in undef");
103
is(DateTime->from_ymdhms(''),                    undef,       "empty argument results in undef");
104
is(DateTime->from_ymdhms('chunky bacon'),        undef,       "invalid argument results in undef");
105
is(DateTime->from_ymdhms('2014-06-23T01:23:45'), $mon_012345, "2014-06-23T01:23:45 is parsed correctly");
106
is(DateTime->from_ymdhms('2014-06-23 01:23:45'), $mon_012345, "2014-06-23 01:23:45 is parsed correctly");

Auch abrufbar als: Unified diff