/usr/share/perl5/Math/Calc/Units/Convert/Time.pm is in libmath-calc-units-perl 1.07-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | package Math::Calc::Units::Convert::Time;
use base 'Math::Calc::Units::Convert::Metric';
use strict;
use vars qw(%units %pref %ranges %total_unit_map);
%units = ( minute => [ 60, 'sec' ],
hour => [ 60, 'minute' ],
day => [ 24, 'hour' ],
week => [ 7, 'day' ],
year => [ 365, 'day' ], # Inexact unit... ugh...
);
%pref = ( default => 1,
hour => 0.8,
day => 0.8,
week => 0.4,
minute => 0.9,
year => 0.9,
);
%ranges = ( default => [ 1, 300 ],
millisec => [ 1, 999 ],
sec => [ 1, 200 ],
minute => [ 2, 100 ],
hour => [ 1, 80 ],
day => [ 1, 500 ],
week => [ 1, 4 ],
year => [ 1, undef ],
);
sub major_pref {
return 2;
}
sub major_variants {
my ($self) = @_;
return grep { ($_ ne 'default') && ($_ ne 'week') } keys %ranges;
}
# Return a list of the variants of the canonical unit of time: 'sec'
sub variants {
my ($self, $base) = @_;
return 'sec', (keys %units), map { "${_}sec" } $self->get_prefixes({ small => 1 });
}
sub unit_map {
my ($self) = @_;
if (keys %total_unit_map == 0) {
%total_unit_map = (%{$self->SUPER::unit_map()}, %units);
}
return \%total_unit_map;
}
sub canonical_unit { return 'sec'; }
sub abbreviated_canonical_unit { return 's'; }
# demetric : string => [ mult, base ]
#
# Must override here to avoid megahours or milliweeks
#
sub demetric {
my ($self, $string) = @_;
if (my $prefix = $self->get_prefix($string)) {
my $tail = substr($string, length($prefix));
if ($tail =~ /^sec(ond)?s?$/) {
return ($self->get_metric($prefix), "sec");
}
return; # Should this fail, or assume it's a non-metric unit?
} else {
return (1, $string);
}
}
# simple_convert : unitName x unitName -> multiplier
#
# Does not allow msec (only millisec or ms)
#
sub simple_convert {
my ($self, $from, $to) = @_;
# sec, secs, second, seconds
$from = "sec" if $from =~ /^sec(ond)?s?$/i;
$from = "minute" if $from =~ /^min(ute)?s?$/i;
if (my $easy = $self->SUPER::simple_convert($from, $to)) {
return $easy;
}
# ms == millisec
if ($from =~ /^(.)s$/) {
my ($expansion) = $self->expand($1);
return $self->simple_convert($expansion . "sec", $to);
}
return; # Failed
}
##############################################################################
sub preference {
my ($self, $v) = @_;
my ($val, $unit) = @$v;
my $base = lc(($self->demetric($unit))[1]);
my $pref = $pref{$base} || $pref{default};
return $pref * $self->prefix_pref(substr($unit, 0, -length($base)));
}
sub get_ranges {
return \%ranges;
}
sub get_prefs {
return \%pref;
}
my @BREAKDOWN = qw(year week day hour minute sec ms us ns ps);
sub render {
my ($self, $val, $name, $power, $options) = @_;
my $full_name = $name;
if ($options->{abbreviate}) {
if ($name =~ /(\w+)sec/) {
my $prefix = $1;
my $mabbrev = $self->metric_abbreviation($prefix);
$name = $mabbrev . "s" unless $mabbrev eq $prefix;
}
}
my $basic = $self->SUPER::render($val, $name, $power, $options);
return $basic if $power != 1;
$val *= $self->simple_convert($full_name, 'sec');
my @spread = $self->spread($val, 'sec', $name, \@BREAKDOWN);
my $spread = join(" ", map { "$_->[0] $_->[1]" } @spread);
return "($basic = $spread)" if @spread > 1;
return $basic;
}
1;
|