2015-04-14 00:43:19 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# 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
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-04-14 00:43:19 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
package PBot::Utils::ParseDate;
|
|
|
|
|
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw/Exporter/;
|
|
|
|
our @EXPORT = qw/parsedate/;
|
|
|
|
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
|
|
|
|
require Time::ParseDate;
|
|
|
|
|
|
|
|
sub parsedate {
|
|
|
|
my $input = shift @_;
|
|
|
|
my $now = gettimeofday;
|
|
|
|
my @inputs = split /(?:,?\s+and\s+|\s*,\s*)/, $input;
|
|
|
|
|
|
|
|
my $seconds = 0;
|
|
|
|
foreach my $input (@inputs) {
|
2015-05-07 06:13:39 +02:00
|
|
|
return -1 if $input =~ m/forever/i;
|
2015-05-28 01:11:16 +02:00
|
|
|
$input .= ' seconds' if $input =~ m/^\s*\d+\s*$/;
|
2015-05-07 06:13:39 +02:00
|
|
|
|
2015-04-14 00:43:19 +02:00
|
|
|
my $parse = Time::ParseDate::parsedate($input, NOW => $now);
|
|
|
|
|
|
|
|
if (not defined $parse) {
|
2017-04-11 04:16:55 +02:00
|
|
|
$input =~ s/\s+$//;
|
|
|
|
return (0, "I don't know what '$input' means. I expected a time duration like '5 minutes' or '24 hours' or 'next tuesday'.\n");
|
2015-04-14 00:43:19 +02:00
|
|
|
} else {
|
|
|
|
$seconds += $parse - $now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 06:13:39 +02:00
|
|
|
return $seconds;
|
2015-04-14 00:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|