commit d6da6320905826fc01b663cb13d008e238c3c6c5
parent 911a11f86ed9fc0d87ce922ea61e2b8248023ae1
Author: Vlad-Stefan Harbuz <vlad@vladh.net>
Date: Fri, 7 Jan 2022 17:27:50 +0100
add is_ymd_valid()
Signed-off-by: Vlad-Stefan Harbuz <vlad@vladh.net>
Diffstat:
1 file changed, 9 insertions(+), 0 deletions(-)
diff --git a/datetime/date.ha b/datetime/date.ha
@@ -34,6 +34,12 @@ fn calc_n_days_in_year(y: int) int = {
};
};
+// Calculates whether a given (year, month, date) is valid
+export fn is_ymd_valid(y: int, m: int, d: int) bool = {
+ return m >= 1 && m <= 12 && d >= 1 &&
+ d <= calc_n_days_in_month(y, m);
+};
+
// Calculates the era, given a year
fn calc_era(y: int) int = {
return if (y >= 0) {
@@ -174,6 +180,9 @@ fn calc_zeroweekday(wd: int) int = {
// Calculates the [[chrono::epochal]], given a (year, month, day) date
fn calc_epochal_from_ymd(y: int, m: int, d: int) (chrono::epochal | errors::invalid) = {
+ if (!is_ymd_valid(y, m, d)) {
+ return errors::invalid;
+ };
// Algorithm adapted from:
// https://en.wikipedia.org/wiki/Julian_day
//