File Coverage

blib/lib/Time/CTime.pm
Criterion Covered Total %
statement 37 39 94.8
branch 4 6 66.6
condition n/a
subroutine 11 12 91.6
pod 1 7 14.2
total 53 64 82.8


line stmt bran cond sub pod time code
1             package Time::CTime;
2              
3              
4             require 5.000;
5              
6 2     2   1938 use Time::Timezone;
  2         5  
  2         106  
7 2     2   15 use Time::CTime;
  2         3  
  2         107  
8             require Exporter;
9             @ISA = qw(Exporter);
10             @EXPORT = qw(ctime asctime strftime);
11             @EXPORT_OK = qw(asctime_n ctime_n @DoW @MoY @DayOfWeek @MonthOfYear);
12              
13 2     2   8 use strict;
  2         3  
  2         46  
14              
15             # constants
16 2     2   8 use vars qw(@DoW @DayOfWeek @MoY @MonthOfYear %strftime_conversion $VERSION);
  2         8  
  2         139  
17 2     2   8 use vars qw($template $sec $min $hour $mday $mon $year $wday $yday $isdst);
  2         4  
  2         2415  
18              
19             $VERSION = 2011.0505;
20              
21             CONFIG: {
22             @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
23             @DayOfWeek = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
24             @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
25             @MonthOfYear = qw(January February March April May June
26             July August September October November December);
27            
28             %strftime_conversion = (
29             '%', sub { '%' },
30             'a', sub { $DoW[$wday] },
31             'A', sub { $DayOfWeek[$wday] },
32             'b', sub { $MoY[$mon] },
33             'B', sub { $MonthOfYear[$mon] },
34             'c', sub { asctime_n($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, "") },
35             'd', sub { sprintf("%02d", $mday); },
36             'D', sub { sprintf("%02d/%02d/%02d", $mon+1, $mday, $year%100) },
37             'e', sub { sprintf("%2d", $mday); },
38             'f', sub { fracprintf ("%3.3f", $sec); },
39             'F', sub { fracprintf ("%6.6f", $sec); },
40             'h', sub { $MoY[$mon] },
41             'H', sub { sprintf("%02d", $hour) },
42             'I', sub { sprintf("%02d", $hour % 12 || 12) },
43             'j', sub { sprintf("%03d", $yday + 1) },
44             'k', sub { sprintf("%2d", $hour); },
45             'l', sub { sprintf("%2d", $hour % 12 || 12) },
46             'm', sub { sprintf("%02d", $mon+1); },
47             'M', sub { sprintf("%02d", $min) },
48             'n', sub { "\n" },
49             'o', sub { sprintf("%d%s", $mday, (($mday < 20 && $mday > 3) ? 'th' : ($mday%10 == 1 ? "st" : ($mday%10 == 2 ? "nd" : ($mday%10 == 3 ? "rd" : "th"))))) },
50             'p', sub { $hour > 11 ? "PM" : "AM" },
51             'r', sub { sprintf("%02d:%02d:%02d %s", $hour % 12 || 12, $min, $sec, $hour > 11 ? 'PM' : 'AM') },
52             'R', sub { sprintf("%02d:%02d", $hour, $min) },
53             'S', sub { sprintf("%02d", $sec) },
54             't', sub { "\t" },
55             'T', sub { sprintf("%02d:%02d:%02d", $hour, $min, $sec) },
56             'U', sub { wkyr(0, $wday, $yday) },
57             'v', sub { sprintf("%2d-%s-%4d", $mday, $MoY[$mon], $year+1900) },
58             'w', sub { $wday },
59             'W', sub { wkyr(1, $wday, $yday) },
60             'y', sub { sprintf("%02d",$year%100) },
61             'Y', sub { $year + 1900 },
62             'x', sub { sprintf("%02d/%02d/%02d", $mon + 1, $mday, $year%100) },
63             'X', sub { sprintf("%02d:%02d:%02d", $hour, $min, $sec) },
64             'Z', sub { &tz2zone(undef,undef,$isdst) }
65             # z sprintf("%+03d%02d", $offset / 3600, ($offset % 3600)/60);
66             );
67              
68              
69             }
70              
71             sub fracprintf {
72 2     2 0 4 my($t,$s) = @_;
73 2         22 my($p) = sprintf($t, $s-int($s));
74 2         6 $p=~s/^0+//;
75 2         6 $p;
76             }
77              
78             sub asctime_n {
79 2     2 0 5 my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $TZname) = @_;
80 2 50       8 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $TZname) = localtime($sec) unless defined $min;
81 2         4 $year += 1900;
82 2 100       7 $TZname .= ' '
83             if $TZname;
84 2         17 sprintf("%s %s %2d %2d:%02d:%02d %s%4d",
85             $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZname, $year);
86             }
87              
88             sub asctime
89             {
90 1     1 0 4 return asctime_n(@_)."\n";
91             }
92              
93             # is this formula right?
94             sub wkyr {
95 2     2 0 3 my($wstart, $wday, $yday) = @_;
96 2         5 $wday = ($wday + 7 - $wstart) % 7;
97 2         7 return int(($yday - $wday + 13) / 7 - 1);
98             }
99              
100             # ctime($time)
101              
102             sub ctime {
103 1     1 0 256 my($time) = @_;
104 1         9 asctime(localtime($time), &tz2zone(undef,$time));
105             }
106              
107             sub ctime_n {
108 0     0 0 0 my($time) = @_;
109 0         0 asctime_n(localtime($time), &tz2zone(undef,$time));
110             }
111              
112             # strftime($template, @time_struct)
113             #
114             # Does not support locales
115              
116             sub strftime {
117 38     38 1 680 local ($template, $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = @_;
118              
119 38         66 undef $@;
120 38         131 $template =~ s/%([%aAbBcdDefFhHIjklmMnopQrRStTUvwWxXyYZ])/&{$Time::CTime::strftime_conversion{$1}}()/egs;
  40         56  
  40         138  
121 38 50       98 die $@ if $@;
122 38         118 return $template;
123             }
124              
125             1;
126              
127             __END__