Sidef/Time/Date.pm untested

filter: Statementstmt 0.0% 0 / 202 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 416 CC 23 SCAR 18.0
CC
23
Cov
0%
new
30.0
from_string
6.0
valid_date
6.0
CRAP 6.1
1 package Sidef::Time::Date;
2  
3 use utf8;
4 use 5.016;
5 use parent qw(
6 Sidef::Object::Object
7 );
8  
9 require Time::Piece;
10  
11 use overload
12 q{""} => \&ctime,
13 q{0+} => sub { $_[0]->{time}->epoch },
14 q{bool} => sub { $_[0]->{time} };
15  
16 use Sidef::Types::String::String;
17 use Sidef::Types::Number::Number;
18  
19 my @_DATE_AUTO_FORMATS = ('%Y-%m-%d %H:%M:%S', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d', '%Y/%m/%d', '%d-%m-%Y', '%m/%d/%Y',);
20  
21 sub _parse_auto {
22 my ($str) = @_;
23 foreach my $format (@_DATE_AUTO_FORMATS) {
24 my $t = eval { Time::Piece->strptime($str, $format) };
25 defined($t) and return $t;
26 }
27  
28 my $tried = join(', ', @_DATE_AUTO_FORMATS);
29 die "[ERROR] Date: unable to parse date string '$str' (tried: $tried)\n";
30 }
31  
32 sub new {
33 my (undef, @args) = @_;
34  
35 # No arguments: current moment
36 if (!@args) {
37 return bless {time => Time::Piece->new(CORE::time)};
38 }
39  
40 # Single argument: epoch seconds, a Date to copy, or a date string to auto-parse
41 if (@args == 1) {
42 my ($arg) = @args;
43  
44 if (ref($arg) eq __PACKAGE__) {
45 return bless {time => Time::Piece->new($arg->{time}->epoch)};
46 }
47  
48 if (ref($arg) eq 'Sidef::Types::Number::Number' or (ref($arg) eq '' and $arg =~ /^[0-9]+\z/)) {
49 return bless {time => Time::Piece->new(CORE::int($arg))};
50 }
51  
52 return bless {time => _parse_auto("$arg")};
53 }
54  
55 # Two or more arguments: calendar components
56 __PACKAGE__->from_ymd(@args);
57 }
58  
59 *call = \&new;
60  
61 sub from_ymd {
62 my (undef, $year, $month, $day, $hour, $min, $sec) = @_;
63  
64 # Safely pad missing components to their POD-specified defaults
65 my $str = CORE::sprintf(
66 '%04d-%02d-%02d %02d:%02d:%02d',
67 CORE::int($year // 0),
68 CORE::int($month // 1),
69 CORE::int($day // 1),
70 CORE::int($hour // 0),
71 CORE::int($min // 0),
72 CORE::int($sec // 0)
73 );
74  
75 bless {time => Time::Piece->strptime($str, '%Y-%m-%d %H:%M:%S')};
76 }
77  
78 sub from_string {
79 my (undef, $str, $format) = @_;
80  
81 defined($format)
82 ? (bless {time => Time::Piece->strptime("$str", "$format")})
83 : (bless {time => _parse_auto("$str")});
84 }
85  
86 sub get_value {
87 $_[0]->{time} // Time::Piece->new(CORE::time);
88 }
89  
90 {
91 no strict 'refs';
92  
93 foreach my $name (qw(sec min hour mon year yy epoch wday mday yday isdst julian_day week month_last_day)) {
94 *{__PACKAGE__ . '::' . $name} = sub {
95 my ($self) = @_;
96 Sidef::Types::Number::Number->new($self->{time}->$name);
97 };
98 }
99  
100 *day = \&mday;
101 *month = \&mon;
102 *minute = \&min;
103 *second = \&sec;
104 *month_day = \&mday;
105 *week_day = \&wday;
106 *year_day = \&yday;
107 *is_dst = \&isdst;
108 *daylight_savings = \&isdst;
109  
110 foreach my $name (qw(monname fullmonth wdayname date)) {
111 *{__PACKAGE__ . '::' . $name} = sub {
112 my ($self) = @_;
113 Sidef::Types::String::String->new($self->{time}->$name);
114 };
115 }
116  
117 foreach my $name (qw(ymd mdy dmy)) {
118 *{__PACKAGE__ . '::' . $name} = sub {
119 my ($self, $sep) = @_;
120 Sidef::Types::String::String->new($self->{time}->$name(defined($sep) ? "$sep" : ()));
121 };
122 }
123  
124 foreach my $name ("year", "quarter", "month", "day", "hour", "minute", "second") {
125 *{__PACKAGE__ . '::' . "truncate_to_" . $name} = sub {
126 my ($self) = @_;
127 bless {time => scalar $self->{time}->truncate(to => $name)};
128 };
129 }
130 }
131  
132 sub today {
133 __PACKAGE__->new(time);
134 }
135  
136 *now = \&today;
137  
138 sub time {
139 my ($self) = @_;
140 Sidef::Time::Time->new(scalar $self->{time}->epoch);
141 }
142  
143 sub localtime {
144 my ($self, $sec) = @_;
145 $sec //= $self->{time}->epoch;
146 bless {time => scalar Time::Piece::localtime($sec)};
147 }
148  
149 *local = \&localtime;
150  
151 sub gmtime {
152 my ($self, $sec) = @_;
153 $sec //= $self->{time}->epoch;
154 bless {time => scalar Time::Piece::gmtime($sec)};
155 }
156  
157 *gmt = \&gmtime;
158  
159 sub ctime {
160 my ($self) = @_;
161 Sidef::Types::String::String->new(scalar $self->{time}->cdate);
162 }
163  
164 *to_s = \&ctime;
165 *to_str = \&ctime;
166 *cdate = \&ctime;
167  
168 sub strftime {
169 my ($self, $format) = @_;
170 Sidef::Types::String::String->new(scalar $self->{time}->strftime("$format"));
171 }
172  
173 *format = \&strftime;
174  
175 sub strptime {
176 my ($self, $string, $format) = @_;
177 __PACKAGE__->new(Time::Piece->strptime("$string", "$format")->epoch);
178 }
179  
180 *parse = \&strptime;
181  
182 sub valid_date {
183 my ($self, $string, $format) = @_;
184  
185 $format = "$format";
186 $string = "$string";
187  
188 eval { Time::Piece->strptime($string, $format)->strftime($format) eq $string }
189 ? Sidef::Types::Bool::Bool::TRUE
190 : Sidef::Types::Bool::Bool::FALSE;
191 }
192  
193 *valid = \&valid_date;
194  
195 sub add_seconds {
196 my ($self, $sec) = @_;
197 bless {time => scalar $self->{time}->add(CORE::int($sec))};
198 }
199  
200 *add = \&add_seconds;
201  
202 sub subtract {
203 my ($this, $that) = @_;
204  
205 if (ref($that) eq __PACKAGE__) {
206 return Sidef::Types::Number::Number->new(scalar $this->{time}->subtract($that->{time}));
207 }
208  
209 bless {time => scalar $this->{time}->subtract(CORE::int($that))};
210 }
211  
212 *sub = \&subtract;
213  
214 sub add_days {
215 my ($self, $days) = @_;
216 $self->add_seconds(86400 * CORE::int($days));
217 }
218  
219 sub add_weeks {
220 my ($self, $weeks) = @_;
221 $self->add_days(7 * CORE::int($weeks));
222 }
223  
224 sub add_months {
225 my ($self, $months) = @_;
226 bless {time => scalar $self->{time}->add_months(CORE::int($months))};
227 }
228  
229 sub add_years {
230 my ($self, $years) = @_;
231 bless {time => scalar $self->{time}->add_years(CORE::int($years))};
232 }
233  
234 sub is_today {
235 my ($self) = @_;
236 my $today = __PACKAGE__->today;
237 ($self->{time}->ymd eq $today->{time}->ymd)
238 ? Sidef::Types::Bool::Bool::TRUE
239 : Sidef::Types::Bool::Bool::FALSE;
240 }
241  
242 sub is_leap_year {
243 my ($self) = @_;
244 my $y = $self->{time}->year;
245 (($y % 4 == 0 and $y % 100 != 0) or $y % 400 == 0)
246 ? Sidef::Types::Bool::Bool::TRUE
247 : Sidef::Types::Bool::Bool::FALSE;
248 }
249  
250 sub is_weekend {
251 my ($self) = @_;
252 my $wday = $self->{time}->wday;
253 ($wday == 1 or $wday == 7) # 1 = Sunday, 7 = Saturday
254 ? Sidef::Types::Bool::Bool::TRUE
255 : Sidef::Types::Bool::Bool::FALSE;
256 }
257  
258 sub is_weekday {
259 my ($self) = @_;
260 my $wday = $self->{time}->wday;
261 ($wday == 1 or $wday == 7)
262 ? Sidef::Types::Bool::Bool::FALSE
263 : Sidef::Types::Bool::Bool::TRUE;
264 }
265  
266 sub is_future {
267 my ($self) = @_;
268 ($self->{time}->epoch > CORE::time)
269 ? Sidef::Types::Bool::Bool::TRUE
270 : Sidef::Types::Bool::Bool::FALSE;
271 }
272  
273 sub is_past {
274 my ($self) = @_;
275 ($self->{time}->epoch < CORE::time)
276 ? Sidef::Types::Bool::Bool::TRUE
277 : Sidef::Types::Bool::Bool::FALSE;
278 }
279  
280 sub is_between {
281 my ($self, $start, $end) = @_;
282 ($self->{time}->epoch >= $start->{time}->epoch and $self->{time}->epoch <= $end->{time}->epoch)
283 ? Sidef::Types::Bool::Bool::TRUE
284 : Sidef::Types::Bool::Bool::FALSE;
285 }
286  
287 sub days_until {
288 my ($self, $other) = @_;
289 state $day_sec = Sidef::Types::Number::Number::_set_int(86400);
290 Sidef::Types::Number::Number->new($self->{time}->subtract($other->{time}))->div($day_sec)->int->neg;
291 }
292  
293 sub is_same_day {
294 my ($self, $other) = @_;
295 ($self->{time}->ymd eq $other->{time}->ymd)
296 ? Sidef::Types::Bool::Bool::TRUE
297 : Sidef::Types::Bool::Bool::FALSE;
298 }
299  
300 sub end_of_month {
301 my ($self) = @_;
302 my $last_day = $self->{time}->month_last_day;
303 bless {time => scalar $self->{time}->truncate(to => 'day')->add(($last_day - $self->{time}->mday) * 86400)};
304 }
305  
306 sub age {
307 my ($self) = @_;
308 my $now = Time::Piece->new(CORE::time);
309 my $years = $now->year - $self->{time}->year;
310 if ($now->mon < $self->{time}->mon
311 or ($now->mon == $self->{time}->mon and $now->mday < $self->{time}->mday)) {
312 --$years;
313 }
314 Sidef::Types::Number::Number::_set_int($years);
315 }
316  
317 sub quarter {
318 my ($self) = @_;
319 Sidef::Types::Number::Number::_set_int(CORE::int(($self->{time}->mon - 1) / 3) + 1);
320 }
321  
322 sub cmp {
323 my ($this, $that) = @_;
324 Sidef::Types::Number::Number::_set_int(CORE::int($this) <=> CORE::int($that));
325 }
326  
327 sub eq {
328 my ($this, $that) = @_;
329 (CORE::int($this) <=> CORE::int($that)) == 0
330 ? Sidef::Types::Bool::Bool::TRUE
331 : Sidef::Types::Bool::Bool::FALSE;
332 }
333  
334 sub ne {
335 my ($this, $that) = @_;
336 (CORE::int($this) <=> CORE::int($that)) != 0
337 ? Sidef::Types::Bool::Bool::TRUE
338 : Sidef::Types::Bool::Bool::FALSE;
339 }
340  
341 sub lt {
342 my ($this, $that) = @_;
343 (CORE::int($this) <=> CORE::int($that)) < 0
344 ? Sidef::Types::Bool::Bool::TRUE
345 : Sidef::Types::Bool::Bool::FALSE;
346 }
347  
348 sub le {
349 my ($this, $that) = @_;
350 (CORE::int($this) <=> CORE::int($that)) <= 0
351 ? Sidef::Types::Bool::Bool::TRUE
352 : Sidef::Types::Bool::Bool::FALSE;
353 }
354  
355 sub gt {
356 my ($this, $that) = @_;
357 (CORE::int($this) <=> CORE::int($that)) > 0
358 ? Sidef::Types::Bool::Bool::TRUE
359 : Sidef::Types::Bool::Bool::FALSE;
360 }
361  
362 sub ge {
363 my ($this, $that) = @_;
364 (CORE::int($this) <=> CORE::int($that)) >= 0
365 ? Sidef::Types::Bool::Bool::TRUE
366 : Sidef::Types::Bool::Bool::FALSE;
367 }
368  
369 sub dump {
370 my ($self) = @_;
371 Sidef::Types::String::String->new('Date(' . CORE::int($self) . ')');
372 }
373  
374 {
375 no strict 'refs';
376  
377 *{__PACKAGE__ . '::' . '+'} = \&add;
378 *{__PACKAGE__ . '::' . '-'} = \&subtract;
379 *{__PACKAGE__ . '::' . '<=>'} = \&cmp;
380 *{__PACKAGE__ . '::' . '=='} = \&eq;
381 *{__PACKAGE__ . '::' . '!='} = \&ne;
382 *{__PACKAGE__ . '::' . '<'} = \&lt;
383 *{__PACKAGE__ . '::' . '>'} = \&gt;
384 *{__PACKAGE__ . '::' . '<='} = \&le;
385 *{__PACKAGE__ . '::' . '>='} = \&ge;
386 }
387  
388 1