Skip to content

Commit b225551

Browse files
committed
parsedate: Support formatting presets --iso, --epoch, --pretty
Suggested by Ben Bernard so common use cases don't require looking up strftime(3) format codes.
1 parent df68c09 commit b225551

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/App/RecordStream/Operation/parsedate.pm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ sub init {
2323
my $options = {
2424
'key|k=s' => sub { $this->{'KEYS'}->add_groups($_[1]) },
2525
'format|f=s' => \($this->{'FORMAT'}),
26+
'iso|iso8601' => sub { $this->{'FORMAT'} = '%FT%T%z' },
27+
'epoch' => sub { $this->{'FORMAT'} = '%s' },
28+
'pretty' => sub { $this->{'FORMAT'} = '%c' },
2629
'dmy' => \($this->{'UK'}),
2730
'past' => \($this->{'PAST'}),
2831
'future' => \($this->{'FUTURE'}),
@@ -34,8 +37,11 @@ sub init {
3437
};
3538
$this->parse_options($args, $options);
3639

37-
die "--key is required\n" unless $this->{'KEYS'}->has_any_group;
38-
die "--format is required\n" unless defined $this->{'FORMAT'};
40+
die "--key is required\n"
41+
unless $this->{'KEYS'}->has_any_group;
42+
43+
die "--format (or one of --iso, --epoch, or --pretty) is required\n"
44+
unless defined $this->{'FORMAT'};
3945
}
4046

4147
sub accept_record {
@@ -77,6 +83,9 @@ sub usage {
7783
my $options = [
7884
['key|-k <keys>', 'Datetime keys to parse and reformat; may be a key spec or key group. Required.'],
7985
['format|-f <strftime>', 'Format string for strftime(3). Required.'],
86+
['iso|--iso8601', 'Output datetimes as an ISO 8601 timestamp (equivalent to -f %FT%T%z)'],
87+
['epoch', 'Output datetimes as the number of seconds since the epoch (equivalent to -f %s)'],
88+
['pretty', 'Output datetimes in the locale-preferred format (equivalent to -f %c)'],
8089
['dmy', 'Assume dd/mm (UK-style) instead of mm/dd (US-style)'],
8190
['past', 'Assume ambiguous years and days of the week are in the past'],
8291
['future', 'Assume ambiguous years and days of the week are in the future'],

tests/RecordStream/Operation/parsedate.t

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,46 @@ use App::RecordStream::Operation::parsedate;
77

88
BEGIN {
99
# Normalize localtime for testing
10-
$ENV{TZ} = 'US/Pacific';
10+
$ENV{TZ} = 'US/Pacific';
11+
12+
# This may cause warnings if en_US isn't available on a system running these
13+
# tests, but it's the only way to standardize testing for --pretty.
14+
$ENV{LC_TIME} = 'en_US';
1115
}
1216

1317
# These tests aim to exercise the interplay of recs-provided options to
1418
# parsedate to ensure they're working correctly, not test Time::ParseDate's
1519
# functionality which is proven elsewhere.
1620

21+
note 'Formatting presets';
22+
{
23+
my @args = qw[ -k when --tz UTC ];
24+
25+
App::RecordStream::Test::OperationHelper->do_match(
26+
'parsedate',
27+
[@args, qw[ --iso ]],
28+
'{"when":"2016-02-28 18:45:18"}',
29+
'{"when":"2016-02-28T10:45:18-0800"}',
30+
"--iso: 2016-02-28 18:45:18 is 2016-02-28T10:45:18-0800",
31+
);
32+
33+
App::RecordStream::Test::OperationHelper->do_match(
34+
'parsedate',
35+
[@args, qw[ --epoch ]],
36+
'{"when":"2016-02-28 18:45:18"}',
37+
'{"when":"1456685118"}',
38+
"--epoch 2016-02-28 18:45:18 is 1456685118",
39+
);
40+
41+
App::RecordStream::Test::OperationHelper->do_match(
42+
'parsedate',
43+
[@args, qw[ --pretty ]],
44+
'{"when":"2016-02-28 18:45:18"}',
45+
'{"when":"Sun 28 Feb 2016 10:45:18 AM PST"}',
46+
"--pretty 2016-02-28 18:45:18 is Sun 28 Feb 2016 10:45:18 AM PST",
47+
);
48+
}
49+
1750
note 'Timezones';
1851
{
1952
my @args = qw[ -k when --format %T ];

0 commit comments

Comments
 (0)