| 1 | package Dancer2::Core::Time; | ||||||||||
| 2 | # ABSTRACT: class to handle common helpers for time manipulations | ||||||||||
| 3 | 198 | $Dancer2::Core::Time::VERSION = '2.2.2'; | |||||||||
| 4 | 198 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 198 | has seconds => ( | |||||||||
| 7 | is => 'ro', | ||||||||||
| 8 | lazy => 1, | ||||||||||
| 9 | builder => '_build_seconds', | ||||||||||
| 10 | ); | ||||||||||
| 11 | |||||||||||
| 12 | 42 | ▶ | sub _build_seconds { | ||||||||
|
Subroutine
_build_seconds: called CC=2 SCAR=7.0
| |||||||||||
| 13 | 42 | my ($self) = @_; | |||||||||
| 14 | 42 | my $seconds = $self->expression; | |||||||||
| 15 | |||||||||||
| 16 | 42 | ▶ | return $seconds | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | if $seconds =~ /^\d+$/; | ||||||||||
| 18 | |||||||||||
| 19 | 42 | return $self->_parse_duration($seconds) | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 198 | has epoch => ( | |||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | lazy => 1, | ||||||||||
| 25 | builder => '_build_epoch', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 38 | ▶ | sub _build_epoch { | ||||||||
|
Subroutine
_build_epoch: called CC=2 SCAR=6.9
| |||||||||||
| 29 | 38 | my ($self) = @_; | |||||||||
| 30 | 38 | ▶ | return $self->seconds if $self->seconds !~ /^[\-\+]?\d+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 30 | $self->seconds + time; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 198 | has gmt_string => ( | |||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | builder => '_build_gmt_string', | ||||||||||
| 37 | lazy => 1, | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | 51 | ▶ | sub _build_gmt_string { | ||||||||
|
Subroutine
_build_gmt_string: called CC=2 SCAR=6.9
| |||||||||||
| 41 | 51 | my ($self) = @_; | |||||||||
| 42 | 51 | my $epoch = $self->epoch; | |||||||||
| 43 | 51 | ▶ | return $epoch if $epoch !~ /^\d+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 43 | my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = gmtime($epoch); | |||||||||
| 46 | 43 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||
| 47 | 43 | my @days = qw(Sun Mon Tue Wed Thu Fri Sat); | |||||||||
| 48 | |||||||||||
| 49 | 43 | return sprintf "%s, %02d-%s-%d %02d:%02d:%02d GMT", | |||||||||
| 50 | $days[$wday], | ||||||||||
| 51 | $mday, | ||||||||||
| 52 | $months[$mon], | ||||||||||
| 53 | ( $year + 1900 ), | ||||||||||
| 54 | $hour, $min, $sec; | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 198 | has expression => ( | |||||||||
| 58 | is => 'ro', | ||||||||||
| 59 | required => 1, | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | 87 | ▶ | sub BUILDARGS { | ||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 63 | 87 | my ($class, %args) = @_; | |||||||||
| 64 | |||||||||||
| 65 | $args{epoch} = $args{expression} | ||||||||||
| 66 | 87 | ▶ | if $args{expression} =~ /^\d+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | 87 | return \%args; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | # private | ||||||||||
| 72 | |||||||||||
| 73 | # This map is taken from Cache and Cache::Cache | ||||||||||
| 74 | # map of expiration formats to their respective time in seconds | ||||||||||
| 75 | #<<< no perl tidy | ||||||||||
| 76 | 198 | my %Units = ( map(($_, 1), qw(s second seconds sec secs)), | |||||||||
| 77 | map(($_, 60), qw(m minute minutes min mins)), | ||||||||||
| 78 | map(($_, 60*60), qw(h hr hour hours)), | ||||||||||
| 79 | map(($_, 60*60*24), qw(d day days)), | ||||||||||
| 80 | map(($_, 60*60*24*7), qw(w week weeks)), | ||||||||||
| 81 | map(($_, 60*60*24*30), qw(M month months)), | ||||||||||
| 82 | map(($_, 60*60*24*365), qw(y year years)) ); | ||||||||||
| 83 | #>>> | ||||||||||
| 84 | |||||||||||
| 85 | # This code is taken from Time::Duration::Parse, except if it isn't | ||||||||||
| 86 | # understood it just passes it through and it adds the current time. | ||||||||||
| 87 | 42 | ▶ | sub _parse_duration { | ||||||||
|
Subroutine
_parse_duration: called CC=6 SCAR=17.9
| |||||||||||
| 88 | 42 | my ( $self, $timespec ) = @_; | |||||||||
| 89 | 42 | my $orig_timespec = $timespec; | |||||||||
| 90 | |||||||||||
| 91 | # Treat a plain number as a number of seconds (and parse it later) | ||||||||||
| 92 | 42 | ▶ | if ( $timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 2 | $timespec = "$1s"; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | # Convert hh:mm(:ss)? to something we understand | ||||||||||
| 97 | 42 | $timespec =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g; | |||||||||
| 98 | 42 | $timespec =~ s/\b(\d+):(\d\d)\b/$1h $2m/g; | |||||||||
| 99 | |||||||||||
| 100 | 42 | my $duration = 0; | |||||||||
| 101 | 42 | ▶ | while ( $timespec | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i ) | ||||||||||
| 103 | { | ||||||||||
| 104 | 46 | my ( $amount, $unit ) = ( $1, $2 ); | |||||||||
| 105 | 46 | ▶ | $unit = lc($unit) unless length($unit) == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | |||||||||||
| 107 | 46 | ▶ | if ( my $value = $Units{$unit} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 43 | $amount =~ s/,/./; | |||||||||
| 109 | 43 | $duration += $amount * $value; | |||||||||
| 110 | } | ||||||||||
| 111 | else { | ||||||||||
| 112 | 3 | return $orig_timespec; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 39 | ▶ | if ( $timespec =~ /\S/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 6 | return $orig_timespec; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 33 | return sprintf "%.0f", $duration; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 198 | 1; | |||||||||
| 124 | |||||||||||
| 125 | __END__ | ||||||||||