File Coverage

blib/lib/Time/Moment/Role/Strptime.pm
Criterion Covered Total %
statement 21 21 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 4 4 100.0
pod 1 1 100.0
total 32 35 91.4


line stmt bran cond sub pod time code
1             package Time::Moment::Role::Strptime;
2              
3 1     1   620 use Role::Tiny;
  1         2  
  1         7  
4 1     1   161 use Carp ();
  1         3  
  1         15  
5 1     1   513 use Time::Piece;
  1         9998  
  1         5  
6              
7             our $VERSION = '0.001';
8              
9             requires 'from_object';
10              
11             sub strptime {
12 2     2 1 4547 my ($class, $str, $format) = @_;
13 2         4 my ($obj, $error);
14             {
15 2         3 local $@;
  2         3  
16 2 100       4 eval { $obj = $class->from_object(Time::Piece->strptime($str, $format)); 1 }
  2         8  
  1         184  
17             or $error = $@;
18             }
19 2 100       58 if (defined $error) {
20 1 50 33     13 die $error if ref $error or $error =~ m/\n(?!\z)/;
21 1         8 $error =~ s/ at .+? line [0-9]+\.\n\z//;
22 1         192 Carp::croak $error;
23             }
24 1         3 return $obj;
25             }
26              
27             1;
28              
29             =head1 NAME
30              
31             Time::Moment::Role::Strptime - strptime constructor for Time::Moment
32              
33             =head1 SYNOPSIS
34              
35             use Time::Moment;
36             use Role::Tiny ();
37              
38             my $class = Role::Tiny->create_class_with_roles('Time::Moment', 'Time::Moment::Role::Strptime');
39             my $moment = $class->strptime('2019-06-01', '%Y-%m-%d');
40              
41             =head1 DESCRIPTION
42              
43             This role composes the L method, which parses the input string
44             according to a L format, and constructs a L object.
45              
46             By default the returned L object is in UTC (possibly adjusted
47             by a parsed offset); to interpret the parsed time in another time zone, you can
48             use L:
49              
50             use Time::Moment;
51             use Role::Tiny ();
52              
53             my $class = Role::Tiny->create_class_with_roles('Time::Moment',
54             'Time::Moment::Role::Strptime', 'Time::Moment::Role::TimeZone');
55             my $moment = $class->strptime($input, $format)->with_system_offset_same_local;
56              
57             use DateTime::TimeZone::Olson 'olson_tz';
58             my $tz = olson_tz('America/Los_Angeles');
59             my $moment = $class->strptime($input, $format)->with_time_zone_offset_same_local($tz);
60              
61             =head1 METHODS
62              
63             =head2 strptime
64              
65             my $moment = $class->strptime($input, $format);
66              
67             Parses the input string according to the L format, and returns a
68             L object in UTC. Throws an exception on parsing or out-of-bounds
69             errors.
70              
71             Currently, L is used to implement C portably, but this
72             is considered an implementation detail.
73              
74             =head1 BUGS
75              
76             Report any issues on the public bugtracker.
77              
78             =head1 AUTHOR
79              
80             Dan Book
81              
82             =head1 COPYRIGHT AND LICENSE
83              
84             This software is Copyright (c) 2019 by Dan Book.
85              
86             This is free software, licensed under:
87              
88             The Artistic License 2.0 (GPL Compatible)
89              
90             =head1 SEE ALSO
91              
92             L