Skip to content

Commit

Permalink
abstract date parse by Time::ParseDate into ParseByTimeParseDate
Browse files Browse the repository at this point in the history
no functions changed.
we are going to add more date parse methods, this makes things more clear
  • Loading branch information
sunnavy committed Aug 12, 2015
1 parent 08df304 commit f446755
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions lib/RT/Date.pm
Expand Up @@ -213,35 +213,7 @@ sub Set {
}
}
elsif ( $format eq 'unknown' ) {
require Time::ParseDate;
# the module supports only legacy timezones like PDT or EST...
# so we parse date as GMT and later apply offset, this only
# should be applied to absolute times, so compensate shift in NOW
my $now = time;
$now += ($self->Localtime( $args{Timezone}, $now ))[9];
my ($date, $error) = Time::ParseDate::parsedate(
$args{'Value'},
GMT => 1,
NOW => $now,
UK => RT->Config->Get('DateDayBeforeMonth'),
PREFER_PAST => RT->Config->Get('AmbiguousDayInPast'),
PREFER_FUTURE => RT->Config->Get('AmbiguousDayInFuture'),
);
unless ( defined $date ) {
$RT::Logger->warning(
"Couldn't parse date '$args{'Value'}' by Time::ParseDate"
);
return $self->Unix(0);
}

# apply timezone offset
$date -= ($self->Localtime( $args{Timezone}, $date ))[9];

$RT::Logger->debug(
"RT::Date used Time::ParseDate to make '$args{'Value'}' $date\n"
);

return $self->Unix($date || 0);
return $self->Unix($self->ParseByTimeParseDate(%args) || 0);
}
else {
$RT::Logger->error(
Expand All @@ -253,6 +225,46 @@ sub Set {
return $self->Unix;
}

=head2 ParseByTimeParseDate
Parse date using Time::ParseDate.
return undef if it fails to parse, otherwise return epoch time.
=cut

sub ParseByTimeParseDate {
my $self = shift;
my %args = @_;
require Time::ParseDate;
# the module supports only legacy timezones like PDT or EST...
# so we parse date as GMT and later apply offset, this only
# should be applied to absolute times, so compensate shift in NOW
my $now = time;
$now += ($self->Localtime( $args{Timezone}, $now ))[9];
my ($date, $error) = Time::ParseDate::parsedate(
$args{'Value'},
GMT => 1,
NOW => $now,
UK => RT->Config->Get('DateDayBeforeMonth'),
PREFER_PAST => RT->Config->Get('AmbiguousDayInPast'),
PREFER_FUTURE => RT->Config->Get('AmbiguousDayInFuture'),
);
unless ( defined $date ) {
$RT::Logger->warning(
"Couldn't parse date '$args{'Value'}' by Time::ParseDate"
);
return undef;
}

# apply timezone offset
$date -= ($self->Localtime( $args{Timezone}, $date ))[9];

$RT::Logger->debug(
"RT::Date used Time::ParseDate to make '$args{'Value'}' $date\n"
);
return $date;
}

=head2 SetToNow
Set the object's time to the current time. Takes no arguments
Expand Down

0 comments on commit f446755

Please sign in to comment.