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