A problem understanding time.Format

Hi,

I am scratching my head trying to understand a very simple one character change in time.Format.

I looked at this web site here to see some examples of formatting. If I use their example of formatting it works correctly, if I change one thing it does not and I don’t understand the difference.

Full code:

    package main

    import (
    "log"
    "time"
    )

    func main() {

    t := time.Now()
    log.Println(t)

    log.Println("Correct:", t.Format("2006-01-02 15:04:05"))
    log.Println("Weird:", t.Format("2007-01-02 15:04:05"))
    }

Output:
2021/07/23 23:44:24 2021-07-23 23:44:24.569482 +0100 BST m=+0.000072078
2021/07/23 23:44:24 Correct: 2021-07-23 23:44:24
2021/07/23 23:44:24 Weird: 23007-07-23 23:44:24

Why is the year so different? Similar things occur if I change year, month, day, hour, etc.

Many thanks.
J.

The date components are all magic numbers. Instead of how most programming languages use things like “yy,” “yyyy,” “MM”, “MMM”, etc., Go decided to use magic numbers for the date format. To get a 2 digit year format in go, use “06,” to get a 4 digit year, use “2006.” To get the hours as a 12-hour time, use “03” or “3.” To make it a 24-hr time, use “15,” etc.

I suspect it was supposed to be easier than remembering “is mm months and MM minutes or is it the other way around?” Everyone seems to get confused, though (myself included!), so I think they didn’t quite get it right.

4 Likes

Aha! Thanks very much for the prompt response. But I’m still confused.

Using the year as an example, what is the difference between 2006 and 2007? They are both four digit numbers, so I would expect a four digit year as the result. Why do I get 5 digits? I don’t understand how I go from 2021 using 2006 as the format, to 23007 by using 2007. Is 2006 the magic number (rather than just 4 digits)?

Thanks again.

J.

Because 2007 is not one of the magic numbers. 2 is a magic number for the day so you get “23” in the place of the “2” and then, I suppose, the formatter doesn’t know what to do with “007,” so it just leaves it.

EDIT: Link to the Go documentation that lists the specific numbers: https://pkg.go.dev/time#Parse

OK, thanks very much. I’ve followed the link and I can see where this comes from now. It’s not the first thing that comes to mind - having to know magic numbers to know how to format a date.

J.

Agreed. Potentially my least favorite function in the standard library!

2 Likes

You can try carbon,a simple, semantic and developer-friendly golang package for datetime

Thanks for the hint. I may try it. I use third party packages where necessary.

I’m going to write something that is perhaps controversial here, which you refer to yourself: if I need to use a party package for ‘developer-friendly’ date and time functions, then the built-in functions are by definition ‘developer-unfriendly’. I took on board what skillian wrote about the confusion between MM and mm - I’ve done this myself quite a few times over the years, realised after that I was using month instead of minute and vice versa, and corrected it. It’s easy to get wrong and equally easy to correct. It would never occur to me that 2007 is so different to 2006, without knowing about the magic numbers being used.

I’m not what you would call a serious developer, I merely dabble. I have found Go very easy to get into, without any serious problems, just those typical with learning something new. This is the first time I’ve encountered something which I would describe as ‘weird’, and 'why would they do that?.

Many thanks,
J.

Yeah - this is one of the few things in go that irks me. MM vs mm seems easier than totally random numbers, and this question comes up a lot. Also, in terms of debugging, it’s easier for me to use MM and get months and then say “oh right; it’s mm” and change it than it is to have to go look up a list of magic numbers.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.