Closed bdarcus closed 1 year ago
Update: not happy with either chronos or icu yet.
https://gist.github.com/bdarcus/36e3957412c569a891d125bf00fd4c6b
I think as a first step, I'll just use chronos, and iterate as needed.
// parse edtf string
let edtf_date: Edtf = match Edtf::parse("2022-04-06") {
Ok(edtf) => edtf,
Err(error) => panic!("Failed to parse EDTF date: {}", error),
};
// convert it to chrono; before doing so, probably want to note qualifiers and such
let ecdate = edtf_date.as_date().unwrap().to_chrono().unwrap();
// let chrono localize it
println!("{}", ecdate.format_localized("%e %B %Y", Locale::en_US));
Closed via #74
Update
I think I've decided to ditch the below because of how much of a hassle it is to integrate these different crates.
Seems like ideally someone would fork the EDTF crate and update it, and add a companion edtf-format.
But I don't have the time for that, so will just write my own bit of code for that, using the locales data for month etc formatting.
Vague ideas on how to do that:
String
orEdtf
(itself an enum)fmt:Display
for the formeryear
method return a 0-year-date for invalid EDTF, since this is critical for processingformat!
for all the other formatting, and return aString
I've tried this a few times, and keep getting tripped up on little stupid details, like integer type-casting.
This is high-priority, and I think icu is the right approach. But hopefully someone can submit a PR that magically fixes it all?
Data types problems
The primary stumbling block I ran into was converting the
Edtf::Date
etc into a form that icu, or even chrono would consume.There is a
to_chrone
method in the Edtf crate, but it only works for complete dates. But bibliographic data is often incomplete.When I manually tried to create these objects, I ran into a brick wall of type errors, and couldn't figure out how to cast the parameters to the correct integer types.
I may just need something like this:
Background
https://users.rust-lang.org/t/localized-date-time-formatting/94868/7
And this is a great thread.
https://www.reddit.com/r/rust/comments/q4xaig/icu_vs_rust_icu/