FAQ
Hello everybody,

I'm wondering what is wrong with "time" package Format() function? It's
behaviour goes beyond my comprehension:

func main() {
t := time.Now()
fmt.Println(t.Format("Jan 2, 2014"))
fmt.Println(t.Format("Jan 2, 2006"))
}

Produces:
Nov 10, 10110
Nov 10, 2009

(http://play.golang.org/p/guU4r3KMad)

Thank you in advance,
Alex


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jérôme Champion at Dec 3, 2014 at 12:44 pm
    It seems to work as it should ( fmt.Println(t.Format("Jan 2, 2006")) is the
    good version )
    Play has a fixed date, clic on the about button and it will shows:

        - In the playground the time begins at 2009-11-10 23:00:00 UTC
        (determining the significance of this date is an exercise for the reader).
        This makes it easier to cache programs by giving them deterministic output.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Manlio Perillo at Dec 3, 2014 at 12:49 pm
    Il giorno mercoledì 3 dicembre 2014 13:30:32 UTC+1, Oleksandr Lobunets ha
    scritto:
    Hello everybody,

    I'm wondering what is wrong with "time" package Format() function? It's
    behaviour goes beyond my comprehension:

    func main() {
    t := time.Now()
    fmt.Println(t.Format("Jan 2, 2014"))
    fmt.Println(t.Format("Jan 2, 2006"))
    }

    Produces:
    Nov 10, 10110
    Nov 10, 2009
    First of all, Playground use a fake time:
    http://blog.golang.org/playground#TOC_3.1

    Finally, the format string in the second example is wrong, since it does
    not represent the reference time.


    Regards Manlio

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Manlio Perillo at Dec 3, 2014 at 12:52 pm
    Il giorno mercoledì 3 dicembre 2014 13:49:43 UTC+1, Manlio Perillo ha
    scritto:
    [...]
    Nov 10, 10110
    Nov 10, 2009
    First of all, Playground use a fake time:
    http://blog.golang.org/playground#TOC_3.1

    Finally, the format string in the second example is wrong, since it does
    not represent the reference time.
    The wrong one is the first example, not the second; sorry.


    Regards Manlio

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matthew Zimmerman at Dec 3, 2014 at 2:59 pm
    This helps me. I was initially very confused about this "reference time"
    because it doesn't seem to be in the documentation, but the code makes it
    very clear what it is.

    https://golang.org/src/pkg/time/format.go#L61
    On Wed Dec 03 2014 at 7:52:43 AM Manlio Perillo wrote:

    Il giorno mercoledì 3 dicembre 2014 13:49:43 UTC+1, Manlio Perillo ha
    scritto:
    [...]
    Nov 10, 10110
    Nov 10, 2009
    First of all, Playground use a fake time:
    http://blog.golang.org/playground#TOC_3.1

    Finally, the format string in the second example is wrong, since it does
    not represent the reference time.
    The wrong one is the first example, not the second; sorry.


    Regards Manlio

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jérôme Champion at Dec 3, 2014 at 3:06 pm
    It's in the doc with a nice example. Did you search at the wrong place?
    ( It's there : http://golang.org/pkg/time/#Time.Format )

    Le mercredi 3 décembre 2014 15:59:46 UTC+1, Matthew Zimmerman a écrit :
    This helps me. I was initially very confused about this "reference time"
    because it doesn't seem to be in the documentation, but the code makes it
    very clear what it is.

    https://golang.org/src/pkg/time/format.go#L61

    On Wed Dec 03 2014 at 7:52:43 AM Manlio Perillo <manlio....@gmail.com
    <javascript:>> wrote:
    Il giorno mercoledì 3 dicembre 2014 13:49:43 UTC+1, Manlio Perillo ha
    scritto:
    [...]
    Nov 10, 10110
    Nov 10, 2009
    First of all, Playground use a fake time:
    http://blog.golang.org/playground#TOC_3.1

    Finally, the format string in the second example is wrong, since it does
    not represent the reference time.
    The wrong one is the first example, not the second; sorry.


    Regards Manlio

    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matthew Zimmerman at Dec 3, 2014 at 3:15 pm
    No, I don't. I see the reference time for one specific case but it's not
    spelled out clearly what each of those times represent.

    Looking only at the documentation, how would you know that "January" is
    what you put in your format if you want the month fully spelled out? What
    if I want a two-digit year? Where is that in the documentation?

    This isn't to point out that the documentation is lacking; I don't think it
    needs it since the source is so clear. A reference to the source would be
    nice I guess; just pointing out that I can agree with the original poster.
    The source is so nice since it's exhaustive where the documentation only
    provides an example.
    On Wed Dec 03 2014 at 10:06:35 AM Jérôme Champion wrote:

    It's in the doc with a nice example. Did you search at the wrong place?
    ( It's there : http://golang.org/pkg/time/#Time.Format )

    Le mercredi 3 décembre 2014 15:59:46 UTC+1, Matthew Zimmerman a écrit :
    This helps me. I was initially very confused about this "reference time"
    because it doesn't seem to be in the documentation, but the code makes it
    very clear what it is.

    https://golang.org/src/pkg/time/format.go#L61

    On Wed Dec 03 2014 at 7:52:43 AM Manlio Perillo <manlio....@gmail.com>
    wrote:
    Il giorno mercoledì 3 dicembre 2014 13:49:43 UTC+1, Manlio Perillo ha
    scritto:
    [...]
    Nov 10, 10110
    Nov 10, 2009
    First of all, Playground use a fake time:
    http://blog.golang.org/playground#TOC_3.1

    Finally, the format string in the second example is wrong, since it
    does not represent the reference time.
    The wrong one is the first example, not the second; sorry.


    Regards Manlio

    --
    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.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Julian Phillips at Dec 3, 2014 at 3:35 pm

    On 03/12/2014 15:15, Matthew Zimmerman wrote:
    No, I don't. I see the reference time for one specific case but it's
    not
    spelled out clearly what each of those times represent.

    Looking only at the documentation, how would you know that "January" is
    what you put in your format if you want the month fully spelled out?
    What
    if I want a two-digit year? Where is that in the documentation?
    I think you are looking for: http://golang.org/pkg/time/#pkg-constants

    --
    Julian

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Cmang at Dec 3, 2014 at 3:43 pm
    The point still stands that those predefined formats do not describe the
    expected values of the layout in a complete way and if your time format is
    not described there, it isn't clear how to make a custom layout that works.
    The comments in time/format.go are good, but that documentation isn't
    exported to godoc.
    On Wednesday, December 3, 2014 7:36:15 AM UTC-8, Julian Phillips wrote:
    On 03/12/2014 15:15, Matthew Zimmerman wrote:
    No, I don't. I see the reference time for one specific case but it's
    not
    spelled out clearly what each of those times represent.

    Looking only at the documentation, how would you know that "January" is
    what you put in your format if you want the month fully spelled out?
    What
    if I want a two-digit year? Where is that in the documentation?
    I think you are looking for: http://golang.org/pkg/time/#pkg-constants

    --
    Julian
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Davis at Dec 3, 2014 at 3:49 pm

    On Wed, Dec 3, 2014, at 03:42 PM, cmang@golang.org wrote:
    The point still stands that those predefined formats do not describe
    the expected values of the layout in a complete way and if your time
    format is not described there, it isn't clear how to make a custom
    layout that works. The comments in time/format.go are good, but that
    documentation isn't exported to godoc.
    This is a common source of confusion because many people have experience
    of other systems that use printf style tokens. I think the key phrase in
    the docs that makes the Go way clear is this one:

    "To define your own format, write down what the reference time would
    look like formatted your way"

    (from http://golang.org/pkg/time/#pkg-constants)

    That's basically all you have to do.

    Ian

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Chris dollin at Dec 3, 2014 at 4:08 pm

    On 3 December 2014 at 15:49, Ian Davis wrote:

    This is a common source of confusion because many people have experience of
    other systems that use printf style tokens. I think the key phrase in the
    docs that makes the Go way clear is this one:

    "To define your own format, write down what the reference time would look
    like formatted your way"

    (from http://golang.org/pkg/time/#pkg-constants)

    That's basically all you have to do.
    But there are unstated non-obvious constraints, eg on month
    names. Suppose my format is

         Mon Janvier 2 15:04:05 -0700 MST 2006

    If I then display a time in the next month, will it use Février?

    Chris

    --
    Chris "who is in fact not currently French" Dollin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Benjamin Measures at Dec 3, 2014 at 5:49 pm

    On Wednesday, 3 December 2014 16:08:59 UTC, ehedgehog wrote:
    But there are unstated non-obvious constraints, eg on month
    names. Suppose my format is

    Mon Janvier 2 15:04:05 -0700 MST 2006

    If I then display a time in the next month, will it use Février?
    Yeah, I was quite disappointed when I wasn't able to
    time.Now().Format("Stardate: -316994.7410895484")

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nick Craig-Wood at Dec 3, 2014 at 12:51 pm

    On 03/12/14 12:30, alexander.lobunets@gmail.com wrote:
    I'm wondering what is wrong with "time" package Format() function? It's
    behaviour goes beyond my comprehension:

    func main() {
    t := time.Now()
    fmt.Println(t.Format("Jan 2, 2014"))
    fmt.Println(t.Format("Jan 2, 2006"))
    }

    Produces:
    Nov 10, 10110
    Nov 10, 2009

    (http://play.golang.org/p/guU4r3KMad)
    Firstly the time on the playground is fixed to a specific time. That
    explains the second one which is correct otherwise.

    As for the first one - you need to use the magic date to specify the
    format and 2006 is the year you need not 2014.

    http://golang.org/pkg/time/#Parse

    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nate Finch at Dec 3, 2014 at 6:26 pm
    I think it is valid criticism that the docs on how to format times could be
    much more complete as to what is actually supported. Writing it "your way"
    will not work unless the code understands and expects "your way". What it
    understands and expects should be better documented. You shouldn't ever
    need to look at the source to see what is supported. One good example is
    time zones.... from the predefined constants, I can see that "-0700" and
    "MST" is supported, but is "Mountain Standard Time"? There's no way to
    know except to read the source.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 3, '14 at 12:30p
activeDec 3, '14 at 6:26p
posts14
users11
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase