blib/lib/autobox/Time/Piece.pm

filter: Statementstmt 70.9% 22 / 31 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod n/a totaltotal 60.0% 27 / 45 CC 4 SCAR 16.1
CC
4
Cov
59%
strptime
12.0
AUTOLOAD
6.0
convert
2.0
CRAP 5.0
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package autobox::Time::Piece;
4  
5 # ABSTRACT: on-the-fly date conversion
6  
7 3 { our $VERSION = '0.001'; }
8  
9 3 ▶use base qw(autobox);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
12 3 my $class = shift;
13  
14 3 $class->SUPER::import(
15 HASH => 'autobox::Time::Piece::Subs',
16 ARRAY => 'autobox::Time::Piece::Subs',
17 SCALAR => 'autobox::Time::Piece::Subs',
18 );
19 }
20  
21 package autobox::Time::Piece::Subs;
22 3 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶sub strptime {
Subroutine strptime: not called CC=3 SCAR=24.8
25 0 my ($self, $fmt) = @_;
26 0 ▶ return $self if (ref $self eq 'Time::Piece');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "Time::Piece" 0 0 0
27 0 ▶ return localtime($self) unless $fmt;
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt 0 0 0
28 0 return Time::Piece->strptime($self, $fmt);
29 }
30 3 *parse = *strptime;
31 3 *format = *strftime;
32  
33 0 ▶sub convert { strptime(shift(), shift())->strftime(shift()) }
Subroutine convert: not called CC=1 SCAR=6.9
34  
35 sub format { shift()->strftime(shift()) }
36  
37 3 *Time::Piece::parse = *Time::Piece::strptime;
38 3 *Time::Piece::format = *Time::Piece::strftime;
39  
40 3 *autobox::Time::Piece::Subs::format = *autobox::Time::Piece::Subs::strftime;
41  
42 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
43 0 our $AUTOLOAD; # keep 'use strict' happy
44 0 my $function = $AUTOLOAD =~ s/.*:://r;
45  
46 0 ▶ die sprintf "Time::Piece has no \"%s\" function\n", $function unless Time::Piece->can($function);
Branch: 1 branch
BranchTrueFalseTotal
unless "Time::Piece"->can($function) 0 0 0
47  
48 0 return strptime(shift())->$function(@_);
49 }
50  
51  
52 3 1;
53  
54 =head1 NAME
55  
56 autobox::Time::Piece - on-the-fly date conversion
57  
58 =head1 SYNOPSIS
59  
60 use autobox::Time::Piece;
61  
62 my $date = '2025-07-12';
63 my $tp = $date->strptime('%Y-%m-%d');
64 my $str = $tp->format('%A, %B %d, %Y');
65  
66 my $converted = $date->convert('%Y-%m-%d', '%d/%m/%Y');
67  
68 =head1 DESCRIPTION
69  
70 This module extends scalars, arrays, and hashes with methods from L<Time::Piece>.
71 It allows strings and other data to behave like date objects through autoboxing.
72  
73 =head1 METHODS
74  
75 All methods are injected into SCALAR, ARRAY, and HASH types.
76  
77 =head2 SCALAR->strptime($format)
78  
79 Parses the string into a L<Time::Piece> object using the given C<strftime>-style format.
80 If no format is given, it falls back to C<localtime()>.
81 If the scalar is already a L<Time::Piece> object, it is returned unchanged.
82  
83 =head2 SCALAR->parse($format)
84  
85 Alias for L</strptime>.
86  
87 =head2 SCALAR->format($format)
88  
89 Formats a L<Time::Piece> object or a parseable string using the given format string.
90 Alias for L</strftime>.
91  
92 =head2 SCALAR->convert($from_format, $to_format)
93  
94 Parses the string using C<$from_format> and re-serializes it using C<$to_format>.
95  
96 =head2 AUTOLOAD
97  
98 Any method called on a scalar (or other supported type) that matches a method in
99 L<Time::Piece> will be dynamically delegated to it. For example:
100  
101 my $day_of_year = '2025-07-12'->strptime('%Y-%m-%d')->yday;
102  
103 # or directly:
104 my $dow = '2025-07-12'->wdayname;
105  
106 This mechanism ensures that common L<Time::Piece> methods like C<year>, C<mon>,
107 C<yday>, etc. are accessible without manual conversion.
108  
109 =head1 SEE ALSO
110  
111 L<autobox>, L<Time::Piece>, L<Time::Piece::strptime>
112  
113 =head1 AUTHOR
114  
115 Simone Cesano
116  
117 This software is copyright (c) 2025 by Simone Cesano.
118  
119 =head1 COPYRIGHT AND LICENSE
120  
121 This software is copyright (c) 2025 by Simone Cesano.
122  
123 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
↑ Summary