| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2024 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package App::csvtool::Timetools 0.05; | ||||||||||
| 7 | |||||||||||
| 8 | 16 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 16 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 16 | ▶ | use experimental 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | C<App::csvtool::Timetools> - commands for F<csvtool> that handle timestamps | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | This module provides commands for the F<csvtool> wrapper script that deal with | ||||||||||
| 19 | timestamp data in fields. | ||||||||||
| 20 | |||||||||||
| 21 | =head2 Timestamp Parsing | ||||||||||
| 22 | |||||||||||
| 23 | When parsing a timestamp in order to generate a UNIX epoch time, only the 6 | ||||||||||
| 24 | basic fields (sec, min, hour, mday, mon, year) are used. Not all fields are | ||||||||||
| 25 | required. | ||||||||||
| 26 | |||||||||||
| 27 | Any missing fields less significant than the ones provided by the format are | ||||||||||
| 28 | filled in with default zeroes (or 1 for the mday field). For example, a format | ||||||||||
| 29 | that specifies only the mday, mon and year fields will take a default time of | ||||||||||
| 30 | 00:00:00 within each day. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | =head1 COMMON OPTIONS | ||||||||||
| 35 | |||||||||||
| 36 | =for highlighter | ||||||||||
| 37 | |||||||||||
| 38 | Commands in this module recognise the following common options | ||||||||||
| 39 | |||||||||||
| 40 | =head3 --timefmt | ||||||||||
| 41 | |||||||||||
| 42 | Format string to use for formatting or parsing timestamps. Defaults to | ||||||||||
| 43 | ISO 8601 standard, i.e. | ||||||||||
| 44 | |||||||||||
| 45 | %Y-%m-%dT%H:%M:%S | ||||||||||
| 46 | |||||||||||
| 47 | =head3 --utc, -U | ||||||||||
| 48 | |||||||||||
| 49 | Use UTC instead of local time. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 16 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | |||||||||||
| 55 | # Common opts | ||||||||||
| 56 | 16 | ▶ | use constant COMMON_COMMAND_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | { name => "timefmt=", description => "Format string to parse timestamps", | ||||||||||
| 58 | default => "%Y-%m-%dT%H:%M:%S" }, | ||||||||||
| 59 | { name => "utc|U", description => "Use UTC instead of local time" }, | ||||||||||
| 60 | 16 | ); | |||||||||
| 61 | |||||||||||
| 62 | 3 | ▶ | sub formattime ( $pkg, $opts, $time ) | ||||||||
|
Subroutine
formattime: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 63 | { | ||||||||||
| 64 | 3 | my $TIMEFMT = $opts->{timefmt}; | |||||||||
| 65 | 3 | ▶ | my $t = $opts->{utc} ? Time::Piece::gmtime( $time ) : Time::Piece::localtime( $time ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 3 | return $t->strftime( $TIMEFMT ); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # Time::Piece doesn't appear to document how to do local- vs UTC-based | ||||||||||
| 71 | # strptime+epoch conversions, but by code-diving and emperical testing it | ||||||||||
| 72 | # appears you can use a precanned instance with isdst set appropriately to | ||||||||||
| 73 | # call the ->strptime method on | ||||||||||
| 74 | # See also https://rt.cpan.org/Ticket/Display.html?id=168828 | ||||||||||
| 75 | 16 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 16 | TIME_PIECE_LOCAL => scalar Time::Piece->localtime, | |||||||||
| 77 | TIME_PIECE_UTC => scalar Time::Piece->gmtime, | ||||||||||
| 78 | 16 | }; | |||||||||
| 79 | |||||||||||
| 80 | 11 | ▶ | sub parsetime ( $pkg, $opts, $str ) | ||||||||
|
Subroutine
parsetime: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 81 | { | ||||||||||
| 82 | 11 | my $TIMEFMT = $opts->{timefmt}; | |||||||||
| 83 | |||||||||||
| 84 | # Time::Piece throws exceptions on parse failures; | ||||||||||
| 85 | 11 | my $t = eval { | |||||||||
| 86 | 11 | ▶ | ( $opts->{utc} ? TIME_PIECE_UTC : TIME_PIECE_LOCAL )->strptime( $str, $TIMEFMT ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | }; | ||||||||||
| 88 | 11 | ▶ | defined $t or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | warn( "Unable to parse '$str' as a timestamp\n" ), return undef; | ||||||||||
| 90 | |||||||||||
| 91 | 11 | return $t->epoch; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head1 COMMANDS | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | package App::csvtool::strftime | ||||||||||
| 99 | 16 | { | |||||||||
| 100 | 16 | ▶ | use base qw( App::csvtool::Timetools ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 101 | |||||||||||
| 102 | =head2 strftime | ||||||||||
| 103 | |||||||||||
| 104 | $ csvtool strftime -fFIELD --timefmt=... FILE | ||||||||||
| 105 | |||||||||||
| 106 | Formats a timestamp by using a C<strftime> format, replacing the field with | ||||||||||
| 107 | the same time formatted as a string. | ||||||||||
| 108 | |||||||||||
| 109 | =head3 --field, -f | ||||||||||
| 110 | |||||||||||
| 111 | The field index to format the timestamp into (defaults to 1). | ||||||||||
| 112 | |||||||||||
| 113 | =cut | ||||||||||
| 114 | |||||||||||
| 115 | 16 | ▶ | use constant COMMAND_DESC => "Format a timestamp from UNIX time"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 116 | |||||||||||
| 117 | 16 | ▶ | use constant COMMAND_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 118 | __PACKAGE__->COMMON_COMMAND_OPTS, | ||||||||||
| 119 | { name => "field|f=", description => "Field to use for timestamp", | ||||||||||
| 120 | default => 1 }, | ||||||||||
| 121 | 16 | ); | |||||||||
| 122 | |||||||||||
| 123 | 16 | ▶ | use constant WANT_READER => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 16 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | |||||||||||
| 126 | 1 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||
|
Subroutine
run: called CC=2 SCAR=6.9
| |||||||||||
| 127 | { | ||||||||||
| 128 | 1 | my $FIELD = $opts->{field}; $FIELD--; | |||||||||
| 129 | |||||||||||
| 130 | 1 | ▶ | while( my $row = $reader->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 3 | $row->[$FIELD] = __PACKAGE__->formattime( $opts, $row->[$FIELD] ); | |||||||||
| 132 | |||||||||||
| 133 | 3 | $output->( $row ); | |||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | package App::csvtool::strptime | ||||||||||
| 139 | 16 | { | |||||||||
| 140 | 16 | ▶ | use base qw( App::csvtool::Timetools ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 141 | |||||||||||
| 142 | =head2 strptime | ||||||||||
| 143 | |||||||||||
| 144 | $ csvtool strptime -fFIELD --timefmt=... FILE | ||||||||||
| 145 | |||||||||||
| 146 | Parses a timestamp by using a C<strptime> format, replacing the field with the | ||||||||||
| 147 | same time expressed as a UNIX epoch integer. | ||||||||||
| 148 | |||||||||||
| 149 | =head3 --field, -f | ||||||||||
| 150 | |||||||||||
| 151 | The field index to parse the timestamp from (defaults to 1). | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | 16 | ▶ | use constant COMMAND_DESC => "Parse a timestamp into UNIX time"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 156 | |||||||||||
| 157 | 16 | ▶ | use constant COMMAND_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 158 | __PACKAGE__->COMMON_COMMAND_OPTS, | ||||||||||
| 159 | { name => "field|f=", description => "Field to use for timestamp", | ||||||||||
| 160 | default => 1 }, | ||||||||||
| 161 | 16 | ); | |||||||||
| 162 | |||||||||||
| 163 | 16 | ▶ | use constant WANT_READER => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 164 | 16 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 165 | |||||||||||
| 166 | 2 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||
|
Subroutine
run: called CC=2 SCAR=6.9
| |||||||||||
| 167 | { | ||||||||||
| 168 | 2 | my $FIELD = $opts->{field}; $FIELD--; | |||||||||
| 169 | |||||||||||
| 170 | 2 | ▶ | while( my $row = $reader->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 6 | $row->[$FIELD] = __PACKAGE__->parsetime( $opts, $row->[$FIELD] ); | |||||||||
| 172 | |||||||||||
| 173 | 6 | $output->( $row ); | |||||||||
| 174 | } | ||||||||||
| 175 | } | ||||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | package App::csvtool::tsort | ||||||||||
| 179 | 16 | { | |||||||||
| 180 | 16 | ▶ | use base qw( App::csvtool::Timetools ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 181 | |||||||||||
| 182 | =head2 tsort | ||||||||||
| 183 | |||||||||||
| 184 | $ csvtool tsort -fFIELD --timefmt=... FILE | ||||||||||
| 185 | |||||||||||
| 186 | A variant of the basic C<sort> command that parses a timestamp from a field | ||||||||||
| 187 | and sorts rows in chronological order based on those timestamps. | ||||||||||
| 188 | |||||||||||
| 189 | =head3 --field, -f | ||||||||||
| 190 | |||||||||||
| 191 | The field index to parse the sorting timestamp from (defaults to 1). | ||||||||||
| 192 | |||||||||||
| 193 | =head3 --reverse, -r | ||||||||||
| 194 | |||||||||||
| 195 | Reverses the order of sorting. | ||||||||||
| 196 | |||||||||||
| 197 | =cut | ||||||||||
| 198 | |||||||||||
| 199 | 16 | ▶ | use constant COMMAND_DESC => "Sort rows into chronological order by a timestamp"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 200 | |||||||||||
| 201 | 16 | ▶ | use constant COMMAND_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 202 | __PACKAGE__->COMMON_COMMAND_OPTS, | ||||||||||
| 203 | { name => "field|f=", description => "Field to use for timestamp", | ||||||||||
| 204 | default => 1 }, | ||||||||||
| 205 | { name => "reverse|r", description => "Reverse order of sorting" }, | ||||||||||
| 206 | 16 | ); | |||||||||
| 207 | |||||||||||
| 208 | 16 | ▶ | use constant WANT_READER => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 209 | 16 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 210 | |||||||||||
| 211 | 1 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||
|
Subroutine
run: called CC=6 SCAR=18.0
| |||||||||||
| 212 | { | ||||||||||
| 213 | 1 | my $FIELD = $opts->{field}; $FIELD--; | |||||||||
| 214 | |||||||||||
| 215 | 1 | my @rows; | |||||||||
| 216 | 1 | ▶ | while( my $row = $reader->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 217 | # Parse the timestamps on each line, rather than doing them all at | ||||||||||
| 218 | # once later using e.g. nsort_by {}, so that warnings come out at the | ||||||||||
| 219 | # right time | ||||||||||
| 220 | 5 | my $time = __PACKAGE__->parsetime( $opts, $row->[$FIELD] ); | |||||||||
| 221 | 5 | push @rows, [ $time, @$row ]; | |||||||||
| 222 | } | ||||||||||
| 223 | |||||||||||
| 224 | 1 | @rows = sort { $a->[0] <=> $b->[0] } @rows; | |||||||||
| 225 | 1 | shift @$_ for @rows; # remove timestamp keys | |||||||||
| 226 | |||||||||||
| 227 | 1 | ▶ | if( $opts->{reverse} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 228 | 0 | $output->( $_ ) for reverse @rows; | |||||||||
| 229 | } | ||||||||||
| 230 | else { | ||||||||||
| 231 | 1 | $output->( $_ ) for @rows; | |||||||||
| 232 | } | ||||||||||
| 233 | } | ||||||||||
| 234 | } | ||||||||||
| 235 | |||||||||||
| 236 | =head1 AUTHOR | ||||||||||
| 237 | |||||||||||
| 238 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 239 | |||||||||||
| 240 | =cut | ||||||||||
| 241 | |||||||||||
| 242 | 16 | 0x55AA; | |||||||||