diff --git a/lib/std/time/clock.c3 b/lib/std/time/clock.c3 index 6cb40d161..47eb052a1 100644 --- a/lib/std/time/clock.c3 +++ b/lib/std/time/clock.c3 @@ -23,11 +23,21 @@ fn Clock Clock.add_nano_duration(self, NanoDuration nano) @operator_s(+) @inline return (Clock)((NanoDuration)self + nano); } +fn Clock Clock.sub_nano_duration(self, NanoDuration nano) @operator(-) @inline +{ + return (Clock)((NanoDuration)self - nano); +} + fn Clock Clock.add_duration(self, Duration duration) @operator_s(+) @inline { return self.add_nano_duration(duration.to_nano()); } +fn Clock Clock.sub_duration(self, Duration duration) @operator(-) @inline +{ + return self.sub_nano_duration(duration.to_nano()); +} + fn NanoDuration Clock.nano_diff(self, Clock other) @operator(-) @inline { return (NanoDuration)self - (NanoDuration)other; diff --git a/lib/std/time/datetime.c3 b/lib/std/time/datetime.c3 index 276fbff33..4a7defe39 100644 --- a/lib/std/time/datetime.c3 +++ b/lib/std/time/datetime.c3 @@ -149,6 +149,8 @@ fn void DateTime.set_time(&self, Time time) self.time = time; } +fn DateTime DateTime.add_us(&self, Duration d) @operator_s(+) => from_time(self.time + d); +fn DateTime DateTime.sub_us(&self, Duration d) @operator(-) => from_time(self.time - d); fn DateTime DateTime.add_seconds(&self, int seconds) => from_time(self.time.add_seconds(seconds)); fn DateTime DateTime.add_minutes(&self, int minutes) => from_time(self.time.add_minutes(minutes)); fn DateTime DateTime.add_hours(&self, int hours) => from_time(self.time.add_hours(hours)); @@ -187,6 +189,8 @@ fn DateTime DateTime.add_months(&self, int months) } +fn TzDateTime TzDateTime.add_us(&self, Duration d) @operator_s(+) => self.date_time.add_us(d).to_gmt_offset(self.gmt_offset); +fn TzDateTime TzDateTime.sub_us(&self, Duration d) @operator(-) => self.date_time.sub_us(d).to_gmt_offset(self.gmt_offset); fn TzDateTime TzDateTime.add_seconds(&self, int seconds) => self.date_time.add_seconds(seconds).to_gmt_offset(self.gmt_offset); fn TzDateTime TzDateTime.add_minutes(&self, int minutes) => self.date_time.add_minutes(minutes).to_gmt_offset(self.gmt_offset); fn TzDateTime TzDateTime.add_hours(&self, int hours) => self.date_time.add_hours(hours).to_gmt_offset(self.gmt_offset); @@ -248,7 +252,8 @@ fn double DateTime.diff_sec(self, DateTime from) { return (double)self.time.diff_us(from.time) / (double)time::SEC; } -fn Duration DateTime.diff_us(self, DateTime from) + +fn Duration DateTime.diff_us(self, DateTime from) @operator(-) { return self.time.diff_us(from.time); } diff --git a/lib/std/time/time.c3 b/lib/std/time/time.c3 index 04d1c865d..57cf40256 100644 --- a/lib/std/time/time.c3 +++ b/lib/std/time/time.c3 @@ -88,6 +88,7 @@ fn Time Time.add_hours(time, long hours) => time + hours * HOUR; fn Time Time.add_days(time, long days) => time + days * DAY; fn Time Time.add_weeks(time, long weeks) => time + weeks * WEEK; fn Time Time.add_duration(time, Duration duration) @operator_s(+) @inline => (Time)((long)time + (long)duration); +fn Time Time.sub_duration(time, Duration duration) @operator(-) @inline => (Time)((long)time - (long)duration); fn int Time.compare_to(time, Time other) { diff --git a/releasenotes.md b/releasenotes.md index a5add73e9..cb755349a 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -18,6 +18,7 @@ - Add `String.tokenize_all` to replace the now deprecated `String.splitter` - Add `String.count` to count the number of instances of a string. - Add `Duration * Int` and `Clock - Clock` overload. +- Add `DateTime + Duration` overloads. ## 0.7.1 Change list diff --git a/test/unit/stdlib/time/datetime.c3 b/test/unit/stdlib/time/datetime.c3 index 81770a83e..a2bdfffcf 100644 --- a/test/unit/stdlib/time/datetime.c3 +++ b/test/unit/stdlib/time/datetime.c3 @@ -3,7 +3,7 @@ import std::time; fn void test_parse_and_add() { -DateTime d = datetime::from_date(1973, APRIL, 27); + DateTime d = datetime::from_date(1973, APRIL, 27); assert(d.year == 1973); assert(d.month == APRIL); assert(d.day == 27); @@ -42,6 +42,11 @@ DateTime d = datetime::from_date(1973, APRIL, 27); x = d.add_months(0); assert(x.year == 1975); assert(x.month == MAY); + test::eq(x.day, 4); + x = d + time::DAY; + test::eq(x.day, 5); + x = x - time::DAY; + test::eq(x.day, 4); } fn void test_timezone() @@ -107,6 +112,10 @@ fn void test_timezone() // keeping the others unchanged. The gmt_offset should be kept as well. dt = d1.add_hours(1); assert(dt.day == 15); + dt = dt + time::DAY; + test::eq(dt.day, 16); + dt = dt - time::DAY; + test::eq(dt.day, 15); assert(dt.hour == 1); tz = d2.add_hours(1); assert(tz.day == 15); diff --git a/test/unit/stdlib/time/time.c3 b/test/unit/stdlib/time/time.c3 index 85d2e1be5..d447f5461 100644 --- a/test/unit/stdlib/time/time.c3 +++ b/test/unit/stdlib/time/time.c3 @@ -9,6 +9,8 @@ fn void time_diff() test::eq(t2 - t, time::SEC); test::ne(t, t2); test::eq(t, t); + t2 = t2 - time::SEC; + test::eq(t, t2); } fn void clock_diff() @@ -18,6 +20,8 @@ fn void clock_diff() test::eq(c2 - c, time::SEC.to_nano()); c2 = c + time::DAY; test::eq(c2 - c, time::DAY.to_nano()); + c2 = c2 - time::DAY; + test::eq(c2, c); } fn void to_format()