| 1 | package Seis::Instant; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use 5.010_001; | ||
| 6 | |||
| 7 | use overload ( | ||
| 8 | q{""} => sub { | ||
| 9 | "Instant:" . $_[0]->x | ||
| 10 | }, | ||
| 11 | q{eq} => sub { | ||
| 12 | $_[0]->x eq $_[1]->x | ||
| 13 | }, | ||
| 14 | q{>} => sub { | ||
| 15 | $_[0]->x > $_[1]->x | ||
| 16 | }, | ||
| 17 | q{<} => sub { | ||
| 18 | $_[0]->x < $_[1]->x | ||
| 19 | }, | ||
| 20 | q{-} => sub { | ||
| 21 | my ($x,$y,$reversed) = @_; | ||
| 22 | if ($reversed) { | ||
| 23 | ... | ||
| 24 | } else { | ||
| 25 | if (UNIVERSAL::isa($_[1], __PACKAGE__)) { | ||
| 26 | Seis::Duration->_new($_[0]->x - $_[1]->x) | ||
| 27 | } else { | ||
| 28 | Seis::Instant->_new($x->{x} - $y); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | }, | ||
| 32 | q{+} => sub { | ||
| 33 | my ($x,$y,$reverse) = @_; | ||
| 34 | if ($reverse) { | ||
| 35 | Seis::Instant->_new($x->{x} + $y); | ||
| 36 | } else { | ||
| 37 | if (UNIVERSAL::isa($_[1], __PACKAGE__)) { | ||
| 38 | Seis::Exception->throw("Instant + Instant is illegal"); | ||
| 39 | } elsif (UNIVERSAL::isa($_[1], 'Seis::Duration')) { | ||
| 40 | Seis::Instant->_new($x->{x} + $$y); | ||
| 41 | } else { | ||
| 42 | Seis::Instant->_new($x->{x} + $y); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | }, | ||
| 46 | ); | ||
| 47 | |||
| 48 | sub _new { | ||
| 49 | my ($class, $x) = @_; | ||
| 50 | bless {x => $x}, $class; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub x { shift->{x} } | ||
| 54 | |||
| 55 | sub fromーposix { | ||
| 56 | my ($class, $x) = @_; | ||
| 57 | $class->_new($x); | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 |