You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Goal: i'm writing a statusbar, and one of its blocks is supposed to update datetime every second.
Draft code:
// dependencies are sys-locale, icu_{locid,calendar,datetime}, jiff and jiff-icuuse jiff_icu::ConvertFrom;// this trait can convert jiff DateTime to icu DateTime fnmain(){let locale: icu_locid::Locale = sys_locale::get_locale().unwrap_or("en-US".to_string()).parse().unwrap();let formatter =
icu_datetime::TypedDateTimeFormatter::try_new(&locale.into(),Default::default()).unwrap();loop{let now = jiff::Zoned::now().datetime();let now_icu =
icu_calendar::DateTime::convert_from(now).to_calendar(icu_calendar::Gregorian);let now_fmt = formatter.format_to_string(&now_icu);println!("{}",&now_fmt);
std::thread::sleep(std::time::Duration::from_secs(1));}}
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's time.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's strftime can't use localized units from icu (yet?). Values in icu_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:
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:
Tip of main and 2.0.0-beta2: icu::datetime::pattern::FixedCalendarDateTimeNames
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 'date icon' E, date 'time icon' time?
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: