go-playground / locales

:earth_americas: a set of locales generated from the CLDR Project which can be used independently or within an i18n package; these were built for use with, but not exclusive to https://github.com/go-playground/universal-translator
MIT License
268 stars 55 forks source link

12 hour time bug #41

Open milesich opened 1 year ago

milesich commented 1 year ago
package main

import (
    "fmt"
    "time"
    "github.com/go-playground/locales/en"
)

func main() {
    datetime := time.Date(2016, 02, 03, 0, 0, 1, 0, time.Local)
    l := en.New()
    fmt.Println(l.FmtTimeShort(datetime))
}

That code should print 12:00 am but instead it prints 0:00 am.

12 hour time does not have 0 hours.

rokane commented 7 months ago

Hi @milesich,

I came across this issue and thought I would do some research and attempt to fix it. After doing some investigation, I believe this is the intended behaviour as defined by the CLDR specification.

My findings:

Unicode Locale Data Markup Language (LDML) defines the following patterns for hour formats:

when looking at the CLDR json specifications for date time formatting, I found the hourFormat field definition for the en locale:

"hourFormat": "+HH:mm;-HH:mm"

You can see this is defined using the HH pattern which would indicate it should adhere to the 00 format for 12 AM.

Further Notes

This does differ from what the standard go time package does if you were to format a time using the time.Kitchen format where it would be represented using the hh format.

package main

import (
    "fmt"
    "time"

    "github.com/go-playground/locales/en"
)

func main() {
    datetime := time.Date(2016, 02, 03, 0, 0, 1, 0, time.Local)
    l := en.New()

    fmt.Printf("Local (en) Short Time : %v \n", l.FmtTimeShort(datetime))
    fmt.Printf("Kitchen Format Time   : %v \n", datetime.Format(time.Kitchen))
}

Output:

Local (en) Short Time : 0:00 am
Kitchen Format Time   : 12:00AM

Outcome:

I see the current implementation working as expected defined by the CLDR specification.

But before closing out, I’d be keen to check in with @deankarn and confirm this is expected and the issue is not a bug.


If not, I’m more than happy to pick this up and implement a fix. I will likely implement something similar to how the go time.Format() function handles this case, inside the en/en.go#L503

h := t.Hour() % 12
if h == 0 {
  h = 12
}
milesich commented 7 months ago

@rokane You were looking at hour format for timezone part. eg. +13:00.

The spec for time format for en locale is using 12h format:

          "timeFormats": {
              "full": "h:mm:ss a zzzz",
              "full-alt-ascii": "h:mm:ss a zzzz",
              "long": "h:mm:ss a z",
              "long-alt-ascii": "h:mm:ss a z",
              "medium": "h:mm:ss a",
              "medium-alt-ascii": "h:mm:ss a",
              "short": "h:mm a",
              "short-alt-ascii": "h:mm a"
            },
rokane commented 7 months ago

@milesich thanks for the prompt response!

Great, I was looking at the wrong file. I see now, that makes sense.

I can take a look into this and see if I can get a fix working. I will let you know how I go once I have a PR raised.

rokane commented 7 months ago

@milesich I had a go at fixing this, if you can take a look and let me know what you think, that would be great

milesich commented 7 months ago

@rokane you should not be fixing individual locales because those files are generated from the spec. The generator needs fixing.

https://github.com/go-playground/locales/blob/master/cmd/generate_resources.go