commit d5851c8cfa58d0bbbc65a54b8cedc2fcf0fe66ae
parent 06c96eda85fbb1f9fd3ea1f4f8d729b26a68189c
Author: Vlad-Stefan Harbuz <vlad@vladh.net>
Date: Fri, 7 Jan 2022 19:48:35 +0100
add clone()
Signed-off-by: Vlad-Stefan Harbuz <vlad@vladh.net>
Signed-off-by: Byron Torres <b@torresjrjr.com>
Diffstat:
1 file changed, 39 insertions(+), 1 deletion(-)
diff --git a/datetime/datetime.ha b/datetime/datetime.ha
@@ -53,7 +53,7 @@ fn init() datetime = datetime {
// Creates a new datetime
//
// // 2038 January 19th 03:14:07.618 +0000
-// datetime::new(2038, 01, 19, 03, 14, 07, 618 0, &chrono::local)
+// datetime::new(2038, 01, 19, 03, 14, 07, 618, 0, chrono::local)
//
export fn new(
year: int,
@@ -90,6 +90,44 @@ export fn new(
return dt;
};
+// Creates a copy of a datetime
+export fn clone(dt: datetime) datetime = {
+ return datetime {
+ date = dt.date,
+ time = dt.time,
+ zone = dt.zone,
+ loc = dt.loc,
+
+ era = dt.era,
+ year = dt.year,
+ month = dt.month,
+ day = dt.day,
+ isoweekyear = dt.isoweekyear,
+ isoweek = dt.isoweek,
+ week = dt.week,
+ weekday = dt.weekday,
+ yearday = dt.yearday,
+
+ hour = dt.hour,
+ min = dt.min,
+ sec = dt.sec,
+ nsec = dt.nsec,
+ };
+};
+
+@test fn clone() void = {
+ let d0 = datetime::new(2038, 01, 19, 03, 14, 07, 0, 0, chrono::local)!;
+ let d1 = clone(d0);
+ assert(d0.year as int == d1.year as int &&
+ d0.month as int == d1.month as int &&
+ d0.day as int == d1.day as int &&
+ d0.hour as int == d1.hour as int &&
+ d0.min as int == d1.min as int &&
+ d0.sec as int == d1.sec as int &&
+ d0.nsec as int == d1.nsec as int,
+ "cloned date not equal to original date");
+};
+
// Returns the current datetime
export fn now() datetime = {
const i = time::now(time::clock::REALTIME);