| 1 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 9 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | package Time::C; | ||
| 4 | $Time::C::VERSION = '0.024'; | ||
| 5 | # ABSTRACT: Convenient time manipulation. | ||
| 6 | |||
| 7 | 9 | ▶ | use overload ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||
| 8 | 0 | ▶ | '""' => sub { shift->string }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 9 | 0 | ▶ | bool => sub { 1 }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 10 | 9 | fallback => 1, | |
| 11 | 9 | ); | |
| 12 | |||
| 13 | 9 | ▶ | use Carp qw/ croak /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 9 | ▶ | use Function::Parameters qw/ :strict /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 9 | ▶ | use Time::C::Sentinel; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 9 | ▶ | use Time::D; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 9 | ▶ | use Time::P (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 9 | ▶ | use Time::F (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 9 | ▶ | use Time::Moment; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 0 | ▶ | use Time::Zone::Olson; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | method new ($c: $year =, $month =, $day =, $hour =, $minute =, $second =, $tz =) { | ||
| 25 | my $t; | ||
| 26 | my %defineds = ( | ||
| 27 | epoch_d => (defined($year) and defined($month) and defined($day) and defined($hour) and defined($minute) and defined($second)), | ||
| 28 | year => defined($year), | ||
| 29 | month => defined($month), | ||
| 30 | mday => defined($day), | ||
| 31 | week => 0, | ||
| 32 | wday => 0, | ||
| 33 | yday => 0, | ||
| 34 | hour => defined($hour), | ||
| 35 | minute => defined($minute), | ||
| 36 | second => defined($second), | ||
| 37 | tz_d => defined($tz), | ||
| 38 | offset => 0, | ||
| 39 | ); | ||
| 40 | |||
| 41 | if (not defined $year) { | ||
| 42 | $t = $c->now_utc(); | ||
| 43 | %$t = ( %$t, %defineds ); | ||
| 44 | return $t; | ||
| 45 | } | ||
| 46 | |||
| 47 | my $tm = Time::Moment->new(year => $year, month => $month // 1, day => $day // 1, hour => $hour // 0, minute => $minute // 0, second => $second // 0, offset => 0); | ||
| 48 | |||
| 49 | if (not defined $tz) { $tz = 'UTC'; } | ||
| 50 | |||
| 51 | if ($tz ne 'UTC' and $tz ne 'GMT') { | ||
| 52 | my $offset = _get_offset($tm->epoch, $tz); | ||
| 53 | $tm = $tm->with_offset_same_local($offset); | ||
| 54 | $offset = _get_offset($tm->epoch, $tz); | ||
| 55 | $tm = $tm->with_offset_same_local($offset); | ||
| 56 | } | ||
| 57 | |||
| 58 | $t = $c->localtime($tm->epoch, $tz); | ||
| 59 | %$t = (%$t, %defineds); | ||
| 60 | return $t; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | method mktime ($c: :$epoch =, :$second =, :$minute =, :$hour =, :$mday =, :$month =, :$wday =, :$week =, :$yday =, :$year =, :$tz =, :$offset =) { | ||
| 65 | |||
| 66 | # Alright, time to try and construct a Time::C object with what we have | ||
| 67 | # We'll start with the easiest one: epoch | ||
| 68 | # Then go on to creating it from the date bits, and the time bits | ||
| 69 | |||
| 70 | my $t; | ||
| 71 | my %defineds = ( | ||
| 72 | epoch_d => defined($epoch), | ||
| 73 | year => defined($year), | ||
| 74 | month => defined($month), | ||
| 75 | mday => defined($mday), | ||
| 76 | week => defined($week), | ||
| 77 | wday => defined($wday), | ||
| 78 | yday => defined($yday), | ||
| 79 | hour => defined($hour), | ||
| 80 | minute => defined($minute), | ||
| 81 | second => defined($second), | ||
| 82 | tz_d => defined($tz), | ||
| 83 | offset => defined($offset), | ||
| 84 | ); | ||
| 85 | |||
| 86 | if (defined $epoch) { | ||
| 87 | $t = Time::C->gmtime($epoch); | ||
| 88 | |||
| 89 | if (defined $tz) { | ||
| 90 | $t->tz = $tz; | ||
| 91 | } elsif (defined $offset) { | ||
| 92 | $t->offset = $offset; | ||
| 93 | } | ||
| 94 | |||
| 95 | $t->{tz_d} = $defineds{tz_d}; | ||
| 96 | |||
| 97 | return $t; | ||
| 98 | } elsif (defined $year) { # We have a year at least... | ||
| 99 | if (defined $month) { | ||
| 100 | if (defined $mday) { | ||
| 101 | $t = Time::C->new($year, $month, $mday); | ||
| 102 | } else { | ||
| 103 | $t = Time::C->new($year, $month); | ||
| 104 | } | ||
| 105 | } elsif (defined $week) { | ||
| 106 | $t = Time::C->new($year); | ||
| 107 | $t->day_of_week++ while ($t->week > 1); | ||
| 108 | $t = $t->week($week)->day_of_week(1); | ||
| 109 | |||
| 110 | if (defined $wday) { $t->day_of_week = $wday; } | ||
| 111 | else { $t->{wday} = 0; } | ||
| 112 | } elsif (defined $yday) { | ||
| 113 | $t = Time::C->new($year)->day_of_year($yday); | ||
| 114 | } else { # we have neither month, week, or day of year! | ||
| 115 | $t = Time::C->new($year); | ||
| 116 | } | ||
| 117 | |||
| 118 | # Now add the time bits on top... | ||
| 119 | if (defined $hour) { $t->hour = $hour; } | ||
| 120 | if (defined $minute) { $t->minute = $minute; } | ||
| 121 | if (defined $second) { $t->second = $second; } | ||
| 122 | } else { | ||
| 123 | # If we don't have a year, let's use the current year | ||
| 124 | $year = Time::C->now($tz)->tz('UTC', 1)->year; | ||
| 125 | if (defined $month) { | ||
| 126 | if (defined $mday) { | ||
| 127 | $t = Time::C->new($year, $month, $mday); | ||
| 128 | } else { | ||
| 129 | $t = Time::C->new($year, $month); | ||
| 130 | } | ||
| 131 | |||
| 132 | # Now add the time bits on top... | ||
| 133 | if (defined $hour) { $t->hour = $hour; } | ||
| 134 | if (defined $minute) { $t->minute = $minute; } | ||
| 135 | if (defined $second) { $t->second = $second; } | ||
| 136 | } elsif (defined $week) { | ||
| 137 | $t = Time::C->new($year); | ||
| 138 | $t->day_of_week++ while ($t->week > 1); | ||
| 139 | $t = $t->week($week)->day_of_week(1); | ||
| 140 | |||
| 141 | if (defined $wday) { $t->day_of_week = $wday; } | ||
| 142 | else { $t->{wday} = 0; } | ||
| 143 | |||
| 144 | # Now add the time bits on top... | ||
| 145 | if (defined $hour) { $t->hour = $hour; } | ||
| 146 | if (defined $minute) { $t->minute = $minute; } | ||
| 147 | if (defined $second) { $t->second = $second; } | ||
| 148 | } elsif (defined $yday) { | ||
| 149 | $t = Time::C->new($year)->day_of_year($yday); | ||
| 150 | |||
| 151 | # Now add the time bits on top... | ||
| 152 | if (defined $hour) { $t->hour = $hour; } | ||
| 153 | if (defined $minute) { $t->minute = $minute; } | ||
| 154 | if (defined $second) { $t->second = $second; } | ||
| 155 | } else { | ||
| 156 | # We have neither year, month, week, or day of year ... | ||
| 157 | # So let's just make a time for today's date | ||
| 158 | $t = Time::C->now($tz)->second_of_day(0)->tz('UTC', 1); | ||
| 159 | |||
| 160 | # Mark these as being undefined | ||
| 161 | $t->{epoch_d} = $t->{tz_d} = $t->{offset} = $t->{year} = | ||
| 162 | $t->{month} = $t->{mday} = $t->{week} = $t->{wday} = $t->{yday} = | ||
| 163 | $t->{hour} = $t->{minute} = $t->{second} = 0; | ||
| 164 | |||
| 165 | croak "Could not mktime: No date specified and no time given." | ||
| 166 | if not defined $hour and not defined $minute and not defined $second; | ||
| 167 | |||
| 168 | # And add the time bits on top... | ||
| 169 | # - if hour not defined, use current hour | ||
| 170 | # - if hour and minute not defined, use current minute | ||
| 171 | if (defined $hour) { $t->hour = $hour; } else { $t->hour = Time::C->now($tz)->tz('UTC', 1)->hour; } | ||
| 172 | if (defined $minute) { $t->minute = $minute; } elsif (not defined $hour) { $t->second_of_day = Time::C->now($tz)->tz('UTC', 1)->second(0)->second_of_day; } | ||
| 173 | if (defined $second) { $t->second = $second; } | ||
| 174 | } | ||
| 175 | $t->{year} = 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | # And last, adjust for timezone bits | ||
| 179 | |||
| 180 | if (defined $tz) { | ||
| 181 | $t = $t->tz($tz, 1); | ||
| 182 | } elsif (defined $offset) { | ||
| 183 | $t->_tm = $t->tm->with_offset_same_local($offset); | ||
| 184 | $t->offset = $offset; | ||
| 185 | } | ||
| 186 | |||
| 187 | return $t; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | method localtime ($c: $epoch, $tz = $ENV{TZ}) { | ||
| 192 | my %defineds = ( | ||
| 193 | epoch_d => 1, | ||
| 194 | year => 1, | ||
| 195 | month => 1, | ||
| 196 | mday => 1, | ||
| 197 | week => 1, | ||
| 198 | wday => 1, | ||
| 199 | yday => 1, | ||
| 200 | hour => 1, | ||
| 201 | minute => 1, | ||
| 202 | second => 1, | ||
| 203 | tz_d => defined($tz), | ||
| 204 | offset => 0, | ||
| 205 | ); | ||
| 206 | $tz = 'UTC' unless defined $tz; | ||
| 207 | _verify_tz($tz); | ||
| 208 | bless {epoch => $epoch, tz => $tz, %defineds}, $c; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | method gmtime ($c: $epoch) { $c->localtime( $epoch, 'UTC' ); } | ||
| 213 | |||
| 214 | |||
| 215 | method now ($c: $tz = $ENV{TZ}) { $c->localtime( time, $tz ); } | ||
| 216 | |||
| 217 | |||
| 218 | method now_utc ($c:) { $c->localtime( time, 'UTC' ); } | ||
| 219 | |||
| 220 | |||
| 221 | method from_string ($c: $str, :$format = undef, :$locale = 'C', :$strict = 1, :$tz = 'UTC') { | ||
| 222 | my %p = _parse($str, $format, $tz, locale => $locale, strict => $strict); | ||
| 223 | |||
| 224 | return $c->mktime(%p); | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | method strptime ($c: $str, $format, :$locale = 'C', :$strict = 1) { | ||
| 229 | my %struct; | ||
| 230 | if (ref $c) { | ||
| 231 | my $t = $c; | ||
| 232 | if ($t->{year}) { $struct{year} = $t->year; } | ||
| 233 | if ($t->{month}) { $struct{month} = $t->month; } | ||
| 234 | if ($t->{mday}) { $struct{mday} = $t->day; } | ||
| 235 | if ($t->{week}) { $struct{week} = $t->week; } | ||
| 236 | if ($t->{wday}) { $struct{wday} = $t->day_of_week; } | ||
| 237 | if ($t->{yday}) { $struct{yday} = $t->day_of_year; } | ||
| 238 | if ($t->{hour}) { $struct{hour} = $t->hour; } | ||
| 239 | if ($t->{minute}) { $struct{minute} = $t->minute; } | ||
| 240 | if ($t->{second}) { $struct{second} = $t->second; } | ||
| 241 | if ($t->{tz_d}) { $struct{tz} = $t->{tz}; } | ||
| 242 | if ($t->{offset}) { $struct{offset} = $t->offset; } | ||
| 243 | } | ||
| 244 | %struct = Time::P::strptime($str, $format, locale => $locale, strict => $strict, struct => \%struct); | ||
| 245 | |||
| 246 | if (ref $c) { | ||
| 247 | my $t = $c; | ||
| 248 | |||
| 249 | if (defined $struct{tz}) { | ||
| 250 | $t->tz = $struct{tz}; | ||
| 251 | } elsif (defined $struct{offset}) { | ||
| 252 | $t->offset = $struct{offset}; | ||
| 253 | } | ||
| 254 | |||
| 255 | if (defined $struct{epoch}) { return $t->epoch($struct{epoch}); } | ||
| 256 | |||
| 257 | if (defined $struct{year}) { $t->year = $struct{year}; } | ||
| 258 | |||
| 259 | if (defined $struct{month}) { | ||
| 260 | $t->month = $struct{month}; | ||
| 261 | if (defined $struct{mday}) { $t->day = $struct{mday}; } | ||
| 262 | } elsif (defined $struct{week}) { | ||
| 263 | $t->week = $struct{week}; | ||
| 264 | if (defined $struct{wday}) { $t->day_of_week = $struct{wday}; } | ||
| 265 | } elsif (defined $struct{yday}) { | ||
| 266 | $t->day_of_year = $struct{yday}; | ||
| 267 | } | ||
| 268 | |||
| 269 | if (defined $struct{hour}) { $t->hour = $struct{hour}; } | ||
| 270 | if (defined $struct{minute}) { $t->minute = $struct{minute}; } | ||
| 271 | if (defined $struct{second}) { $t->second = $struct{second}; } | ||
| 272 | |||
| 273 | return $t; | ||
| 274 | } else { | ||
| 275 | return $c->mktime(%struct); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | fun _verify_tz ($tz) { | ||
| 280 | _get_offset(time, $tz); | ||
| 281 | } | ||
| 282 | |||
| 283 | my %tz_offset = ( | ||
| 284 | -720 => 'Etc/GMT+12', | ||
| 285 | -660 => 'Etc/GMT+11', | ||
| 286 | -600 => 'Etc/GMT+10', | ||
| 287 | -540 => 'Etc/GMT+9', | ||
| 288 | -480 => 'Etc/GMT+8', | ||
| 289 | -420 => 'Etc/GMT+7', | ||
| 290 | -360 => 'Etc/GMT+6', | ||
| 291 | -300 => 'Etc/GMT+5', | ||
| 292 | -240 => 'Etc/GMT+4', | ||
| 293 | -180 => 'Etc/GMT+3', | ||
| 294 | -120 => 'Etc/GMT+2', | ||
| 295 | -60 => 'Etc/GMT+1', | ||
| 296 | 0 => 'UTC', | ||
| 297 | 60 => 'Etc/GMT-1', | ||
| 298 | 120 => 'Etc/GMT-2', | ||
| 299 | 180 => 'Etc/GMT-3', | ||
| 300 | 240 => 'Etc/GMT-4', | ||
| 301 | 300 => 'Etc/GMT-5', | ||
| 302 | 360 => 'Etc/GMT-6', | ||
| 303 | 420 => 'Etc/GMT-7', | ||
| 304 | 480 => 'Etc/GMT-8', | ||
| 305 | 540 => 'Etc/GMT-9', | ||
| 306 | 600 => 'Etc/GMT-10', | ||
| 307 | 660 => 'Etc/GMT-11', | ||
| 308 | 720 => 'Etc/GMT-12', | ||
| 309 | 780 => 'Etc/GMT-13', | ||
| 310 | 840 => 'Etc/GMT-14', | ||
| 311 | ); | ||
| 312 | |||
| 313 | fun _get_tz ($offset) { | ||
| 314 | return 'UTC' unless $offset; | ||
| 315 | |||
| 316 | return $tz_offset{$offset} if defined $tz_offset{$offset}; | ||
| 317 | |||
| 318 | my $min = $offset % 60; | ||
| 319 | my $hour = int $offset / 60; | ||
| 320 | my $sign = '+'; | ||
| 321 | if ($hour < 0) { $sign = '-'; $hour = -$hour; } | ||
| 322 | |||
| 323 | return sprintf "%s%02s:%02s", $sign, $hour, $min; | ||
| 324 | } | ||
| 325 | |||
| 326 | fun _parse ($str, $format = undef, $tz = 'UTC', :$locale = 'C', :$strict = 1) { | ||
| 327 | if (defined $format) { | ||
| 328 | my %struct = (); | ||
| 329 | my $e = eval { %struct = Time::P::strptime($str, $format, locale => $locale, strict => $strict); 1; }; | ||
| 330 | return %struct if $e; | ||
| 331 | |||
| 332 | croak sprintf "Could not parse %s using %s: %s", $str, $format, $@; | ||
| 333 | } | ||
| 334 | |||
| 335 | my $tm = eval { Time::Moment->from_string($str); }; | ||
| 336 | croak sprintf "Could not parse %s.", $str if not defined $tm; | ||
| 337 | |||
| 338 | my $epoch = $tm->epoch; | ||
| 339 | my $offset = $tm->offset; | ||
| 340 | |||
| 341 | my @ret = (epoch => $epoch, offset => $offset); | ||
| 342 | if ($offset == Time::Zone::Olson->new({timezone => $tz})->local_offset($epoch)) { | ||
| 343 | push @ret, tz => $tz; | ||
| 344 | } | ||
| 345 | |||
| 346 | return @ret; | ||
| 347 | } | ||
| 348 | |||
| 349 | |||
| 350 | |||
| 351 | method epoch ($t: $new_epoch = undef) :lvalue { | ||
| 352 | my $epoch = $t->{epoch}; | ||
| 353 | |||
| 354 | my $setter = sub { | ||
| 355 | $t->{epoch} = $_[0]; | ||
| 356 | |||
| 357 | %$t = (%$t, epoch_d => 1, year => 1, yday => 1, month => 1, mday => 1, week => 1, wday => 1, hour => 1, minute => 1, second => 1, ); | ||
| 358 | |||
| 359 | return $t if defined $new_epoch; | ||
| 360 | return $_[0]; | ||
| 361 | }; | ||
| 362 | |||
| 363 | return $setter->($new_epoch) if defined $new_epoch; | ||
| 364 | |||
| 365 | sentinel value => $epoch, set => $setter; | ||
| 366 | } | ||
| 367 | |||
| 368 | |||
| 369 | method tz ($t: $new_tz = undef, $override = 0) :lvalue { | ||
| 370 | my $setter = sub { | ||
| 371 | _verify_tz($_[0]); | ||
| 372 | |||
| 373 | if ($override) { | ||
| 374 | my $l = Time::C->new($t->year, $t->month, $t->day, $t->hour, $t->minute, $t->second, $_[0]); | ||
| 375 | $t->{epoch} = $l->epoch; | ||
| 376 | } | ||
| 377 | |||
| 378 | $t->{tz} = $_[0]; | ||
| 379 | $t->{tz_d} = 1; | ||
| 380 | |||
| 381 | return $t if defined $new_tz; | ||
| 382 | return $t->{tz}; | ||
| 383 | }; | ||
| 384 | |||
| 385 | return $setter->($new_tz) if defined $new_tz; | ||
| 386 | |||
| 387 | sentinel value => $t->{tz}, set => $setter; | ||
| 388 | } | ||
| 389 | |||
| 390 | fun _get_offset ($epoch, $tz) { | ||
| 391 | my $offset = eval { Time::Zone::Olson->new({timezone => $tz}) | ||
| 392 | ->local_offset($epoch); }; | ||
| 393 | |||
| 394 | if (not defined $offset) { | ||
| 395 | if ($tz =~ /^([+-])(\d+):(\d+)$/) { | ||
| 396 | my ($sign, $hour, $min) = ($1, $2, $3); | ||
| 397 | $offset = 60 * $hour + $min; | ||
| 398 | $offset = -$offset if $sign eq '-'; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | croak sprintf "Unknown timezone %s.", $tz | ||
| 403 | if not defined $offset; | ||
| 404 | |||
| 405 | return $offset; | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | method offset ($t: $new_offset = undef) :lvalue { | ||
| 410 | my $setter = sub { | ||
| 411 | $t->{tz} = _get_tz($_[0]); | ||
| 412 | $t->{offset} = 1; | ||
| 413 | |||
| 414 | return $t if defined $new_offset; | ||
| 415 | return $_[0]; | ||
| 416 | }; | ||
| 417 | |||
| 418 | return $setter->($new_offset) if defined $new_offset; | ||
| 419 | |||
| 420 | my $offset = _get_offset($t->{epoch}, $t->{tz}); | ||
| 421 | |||
| 422 | sentinel value => $offset, set => $setter; | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | method tm ($t: $new_tm = undef) :lvalue { | ||
| 427 | my $setter = sub { | ||
| 428 | $t->_tm = $_[0]; | ||
| 429 | $t->epoch = $t->epoch; # update definedness values | ||
| 430 | |||
| 431 | return $t if defined $new_tm; | ||
| 432 | return $_[0]; | ||
| 433 | }; | ||
| 434 | |||
| 435 | return $setter->($new_tm) if defined $new_tm; | ||
| 436 | |||
| 437 | sentinel value => $t->_tm(), set => $setter; | ||
| 438 | } | ||
| 439 | |||
| 440 | method _tm ($t: $new_tm = undef) :lvalue { | ||
| 441 | $t->{tz} = 'UTC' if not defined $t->{tz}; | ||
| 442 | |||
| 443 | my $setter = sub { | ||
| 444 | $t->{epoch} = $_[0]->with_offset_same_instant(0)->epoch; | ||
| 445 | |||
| 446 | return $t if defined $new_tm; | ||
| 447 | return $_[0]; | ||
| 448 | }; | ||
| 449 | |||
| 450 | return $setter->($new_tm) if defined $new_tm; | ||
| 451 | |||
| 452 | my $tm = Time::Moment->from_epoch($t->{epoch}); | ||
| 453 | |||
| 454 | if ($t->{tz} ne 'GMT' and $t->{tz} ne 'UTC') { | ||
| 455 | $tm = $tm->with_offset_same_instant($t->offset); | ||
| 456 | } | ||
| 457 | |||
| 458 | sentinel value => $tm, set => $setter; | ||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | # need to parse the @args specially since F::P can't handle the signature | ||
| 463 | method string ($t: @args) :lvalue { | ||
| 464 | my ($new_str, $format, $locale, $strict) = (undef, undef, 'C', 1); | ||
| 465 | if (@args % 2) { $new_str = shift @args; } | ||
| 466 | my %args = @args; | ||
| 467 | $format = delete $args{format} if exists $args{format}; | ||
| 468 | $locale = delete $args{locale} if exists $args{locale}; | ||
| 469 | $strict = delete $args{strict} if exists $args{strict}; | ||
| 470 | croak sprintf "In method string: no such named parameter: %s", sort keys %args if %args; | ||
| 471 | |||
| 472 | $t->{tz} = 'UTC' if not defined $t->{tz}; | ||
| 473 | |||
| 474 | my $setter = sub { | ||
| 475 | my %struct = _parse($_[0], $format, $t->{tz}, locale => $locale, strict => $strict); | ||
| 476 | |||
| 477 | if (defined $struct{tz}) { | ||
| 478 | $t->tz = $struct{tz}; | ||
| 479 | } elsif (defined $struct{offset}) { | ||
| 480 | $t->offset = $struct{offset}; | ||
| 481 | } | ||
| 482 | |||
| 483 | if (defined $struct{epoch}) { return $t->epoch($struct{epoch}); } | ||
| 484 | |||
| 485 | if (defined $struct{year}) { $t->year = $struct{year}; } | ||
| 486 | |||
| 487 | if (defined $struct{month}) { | ||
| 488 | $t->month = $struct{month}; | ||
| 489 | if (defined $struct{mday}) { $t->day = $struct{mday}; } | ||
| 490 | } elsif (defined $struct{week}) { | ||
| 491 | $t->week = $struct{week}; | ||
| 492 | if (defined $struct{wday}) { $t->day_of_week = $struct{wday}; } | ||
| 493 | } elsif (defined $struct{yday}) { | ||
| 494 | $t->day_of_year = $struct{yday}; | ||
| 495 | } | ||
| 496 | |||
| 497 | if (defined $struct{hour}) { $t->hour = $struct{hour}; } | ||
| 498 | if (defined $struct{minute}) { $t->minute = $struct{minute}; } | ||
| 499 | if (defined $struct{second}) { $t->second = $struct{second}; } | ||
| 500 | |||
| 501 | return $t if defined $new_str; | ||
| 502 | return $_[0]; | ||
| 503 | }; | ||
| 504 | |||
| 505 | return $setter->($new_str) if defined $new_str; | ||
| 506 | |||
| 507 | my $str; | ||
| 508 | if (defined $format) { | ||
| 509 | $str = Time::F::strftime($t, $format, locale => $locale); | ||
| 510 | } else { | ||
| 511 | $str = $t->tm->to_string; | ||
| 512 | } | ||
| 513 | |||
| 514 | sentinel value => $str, set => $setter; | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | method strftime ($t: @args) :lvalue { $t->string(@args); } | ||
| 519 | |||
| 520 | |||
| 521 | method year ($t: $new_year = undef) :lvalue { | ||
| 522 | my $tm = $t->tm(); | ||
| 523 | |||
| 524 | my $setter = sub { | ||
| 525 | my $ret = ($t->_tm = $tm->with_year($_[0]))->year; | ||
| 526 | $t->{year} = 1; | ||
| 527 | |||
| 528 | return $t if defined $new_year; | ||
| 529 | return $ret; | ||
| 530 | }; | ||
| 531 | |||
| 532 | return $setter->($new_year) if defined $new_year; | ||
| 533 | |||
| 534 | sentinel value => $tm->year, set => $setter; | ||
| 535 | } | ||
| 536 | |||
| 537 | |||
| 538 | method quarter ($t: $new_quarter = undef) :lvalue { | ||
| 539 | my $tm = $t->tm(); | ||
| 540 | |||
| 541 | my $setter = sub { | ||
| 542 | my $ret = ($t->_tm = $tm->plus_months(3*$_[0] - $tm->month))->quarter; | ||
| 543 | $t->{month} = 1; | ||
| 544 | |||
| 545 | return $t if defined $new_quarter; | ||
| 546 | return $ret; | ||
| 547 | }; | ||
| 548 | |||
| 549 | return $setter->($new_quarter) if defined $new_quarter; | ||
| 550 | |||
| 551 | sentinel value => $tm->quarter, set => $setter; | ||
| 552 | } | ||
| 553 | |||
| 554 | |||
| 555 | method month ($t: $new_month = undef) :lvalue { | ||
| 556 | my $tm = $t->tm(); | ||
| 557 | |||
| 558 | my $setter = sub { | ||
| 559 | my $ret = ($t->_tm = $tm->plus_months($_[0] - $tm->month))->month; | ||
| 560 | $t->{month} = 1; | ||
| 561 | |||
| 562 | return $t if defined $new_month; | ||
| 563 | return $ret; | ||
| 564 | }; | ||
| 565 | |||
| 566 | return $setter->($new_month) if defined $new_month; | ||
| 567 | |||
| 568 | sentinel value => $tm->month, set => $setter; | ||
| 569 | } | ||
| 570 | |||
| 571 | |||
| 572 | method week ($t: $new_week = undef) :lvalue { | ||
| 573 | my $tm = $t->tm(); | ||
| 574 | |||
| 575 | my $setter = sub { | ||
| 576 | my $ret = ($t->_tm = $tm->plus_weeks($_[0] - $tm->week))->week; | ||
| 577 | $t->{week} = 1; | ||
| 578 | |||
| 579 | return $t if defined $new_week; | ||
| 580 | return $ret; | ||
| 581 | }; | ||
| 582 | |||
| 583 | return $setter->($new_week) if defined $new_week; | ||
| 584 | |||
| 585 | sentinel value => $tm->week, set => $setter; | ||
| 586 | } | ||
| 587 | |||
| 588 | |||
| 589 | method day ($t: $new_day = undef) :lvalue { $t->day_of_month(@_) } | ||
| 590 | |||
| 591 | |||
| 592 | method day_of_month ($t: $new_day = undef) :lvalue { | ||
| 593 | my $tm = $t->tm(); | ||
| 594 | |||
| 595 | my $setter = sub { | ||
| 596 | my $ret = ($t->_tm = $tm->plus_days($_[0] - $tm->day_of_month))->day_of_month; | ||
| 597 | $t->{mday} = 1; | ||
| 598 | |||
| 599 | return $t if defined $new_day; | ||
| 600 | return $ret; | ||
| 601 | }; | ||
| 602 | |||
| 603 | return $setter->($new_day) if defined $new_day; | ||
| 604 | |||
| 605 | sentinel value => $tm->day_of_month, set => $setter; | ||
| 606 | } | ||
| 607 | |||
| 608 | |||
| 609 | method day_of_year ($t: $new_day = undef) :lvalue { | ||
| 610 | my $tm = $t->tm(); | ||
| 611 | |||
| 612 | my $setter = sub { | ||
| 613 | my $ret = ($t->_tm = $tm->plus_days($_[0] - $tm->day_of_year))->day_of_year; | ||
| 614 | $t->{yday} = 1; | ||
| 615 | |||
| 616 | return $t if defined $new_day; | ||
| 617 | return $ret; | ||
| 618 | }; | ||
| 619 | |||
| 620 | return $setter->($new_day) if defined $new_day; | ||
| 621 | |||
| 622 | sentinel value => $tm->day_of_year, set => $setter; | ||
| 623 | } | ||
| 624 | |||
| 625 | |||
| 626 | method day_of_quarter ($t: $new_day = undef) :lvalue { | ||
| 627 | my $tm = $t->tm(); | ||
| 628 | |||
| 629 | my $setter = sub { | ||
| 630 | my $ret = ($t->_tm = $tm->plus_days($_[0] - $tm->day_of_quarter))->day_of_quarter; | ||
| 631 | $t->{mday} = 1; | ||
| 632 | |||
| 633 | return $t if defined $new_day; | ||
| 634 | return $ret; | ||
| 635 | }; | ||
| 636 | |||
| 637 | return $setter->($new_day) if defined $new_day; | ||
| 638 | |||
| 639 | sentinel value => $tm->day_of_quarter, set => $setter; | ||
| 640 | } | ||
| 641 | |||
| 642 | |||
| 643 | method day_of_week ($t: $new_day = undef) :lvalue { | ||
| 644 | my $tm = $t->tm(); | ||
| 645 | |||
| 646 | my $setter = sub { | ||
| 647 | my $ret = ($t->_tm = $tm->plus_days($_[0] - $tm->day_of_week))->day_of_week; | ||
| 648 | $t->{wday} = 1; | ||
| 649 | |||
| 650 | return $t if defined $new_day; | ||
| 651 | return $ret; | ||
| 652 | }; | ||
| 653 | |||
| 654 | return $setter->($new_day) if defined $new_day; | ||
| 655 | |||
| 656 | sentinel value => $tm->day_of_week, set => $setter; | ||
| 657 | } | ||
| 658 | |||
| 659 | |||
| 660 | method hour ($t: $new_hour = undef) :lvalue { | ||
| 661 | my $tm = $t->tm(); | ||
| 662 | |||
| 663 | my $setter = sub { | ||
| 664 | my $ret = ($t->_tm = $tm->plus_hours($_[0] - $tm->hour))->hour; | ||
| 665 | $t->{hour} = 1; | ||
| 666 | |||
| 667 | return $t if defined $new_hour; | ||
| 668 | return $ret; | ||
| 669 | }; | ||
| 670 | |||
| 671 | return $setter->($new_hour) if defined $new_hour; | ||
| 672 | |||
| 673 | sentinel value => $tm->hour, set => $setter; | ||
| 674 | } | ||
| 675 | |||
| 676 | |||
| 677 | method minute ($t: $new_minute = undef) :lvalue { | ||
| 678 | my $tm = $t->tm(); | ||
| 679 | |||
| 680 | my $setter = sub { | ||
| 681 | my $ret = ($t->_tm = $tm->plus_minutes($_[0] - $tm->minute))->minute; | ||
| 682 | $t->{minute} = 1; | ||
| 683 | |||
| 684 | return $t if defined $new_minute; | ||
| 685 | return $ret; | ||
| 686 | }; | ||
| 687 | |||
| 688 | return $setter->($new_minute) if defined $new_minute; | ||
| 689 | |||
| 690 | sentinel value => $tm->minute, set => $setter; | ||
| 691 | } | ||
| 692 | |||
| 693 | |||
| 694 | method second ($t: $new_second = undef) :lvalue { | ||
| 695 | my $tm = $t->tm(); | ||
| 696 | |||
| 697 | my $setter = sub { | ||
| 698 | my $ret = ($t->_tm = $tm->plus_seconds($_[0] - $tm->second))->second; | ||
| 699 | $t->{second} = 1; | ||
| 700 | |||
| 701 | return $t if defined $new_second; | ||
| 702 | return $ret; | ||
| 703 | }; | ||
| 704 | |||
| 705 | return $setter->($new_second) if defined $new_second; | ||
| 706 | |||
| 707 | sentinel value => $tm->second, set => $setter; | ||
| 708 | } | ||
| 709 | |||
| 710 | |||
| 711 | method second_of_day ($t: $new_second = undef) :lvalue { | ||
| 712 | my $tm = $t->tm(); | ||
| 713 | |||
| 714 | my $setter = sub { | ||
| 715 | my $ret = ($t->_tm = $tm->plus_seconds($_[0] - $tm->second_of_day))->second_of_day; | ||
| 716 | $t->{second} = $t->{minute} = $t->{hour} = 1; | ||
| 717 | |||
| 718 | return $t if defined $new_second; | ||
| 719 | return $ret; | ||
| 720 | }; | ||
| 721 | |||
| 722 | return $setter->($new_second) if defined $new_second; | ||
| 723 | |||
| 724 | sentinel value => $tm->second_of_day, set => $setter; | ||
| 725 | } | ||
| 726 | |||
| 727 | |||
| 728 | |||
| 729 | method diff ($t: $t2) { | ||
| 730 | my $epoch = | ||
| 731 | ref $t2 ? | ||
| 732 | $t2->can('epoch') ? | ||
| 733 | $t2->epoch : | ||
| 734 | croak "Object with no ->epoch method passed (". ref $t2 .")." : | ||
| 735 | $t2; | ||
| 736 | return Time::D->new($t->epoch, $epoch); | ||
| 737 | } | ||
| 738 | |||
| 739 | |||
| 740 | method clone($t:) { | ||
| 741 | my $c = ref $t; | ||
| 742 | |||
| 743 | my $t2 = { %$t }; | ||
| 744 | bless $t2, $c; | ||
| 745 | } | ||
| 746 | |||
| 747 | 1; | ||
| 748 | |||
| 749 | __END__ | ||