commit 7396d5b7fcd0fdb68841bd66456e57b14ff96374
parent 11f0c0813c4398cb4efcf337a02e2db9b0b50b51
Author: Byron Torres <b@torresjrjr.com>
Date: Sat, 13 Nov 2021 02:33:46 +0000
outline datetime parsing/formatting
Signed-off-by: Byron Torres <b@torresjrjr.com>
Diffstat:
3 files changed, 46 insertions(+), 6 deletions(-)
diff --git a/datetime/format.ha b/datetime/format.ha
@@ -0,0 +1,37 @@
+use errors;
+use fmt;
+use io;
+use strings;
+use strio;
+
+// Parses a string into a [[datetime]]
+export fn strptime(format: str, s: str, dt: *datetime) (void | errors::invalid) = {
+ // TODO
+ return void;
+};
+
+// Formats a [[datetime]] and writes it into a caller supplied buffer.
+// The returned string is borrowed from this buffer.
+// Fails if a particular datetime field is required but void.
+export fn bstrftime(buf: []u8, format: str, dt: *datetime) (str | errors::invalid | io::error) = {
+ let sink = strio::fixed(buf);
+ defer io::close(sink);
+ fmttime(sink, format, dt)?;
+ return strio::string(sink);
+};
+
+// Formats a [[datetime]] and writes it into a heap-allocated string.
+// The caller must free the return value.
+// Fails if a particular datetime field is required but void.
+export fn strftime(format: str, dt: *datetime) (str | errors::invalid | io::error) = {
+ let sink = strio::dynamic();
+ fmttime(sink, format, dt)?;
+ return strio::finish(sink);
+};
+
+// Formats a [[datetime]] and writes it into a [[io::handle]].
+// Fails a particular field is required but void.
+export fn fmttime(h: io::handle, format: str, dt: *datetime) (size | errors::invalid | io::error) = {
+ // TODO
+ return 0z;
+};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -187,8 +187,9 @@ datetime() {
datetime.ha \
timezone.ha \
date.ha \
- time.ha
- gen_ssa datetime time time::chrono
+ time.ha \
+ format.ha
+ gen_ssa datetime errors fmt strings strio time time::chrono
}
compress_flate() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -933,9 +933,10 @@ stdlib_datetime_any_srcs= \
$(STDLIB)/datetime/datetime.ha \
$(STDLIB)/datetime/timezone.ha \
$(STDLIB)/datetime/date.ha \
- $(STDLIB)/datetime/time.ha
+ $(STDLIB)/datetime/time.ha \
+ $(STDLIB)/datetime/format.ha
-$(HARECACHE)/datetime/datetime-any.ssa: $(stdlib_datetime_any_srcs) $(stdlib_rt) $(stdlib_time_$(PLATFORM)) $(stdlib_time_chrono_$(PLATFORM))
+$(HARECACHE)/datetime/datetime-any.ssa: $(stdlib_datetime_any_srcs) $(stdlib_rt) $(stdlib_errors_$(PLATFORM)) $(stdlib_fmt_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_strio_$(PLATFORM)) $(stdlib_time_$(PLATFORM)) $(stdlib_time_chrono_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/datetime
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Ndatetime \
@@ -2895,9 +2896,10 @@ testlib_datetime_any_srcs= \
$(STDLIB)/datetime/datetime.ha \
$(STDLIB)/datetime/timezone.ha \
$(STDLIB)/datetime/date.ha \
- $(STDLIB)/datetime/time.ha
+ $(STDLIB)/datetime/time.ha \
+ $(STDLIB)/datetime/format.ha
-$(TESTCACHE)/datetime/datetime-any.ssa: $(testlib_datetime_any_srcs) $(testlib_rt) $(testlib_time_$(PLATFORM)) $(testlib_time_chrono_$(PLATFORM))
+$(TESTCACHE)/datetime/datetime-any.ssa: $(testlib_datetime_any_srcs) $(testlib_rt) $(testlib_errors_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_time_$(PLATFORM)) $(testlib_time_chrono_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/datetime
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ndatetime \