time/format: how to add custom formats (24-hour without 0 padding)

565 views
Skip to first unread message

iked...@gmail.com

unread,
Jul 1, 2017, 9:49:47 AM7/1/17
to golang-nuts
Hello,

I want to format times like this (for Canadian French users):
  06:00 -> 6:00
  15:00 -> 15:00

So I want format hours in 24-hour clock without 0 padding.
Unfortunately, predefined formats in time/format provides only 0 padded hours for 24-hour clock.

Is there a way to extend time/format and add custom formats?

Regards,
Ikedam

Sam Whited

unread,
Jul 1, 2017, 12:02:13 PM7/1/17
to iked...@gmail.com, golang-nuts
On Sat, Jul 1, 2017 at 2:39 AM, <iked...@gmail.com> wrote:
> Is there a way to extend time/format and add custom formats?

Check out the documentation for the "Format" function:

https://godoc.org/time#Time.Format

—Sam

Sam Whited

unread,
Jul 1, 2017, 12:18:14 PM7/1/17
to iked...@gmail.com, golang-nuts
On Sat, Jul 1, 2017 at 11:01 AM, Sam Whited <s...@samwhited.com> wrote:
> Check out the documentation for the "Format" function:

Follow up because I only answered part of the question (my apologies):
unfortunately, while Format does allow you to add custom formats, it
won't let you change the padding for 24-hour formats. You could always
format a simple timestamp like this using the generic string
formatting methods:

fmt.Println(fmt.Sprintf("%d:%02d", t.Hour(), t.Minute()))

—Sam

iked...@gmail.com

unread,
Jul 1, 2017, 1:01:25 PM7/1/17
to golang-nuts, iked...@gmail.com
Thanks for the answer.

I believe it would be nice if custom formats can be added by developers.
Does it make sense to create a pull request for time/format to allow developers add custom formats?

Custom formats can be defined if I could extend `nextStdChunk` (extracting `std` from layouts) and a part of `AppendFormat` (converting `std` to outputs).
So I plan to extract and export those two functions, and provide alternate `Format` (e.g. `FormatWithCustomStd) that receives customized version of them.

Or should it be provided as some third-party library?

Regards,
Ikedam

2017年7月2日日曜日 1時18分14秒 UTC+9 Sam Whited:

Jakob Borg

unread,
Jul 1, 2017, 2:22:27 PM7/1/17
to iked...@gmail.com, golang-nuts
I think this would be difficult to do correctly, given the (very unusual) choice of using an example time as the format string. I don’t see an obvious way of expressing the example hour (15) in a way that would imply “24 hour format, but no zero padding”.

//jb
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Collier-Brown

unread,
Jul 1, 2017, 8:19:19 PM7/1/17
to golang-nuts, iked...@gmail.com
This looks good:  if there is a mechanism to align times with language, it should be the implementation of Canadian French (along with things like circiumflex-E)

iked...@gmail.com

unread,
Jul 2, 2017, 10:44:57 AM7/2/17
to golang-nuts, iked...@gmail.com
> I don’t see an obvious way of expressing the example hour (15) in a way that would imply “24 hour format, but no zero padding”.

I agree with that.
It's difficult to decide an example format for 24-hour without zero padding as a part of the standard feature.

And that makes me believe the customizing feature is useful.
Developers can decide the example expression for their custom formats as it works fine with their usecases.
They may want to use 8 (unused in the existing example formats) for that, or may want to use f (hexiadecimal of 15) for that.


2017年7月2日日曜日 3時22分27秒 UTC+9 Jakob Borg:
Reply all
Reply to author
Forward
0 new messages