Debian Codegraph @

lib/Dancer2/Core/Time.pm

raw
1 package Dancer2::Core::Time;
2 # ABSTRACT: class to handle common helpers for time manipulations
3 $Dancer2::Core::Time::VERSION = '2.2.2';
4 use Moo;
5
6 has seconds => (
7 is => 'ro',
8 lazy => 1,
9 builder => '_build_seconds',
10 );
11
12 sub _build_seconds {
13 my ($self) = @_;
14 my $seconds = $self->expression;
15
16 return $seconds
17 if $seconds =~ /^\d+$/;
18
19 return $self->_parse_duration($seconds)
20 }
21
22 has epoch => (
23 is => 'ro',
24 lazy => 1,
25 builder => '_build_epoch',
26 );
27
28 sub _build_epoch {
29 my ($self) = @_;
30 return $self->seconds if $self->seconds !~ /^[\-\+]?\d+$/;
31 $self->seconds + time;
32 }
33
34 has gmt_string => (
35 is => 'ro',
36 builder => '_build_gmt_string',
37 lazy => 1,
38 );
39
40 sub _build_gmt_string {
41 my ($self) = @_;
42 my $epoch = $self->epoch;
43 return $epoch if $epoch !~ /^\d+$/;
44
45 my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = gmtime($epoch);
46 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
47 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
48
49 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 has expression => (
58 is => 'ro',
59 required => 1,
60 );
61
62 sub BUILDARGS {
63 my ($class, %args) = @_;
64
65 $args{epoch} = $args{expression}
66 if $args{expression} =~ /^\d+$/;
67
68 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 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 sub _parse_duration {
88 my ( $self, $timespec ) = @_;
89 my $orig_timespec = $timespec;
90
91 # Treat a plain number as a number of seconds (and parse it later)
92 if ( $timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/ ) {
93 $timespec = "$1s";
94 }
95
96 # Convert hh:mm(:ss)? to something we understand
97 $timespec =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g;
98 $timespec =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
99
100 my $duration = 0;
101 while ( $timespec
102 =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i )
103 {
104 my ( $amount, $unit ) = ( $1, $2 );
105 $unit = lc($unit) unless length($unit) == 1;
106
107 if ( my $value = $Units{$unit} ) {
108 $amount =~ s/,/./;
109 $duration += $amount * $value;
110 }
111 else {
112 return $orig_timespec;
113 }
114 }
115
116 if ( $timespec =~ /\S/ ) {
117 return $orig_timespec;
118 }
119
120 return sprintf "%.0f", $duration;
121 }
122
123 1;
124
125 __END__
126
127 =pod
128
129 =encoding UTF-8
130
131 =head1 NAME
132
133 Dancer2::Core::Time - class to handle common helpers for time manipulations
134
135 =head1 VERSION
136
137 version 2.2.2
138
139 =head1 SYNOPSIS
140
141 my $time = Dancer2::Core::Time->new( expression => "1h" );
142 $time->seconds; # return 3600
143
144 =head1 DESCRIPTION
145
146 For consistency, whenever something needs to work with time, it
147 needs to be expressed in seconds, with a timestamp. Although it's very
148 convenient for the machine and calculations, it's not very handy for a
149 human-being, for instance in a configuration file.
150
151 This class provides everything needed to translate any human-understandable
152 expression into a number of seconds.
153
154 =head1 ATTRIBUTES
155
156 =head2 seconds
157
158 Number of seconds represented by the object. Defaults to 0.
159
160 =head2 epoch
161
162 The current epoch to handle. Defaults to seconds + time.
163
164 =head2 gmt_string
165
166 Convert the current value in epoch as a GMT string.
167
168 =head2 expression
169
170 Required. A human readable expression representing the number of seconds to provide.
171
172 The format supported is a number followed by an expression. It currently
173 understands:
174
175 s second seconds sec secs
176 m minute minutes min mins
177 h hr hour hours
178 d day days
179 w week weeks
180 M month months
181 y year years
182
183 Months and years are currently fixed at 30 and 365 days. This may change.
184 Anything else is used verbatim as the expression of a number of seconds.
185
186 Example:
187
188 2 hours, 3 days, 3d, 1 week, 3600, etc...
189
190 =head1 AUTHOR
191
192 Dancer Core Developers
193
194 =head1 COPYRIGHT AND LICENSE
195
196 This software is copyright (c) 2026 by Alexis Sukrieh.
197
198 This is free software; you can redistribute it and/or modify it under
199 the same terms as the Perl 5 programming language system itself.
200
201 =cut