-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the
Date
built-in type, to work with dates.
Example: var d1 = Date() # localtime date say d1.day #=> 5 say d1.mon #=> 2 say d1.year #=> 2020 # Parse date var d2 = Date.parse("2020-02-02", "%Y-%m-%d") say (d1 - d2) # difference in seconds say (d1 <=> d2) # date comparison say d1.add_days(3) # add n days say d1.add_months(5) # add n months say d1.add_years(9) # add n years say (d1 == d2) # check equality say (d1 != d2) # check inequality say d1.epoch # seconds since the epoch
- Loading branch information
Showing
24 changed files
with
806 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
package Sidef::Time::Date { | ||
|
||
use 5.016; | ||
use parent qw( | ||
Sidef::Object::Object | ||
); | ||
|
||
require Time::Piece; | ||
|
||
use overload | ||
q{""} => \&ctime, | ||
q{0+} => sub { $_[0]->{time}->epoch }, | ||
q{bool} => sub { $_[0]->{time} }; | ||
|
||
use Sidef::Types::String::String; | ||
use Sidef::Types::Number::Number; | ||
|
||
sub new { | ||
my (undef, $sec) = @_; | ||
|
||
if (defined $sec) { | ||
$sec = CORE::int($sec) if ref($sec); | ||
} | ||
else { | ||
$sec = CORE::time; | ||
} | ||
|
||
bless {time => Time::Piece->new($sec),}; | ||
} | ||
|
||
*call = \&new; | ||
|
||
sub get_value { | ||
$_[0]->{time} // Time::Piece->new(CORE::time); | ||
} | ||
|
||
{ | ||
no strict 'refs'; | ||
|
||
foreach my $name (qw(sec min hour mon year yy epoch wday mday yday isdst julian_day week month_last_day)) { | ||
*{__PACKAGE__ . '::' . $name} = sub { | ||
my ($self) = @_; | ||
Sidef::Types::Number::Number->new($self->{time}->$name); | ||
}; | ||
} | ||
|
||
*day = \&mday; | ||
*month = \&mon; | ||
*minute = \&min; | ||
*second = \&sec; | ||
*month_day = \&mday; | ||
*week_day = \&wday; | ||
*year_day = \&yday; | ||
*daylight_savings = \&isdst; | ||
|
||
foreach my $name (qw(monname fullmonth wdayname date)) { | ||
*{__PACKAGE__ . '::' . $name} = sub { | ||
my ($self) = @_; | ||
Sidef::Types::String::String->new($self->{time}->$name); | ||
}; | ||
} | ||
|
||
foreach my $name (qw(ymd mdy dmy)) { | ||
*{__PACKAGE__ . '::' . $name} = sub { | ||
my ($self, $sep) = @_; | ||
Sidef::Types::String::String->new($self->{time}->$name(defined($sep) ? "$sep" : ())); | ||
}; | ||
} | ||
|
||
foreach my $name ("year", "quarter", "month", "day", "hour", "minute", "second") { | ||
*{__PACKAGE__ . '::' . "truncate_to_" . $name} = sub { | ||
my ($self) = @_; | ||
bless {time => scalar $self->{time}->truncate(to => $name)}; | ||
}; | ||
} | ||
} | ||
|
||
sub today { | ||
my ($self) = @_; | ||
__PACKAGE__->new(time); | ||
} | ||
|
||
*now = \&today; | ||
|
||
sub time { | ||
my ($self) = @_; | ||
Sidef::Time::Time->new(scalar $self->{time}->epoch); | ||
} | ||
|
||
sub localtime { | ||
my ($self, $sec) = @_; | ||
$sec //= $self->{time}->epoch; | ||
bless {time => scalar Time::Piece::localtime($sec)}; | ||
} | ||
|
||
*local = \&localtime; | ||
|
||
sub gmtime { | ||
my ($self, $sec) = @_; | ||
$sec //= $self->{time}->epoch; | ||
bless {time => scalar Time::Piece::gmtime($sec)}; | ||
} | ||
|
||
*gmt = \&gmtime; | ||
|
||
sub ctime { | ||
my ($self) = @_; | ||
Sidef::Types::String::String->new(scalar $self->{time}->cdate); | ||
} | ||
|
||
*to_s = \&ctime; | ||
*to_str = \&ctime; | ||
*cdate = \&ctime; | ||
|
||
sub strftime { | ||
my ($self, $format) = @_; | ||
Sidef::Types::String::String->new(scalar $self->{time}->strftime("$format")); | ||
} | ||
|
||
*format = \&strftime; | ||
|
||
sub strptime { | ||
my ($self, $string, $format) = @_; | ||
__PACKAGE__->new(Time::Piece->strptime("$string", "$format")->epoch); | ||
} | ||
|
||
*parse = \&strptime; | ||
|
||
sub add_seconds { | ||
my ($self, $sec) = @_; | ||
bless {time => scalar $self->{time}->add(CORE::int($sec))}; | ||
} | ||
|
||
*add = \&add_seconds; | ||
|
||
sub subtract { | ||
my ($this, $that) = @_; | ||
|
||
if (ref($that) eq __PACKAGE__) { | ||
return Sidef::Types::Number::Number->new(scalar $this->{time}->subtract($that->{time})); | ||
} | ||
|
||
bless {time => scalar $this->{time}->subtract(CORE::int($that))}; | ||
} | ||
|
||
*sub = \&subtract; | ||
|
||
sub add_days { | ||
my ($self, $days) = @_; | ||
$self->add_seconds(86400 * CORE::int($days)); | ||
} | ||
|
||
sub add_months { | ||
my ($self, $months) = @_; | ||
bless {time => scalar $self->{time}->add_months(CORE::int($months))}; | ||
} | ||
|
||
sub add_years { | ||
my ($self, $years) = @_; | ||
bless {time => scalar $self->{time}->add_years(CORE::int($years))}; | ||
} | ||
|
||
sub cmp { | ||
my ($this, $that) = @_; | ||
Sidef::Types::Number::Number->new(CORE::int($this) <=> CORE::int($that)); | ||
} | ||
|
||
sub eq { | ||
my ($this, $that) = @_; | ||
(CORE::int($this) <=> CORE::int($that)) == 0 | ||
? Sidef::Types::Bool::Bool::TRUE | ||
: Sidef::Types::Bool::Bool::FALSE; | ||
} | ||
|
||
sub ne { | ||
my ($this, $that) = @_; | ||
(CORE::int($this) <=> CORE::int($that)) != 0 | ||
? Sidef::Types::Bool::Bool::TRUE | ||
: Sidef::Types::Bool::Bool::FALSE; | ||
} | ||
|
||
sub lt { | ||
my ($this, $that) = @_; | ||
(CORE::int($this) <=> CORE::int($that)) < 0 | ||
? Sidef::Types::Bool::Bool::TRUE | ||
: Sidef::Types::Bool::Bool::FALSE; | ||
} | ||
|
||
sub le { | ||
my ($this, $that) = @_; | ||
(CORE::int($this) <=> CORE::int($that)) <= 0 | ||
? Sidef::Types::Bool::Bool::TRUE | ||
: Sidef::Types::Bool::Bool::FALSE; | ||
} | ||
|
||
sub gt { | ||
my ($this, $that) = @_; | ||
(CORE::int($this) <=> CORE::int($that)) > 0 | ||
? Sidef::Types::Bool::Bool::TRUE | ||
: Sidef::Types::Bool::Bool::FALSE; | ||
} | ||
|
||
sub ge { | ||
my ($this, $that) = @_; | ||
(CORE::int($this) <=> CORE::int($that)) >= 0 | ||
? Sidef::Types::Bool::Bool::TRUE | ||
: Sidef::Types::Bool::Bool::FALSE; | ||
} | ||
|
||
sub dump { | ||
my ($self) = @_; | ||
Sidef::Types::String::String->new('Date(' . CORE::int($self) . ')'); | ||
} | ||
|
||
{ | ||
no strict 'refs'; | ||
|
||
*{__PACKAGE__ . '::' . '+'} = \&add; | ||
*{__PACKAGE__ . '::' . '-'} = \&subtract; | ||
*{__PACKAGE__ . '::' . '<=>'} = \&cmp; | ||
*{__PACKAGE__ . '::' . '=='} = \&eq; | ||
*{__PACKAGE__ . '::' . '!='} = \≠ | ||
*{__PACKAGE__ . '::' . '<'} = \< | ||
*{__PACKAGE__ . '::' . '>'} = \> | ||
*{__PACKAGE__ . '::' . '<='} = \≤ | ||
*{__PACKAGE__ . '::' . '>='} = \≥ | ||
} | ||
}; | ||
|
||
1; |
Oops, something went wrong.