From 0732e925cb09851a51b366a41c8b6530e5dccfaa Mon Sep 17 00:00:00 2001 From: photino Date: Fri, 1 Dec 2023 17:50:14 +0800 Subject: [PATCH] Release version 0.16.3 --- examples/actix-app/Cargo.toml | 8 ++--- examples/axum-app/Cargo.toml | 8 ++--- examples/dioxus-desktop/Cargo.toml | 6 ++-- zino-cli/Cargo.toml | 2 +- zino-core/Cargo.toml | 2 +- zino-core/src/datetime/date.rs | 57 ++++++++++++++++++++++++++++-- zino-core/src/datetime/mod.rs | 27 +++++++++++++- zino-core/src/datetime/time.rs | 15 +++++++- zino-derive/Cargo.toml | 4 +-- zino-model/Cargo.toml | 6 ++-- zino/Cargo.toml | 4 +-- 11 files changed, 114 insertions(+), 25 deletions(-) diff --git a/examples/actix-app/Cargo.toml b/examples/actix-app/Cargo.toml index 749cd2eb..65f85a65 100644 --- a/examples/actix-app/Cargo.toml +++ b/examples/actix-app/Cargo.toml @@ -16,18 +16,18 @@ features = ["derive"] [dependencies.zino] path = "../../zino" -version = "0.15.2" +version = "0.15.3" features = ["actix"] [dependencies.zino-core] path = "../../zino-core" -version = "0.16.2" +version = "0.16.3" features = ["orm-postgres", "view-minijinja"] [dependencies.zino-derive] path = "../../zino-derive" -version = "0.13.2" +version = "0.13.3" [dependencies.zino-model] path = "../../zino-model" -version = "0.13.2" +version = "0.13.3" diff --git a/examples/axum-app/Cargo.toml b/examples/axum-app/Cargo.toml index 7deb67a7..8e2e568a 100644 --- a/examples/axum-app/Cargo.toml +++ b/examples/axum-app/Cargo.toml @@ -16,12 +16,12 @@ features = ["derive"] [dependencies.zino] path = "../../zino" -version = "0.15.2" +version = "0.15.3" features = ["axum"] [dependencies.zino-core] path = "../../zino-core" -version = "0.16.2" +version = "0.16.3" features = [ "crypto-sm", "orm-mysql", @@ -32,8 +32,8 @@ features = [ [dependencies.zino-derive] path = "../../zino-derive" -version = "0.13.2" +version = "0.13.3" [dependencies.zino-model] path = "../../zino-model" -version = "0.13.2" +version = "0.13.3" diff --git a/examples/dioxus-desktop/Cargo.toml b/examples/dioxus-desktop/Cargo.toml index 7591717b..750e0c8e 100644 --- a/examples/dioxus-desktop/Cargo.toml +++ b/examples/dioxus-desktop/Cargo.toml @@ -26,17 +26,17 @@ features = ["derive"] [dependencies.zino] path = "../../zino" -version = "0.15.2" +version = "0.15.3" features = ["dioxus-desktop"] [dependencies.zino-core] path = "../../zino-core" -version = "0.16.2" +version = "0.16.3" features = ["orm-sqlite"] [dependencies.zino-model] path = "../../zino-model" -version = "0.13.2" +version = "0.13.3" [dependencies.zino-dioxus] path = "../../zino-dioxus" diff --git a/zino-cli/Cargo.toml b/zino-cli/Cargo.toml index 807b164e..5707b92c 100644 --- a/zino-cli/Cargo.toml +++ b/zino-cli/Cargo.toml @@ -20,7 +20,7 @@ path = "src/main.rs" log = "0.4.20" [dependencies.clap] -version = "4.4.8" +version = "4.4.10" features = ["color", "derive"] [dependencies.zino-core] diff --git a/zino-core/Cargo.toml b/zino-core/Cargo.toml index 3958b950..6625942c 100644 --- a/zino-core/Cargo.toml +++ b/zino-core/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "zino-core" description = "Core types and traits for zino." -version = "0.16.2" +version = "0.16.3" rust-version = "1.73" edition = "2021" license = "MIT" diff --git a/zino-core/src/datetime/date.rs b/zino-core/src/datetime/date.rs index 5bb9a533..043fcc92 100644 --- a/zino-core/src/datetime/date.rs +++ b/zino-core/src/datetime/date.rs @@ -1,5 +1,5 @@ -use crate::{AvroValue, JsonValue}; -use chrono::{format::ParseError, Datelike, Local, NaiveDate}; +use crate::{error::Error, AvroValue, JsonValue}; +use chrono::{format::ParseError, Datelike, Days, Local, Months, NaiveDate}; use serde::{Deserialize, Serialize, Serializer}; use std::{ fmt, @@ -13,6 +13,19 @@ use std::{ pub struct Date(NaiveDate); impl Date { + /// Attempts to create a new instance. + #[inline] + pub fn try_new(year: i32, month: u32, day: u32) -> Result { + NaiveDate::from_ymd_opt(year, month, day) + .map(Self) + .ok_or_else(|| { + let message = format!( + "fail to create a date from year: `{year}`, month: `{month}`, day: `{day}`" + ); + Error::new(message) + }) + } + /// Returns a new instance which corresponds to the current date. #[inline] pub fn today() -> Self { @@ -122,7 +135,13 @@ impl Date { /// Returns the day of week starting from 0 (Sunday) to 6 (Saturday). #[inline] pub fn day_of_week(&self) -> u8 { - self.0.weekday() as u8 + self.iso_day_of_week() % 7 + } + + /// Returns the ISO day of week starting from 1 (Monday) to 7 (Sunday). + #[inline] + pub fn iso_day_of_week(&self) -> u8 { + (self.0.weekday() as u8) + 1 } /// Returns `true` if the current year is a leap year. @@ -202,6 +221,38 @@ impl Date { }; Self(date_opt.unwrap_or_default()) } + + /// Adds a duration in months to the date. + /// Returns `None` if the resulting date would be out of range. + #[inline] + pub fn checked_add_months(self, months: u32) -> Option { + self.0.checked_add_months(Months::new(months)).map(Self) + } + + /// Subtracts a duration in months from the date. + /// Returns `None` if the resulting date would be out of range. + #[inline] + pub fn checked_sub_months(self, months: u32) -> Option { + self.0.checked_sub_months(Months::new(months)).map(Self) + } + + /// Adds a duration in days to the date. + /// Returns `None` if the resulting date would be out of range. + #[inline] + pub fn checked_add_days(self, days: u32) -> Option { + self.0 + .checked_add_days(Days::new(u64::from(days))) + .map(Self) + } + + /// Subtracts a duration in days from the date. + /// Returns `None` if the resulting date would be out of range. + #[inline] + pub fn checked_sub_days(self, days: u32) -> Option { + self.0 + .checked_sub_days(Days::new(u64::from(days))) + .map(Self) + } } impl Default for Date { diff --git a/zino-core/src/datetime/mod.rs b/zino-core/src/datetime/mod.rs index bc31f109..37e807ef 100644 --- a/zino-core/src/datetime/mod.rs +++ b/zino-core/src/datetime/mod.rs @@ -275,6 +275,24 @@ impl DateTime { self.0.second() } + /// Returns the millisecond number from 0 to 999. + #[inline] + pub fn millisecond(&self) -> u32 { + self.0.timestamp_subsec_millis() % 1000 + } + + /// Returns the microsecond number from 0 to 999. + #[inline] + pub fn microsecond(&self) -> u32 { + self.0.timestamp_subsec_micros() % 1000 + } + + /// Returns the nanosecond number from 0 to 999. + #[inline] + pub fn nanosecond(&self) -> u32 { + self.0.timestamp_subsec_nanos() % 1_000_000 + } + /// Returns the ISO week number starting from 1. /// /// The return value ranges from 1 to 53. (The last week of year differs by years.) @@ -294,7 +312,13 @@ impl DateTime { /// Returns the day of week starting from 0 (Sunday) to 6 (Saturday). #[inline] pub fn day_of_week(&self) -> u8 { - self.0.weekday() as u8 + self.iso_day_of_week() % 7 + } + + /// Returns the ISO day of week starting from 1 (Monday) to 7 (Sunday). + #[inline] + pub fn iso_day_of_week(&self) -> u8 { + (self.0.weekday() as u8) + 1 } /// Returns `true` if the current year is a leap year. @@ -666,6 +690,7 @@ mod tests { let date = "2023-11-30".parse::().unwrap(); let datetime = DateTime::from(date); + assert!(datetime.day_of_week() == 4); assert_eq!("2023-11-30", datetime.format_date()); assert_eq!("00:00:00", datetime.format_time()); } diff --git a/zino-core/src/datetime/time.rs b/zino-core/src/datetime/time.rs index b2b3cc58..3e1e0aeb 100644 --- a/zino-core/src/datetime/time.rs +++ b/zino-core/src/datetime/time.rs @@ -1,4 +1,4 @@ -use crate::{AvroValue, JsonValue}; +use crate::{error::Error, AvroValue, JsonValue}; use chrono::{format::ParseError, Local, NaiveTime, Timelike}; use serde::{Deserialize, Serialize, Serializer}; use std::{ @@ -13,6 +13,19 @@ use std::{ pub struct Time(NaiveTime); impl Time { + /// Attempts to create a new instance. + #[inline] + pub fn try_new(hour: u32, minute: u32, second: u32) -> Result { + NaiveTime::from_hms_opt(hour, minute, second) + .map(Self) + .ok_or_else(|| { + let message = format!( + "fail to create a time from hour: `{hour}`, minute: `{minute}`, second: `{second}`" + ); + Error::new(message) + }) + } + /// Returns a new instance which corresponds to the current time. #[inline] pub fn now() -> Self { diff --git a/zino-derive/Cargo.toml b/zino-derive/Cargo.toml index 0e191d22..99018a60 100644 --- a/zino-derive/Cargo.toml +++ b/zino-derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "zino-derive" description = "Derived traits for zino." -version = "0.13.2" +version = "0.13.3" rust-version = "1.73" edition = "2021" license = "MIT" @@ -21,5 +21,5 @@ syn = "2.0.39" [dependencies.zino-core] path = "../zino-core" -version = "0.16.2" +version = "0.16.3" features = ["orm"] diff --git a/zino-model/Cargo.toml b/zino-model/Cargo.toml index 2002a2f5..07519caa 100644 --- a/zino-model/Cargo.toml +++ b/zino-model/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "zino-model" description = "Domain models for zino." -version = "0.13.2" +version = "0.13.3" rust-version = "1.73" edition = "2021" license = "MIT" @@ -43,7 +43,7 @@ features = ["derive"] [dependencies.zino-core] path = "../zino-core" -version = "0.16.2" +version = "0.16.3" features = [ "orm", "validator-email", @@ -52,4 +52,4 @@ features = [ [dependencies.zino-derive] path = "../zino-derive" -version = "0.13.2" +version = "0.13.3" diff --git a/zino/Cargo.toml b/zino/Cargo.toml index 49613a1a..520046db 100644 --- a/zino/Cargo.toml +++ b/zino/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "zino" description = "Next-generation framework for composable applications in Rust." -version = "0.15.2" +version = "0.15.3" rust-version = "1.73" edition = "2021" license = "MIT" @@ -175,4 +175,4 @@ optional = true [dependencies.zino-core] path = "../zino-core" -version = "0.16.2" +version = "0.16.3"