erickguan / ffi-icu

FFI wrappers for ICU. MRI extension with the dynamic C library.
https://github.com/erickguan/ffi-icu
MIT License
35 stars 22 forks source link

Add support for h11/h12/h23/h24 hour cycle #53

Closed KJTsanaktsidis closed 2 years ago

KJTsanaktsidis commented 2 years ago

The hour cycle can be directly specified to the call to #format, or inferred from an "@hours=" keyword on the locale.

The algorithm for fixing up the skeletons & patterns is the same as that used in Firefox: https://github.com/tc39/ecma402/issues/665#issuecomment-1084833809

There are two main ways to call this API. Either hour_cycle can be specified directly, or it can be set to :locale in which case the hours keyword in the locale will be used.

irb(main):010:0> t = Time.new(2021, 04, 01, 13, 05, 0, "+00:00")
=> 2021-04-01 13:05:00 +0000
irb(main):011:0> ICU::TimeFormatting.format(t, zone: 'UTC', locale: 'en_AU', hour_cycle: 'h12')
=> "1/4/2021, 1:05 pm"
irb(main):012:0> ICU::TimeFormatting.format(t, zone: 'UTC', locale: 'en_AU', hour_cycle: 'h23')
=> "1/4/2021, 13:05"
irb(main):013:0> ICU::TimeFormatting.format(t, zone: 'UTC', locale: 'en_AU@hours=h12;', hour_cycle: :locale)
=> "1/4/2021, 1:05 pm"
irb(main):014:0> ICU::TimeFormatting.format(t, zone: 'UTC', locale: 'en_AU@hours=h23;', hour_cycle: :locale)
=> "1/4/2021, 13:05"
lwelti commented 2 years ago

@KJTsanaktsidis could you post in the description the different ways to call the API, just to be sure. thanks!

KJTsanaktsidis commented 2 years ago

Done - is this what you meant?

lwelti commented 2 years ago

yes! thanks @KJTsanaktsidis

lwelti commented 2 years ago

and just to be 110% sure :) , is backward compatible, so any previous call such as:

ICU::TimeFormatting.format(t, zone: 'UTC', :locale => 'cs_CZ', :date => :pattern, :time => :pattern, :skeleton => 'MMMMY')

still works right? I guess if not then the test will be failing

KJTsanaktsidis commented 2 years ago

Yup! The default value for hour_cycle is :pattern, which short circuits out all of this new logic