| 1 | package DateTime::Format::DateParse; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2005-6 Joshua Hoblitt | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # $Id: DateParse.pm 4429 2010-04-10 19:14:33Z jhoblitt $ | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use DateTime::TimeZone; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Date::Parse qw( strptime ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Time::Zone qw( tz_offset ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 260 | ▶ | sub parse_datetime { | ||||||||||||||||||||||||||
|
Subroutine
parse_datetime: called CC=15 SCAR=28.6
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 260 | my ($class, $date, $zone) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # str2time() calls strptime() internally so it's more efficent to use | ||||||||||||||||||||||||||||
| 21 | # strptime() directly. However, the extra validation done by using | ||||||||||||||||||||||||||||
| 22 | # DateTime->new() instad of DateTime->from_epoch() may make it into a net | ||||||||||||||||||||||||||||
| 23 | # loss. In the end, it turns out that strptime()'s offset information is | ||||||||||||||||||||||||||||
| 24 | # needed anyways. | ||||||||||||||||||||||||||||
| 25 | 260 | my @t = strptime( $date, $zone ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 260 | ▶ | return undef unless @t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 260 | my ($ss, $mm, $hh, $day, $month, $year, $offset) = @t; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 260 | my %p; | |||||||||||||||||||||||||||
| 32 | 260 | ▶ | if ( $ss ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 230 | my $fraction = $ss - int( $ss ); | |||||||||||||||||||||||||||
| 34 | 230 | ▶ | if ($fraction) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 4 | my $nano = $fraction * 1e9; | |||||||||||||||||||||||||||
| 36 | 4 | ▶ | if ( $nano != int( $nano ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | $nano++ if $nano - int( $nano ) >= 0.5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 4 | $p{ nanosecond } = int( $nano ); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 230 | $p{ second } = int $ss; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 260 | ▶ | $p{ minute } = $mm if $mm; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 260 | ▶ | $p{ hour } = $hh if $hh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 260 | ▶ | $p{ day } = $day if $day; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 260 | ▶ | $p{ month } = $month + 1 if $month; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 260 | ▶ | $p{ year } = $year ? $year + 1900 : DateTime->now->year; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # unless there is an explict ZONE, Date::Parse seems to parse date only | ||||||||||||||||||||||||||||
| 50 | # formats, eg. "1995-01-24", as being in the 'local' timezone. | ||||||||||||||||||||||||||||
| 51 | 260 | ▶ | unless ( defined $zone || defined $offset ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $zone or defined $offset
MC/DC0%0/2
defined $zone or defined $offset
defined $zone
defined $offset
Truth table33%1/3
A or B
A= defined $zoneB= defined $offset
| |||||||||||||||||||||||||||||
| 52 | 0 | return DateTime->new( | |||||||||||||||||||||||||||
| 53 | %p, | ||||||||||||||||||||||||||||
| 54 | time_zone => 'local', | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 260 | ▶ | if ( $zone ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( DateTime::TimeZone->is_valid_name( $zone ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | return DateTime->new( | |||||||||||||||||||||||||||
| 61 | %p, | ||||||||||||||||||||||||||||
| 62 | time_zone => $zone, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | # attempt to convert Time::Zone tz's into an offset | ||||||||||||||||||||||||||||
| 66 | 0 | return DateTime->new( | |||||||||||||||||||||||||||
| 67 | %p, | ||||||||||||||||||||||||||||
| 68 | time_zone => | ||||||||||||||||||||||||||||
| 69 | # not an Olson timezone, no DST info | ||||||||||||||||||||||||||||
| 70 | DateTime::TimeZone::offset_as_string( tz_offset( $zone ) ), | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 260 | return DateTime->new( | |||||||||||||||||||||||||||
| 76 | %p, | ||||||||||||||||||||||||||||
| 77 | time_zone => | ||||||||||||||||||||||||||||
| 78 | # not an Olson timezone, no DST info | ||||||||||||||||||||||||||||
| 79 | DateTime::TimeZone::offset_as_string( $offset ), | ||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | 1; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||