| 1 | package Sidef::Time::Time; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | use parent qw(Sidef::Object::Object); | ||
| 6 | |||
| 7 | use overload | ||
| 8 | q{""} => \&get_value, | ||
| 9 | q{bool} => \&get_value, | ||
| 10 | q{0+} => \&get_value; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my (undef, $sec) = @_; | ||
| 14 | |||
| 15 | if (defined $sec) { | ||
| 16 | $sec = CORE::int($sec) if ref($sec); | ||
| 17 | } | ||
| 18 | else { | ||
| 19 | $sec = time; | ||
| 20 | } | ||
| 21 | |||
| 22 | bless {sec => $sec,}; | ||
| 23 | } | ||
| 24 | |||
| 25 | *call = \&new; | ||
| 26 | |||
| 27 | sub get_value { | ||
| 28 | |||
| 29 | if (ref($_[0]) ne __PACKAGE__) { | ||
| 30 | return CORE::time; | ||
| 31 | } | ||
| 32 | |||
| 33 | $_[0]->{sec} // CORE::time; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub time { | ||
| 37 | my ($self) = @_; | ||
| 38 | Sidef::Types::Number::Number->new($self->get_value); | ||
| 39 | } | ||
| 40 | |||
| 41 | *sec = \&time; | ||
| 42 | |||
| 43 | sub now { | ||
| 44 | Sidef::Types::Number::Number->new(CORE::time); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub micro { | ||
| 48 | my ($self) = @_; | ||
| 49 | state $x = require Time::HiRes; | ||
| 50 | Sidef::Types::Number::Number->new(scalar Time::HiRes::gettimeofday()); | ||
| 51 | } | ||
| 52 | |||
| 53 | *micro_sec = \µ | ||
| 54 | *micro_seconds = \µ | ||
| 55 | |||
| 56 | sub localtime { | ||
| 57 | my ($self) = @_; | ||
| 58 | Sidef::Time::Date->localtime($self->get_value); | ||
| 59 | } | ||
| 60 | |||
| 61 | *local = \&localtime; | ||
| 62 | |||
| 63 | sub gmtime { | ||
| 64 | my ($self) = @_; | ||
| 65 | Sidef::Time::Date->gmtime($self->get_value); | ||
| 66 | } | ||
| 67 | |||
| 68 | *gmt = \&gmtime; | ||
| 69 | |||
| 70 | sub dump { | ||
| 71 | my ($self) = @_; | ||
| 72 | Sidef::Types::String::String->new('Time(' . $self->get_value . ')'); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub to_str { | ||
| 76 | my ($self) = @_; | ||
| 77 | Sidef::Types::String::String->new($self->get_value); | ||
| 78 | } | ||
| 79 | |||
| 80 | *to_s = \&to_str; | ||
| 81 | |||
| 82 | 1 |