#!/usr/bin/perl use strict; use warnings; use boolean qw(true false); use DateTime::Format::Natural; use Getopt::Long qw(:config no_auto_abbrev no_ignore_case); use Term::ReadLine; use constant LANG_DEFAULT => 'en'; my %args; my $lang; my @supported_languages = qw(en); my $trace; my %valid_languages = map { $_ => true } @supported_languages; { my $opts = {}; $opts = parse_switches() if @ARGV; set_values($opts); process(); } sub parse_switches { my %opts; GetOptions(\%opts, qw(f|format=s h|help l|lang=s p|prefer_future s|supported t|time_zone=s T|trace V|version)) or usage(); usage() if $opts{h}; version() if $opts{V}; supported() if $opts{s}; return \%opts; } sub set_values { my $opts = shift; $lang = $opts->{l} || LANG_DEFAULT; $trace = $opts->{T} || false; my %table = ( l => 'lang', f => 'format', p => 'prefer_future', t => 'time_zone', ); foreach my $opt (keys %$opts) { if (exists $table{$opt}) { $args{$table{$opt}} = $opts->{$opt}; } } } sub usage { print <new('dateparse'); my $prompt = 'dateparse> '; while (defined(my $input = $term->readline($prompt))) { $term->addhistory($input) if $input =~ /\S/; last if $input =~ /^(?:q(?:uit)?|exit)$/; if ($input =~ /^(?:\?|help)$/i) { print <new(%args); my @dt = $parse->parse_datetime_duration(string => $input); if ($parse->success) { foreach my $dt (@dt) { printf("%02d.%02d.%4d %02d:%02d:%02d\n", $dt->day, $dt->month, $dt->year, $dt->hour, $dt->min, $dt->sec); } } else { warn $parse->error, "\n"; } if ($trace) { print $parse->trace, "\n"; } } } =head1 NAME dateparse - frontend to DateTime::Format::Natural =head1 SYNOPSIS Usage: dateparse [switches] -f, --format format of numeric dates -h, --help this help screen -l, --lang language code -p, --prefer_future use futuristic dates (when possible) -s, --supported list of supported languages -t, --time_zone time zone string -T, --trace print trace after processing -V, --version print version =head1 AUTHOR Steven Schubiger =head1 LICENSE This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. See L =cut