File Coverage

blib/lib/Time/Human.pm
Criterion Covered Total %
statement 6 37 16.2
branch 0 20 0.0
condition 0 3 0.0
subroutine 2 4 50.0
pod 0 2 0.0
total 8 66 12.1


line stmt bran cond sub pod time code
1             package Time::Human;
2              
3             require 5.005_62;
4 1     1   2883 use strict;
  1         2  
  1         42  
5 1     1   7 use warnings;
  1         2  
  1         1915  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10              
11             # Items to export into callers namespace by default. Note: do not export
12             # names by default without a very good reason. Use EXPORT_OK instead.
13             # Do not simply export all your public functions/methods/constants.
14              
15             # This allows declaration use Time::Human ':all';
16             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17             # will save memory.
18             our %EXPORT_TAGS = ( 'all' => [ qw(
19             humantime
20             ) ] );
21              
22             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23              
24             our @EXPORT = qw(
25             humanize
26            
27             );
28             our $VERSION = '1.03';
29              
30             our %templates = (
31              
32             English => {
33             numbers => [ qw(one two three four five six seven eight nine ten eleven twelve) ],
34             vagueness=> [ "exactly", "just after", "a little after", "coming up to", "almost"],
35             daytime => [ "in the morning", "in the afternoon", "in the evening", "at night" ],
36             minutes => ["five past", "ten past", "quarter past", "twenty past",
37             "twenty-five past", "half past", "twenty-five to",
38             "twenty to", "quarter to", "ten to", "five to"],
39             oclock => "o'clock",
40             midnight => "midnight",
41             midday => "midday",
42             format => "%v %m %h %d",
43             }
44             );
45              
46             our $Language = "English";
47             our $Evening = 18;
48             our $Night = 22;
49              
50             # Preloaded methods go here.
51              
52             sub humanize_base {
53 0     0 0   my ($hour, $minute) = @_[2,1];
54 0           my $vague = $minute % 5;
55 0           my $close_minute = $minute-$vague;
56 0           my $t = $templates{$Language};
57 0           my $say_hour;
58 0           my $daytime ="";
59 0 0         if ($vague > 2) {$close_minute += 5}
  0            
60 0 0         if ($close_minute >30) { $hour++; $hour %=24; }
  0            
  0            
61 0           $close_minute /= 5;
62 0           $close_minute %= 12;
63 0 0         if ($hour ==0) {
    0          
64 0           $say_hour = $t->{midnight};
65             } elsif ($hour == 12) {
66 0           $say_hour = $t->{midday};
67             } else {
68 0           $say_hour = $t->{numbers}[$hour%12-1];
69 0 0         $daytime = $hour <= 12 ? ($t->{daytime}[0]) :
    0          
    0          
70             $hour >= $Night ? $t->{daytime}[3] :
71             ($hour >= $Evening ? $t->{daytime}[2] :
72             $t->{daytime}[1]); # Afternoon
73             }
74 0 0         if ($close_minute==0) {
75 0 0 0       $say_hour .= " ". $t->{oclock} unless $hour ==0 or $hour == 12;
76             }
77 0 0         my $say_min = $close_minute ==0? "" : $t->{minutes}[$close_minute-1];
78 0           my $rv = $t->{format};
79 0           $rv =~ s/%v/$t->{vagueness}[$vague]/eg;
  0            
80 0           $rv =~ s/%m/$say_min/g;
81 0           $rv =~ s/%h/$say_hour/g;
82 0           $rv =~ s/%d/$daytime/g;
83 0           $rv =~ s/^\s+|(?<=\s)\s|\s+$//g;
84 0           return $rv;
85             }
86              
87             sub humanize {
88 0     0 0   my @foo = humanize_base(@_);
89 0           return (shift(@foo)." @foo");
90             }
91              
92             1;
93             __END__