Skip to content

Commit

Permalink
- Added the Date built-in type, to work with dates.
Browse files Browse the repository at this point in the history
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
trizen committed Feb 5, 2020
1 parent 09dc805 commit 9399bbc
Show file tree
Hide file tree
Showing 24 changed files with 806 additions and 405 deletions.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ my $builder = Module::Build->new(
'Fcntl' => 0,
'Encode' => 0,
'Scalar::Util' => 0,
'Time::Piece' => 0,
'Time::HiRes' => 0,
'Getopt::Std' => 0,
'Term::ReadLine' => 0,
Expand Down
7 changes: 3 additions & 4 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ lib/Sidef/Sys/Sig.pm
lib/Sidef/Sys/Sig.pod
lib/Sidef/Sys/Sys.pm
lib/Sidef/Sys/Sys.pod
lib/Sidef/Time/Gmtime.pm
lib/Sidef/Time/Gmtime.pod
lib/Sidef/Time/Localtime.pm
lib/Sidef/Time/Localtime.pod
lib/Sidef/Time/Date.pm
lib/Sidef/Time/Date.pod
lib/Sidef/Time/Time.pm
lib/Sidef/Time/Time.pod
lib/Sidef/Types/Array/Array.pm
Expand Down Expand Up @@ -709,6 +707,7 @@ scripts/Tests/cyclic_dclone.sf
scripts/Tests/cyclic_references_eq_cmp.sf
scripts/Tests/data_digging.sf
scripts/Tests/data_handle.sf
scripts/Tests/date_time.sf
scripts/Tests/dec_2016_changes.sf
scripts/Tests/def_primitive_type.sf
scripts/Tests/def_primitive_type_2.sf
Expand Down
8 changes: 3 additions & 5 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"Socket" : "0",
"Term::ReadLine" : "0",
"Time::HiRes" : "0",
"Time::Piece" : "0",
"parent" : "0",
"perl" : "v5.18.0",
"utf8" : "0"
Expand Down Expand Up @@ -114,11 +115,8 @@
"Sidef::Sys::Sys" : {
"file" : "lib/Sidef/Sys/Sys.pm"
},
"Sidef::Time::Gmtime" : {
"file" : "lib/Sidef/Time/Gmtime.pm"
},
"Sidef::Time::Localtime" : {
"file" : "lib/Sidef/Time/Localtime.pm"
"Sidef::Time::Date" : {
"file" : "lib/Sidef/Time/Date.pm"
},
"Sidef::Time::Time" : {
"file" : "lib/Sidef/Time/Time.pm"
Expand Down
7 changes: 3 additions & 4 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ provides:
file: lib/Sidef/Sys/Sig.pm
Sidef::Sys::Sys:
file: lib/Sidef/Sys/Sys.pm
Sidef::Time::Gmtime:
file: lib/Sidef/Time/Gmtime.pm
Sidef::Time::Localtime:
file: lib/Sidef/Time/Localtime.pm
Sidef::Time::Date:
file: lib/Sidef/Time/Date.pm
Sidef::Time::Time:
file: lib/Sidef/Time/Time.pm
Sidef::Types::Array::Array:
Expand Down Expand Up @@ -147,6 +145,7 @@ requires:
Socket: '0'
Term::ReadLine: '0'
Time::HiRes: '0'
Time::Piece: '0'
parent: '0'
perl: v5.18.0
utf8: '0'
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ WriteMakefile
'Term::ReadLine' => 0,
'Test::More' => 0,
'Time::HiRes' => 0,
'Time::Piece' => 0,
'parent' => 0,
'utf8' => 0
},
Expand Down
6 changes: 3 additions & 3 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ package Sidef::Deparse::Perl {
Sidef::Sys::Sys Sidef::Sys::Sys
Sidef::Perl::Perl Sidef::Perl::Perl
Sidef::Math::Math Sidef::Math::Math
Sidef::Time::Time Sidef::Time::Time
Sidef::Time::Date Sidef::Time::Date
)
},

Expand Down Expand Up @@ -1377,9 +1380,6 @@ HEADER
\$Sidef::PARSER->parse_script(code => do{my\$o=~ . $self->deparse_args($obj->{expr}) . qq~;\\"\$o"});
})}~;
}
elsif ($ref eq 'Sidef::Time::Time') {
$code = $ref . '->new';
}
elsif ($ref eq 'Sidef::Types::Number::Complex') {
my ($real, $imag) = $obj->reals;
my $name = "Complex$refaddr";
Expand Down
1 change: 1 addition & 0 deletions lib/Sidef/Deparse/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ package Sidef::Deparse::Sidef {
Sidef::Perl::Perl Perl
Sidef::Time::Time Time
Sidef::Time::Date Date
Sidef::Sys::Sig Sig
Sidef::Sys::Sys Sys
Sidef::Meta::Unimplemented ...
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ package Sidef::Parser {
| Perl\b (?{ state $x = bless({}, 'Sidef::Perl::Perl') })
| Math\b (?{ state $x = bless({}, 'Sidef::Math::Math') })
| Time\b (?{ state $x = Sidef::Time::Time->new })
| Date\b (?{ state $x = Sidef::Time::Date->new })
| \$\. (?{ state $x = bless({name => '$.'}, 'Sidef::Variable::Magic') })
| \$\? (?{ state $x = bless({name => '$?'}, 'Sidef::Variable::Magic') })
| \$\$ (?{ state $x = bless({name => '$$'}, 'Sidef::Variable::Magic') })
Expand Down Expand Up @@ -215,6 +216,7 @@ package Sidef::Parser {
Sig
Regex Regexp
Time
Date
Perl
Sidef
Object
Expand Down
230 changes: 230 additions & 0 deletions lib/Sidef/Time/Date.pm
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__ . '::' . '!='} = \&ne;
*{__PACKAGE__ . '::' . '<'} = \&lt;
*{__PACKAGE__ . '::' . '>'} = \&gt;
*{__PACKAGE__ . '::' . '<='} = \&le;
*{__PACKAGE__ . '::' . '>='} = \&ge;
}
};

1;
Loading

0 comments on commit 9399bbc

Please sign in to comment.