| 1 | package Catmandu::Fix::expand_date; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | my $DATE_REGEX = qr{ | |||||||||
| 12 | ^([0-9]{4}) | ||||||||||
| 13 | (?: [:-] ([0-9]{1,2}) | ||||||||||
| 14 | (?: [:-] ([0-9]{1,2}) )? | ||||||||||
| 15 | )? | ||||||||||
| 16 | }x; | ||||||||||
| 17 | |||||||||||
| 18 | 1 | with 'Catmandu::Fix::Inlineable'; | |||||||||
| 19 | |||||||||||
| 20 | 1 | ▶ | has date_field => (fix_arg => 1, default => sub {'date'}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | sub fix { | ||||||||||
| 23 | 2 | ▶ | my ($self, $data) = @_; | ||||||||
|
Subroutine
fix: called CC=5 SCAR=16.5
Pod
undocumented
| |||||||||||
| 24 | 2 | ▶ | if (my $date = $data->{$self->date_field}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 2 | ▶ | if (my ($y, $m, $d) = $date =~ $DATE_REGEX) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 2 | $data->{year} = $y; | |||||||||
| 27 | 2 | ▶ | $data->{month} = 1 * $m if $m; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 2 | ▶ | $data->{day} = 1 * $d if $d; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | } | ||||||||||
| 30 | } | ||||||||||
| 31 | 2 | $data; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 1 | 1; | |||||||||
| 35 | |||||||||||
| 36 | __END__ | ||||||||||
| 37 | |||||||||||
| 38 | =pod | ||||||||||
| 39 | |||||||||||
| 40 | =head1 NAME | ||||||||||
| 41 | |||||||||||
| 42 | Catmandu::Fix::expand_date - expand a date field into year, month and date | ||||||||||
| 43 | |||||||||||
| 44 | =head1 NOTE | ||||||||||
| 45 | |||||||||||
| 46 | This package is DEPRECATED and will be removed in the future. | ||||||||||
| 47 | Please use L<Catmandu::Fix::split_date>. | ||||||||||
| 48 | |||||||||||
| 49 | Reasons: | ||||||||||
| 50 | |||||||||||
| 51 | =over 4 | ||||||||||
| 52 | |||||||||||
| 53 | =item | ||||||||||
| 54 | |||||||||||
| 55 | it writes directly in the root of the hash, which is a different | ||||||||||
| 56 | behaviour compared to all the other fixes (sum, count, hash, array ..) | ||||||||||
| 57 | |||||||||||
| 58 | =item | ||||||||||
| 59 | |||||||||||
| 60 | it adds the new keys in a different location, instead of "in place". | ||||||||||
| 61 | |||||||||||
| 62 | =item | ||||||||||
| 63 | |||||||||||
| 64 | its behaviour cannot be changed without breaking its current use | ||||||||||
| 65 | |||||||||||
| 66 | =back | ||||||||||
| 67 | |||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||
| 69 | |||||||||||
| 70 | # {date => "2001-09-11"} | ||||||||||
| 71 | expand_date() | ||||||||||
| 72 | # => {year => 2001, month => "9", day => "11", date => "2001-09-11"} | ||||||||||
| 73 | |||||||||||
| 74 | # {datestamp => "2001:09"} | ||||||||||
| 75 | expand_date(datestamp) | ||||||||||
| 76 | # => {year => 2001, month => "9", datestamp => "2001:09"} | ||||||||||
| 77 | |||||||||||
| 78 | =head1 DESCRIPTION | ||||||||||
| 79 | |||||||||||
| 80 | The date field is expanded if it contains a year, optionally followed by | ||||||||||
| 81 | numeric month and day, each separated by C<-> or C<:>. | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||