Skip to content
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

Open
acid-bong opened this issue Feb 23, 2025 · 3 comments
Labels
C-datetime Component: datetime, calendars, time zones question Unresolved questions; type unclear

Comments

@acid-bong
Copy link

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-icu
use jiff_icu::ConvertFrom; // this trait can convert jiff DateTime to icu DateTime 

fn main() {
    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?

@sffc
Copy link
Member

sffc commented Feb 23, 2025

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.

@sffc sffc added question Unresolved questions; type unclear C-datetime Component: datetime, calendars, time zones labels Feb 23, 2025
@acid-bong
Copy link
Author

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

@acid-bong
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-datetime Component: datetime, calendars, time zones question Unresolved questions; type unclear
Projects
None yet
Development

No branches or pull requests

2 participants