File Coverage

blib/lib/App/Sqitch/DateTime.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
1              
2             use 5.010;
3 39     39   93809 use strict;
  39         223  
4 39     39   775 use warnings;
  39         272  
  39         984  
5 39     39   478 use utf8;
  39         82  
  39         1215  
6 39     39   208 use parent 'DateTime';
  39         84  
  39         307  
7 39     39   1205 use DateTime 1.04;
  39         261  
  39         342  
8 38     38   16445385 use Locale::TextDomain qw(App-Sqitch);
  38         832  
  37         1182  
9             use App::Sqitch::X qw(hurl);
10             use List::Util qw(first);
11             use constant ISWIN => $^O eq 'MSWin32';
12              
13             our $VERSION = 'v1.3.0'; # VERSION
14              
15             return qw(
16             raw
17             iso
18             iso8601
19             rfc
20             rfc2822
21             full
22             long
23             medium
24             short
25             );
26             }
27              
28             my ( $self, $format ) = @_;
29             hurl datetime => __x(
30             'Unknown date format "{format}"',
31             format => $format
32             ) unless (first { $format eq $_ } $self->as_string_formats)
33             || $format =~ /^(?:cldr|strftime):/;
34             return $self;
35             }
36              
37             my ( $self, %opts ) = @_;
38             my $format = $opts{format} || 'raw';
39             my $dt = $self->clone;
40              
41             if ($format eq 'raw') {
42             $dt->set_time_zone('UTC');
43             return $dt->iso8601 . 'Z';
44             }
45              
46             $dt->set_time_zone('local');
47              
48             if ( first { $format eq $_ } qw(iso iso8601) ) {
49             return join ' ', $dt->ymd('-'), $dt->hms(':'), $dt->strftime('%z');
50             } elsif ( first { $format eq $_ } qw(rfc rfc2822) ) {
51             $dt->set_locale('en_US');
52             ( my $rv = $dt->strftime('%a, %d %b %Y %H:%M:%S %z') ) =~
53             s/\+0000$/-0000/;
54             return $rv;
55             } else {
56             if (ISWIN) {
57             require Win32::Locale;
58             $dt->set_locale( Win32::Locale::get_locale() );
59             } else {
60             require POSIX;
61             $dt->set_locale( POSIX::setlocale( POSIX::LC_TIME() ) );
62             }
63             return $dt->format_cldr($format) if $format =~ s/^cldr://;
64             return $dt->strftime($format) if $format =~ s/^strftime://;
65             my $meth = $dt->locale->can("datetime_format_$format") or hurl(
66             datetime => __x(
67             'Unknown date format "{format}"',
68             format => $format
69             )
70             );
71             return $dt->format_cldr( $dt->locale->$meth );
72             }
73             }
74              
75             1;
76              
77              
78             =head1 Name
79              
80             App::Sqitch::DateTime - Sqitch DateTime object
81              
82             =head1 Synopsis
83              
84             my $dt = App::Sqitch::DateTime->new(%params);
85             say $dt->as_string( format => 'iso' );
86              
87             =head1 Description
88              
89             This subclass of L<DateTime> provides additional interfaces to support named
90             formats. These can be used for L<status|sqitch-status> or L<log|sqitch-log>
91             C<--date-format> options. App::Sqitch::DateTime provides a list of supported
92             formats, validates that a format string, and uses the formats to convert
93             itself into the appropriate string.
94              
95             =head1 Interface
96              
97             =head2 Class Methods
98              
99             =head3 C<as_string_formats>
100              
101             my @formats = App::Sqitch::DateTime->as_string_formats;
102              
103             Returns a list of formats supported by the C<format> parameter to
104             C<as_string>. The list currently includes:
105              
106             =over
107              
108             =item C<iso>
109              
110             =item C<iso8601>
111              
112             ISO-8601 format.
113              
114             =item C<rfc>
115              
116             =item C<rfc2822>
117              
118             RFC-2822 format.
119              
120             =item C<full>
121              
122             =item C<long>
123              
124             =item C<medium>
125              
126             =item C<short>
127              
128             Localized format of the specified length.
129              
130             =item C<raw>
131              
132             Show timestamps in raw format, which is strict ISO-8601 in the UTC time zone.
133              
134             =item C<strftime:$string>
135              
136             Show timestamps using an arbitrary C<strftime> pattern. See
137             L<DateTime/strftime Paterns> for comprehensive documentation of supported
138             patterns.
139              
140             =item C<cldr:$string>
141              
142             Show timestamps using an arbitrary C<cldr> pattern. See L<DateTime/CLDR
143             Paterns> for comprehensive documentation of supported patterns.
144              
145             =back
146              
147             =head3 C<validate_as_string_format>
148              
149             App::Sqitch::DateTime->validate_as_string_format($format);
150              
151             Validates that a format is supported by C<as_string>. Throws an exception if
152             it's not, and returns if it is.
153              
154             =head2 Instance Methods
155              
156             =head3 C<as_string>
157              
158             $dt->as_string;
159             $dt->as_string( format => $format );
160              
161             Returns a string representation using the provided format. The format must be
162             one of those listed by C<as_string_formats> or an exception will be thrown. If
163             no format is passed, the string will be formatted with the C<raw> format.
164              
165             =head1 See Also
166              
167             =over
168              
169             =item L<sqitch-status>
170              
171             Documentation for the C<status> command to the Sqitch command-line client.
172              
173             =item L<sqitch-log>
174              
175             Documentation for the C<log> command to the Sqitch command-line client.
176              
177             =item L<sqitch>
178              
179             The Sqitch command-line client.
180              
181             =back
182              
183             =head1 Author
184              
185             David E. Wheeler <david@justatheory.com>
186              
187             =head1 License
188              
189             Copyright (c) 2012-2022 iovation Inc., David E. Wheeler
190              
191             Permission is hereby granted, free of charge, to any person obtaining a copy
192             of this software and associated documentation files (the "Software"), to deal
193             in the Software without restriction, including without limitation the rights
194             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
195             copies of the Software, and to permit persons to whom the Software is
196             furnished to do so, subject to the following conditions:
197              
198             The above copyright notice and this permission notice shall be included in all
199             copies or substantial portions of the Software.
200              
201             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
202             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
204             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
205             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
206             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
207             SOFTWARE.
208              
209             =cut