3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00

Utils/ParseDate: alias UTC to GMT; minor improvements

This commit is contained in:
Pragmatic Software 2021-07-05 13:48:51 -07:00
parent 40c0bcfbb4
commit 17b2eb5a3a

View File

@ -1,7 +1,7 @@
# File: ParseDate.pm # File: ParseDate.pm
# #
# Purpose: Intelligently parses strings like "3pm", "5 minutes", "next week", # Purpose: Intelligently parses strings like "1h30m", "5 minutes", "next week",
# etc, into seconds. # "3:30 am pdt", "11 pm utc", etc, into seconds.
# This Source Code Form is subject to the terms of the Mozilla Public # This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
@ -17,9 +17,9 @@ use DateTime::Format::Duration;
sub new { sub new {
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH'; Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH';
my ($class, %conf) = @_; my ($class, %args) = @_;
my $self = bless {}, $class; my $self = bless {}, $class;
$self->initialize(%conf); $self->initialize(%args);
return $self; return $self;
} }
@ -42,10 +42,17 @@ sub unconcise {
sub parsedate { sub parsedate {
my ($self, $input) = @_; my ($self, $input) = @_;
my $examples = "Try `30s`, `1h30m`, `tomorrow`, `next monday`, `9:30am pdt`, `11pm utc`, etc.";
my $attempts = 0;
my $original_input = $input;
my $override = ""; my $override = "";
TRY_AGAIN: TRY_AGAIN:
$input = "$override$input" if length $override; $input = "$override$input" if length $override;
return (0, "Could not parse `$original_input`. $examples") if ++$attempts > 10;
# expand stuff like 7d3h # expand stuff like 7d3h
$input = unconcise($input); $input = unconcise($input);
@ -55,6 +62,7 @@ sub parsedate {
$input =~ s/\bhrs?\b/hours/g; $input =~ s/\bhrs?\b/hours/g;
$input =~ s/\bwks?\b/weeks/g; $input =~ s/\bwks?\b/weeks/g;
$input =~ s/\byrs?\b/years/g; $input =~ s/\byrs?\b/years/g;
$input =~ s/\butc\b/gmt/g;
# sanitizers # sanitizers
$input =~ s/\b(\d+)\s+(am?|pm?)\b/$1$2/; # remove leading spaces from am/pm $input =~ s/\b(\d+)\s+(am?|pm?)\b/$1$2/; # remove leading spaces from am/pm
@ -107,16 +115,17 @@ sub parsedate {
$to = eval { return DateTime::Format::Flexible->parse_datetime($input, lang => ['en'], base => $base); }; $to = eval { return DateTime::Format::Flexible->parse_datetime($input, lang => ['en'], base => $base); };
# If there's still an error, it's bad input # If there's still an error, it's bad input
if ($@) { if (my $error = $@) {
$@ =~ s/ ${override}from now at .*$//; $error =~ s/ ${override}from now at .*$//;
return (0, $@); $error =~ s/\s*$/. $examples/;
return (0, $error);
} }
} }
# there was a timezone parsed, set the tz override and try again # there was a timezone parsed, set the tz override and try again
if ($to->time_zone_short_name ne 'floating' and $to->time_zone_short_name ne 'UTC' and $tz_override eq 'UTC') { if ($to->time_zone_short_name ne 'floating' and $to->time_zone_short_name ne 'UTC' and $tz_override eq 'UTC') {
$tz_override = $to->time_zone_long_name; $tz_override = $to->time_zone_long_name;
$to = undef; $to = undef;
goto ADJUST_TIMEZONE; goto ADJUST_TIMEZONE;
} }
@ -126,8 +135,11 @@ sub parsedate {
# If the time is in the past, prepend "tomorrow" or "next" and reparse # If the time is in the past, prepend "tomorrow" or "next" and reparse
if ($duration->is_negative) { if ($duration->is_negative) {
if ($input =~ m/^\d/) { $override = "tomorrow "; } if ($input =~ m/^\d/) {
else { $override = "next "; } $override = "tomorrow ";
} else {
$override = "next ";
}
$to = undef; $to = undef;
goto TRY_AGAIN; goto TRY_AGAIN;
} }