Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,85 @@
package DateTime::Format::DateParse;
# Copyright (C) 2005-6 Joshua Hoblitt
#
# $Id: DateParse.pm 4429 2010-04-10 19:14:33Z jhoblitt $
use strict;
use vars qw($VERSION);
$VERSION = '0.05';
use DateTime;
use DateTime::TimeZone;
use Date::Parse qw( strptime );
use Time::Zone qw( tz_offset );
sub parse_datetime {
my ($class, $date, $zone) = @_;
# str2time() calls strptime() internally so it's more efficent to use
# strptime() directly. However, the extra validation done by using
# DateTime->new() instad of DateTime->from_epoch() may make it into a net
# loss. In the end, it turns out that strptime()'s offset information is
# needed anyways.
my @t = strptime( $date, $zone );
return undef unless @t;
my ($ss, $mm, $hh, $day, $month, $year, $offset) = @t;
my %p;
if ( $ss ) {
my $fraction = $ss - int( $ss );
if ($fraction) {
my $nano = $fraction * 1e9;
if ( $nano != int( $nano ) ) {
$nano++ if $nano - int( $nano ) >= 0.5;
}
$p{ nanosecond } = int( $nano );
}
$p{ second } = int $ss;
}
$p{ minute } = $mm if $mm;
$p{ hour } = $hh if $hh;
$p{ day } = $day if $day;
$p{ month } = $month + 1 if $month;
$p{ year } = $year ? $year + 1900 : DateTime->now->year;
# unless there is an explict ZONE, Date::Parse seems to parse date only
# formats, eg. "1995-01-24", as being in the 'local' timezone.
unless ( defined $zone || defined $offset ) {
return DateTime->new(
%p,
time_zone => 'local',
);
}
if ( $zone ) {
if ( DateTime::TimeZone->is_valid_name( $zone ) ) {
return DateTime->new(
%p,
time_zone => $zone,
);
} else {
# attempt to convert Time::Zone tz's into an offset
return DateTime->new(
%p,
time_zone =>
# not an Olson timezone, no DST info
DateTime::TimeZone::offset_as_string( tz_offset( $zone ) ),
);
}
}
return DateTime->new(
%p,
time_zone =>
# not an Olson timezone, no DST info
DateTime::TimeZone::offset_as_string( $offset ),
);
}
1;
__END__

View File

@@ -0,0 +1,90 @@
=pod
=head1 NAME
DateTime::Format::DateParse - Parses Date::Parse compatible formats
=head1 SYNOPSIS
use DateTime::Format::DateParse;
my $dt = DateTime::Format::DateParse->parse_datetime( $date );
my $dt = DateTime::Format::DateParse->parse_datetime( $date, $zone );
=head1 DESCRIPTION
This module is a compatibility wrapper around L<Date::Parse>.
=head1 USAGE
=head2 Import Parameters
This module accepts no arguments to it's C<import> method and exports no
I<symbols>.
=head2 Methods
=head3 Class Methods
=over 4
=item * parse_datetime($date [, $zone])
Accepts a L<Date::Parse> compatible C<$date> string and optionally a
L<Time::Zone> compatible C<$zone> string.
Returns a L<DateTime> object.
=back
=head1 GOTCHAS
=over 4
=item * If L<parse_datetime> is called on a C<$date> that doesn't know specify
a timezone and C<$zone> is not set, then the timezone of the returned
L<DateTime> object will be set to the C<local> timezone. This is consistent
with the behavior of L<Date::Parse>.
=item * If L<parse_datetime> is called without a C<$zone> but the C<$date>
string I<does> specify a timezone/offset or if L<parse_datetime> is called with
a C<$zone> that L<DateTime::TimeZone> does not understand, the returned
L<DateTime> object will have it's timezone set to a fixed offset from UTC.
This means that C<DST> information is not available and date math will not
reflect C<DST> transitions. This may be resolved for true timezones by using
the L<DateTime::TimeZone::Alias> module to C<alias> the L<Time::Zone> timezone
to an Olson DB name. This may be done automatically in a future release.
=back
=head1 CREDITS
Graham Barr (GBARR) <gbarr@pobox.com>, author of L<Date::Parse>
Everyone at the DateTime C<Asylum>.
=head1 SUPPORT
Support for this module is provided via the <datetime@perl.org> email list. See
L<http://lists.perl.org/> for more details.
=head1 AUTHOR
Joshua Hoblitt (JHOBLITT) <jhoblitt@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2005-6 Joshua Hoblitt. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms as Perl
itself.
The full text of the licenses can be found in the I<LICENSE> file included with
this module, or in L<perlartistic> and L<perlgpl> as supplied with Perl 5.8.1
and later.
=head1 SEE ALSO
L<Date::Parse>, L<Time::Zone>, L<DateTime>, L<DateTime::TimeZone>,
L<DateTime::TimeZone::Alias>, L<http://datetime.perl.org/>
=cut