| 1 | |||||||||||||||||
| 2 | package Time::Duration::sv; | ||||||||||||||||
| 3 | # Time-stamp: "2002-10-08 01:04:09 MDT" POD is at the end. | ||||||||||||||||
| 4 | 1 | $VERSION = '1.01'; | |||||||||||||||
| 5 | 1 | require Exporter; | |||||||||||||||
| 6 | 1 | @ISA = ('Exporter'); | |||||||||||||||
| 7 | 1 | @EXPORT = qw( later later_exact earlier earlier_exact | |||||||||||||||
| 8 | ago ago_exact from_now from_now_exact | ||||||||||||||||
| 9 | duration duration_exact | ||||||||||||||||
| 10 | concise | ||||||||||||||||
| 11 | ); | ||||||||||||||||
| 12 | 1 | @EXPORT_OK = ('interval', @EXPORT); | |||||||||||||||
| 13 | |||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 15 | 1 | ▶ | use constant DEBUG => 0; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 16 | 1 | ▶ | use Time::Duration qw(); | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 17 | # ALL SUBS ARE PURE FUNCTIONS | ||||||||||||||||
| 18 | |||||||||||||||||
| 19 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||
| 20 | |||||||||||||||||
| 21 | 29 | ▶ | sub concise ($) { | ||||||||||||||
|
Subroutine
concise: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||
| 22 | 29 | my $string = $_[0]; | |||||||||||||||
| 23 | #print "in : $string\n"; | ||||||||||||||||
| 24 | 29 | $string =~ s/,//g; | |||||||||||||||
| 25 | 29 | $string =~ s/\boch\b//; | |||||||||||||||
| 26 | 29 | $string =~ s/\b(dag|dagar|timme|timmar|minut|minuter|sekund|sekunder)\b/substr($1,0,1)/eg; | |||||||||||||||
| 27 | 29 | $string =~s/år/å/g; | |||||||||||||||
| 28 | 29 | my $save = ''; | |||||||||||||||
| 29 | 29 | ▶ | if($string =~s/(för|om)//) { | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 30 | 4 | $save = $1; | |||||||||||||||
| 31 | } | ||||||||||||||||
| 32 | 29 | $string =~ s/\s*(\d+)\s*/$1/g; | |||||||||||||||
| 33 | |||||||||||||||||
| 34 | 29 | ▶ | return $save ? $save . " " . $string : $string; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 35 | } | ||||||||||||||||
| 36 | |||||||||||||||||
| 37 | 68 | ▶ | sub later { | ||||||||||||||
|
Subroutine
later: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 38 | 68 | interval( $_[0], $_[1], '%s tidigare', '%s senare', 'just då'); } | |||||||||||||||
| 39 | 31 | ▶ | sub later_exact { | ||||||||||||||
|
Subroutine
later_exact: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 40 | 31 | interval_exact($_[0], $_[1], '%s tidigare', '%s senare', 'just då'); } | |||||||||||||||
| 41 | 6 | ▶ | sub earlier { | ||||||||||||||
|
Subroutine
earlier: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 42 | 6 | interval( $_[0], $_[1], '%s senare', '%s tidigare', 'just då'); } | |||||||||||||||
| 43 | 0 | ▶ | sub earlier_exact { | ||||||||||||||
|
Subroutine
earlier_exact: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||
| 44 | 0 | interval_exact($_[0], $_[1], '%s senare', '%s tidigare', 'just då'); } | |||||||||||||||
| 45 | 6 | ▶ | sub ago { | ||||||||||||||
|
Subroutine
ago: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 46 | 6 | interval( $_[0], $_[1], 'om %s', 'för %s sen', 'just nu'); } | |||||||||||||||
| 47 | 0 | ▶ | sub ago_exact { | ||||||||||||||
|
Subroutine
ago_exact: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||
| 48 | 0 | interval_exact($_[0], $_[1], 'för %s sen', '%s ago', 'just nu'); } | |||||||||||||||
| 49 | 6 | ▶ | sub from_now { | ||||||||||||||
|
Subroutine
from_now: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 50 | 6 | interval( $_[0], $_[1], 'för %s sen', 'om %s', 'just nu'); } | |||||||||||||||
| 51 | 0 | ▶ | sub from_now_exact { | ||||||||||||||
|
Subroutine
from_now_exact: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||
| 52 | 0 | interval_exact($_[0], $_[1], 'för %s sen', 'om %s', 'just nu'); } | |||||||||||||||
| 53 | |||||||||||||||||
| 54 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||
| 55 | 2 | ▶ | sub duration_exact { | ||||||||||||||
|
Subroutine
duration_exact: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||||||||
| 56 | 2 | my $span = $_[0]; # interval in seconds | |||||||||||||||
| 57 | 2 | ▶ | my $precision = int($_[1] || 0) || 2; # precision (default: 2) | ||||||||||||||
|
Condition50%2/4
int($_[1] || 0) || 2
$_[1] || 0
MC/DC0%0/1
int($_[1] || 0) || 2
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||
| 58 | 2 | ▶ | return '0 sekunder' unless $span; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 59 | 2 | _render('%s', | |||||||||||||||
| 60 | Time::Duration::_separate(abs $span)); | ||||||||||||||||
| 61 | } | ||||||||||||||||
| 62 | |||||||||||||||||
| 63 | 14 | ▶ | sub duration { | ||||||||||||||
|
Subroutine
duration: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||
| 64 | 14 | my $span = $_[0]; # interval in seconds | |||||||||||||||
| 65 | 14 | ▶ | my $precision = int($_[1] || 0) || 2; # precision (default: 2) | ||||||||||||||
|
Condition100%4/4
int($_[1] || 0) || 2
$_[1] || 0
MC/DC100%1/1
int($_[1] || 0) || 2
$_[1]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||
| 66 | 14 | ▶ | return '0 sekunder' unless $span; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 67 | 12 | _render('%s', | |||||||||||||||
| 68 | Time::Duration::_approximate($precision, | ||||||||||||||||
| 69 | Time::Duration::_separate(abs $span))); | ||||||||||||||||
| 70 | } | ||||||||||||||||
| 71 | |||||||||||||||||
| 72 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||
| 73 | |||||||||||||||||
| 74 | 31 | ▶ | sub interval_exact { | ||||||||||||||
|
Subroutine
interval_exact: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||
| 75 | 31 | my $span = $_[0]; # interval, in seconds | |||||||||||||||
| 76 | # precision is ignored | ||||||||||||||||
| 77 | 31 | ▶ | my $direction = ($span <= -1) ? $_[2] # what a neg number gets | ||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||
| 78 | : ($span >= 1) ? $_[3] # what a pos number gets | ||||||||||||||||
| 79 | : return $_[4]; # what zero gets | ||||||||||||||||
| 80 | 30 | _render($direction, | |||||||||||||||
| 81 | Time::Duration::_separate($span)); | ||||||||||||||||
| 82 | } | ||||||||||||||||
| 83 | |||||||||||||||||
| 84 | 86 | ▶ | sub interval { | ||||||||||||||
|
Subroutine
interval: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||
| 85 | 86 | my $span = $_[0]; # interval, in seconds | |||||||||||||||
| 86 | 86 | ▶ | my $precision = int($_[1] || 0) || 2; # precision (default: 2) | ||||||||||||||
|
Condition100%4/4
int($_[1] || 0) || 2
$_[1] || 0
MC/DC100%1/1
int($_[1] || 0) || 2
$_[1]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||
| 87 | 86 | ▶ | my $direction = ($span <= -1) ? $_[2] # what a neg number gets | ||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||
| 88 | : ($span >= 1) ? $_[3] # what a pos number gets | ||||||||||||||||
| 89 | : return $_[4]; # what zero gets | ||||||||||||||||
| 90 | 76 | _render($direction, | |||||||||||||||
| 91 | Time::Duration::_approximate($precision, | ||||||||||||||||
| 92 | Time::Duration::_separate($span))); | ||||||||||||||||
| 93 | } | ||||||||||||||||
| 94 | |||||||||||||||||
| 95 | |||||||||||||||||
| 96 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||
| 97 | 1 | my %env2sv = ( | |||||||||||||||
| 98 | second => ['sekund', 'sekunder'], | ||||||||||||||||
| 99 | minute => ['minut', 'minuter'], | ||||||||||||||||
| 100 | hour => ['timme', 'timmar'], | ||||||||||||||||
| 101 | day => ['dag','dagar'], | ||||||||||||||||
| 102 | year => ['år','år'], | ||||||||||||||||
| 103 | |||||||||||||||||
| 104 | ); | ||||||||||||||||
| 105 | |||||||||||||||||
| 106 | |||||||||||||||||
| 107 | 120 | ▶ | sub _render { | ||||||||||||||
|
Subroutine
_render: called CC=6 SCAR=17.9
| |||||||||||||||||
| 108 | # Make it into English | ||||||||||||||||
| 109 | 1 | ▶ | use Data::Dumper; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 110 | |||||||||||||||||
| 111 | 120 | my $direction = shift @_; | |||||||||||||||
| 112 | my @wheel = map | ||||||||||||||||
| 113 | { | ||||||||||||||||
| 114 | 120 | ( $_->[1] == 0) ? () # zero wheels | |||||||||||||||
| 115 | 600 | ▶ | : $_->[1] . ' ' . $env2sv{ $_->[0] }[ $_->[1] == 1 ? 0 : 1 ] | ||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||
| 116 | } | ||||||||||||||||
| 117 | @_; | ||||||||||||||||
| 118 | |||||||||||||||||
| 119 | 120 | ▶ | return "just now" unless @wheel; # sanity | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 120 | 120 | my $result; | |||||||||||||||
| 121 | 120 | ▶ | if(@wheel == 1) { | ||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||
| 122 | 48 | $result = $wheel[0]; | |||||||||||||||
| 123 | } elsif(@wheel == 2) { | ||||||||||||||||
| 124 | 34 | $result = "$wheel[0] och $wheel[1]"; | |||||||||||||||
| 125 | } else { | ||||||||||||||||
| 126 | 38 | $wheel[-1] = "och $wheel[-1]"; | |||||||||||||||
| 127 | 38 | $result = join q{, }, @wheel; | |||||||||||||||
| 128 | } | ||||||||||||||||
| 129 | 120 | return sprintf($direction, $result); | |||||||||||||||
| 130 | } | ||||||||||||||||
| 131 | |||||||||||||||||
| 132 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||
| 133 | 1 | 1; | |||||||||||||||
| 134 | |||||||||||||||||
| 135 | __END__ | ||||||||||||||||