File Coverage

lib/DateTime/Format/x509.pm
Criterion Covered Total %
statement 24 25 96.0
branch 2 4 50.0
condition 2 5 40.0
subroutine 7 7 100.0
pod 2 3 66.6
total 37 44 84.0


line stmt bran cond sub pod time code
1             package DateTime::Format::x509;
2             {
3             $DateTime::Format::x509::VERSION = '0.01';
4             }
5              
6             # ABSTRACT: parse and format x509 type dates
7              
8 2     2   257689 use strict;
  2         5  
  2         71  
9 2     2   9 use warnings;
  2         5  
  2         50  
10              
11 2     2   1521 use DateTime;
  2         227801  
  2         62  
12 2     2   19 use DateTime::TimeZone;
  2         4  
  2         796  
13              
14             sub new {
15 2     2 0 28 my $class = shift;
16 2         7 my %opts = @_;
17              
18 2         10 return bless \%opts, $class;
19             }
20              
21             sub parse_datetime {
22 5     5 1 2286 my ($self, $string) = @_;
23              
24 5 50       30 $string =~ s/(...) (..) (..):(..):(..) (....) (...)// || die 'Incorrectly formatted datetime';
25 5         26 my ( $b, $d, $H, $M, $S, $Y, $Z ) = ($1, $2, $3, $4, $5, $6, $7);
26              
27 5         38 my $month_by_name = {
28             Jan => 1,
29             Feb => 2,
30             Mar => 3,
31             Apr => 4,
32             May => 5,
33             Jun => 6,
34             Jul => 7,
35             Aug => 8,
36             Sep => 9,
37             Oct => 10,
38             Nov => 11,
39             Dec => 12
40             };
41              
42 5   50     41 my $month = $month_by_name->{$b} || die 'Invalid month';
43              
44 5 50 33     27 if($Z ne 'GMT' && $Z ne 'UTC') {
45 0         0 die "Invalid time zone '$Z'. RFC3161 requires times to be in UTC.";
46             }
47              
48 5         26 return DateTime->new(
49             year => 0 + $Y,
50             month => $month,
51             day => 0 + $d,
52             hour => 0 + $H,
53             minute => 0 + $M,
54             second => 0 + $S,
55             time_zone => 'UTC',
56             formatter => $self
57             );
58             }
59              
60             sub format_datetime {
61 5     5 1 5169 my ($self, $dt) = @_;
62 5         24 return $dt->strftime('%b %d %H:%M:%S %Y UTC');
63             }
64              
65             1;
66              
67              
68              
69             =pod
70              
71             =head1 NAME
72              
73             DateTime::Format::x509 - parse and format x509 type dates
74              
75             =head1 VERSION
76              
77             version 0.01
78              
79             =head1 SYNOPSIS
80              
81             use DateTime::Format::x509;
82              
83             my $f = DateTime::Format::x509->new();
84             my $dt = $f->parse_datetime('Mar 11 03:05:34 2013 UTC');
85              
86             # Mar 11 03:05:34 2013 UTC
87             print $f->format_datetime($dt);
88              
89             =head1 DESCRIPTION
90              
91             This module parses and formats x509 style datetime strings, used in certificates.
92              
93             =head1 NAME
94              
95             DateTime::Format::x509 - parse and format x509 type dates
96              
97             =head1 VERSION
98              
99             Version 1.0.2
100              
101             =head1 METHODS
102              
103             =over
104              
105             =item C<parse_datetime($string)>
106              
107             Given an x509 datetime string, this method will return a new L<DateTime> object.
108              
109             If given an improperly formatted string, this method will die.
110              
111             =item C<format_datetime($datetime)>
112              
113             Given a L<DateTime> object, this method returns an x509 timestamp string.
114              
115             =back
116              
117             =head1 AUTHOR
118              
119             Andrew Nelson <anelson@cpan.org>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2012 by Andrew Nelson.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut
129              
130              
131             __END__
132