NAME
Time::Duration::Parse::More - parse natural language time duration expressions
VERSION
version 0.008
SYNOPSIS
use Time::Duration::Parse::More;
my ($seconds);
$seconds = parse_duration('1 minute, 30 seconds'); ## 90
$seconds = parse_duration('1 minute plus 15 seconds'); ## 75
$seconds = parse_duration('1 minute minus 15 seconds'); ## 45
$seconds = parse_duration('1 day minus 2.5 hours and 10 minutes plus 15 seconds'); ## 76815
$seconds = parse_duration('minus 15 seconds'); ## -15
$seconds = parse_duration('midnight'); ## it depends :)
DESCRIPTION
The module parses a limited set of natural language expressions and converts them into seconds.
It is backwards compatible with Time::Duration::Parse (passes the same test cases), but adds more expressions and memoization.
At the moment, the module is limited to english language expressions.
Rules
The following rules are used to parse the expressions:
horizantal white-space, commas and the token and are ignored;
an expresion in the form
N factor
is translated toN * factor_in_seconds
.factor
is optional, defaults to seconds. Negative and fractional values ofN
are suported. Singular, plural and single letter versions offactor
are also recognised. All are case-insensitive except the single letter versions;expressions in the form
hh:mm:ss
,hh:mm
, andXhYmZs
(any order, all parts optional) are also supported;the tokens plus or minus change the signal of the expressions that follow them;
the final value is the sum of all the expressions taking in account the sign defined by the previous rule.
The hard-coded 'midnight' expression is also understood and returns the number of seconds up to 00:00:00 of the next day.
Factors
The following factors are understood, with the corresponding value in seconds between parentesis:
seconds (1): s, second, seconds, sec, and secs;
minutes (60): m, minute, minutes, min, and mins;
hours (60 * minutes factor): h, hr, hour, and hours;
days (24 * hours factor): d, day, and days;
weeks (7 * days factor): w, week, and weeks;
months (30 * days factor): M (note the case), month and months;
years (365 * days factor): y, year, and years;
FUNCTIONS
parse_duration
$seconds = parse_duration($expression);
Given an $expression
in natural lanaguage returns the number of seconds it represents. This result, with the exception of the 'midnight' expression, is cached so future calls with the same expression will be faster.
If the expression cannot be parsed, parse_duration
will croak.
parse_duration_nc
Same as "parse_duration", but the result will not be cached.
HISTORY
This module started as a private module for a closed-source project. I started to release it as Time::Delta
when I discovered Time::Duration::Parse. I updated the API to match it, and added my own improvements. This is the result.
SEE ALSO
Time::Duration::Parse and Time::Duration.
SUPPORT
Perldoc
You can find documentation for this module with the perldoc command.
perldoc Time::Duration::Parse::More
Websites
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
MetaCPAN
A modern, open-source CPAN search engine, useful to view POD in HTML format.
CPAN Testers
The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
http://www.cpantesters.org/distro/T/Time-Duration-Parse-More
CPAN Testers Matrix
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
http://matrix.cpantesters.org/?dist=Time-Duration-Parse-More
CPAN Testers Dependencies
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
http://deps.cpantesters.org/?module=Time::Duration::Parse::More
CPAN Ratings
The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
You can email the author of this module at MELO at cpan.org
asking for help with any problems you have.
Bugs / Feature Requests
Please report any bugs or feature requests through the web interface at https://github.com/melo/perl-time-duration-parse-more/issues. You will be automatically notified of any progress on the request by the system.
Source Code
The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)
https://github.com/melo/perl-time-duration-parse-more
git clone git://github.com/melo/perl-time-duration-parse-more.git
ACKNOWLEDGEMENTS
Stole test cases and other small tidbits from Miyagawa's Time::Duration::Parse.
AUTHOR
Pedro Melo <melo@cpan.org>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by Pedro Melo.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)