-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to use localized units (months, week days) in a more specific datetime formatting? #6180
Comments
The best i18n behavior is almost always to use the locale-provided patterns. You shouldn't expect that the year/month/day are always in a particular order, or that the month is always numeric, etc. With that disclaimer out of the way, you can do what you are asking for with:
Sorry for this not being discoverable on 1.5; it is improved on the 2.0 branch. |
Hmm, looks quite complicated for what I expected to be a basically oneliner. Looks like I have to use a custom pattern, since none of the default ones include days of the week and shortened months together |
I guess i can leave day/month/year to the locale, but can I prepend and append additional stuff to the existing format, like |
This is with use icu::datetime::{
fieldsets,
input::{Date, DateTime, Time},
DateTimeFormatter, NoCalendarFormatter,
};
use icu::locale::locale;
fn main() {
let prefs = locale!("fr").into();
let ymd = DateTimeFormatter::try_new(prefs, fieldsets::YMDE::medium()).unwrap();
let time = NoCalendarFormatter::try_new(prefs, fieldsets::T::short()).unwrap();
loop {
let now = jiff::Zoned::now().datetime();
// jiff-icu doesn't support 2.0 yet
let datetime = DateTime {
date: Date::try_new_gregorian(
i32::from(now.year()),
now.month().unsigned_abs(),
now.day().unsigned_abs(),
)
.unwrap(),
time: Time::try_new(
now.hour().unsigned_abs(),
now.minute().unsigned_abs(),
now.second().unsigned_abs(),
now.subsec_nanosecond().unsigned_abs(),
)
.unwrap(),
};
println!(
"{} :: {}",
ymd.format(&datetime),
time.format(&datetime.time)
); // jeu. 27 févr. 2025 :: 21:01:17
std::thread::sleep(core::time::Duration::from_secs(1));
}
} |
@robertbastian's example looks good if you wish to manually interpolate the date and time together into a pattern. Most users wishing to use datetime formatting though should prefer to use |
That example looks epic, I'll try it out. However, I'd rather use YMDE and T separately, as I wanna insert a coloured icon between the date and the time. Thanks for the tips |
Goal: i'm writing a statusbar, and one of its blocks is supposed to update datetime every second.
Draft code:
Expected functionality: means to format the date using localized values (
strftime
style), so it looks like%a, %d %b %Y :: %T
->Вс, 23 фев 2025 :: 18:47:05
, akin to Python'stime.strftime(format)
or GNU coreutils'date +FORMAT
, which do follow a given locale.Actual functionality:
[Typed]DateTimeFormatter
isn't able to format datetime after a pattern, and jiff'sstrftime
can't use localized units from icu (yet?). Values inicu_datetime::options::length
don't seem to give full control over the output.Is there a different way I can achieve that?
The text was updated successfully, but these errors were encountered: