Add DateTime + Duration overloads.

This commit is contained in:
Christoffer Lerno
2025-05-07 10:49:22 +02:00
parent 30ec200492
commit 54e70cae0f
6 changed files with 32 additions and 2 deletions

View File

@@ -23,11 +23,21 @@ fn Clock Clock.add_nano_duration(self, NanoDuration nano) @operator_s(+) @inline
return (Clock)((NanoDuration)self + nano); 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 fn Clock Clock.add_duration(self, Duration duration) @operator_s(+) @inline
{ {
return self.add_nano_duration(duration.to_nano()); 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 fn NanoDuration Clock.nano_diff(self, Clock other) @operator(-) @inline
{ {
return (NanoDuration)self - (NanoDuration)other; return (NanoDuration)self - (NanoDuration)other;

View File

@@ -149,6 +149,8 @@ fn void DateTime.set_time(&self, Time 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_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_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)); 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_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_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); 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; 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); return self.time.diff_us(from.time);
} }

View File

@@ -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_days(time, long days) => time + days * DAY;
fn Time Time.add_weeks(time, long weeks) => time + weeks * WEEK; 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.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) fn int Time.compare_to(time, Time other)
{ {

View File

@@ -18,6 +18,7 @@
- Add `String.tokenize_all` to replace the now deprecated `String.splitter` - Add `String.tokenize_all` to replace the now deprecated `String.splitter`
- Add `String.count` to count the number of instances of a string. - Add `String.count` to count the number of instances of a string.
- Add `Duration * Int` and `Clock - Clock` overload. - Add `Duration * Int` and `Clock - Clock` overload.
- Add `DateTime + Duration` overloads.
## 0.7.1 Change list ## 0.7.1 Change list

View File

@@ -3,7 +3,7 @@ import std::time;
fn void test_parse_and_add() 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.year == 1973);
assert(d.month == APRIL); assert(d.month == APRIL);
assert(d.day == 27); assert(d.day == 27);
@@ -42,6 +42,11 @@ DateTime d = datetime::from_date(1973, APRIL, 27);
x = d.add_months(0); x = d.add_months(0);
assert(x.year == 1975); assert(x.year == 1975);
assert(x.month == MAY); 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() fn void test_timezone()
@@ -107,6 +112,10 @@ fn void test_timezone()
// keeping the others unchanged. The gmt_offset should be kept as well. // keeping the others unchanged. The gmt_offset should be kept as well.
dt = d1.add_hours(1); dt = d1.add_hours(1);
assert(dt.day == 15); 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); assert(dt.hour == 1);
tz = d2.add_hours(1); tz = d2.add_hours(1);
assert(tz.day == 15); assert(tz.day == 15);

View File

@@ -9,6 +9,8 @@ fn void time_diff()
test::eq(t2 - t, time::SEC); test::eq(t2 - t, time::SEC);
test::ne(t, t2); test::ne(t, t2);
test::eq(t, t); test::eq(t, t);
t2 = t2 - time::SEC;
test::eq(t, t2);
} }
fn void clock_diff() fn void clock_diff()
@@ -18,6 +20,8 @@ fn void clock_diff()
test::eq(c2 - c, time::SEC.to_nano()); test::eq(c2 - c, time::SEC.to_nano());
c2 = c + time::DAY; c2 = c + time::DAY;
test::eq(c2 - c, time::DAY.to_nano()); test::eq(c2 - c, time::DAY.to_nano());
c2 = c2 - time::DAY;
test::eq(c2, c);
} }
fn void to_format() fn void to_format()