| Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/Time/Duration/Parse.pm |
| Statements | Executed 14 statements in 351µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 343µs | 396µs | Time::Duration::Parse::BEGIN@8 |
| 1 | 1 | 1 | 11µs | 11µs | Time::Duration::Parse::BEGIN@3 |
| 1 | 1 | 1 | 5µs | 6µs | Time::Duration::Parse::BEGIN@4 |
| 1 | 1 | 1 | 4µs | 24µs | Time::Duration::Parse::BEGIN@7 |
| 1 | 1 | 1 | 4µs | 8µs | Time::Duration::Parse::BEGIN@5 |
| 0 | 0 | 0 | 0s | 0s | Time::Duration::Parse::parse_duration |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Time::Duration::Parse; | ||||
| 2 | 1 | 300ns | $Time::Duration::Parse::VERSION = '0.13'; | ||
| 3 | 2 | 27µs | 1 | 11µs | # spent 11µs within Time::Duration::Parse::BEGIN@3 which was called:
# once (11µs+0s) by CHI::Util::BEGIN@10 at line 3 # spent 11µs making 1 call to Time::Duration::Parse::BEGIN@3 |
| 4 | 2 | 12µs | 2 | 8µs | # spent 6µs (5+1) within Time::Duration::Parse::BEGIN@4 which was called:
# once (5µs+1µs) by CHI::Util::BEGIN@10 at line 4 # spent 6µs making 1 call to Time::Duration::Parse::BEGIN@4
# spent 1µs making 1 call to strict::import |
| 5 | 2 | 12µs | 2 | 11µs | # spent 8µs (4+3) within Time::Duration::Parse::BEGIN@5 which was called:
# once (4µs+3µs) by CHI::Util::BEGIN@10 at line 5 # spent 8µs making 1 call to Time::Duration::Parse::BEGIN@5
# spent 3µs making 1 call to warnings::import |
| 6 | |||||
| 7 | 2 | 13µs | 2 | 44µs | # spent 24µs (4+20) within Time::Duration::Parse::BEGIN@7 which was called:
# once (4µs+20µs) by CHI::Util::BEGIN@10 at line 7 # spent 24µs making 1 call to Time::Duration::Parse::BEGIN@7
# spent 20µs making 1 call to Exporter::import |
| 8 | 2 | 266µs | 2 | 412µs | # spent 396µs (343+53) within Time::Duration::Parse::BEGIN@8 which was called:
# once (343µs+53µs) by CHI::Util::BEGIN@10 at line 8 # spent 396µs making 1 call to Time::Duration::Parse::BEGIN@8
# spent 16µs making 1 call to Exporter::Lite::import |
| 9 | 1 | 500ns | our @EXPORT = qw( parse_duration ); | ||
| 10 | |||||
| 11 | # This map is taken from Cache and Cache::Cache | ||||
| 12 | # map of expiration formats to their respective time in seconds | ||||
| 13 | 1 | 15µs | my %Units = ( map(($_, 1), qw(s second seconds sec secs)), | ||
| 14 | map(($_, 60), qw(m minute minutes min mins)), | ||||
| 15 | map(($_, 60*60), qw(h hr hour hours)), | ||||
| 16 | map(($_, 60*60*24), qw(d day days)), | ||||
| 17 | map(($_, 60*60*24*7), qw(w week weeks)), | ||||
| 18 | map(($_, 60*60*24*30), qw(M month months mo mon mons)), | ||||
| 19 | map(($_, 60*60*24*365), qw(y year years)) ); | ||||
| 20 | |||||
| 21 | sub parse_duration { | ||||
| 22 | my $timespec = shift; | ||||
| 23 | |||||
| 24 | # You can have an optional leading '+', which has no effect | ||||
| 25 | $timespec =~ s/^\s*\+\s*//; | ||||
| 26 | |||||
| 27 | # Treat a plain number as a number of seconds (and parse it later) | ||||
| 28 | if ($timespec =~ /^\s*(-?\d+(?:[.,]\d+)?)\s*$/) { | ||||
| 29 | $timespec = "$1s"; | ||||
| 30 | } | ||||
| 31 | |||||
| 32 | # Convert hh:mm(:ss)? to something we understand | ||||
| 33 | $timespec =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g; | ||||
| 34 | $timespec =~ s/\b(\d+):(\d\d)\b/$1h $2m/g; | ||||
| 35 | |||||
| 36 | my $duration = 0; | ||||
| 37 | while ($timespec =~ s/^\s*(-?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) { | ||||
| 38 | my($amount, $unit) = ($1, $2); | ||||
| 39 | $unit = lc($unit) unless length($unit) == 1; | ||||
| 40 | |||||
| 41 | if (my $value = $Units{$unit}) { | ||||
| 42 | $amount =~ s/,/./; | ||||
| 43 | $duration += $amount * $value; | ||||
| 44 | } else { | ||||
| 45 | Carp::croak "Unknown timespec: $1 $2"; | ||||
| 46 | } | ||||
| 47 | } | ||||
| 48 | |||||
| 49 | if ($timespec =~ /\S/) { | ||||
| 50 | Carp::croak "Unknown timespec: $timespec"; | ||||
| 51 | } | ||||
| 52 | |||||
| 53 | return sprintf "%.0f", $duration; | ||||
| 54 | } | ||||
| 55 | |||||
| 56 | 1 | 5µs | 1; | ||
| 57 | __END__ |