File Coverage

lib/POSIX/1003/Time.pm
Criterion Covered Total %
statement 39 53 73.5
branch 5 12 41.6
condition 2 18 11.1
subroutine 11 14 78.5
pod 3 3 100.0
total 60 100 60.0


line stmt bran cond sub pod time code
1             # Copyrights 2011-2020 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution POSIX-1003. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package POSIX::1003::Time;
10 3     3   74549 use vars '$VERSION';
  3         17  
  3         183  
11             $VERSION = '1.02';
12              
13 3     3   19 use base 'POSIX::1003::Module';
  3         5  
  3         568  
14              
15 3     3   18 use warnings;
  3         6  
  3         82  
16 3     3   13 use strict;
  3         4  
  3         105  
17              
18 3     3   724 use POSIX::1003::Locale qw(setlocale LC_TIME);
  3         5  
  3         30  
19 3     3   1891 use Encode qw(find_encoding is_utf8 decode);
  3         31790  
  3         504  
20              
21             our @IN_CORE = qw/gmtime localtime/;
22              
23             my @constants;
24             my @functions = qw/
25             asctime ctime strftime
26             clock difftime mktime
27             tzset tzname strptime/;
28             push @functions, @IN_CORE;
29              
30             our %EXPORT_TAGS =
31             ( constants => \@constants
32             , functions => \@functions
33             , tables => [ '%time' ]
34             );
35              
36             my $time;
37             our %time;
38              
39             BEGIN {
40 3     3   22 $time = time_table;
41 3         11 push @constants, keys %$time;
42 3         26 tie %time, 'POSIX::1003::ReadOnlyTable', $time;
43             }
44              
45              
46             sub _tm_flatten($)
47 0     0   0 { my $tm = shift;
48             ( $tm->{sec} // 0, $tm->{min} // 0, $tm->{hour} // 0
49             , $tm->{day}-1, $tm->{month}-1, $tm->{year}-1900
50 0   0     0 , $tm->{wday} // -1, $tm->{yday} // -1, $tm->{is_dst} // -1
      0        
      0        
      0        
      0        
      0        
51             );
52             }
53              
54             sub _tm_build($@)
55 1     1   2 { my $tm = shift;
56 1         4 @{$tm}{qw/sec min hour day month year wday yday isdst/} = @_;
  1         6  
57 1         3 $tm->{month}++;
58 1         2 $tm->{year} += 1900;
59 1         4 $tm;
60             }
61              
62             sub mktime(@)
63 0     0 1 0 { my @p;
64              
65             my $time;
66 0 0       0 if(@_==1)
67 0         0 { my $tm = shift;
68 0         0 ($time, my @t) = _mktime _tm_flatten $tm;
69 0 0       0 _tm_build $tm, @t if defined $time; # All fields may have changed
70             }
71             else
72 0         0 { ($time) = _mktime @_;
73             }
74              
75 0         0 $time;
76             }
77              
78              
79             sub strftime($@)
80 1     1 1 675 { my $fmt = shift;
81 1 50       5 local @_ = _tm_flatten $_[0] if @_==1;
82              
83             #XXX See https://github.com/abeltje/lc_time for the correct implementation,
84             # using nl_langinfo(CODESET)
85              
86 1         8 my $lc = setlocale LC_TIME;
87 1 50 33     10 if($lc && $lc =~ m/\.([\w-]+)/ && (my $enc = find_encoding $1))
      33        
88             { # enforce the format string (may contain any text) to the same
89             # charset as the locale is using.
90 0         0 my $rawfmt = $enc->encode($fmt);
91 0         0 return $enc->decode(_strftime($rawfmt, @_));
92             }
93              
94 1 50       7 if(is_utf8($fmt))
95             { # no charset in locale, hence ascii inserts
96 0         0 my $out = _strftime(encode($fmt, 'utf8'), @_);
97 0         0 return decode $out, 'utf8';
98             }
99              
100             # don't know about the charset
101 1         15 _strftime($fmt, @_);
102             }
103              
104              
105              
106             sub strptime($$)
107 2 100   2 1 3473 { return _strptime @_
108             if wantarray;
109              
110 1         2 my $tm = {};
111 1         16 _tm_build $tm, _strptime @_;
112             }
113              
114              
115             sub _create_constant($)
116 1     1   3 { my ($class, $name) = @_;
117 1         4 my $val = $time->{$name};
118 1     0   9 sub () {$val};
  0            
119             }
120              
121             1;