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

12 hour time bug #41

Open
milesich opened this issue Nov 3, 2022 · 5 comments
Open

12 hour time bug #41

milesich opened this issue Nov 3, 2022 · 5 comments

Comments

@milesich
Copy link

milesich commented Nov 3, 2022

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
Copy link

rokane commented Jan 13, 2024

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:

  • h: [1, 12]
  • hh: [01, 12]
  • H: [0, 23]
  • HH: [00, 23]

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
Copy link
Author

milesich commented Jan 13, 2024

@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
Copy link

rokane commented Jan 13, 2024

@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
Copy link

rokane commented Jan 13, 2024

@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
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants