File Coverage

blib/lib/DateTime/Format/JavaScript.pm
Criterion Covered Total %
statement 38 40 95.0
branch n/a
condition n/a
subroutine 9 10 90.0
pod 1 1 100.0
total 48 51 94.1


line stmt bran cond sub pod time code
1             package DateTime::Format::JavaScript;
2              
3 2     2   23890 use warnings;
  2         5  
  2         69  
4 2     2   12 use strict;
  2         4  
  2         95  
5              
6             our $VERSION = '0.02';
7              
8 2     2   1874 use DateTime::TimeZone;
  2         112143  
  2         67  
9              
10 2     2   23 use constant WDAYS => qw/Mon Tue Wed Thu Fri Sat Sun/;
  2         3  
  2         121  
11 2     2   9 use constant MONTHS => qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
  2         4  
  2         137  
12 2     2   10 use constant RE_WDAYS => qr/@{[ join "|", WDAYS ]}/;
  2         5  
  2         3  
  2         209  
13 2     2   11 use constant RE_MONTHS => qr/@{[ join "|", MONTHS ]}/;
  2         3  
  2         3  
  2         744  
14              
15             {
16             my %mon2num;
17             my $i = 1;
18             foreach ( MONTHS ) {
19             $mon2num{$_} = $i++;
20             }
21              
22             sub _fix_month {
23 3     3   7836 my %args = @_;
24 3         9 my $p = $args{parsed};
25 3         12 $p->{month} = $mon2num{$p->{month}};
26 3         13 return 1;
27             }
28             }
29              
30             use DateTime::Format::Builder (
31 2         13 parsers => {
32             parse_datetime => [
33             { # UTC for IE style, GMT for Mozilla style
34             params => [qw/ month day hour minute second time_zone year /],
35 2         8 regex => qr/^@{[RE_WDAYS]} (@{[RE_MONTHS]}) (\d{1,2}) (\d\d):(\d\d):(\d\d) (?:UTC|GMT)([-+]\d{4}) (\d{4})$/,
  2         185  
36             postprocess => \&_fix_month,
37             },
38             { # For IE (when Date constructor called as function, it returns string representing current time without time-zone).
39             params => [qw/ month day hour minute second year /],
40 2         5 regex => qr/^@{[RE_WDAYS]} (@{[RE_MONTHS]}) (\d{1,2}) (\d\d):(\d\d):(\d\d) (\d{4})$/,
  2         119  
41             postprocess => \&_fix_month,
42             },
43             { # For Opera 9
44             params => [qw/ day month year hour minute second time_zone /],
45 2         5 regex => qr/^@{[RE_WDAYS]}, (\d{1,2}) (@{[RE_MONTHS]}) (\d{4}) (\d\d):(\d\d):(\d\d) GMT([-+]\d{4})$/,
  2         143  
46             postprocess => \&_fix_month,
47             },
48             { # Safari 7 / Chrome 36 / Firefox 30
49             params => [qw/ month day year hour minute second time_zone /],
50 2         6 regex => qr/^@{[RE_WDAYS]} (@{[RE_MONTHS]}) (\d{1,2}) (\d{4}) (\d\d):(\d\d):(\d\d) GMT([-+]\d{4})/,
  2         148  
51             postprocess => \&_fix_month,
52             },
53             ],
54             }
55 2     2   2047 );
  2         309225  
56              
57              
58             sub format_datetime {
59 0     0 1   my ($self, $dt) = @_;
60 0           sprintf "%s %s %d %02d:%02d:%02d UTC%+05d %04d",
61             (WDAYS)[$dt->wday-1],
62             (MONTHS)[$dt->mon-1],
63             $dt->day,
64             $dt->hour,
65             $dt->min,
66             $dt->sec,
67             DateTime::TimeZone->offset_as_string($dt->offset),
68             $dt->year;
69             }
70              
71              
72              
73             1;
74             __END__