feat: finish DateTime implementation

* format_time: formats the current DateTime to a string with a specified
  locale and timezone
This commit is contained in:
Erica Marigold 2023-08-23 19:05:01 +05:30
parent 40b1494536
commit 2efc0be6f7
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
3 changed files with 76 additions and 20 deletions

44
Cargo.lock generated
View file

@ -324,6 +324,20 @@ dependencies = [
"winapi",
]
[[package]]
name = "chrono_locale"
version = "0.1.1"
source = "git+https://github.com/0x5eal/chrono-locale.git?rev=ff1df09#ff1df090a1bc55574ffe2bb4c04014ba017c6f36"
dependencies = [
"chrono",
"lazy_static",
"num-integer",
"serde",
"serde_derive",
"serde_json",
"walkdir",
]
[[package]]
name = "clap"
version = "4.3.21"
@ -1093,6 +1107,7 @@ dependencies = [
"async-compression",
"async-trait",
"chrono",
"chrono_locale",
"clap",
"console",
"dialoguer",
@ -1240,6 +1255,16 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.16"
@ -1848,6 +1873,15 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
@ -2527,6 +2561,16 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.1"

View file

@ -109,6 +109,7 @@ tokio-tungstenite = { version = "0.20", features = ["rustls-tls-webpki-roots"] }
### DATETIME
chrono = "0.4.26"
chrono_locale = { git = "https://github.com/0x5eal/chrono-locale.git", rev = "ff1df09" }
### CLI

View file

@ -1,5 +1,6 @@
use chrono::prelude::*;
use chrono::DateTime as ChronoDateTime;
use chrono_locale::LocaleDate;
use once_cell::sync::Lazy;
// TODO: Proper error handling and stuff
@ -160,33 +161,35 @@ impl DateTime {
pub fn to_iso_date(&self) -> String {
self.to_universal_time()
.to_string::<&str>(Timezone::UTC, None)
.to_string::<&str>(Timezone::UTC, None, None)
}
// TODO: Implement localization
// There seems to be only one localization crate for chrono,
// which has been committed to last 5 years ago. Thus, this crate doesn't
// work with the version of chrono we're using. I'll be forking this crate
// and making it compatible with our version of chrono for now.
pub fn format_universal_time<T>(&self, fmt_str: T, locale: T) -> String
// work with the version of chrono we're using. I've forked the crate
// and have made it compatible with the latest version of chrono.
// TODO: Implement more locales for chrono-locale.
pub fn format_time<T>(&self, timezone: Timezone, fmt_str: T, locale: T) -> String
where
T: ToString,
{
let format = fmt_str.to_string();
self.to_universal_time()
.to_string(Timezone::UTC, Some(format))
self.to_universal_time().to_string(
timezone,
Some(fmt_str.to_string()),
Some(locale.to_string()),
)
}
}
pub struct DateTimeConstructor {
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
millisecond: u32,
pub year: i32,
pub month: u32,
pub day: u32,
pub hour: u32,
pub minute: u32,
pub second: u32,
pub millisecond: u32,
}
impl Default for DateTimeConstructor {
@ -204,7 +207,7 @@ impl Default for DateTimeConstructor {
}
}
enum Timezone {
pub enum Timezone {
UTC,
Local,
}
@ -252,7 +255,7 @@ impl DateTimeConstructor {
self
}
fn to_string<T>(&self, timezone: Timezone, format: Option<T>) -> String
fn to_string<T>(&self, timezone: Timezone, format: Option<T>, locale: Option<T>) -> String
where
T: ToString,
{
@ -264,6 +267,14 @@ impl DateTimeConstructor {
}
});
let locale_lazy: Lazy<String, _> = Lazy::new(|| {
if let Some(locale) = locale {
locale.to_string()
} else {
"en".to_string()
}
});
match timezone {
Timezone::UTC => Utc
.with_ymd_and_hms(
@ -275,7 +286,7 @@ impl DateTimeConstructor {
self.second,
)
.unwrap()
.format((*format_lazy).as_str())
.formatl((*format_lazy).as_str(), (*locale_lazy).as_str())
.to_string(),
Timezone::Local => Local
.with_ymd_and_hms(
@ -287,7 +298,7 @@ impl DateTimeConstructor {
self.second,
)
.unwrap()
.format((*format_lazy).as_str())
.formatl((*format_lazy).as_str(), (*locale_lazy).as_str())
.to_string(),
}
}