File Coverage

blib/lib/DateTime/Format/Indonesian.pm
Criterion Covered Total %
statement 33 36 91.6
branch 5 8 62.5
condition 1 3 33.3
subroutine 8 8 100.0
pod 2 2 100.0
total 49 57 85.9


line stmt bran cond sub pod time code
1             package DateTime::Format::Indonesian;
2              
3 1     1   143011 use 5.010001;
  1         3  
4 1     1   6 use strict;
  1         2  
  1         20  
5 1     1   4 use warnings;
  1         2  
  1         22  
6              
7 1     1   5 use DateTime;
  1         2  
  1         511  
8              
9             our $VERSION = '0.03'; # VERSION
10              
11             our $_Current_Dt; # for testing only, to mock current time
12              
13             my %short_id_month_names = (
14             jan => 1,
15             feb => 2, peb => 2,
16             mar => 3, mrt => 3,
17             apr => 4,
18             mei => 5,
19             jun => 6,
20             jul => 7,
21             agu => 8, ags => 8, agt => 8,
22             sep => 9, sept => 9,
23             okt => 10,
24             nop => 11, nov => 11,
25             des => 12,
26             );
27              
28             my %short_en_month_names = (
29             jan => 1,
30             feb => 2,
31             mar => 3,
32             apr => 4,
33             may => 5,
34             jun => 6,
35             jul => 7,
36             aug => 8,
37             sep => 9,
38             oct => 10,
39             nov => 11,
40             dec => 12,
41             );
42              
43             my %long_id_month_names = (
44             januari => 1,
45             februari => 2, pebruari => 2,
46             maret => 3,
47             april => 4,
48             mei => 5,
49             juni => 6,
50             juli => 7,
51             agustus => 8,
52             september => 9,
53             oktober => 10,
54             november => 11, nopember => 12,
55             desember => 12,
56             );
57              
58             my %long_en_month_names = (
59             january => 1,
60             february => 2,
61             march => 3,
62             april => 4,
63             may => 5,
64             june => 6,
65             july => 7,
66             august => 8,
67             september => 9,
68             october => 10,
69             november => 11,
70             december => 12,
71             );
72              
73             # assume last century if year is larger than current year
74             sub _convert_to_4dig_year {
75 5     5   8 my ($self, $y) = @_;
76              
77 5   33     11 state $curdt = $_Current_Dt // DateTime->now;
78 5         41 state $curyear = $curdt->year;
79 5         9 state $cur2digy = $curyear % 100;
80 5         6 state $curcent = $curyear - $cur2digy;
81              
82 5 50       20 return $y if $y >= 100;
83 0 0       0 if ($y > $cur2digy) {
84 0         0 return $y + $curcent-100;
85             } else {
86 0         0 return $y + $curcent;
87             }
88             }
89              
90             sub _find_month {
91 6     6   12 my ($self, $m) = @_;
92              
93 6         8 state $months = do {
94 1         36 my %mm = (%short_en_month_names, %long_en_month_names,
95             %short_id_month_names, %long_id_month_names);
96 1         6 \%mm;
97             };
98              
99 6         9 $m = lc($m);
100 6 100       26 die "Invalid month name '$m'" unless $months->{$m};
101 5         10 $months->{$m};
102             }
103              
104             sub new {
105 1     1 1 17 my ($class) = @_;
106 1         4 bless {}, $class;
107             }
108              
109             sub parse_datetime {
110 7     7 1 3155 my ($self, $str) = @_;
111              
112 7 100       36 if ($str =~ m!^(\d+)[ /-]+(\w{3,15})[ ,/-]+(\d\d\d\d|\d\d)$!) {
113 6         13 my $d = $1;
114 6         16 my $m = $self->_find_month($2);
115 5         13 my $y = $self->_convert_to_4dig_year($3);
116 5         18 return DateTime->new(day=>$d, month=>$m, year=>$y);
117             } else {
118 1         7 return undef;
119             }
120             }
121              
122             1;
123             # ABSTRACT: Parse and format Indonesian dates
124              
125             __END__