125

What is the equivalent code in golang for the following shell command ? date -u +%Y-%m-%dT%T%z

Koryonik
  • 2,728
  • 3
  • 22
  • 27
codefx
  • 9,872
  • 16
  • 53
  • 81

5 Answers5

252

If you're looking for a simple, but not perfect solution consider using time.RFC3339 constant. But also know that there are differences between ISO8601 which are too complex for this answer.

See https://ijmacd.github.io/rfc3339-iso8601/ for differences and also has a handy test file generator to show differences. There is also a good discussion on SO here What's the difference between ISO 8601 and RFC 3339 Date Formats?

package main

import (
    "time"
    "fmt"
)

func main(){
    fmt.Println(time.Now().Format(time.RFC3339))
}

golang Time.Format

Mike Graf
  • 5,077
  • 4
  • 45
  • 58
cikenerd
  • 2,660
  • 1
  • 10
  • 11
  • 2
    This should be the accepted answer. The others may work, but they would certainly confuse someone who was unfamiliar with the code. – Shadoninja Oct 24 '17 at 17:05
  • 28
    ISO 8601 and RFC3339 are not technically the same thing. https://stackoverflow.com/questions/522251/whats-the-difference-between-iso-8601-and-rfc-3339-date-formats – 425nesp Dec 09 '17 at 06:33
  • 12
    From what I could tell, RFC3339 is a stricter version of ISO 8601. So it is probably safe to use the RFC format if a system expects the ISO. – Noah Huppert Dec 28 '17 at 08:17
  • 1
    Note: It appears that converting to UTC first is not necessary: https://play.golang.org/p/UDrkigOfgIc – lukeic Nov 09 '18 at 02:04
  • 1
    @lukeic that's because golang playground is working in UTC timezone. Try to run on your local machine. – Flexoid Dec 11 '18 at 11:24
  • this answer worked for me: `time.Now().UTC().Add(time.Hour * 24).Format(time.RFC3339)` – elulcao Feb 11 '22 at 02:25
54
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
}
CrazyCrow
  • 4,125
  • 1
  • 28
  • 39
11

I had the following spec:

YYYY-MM-DDThh:mm:ss.sssZ

with the final Z being explicitly present in the examples.

Here's how I dealt with it:

  • first I found the time.RFCxxx that was the closest to my target
  • I copied its value
  • I fiddled with it until I found the expected result

which is

2006-01-02T15:04:05.999Z
avi.elkharrat
  • 6,100
  • 6
  • 41
  • 47
7

ISO8601 allows for variable levels of granularity. You can have just a year, year+month, year+month+day, add a time portion, and optionally have a timezone portion. Go's built-in time parsing, however, requires you to know ahead-of-time which parts will be included.

The github.com/btubbs/datetime library provides a more flexible parser that can handle all the commonly used ISO8601 formats. See https://github.com/btubbs/datetime

Disclosure: I wrote that library.

btubbs
  • 1,845
  • 1
  • 16
  • 19
  • "UnmarshalJSON methods support easy parsing of ISO 8601 timestamps from external systems" – lsl Jan 05 '21 at 11:46
  • Thanks a lot! Works as advertised, parses "20220201T204925Z" which is super useful when you cant have colon in the format. I wish there was a datetime.Format(...) function as well. – Steven Varga Feb 01 '22 at 22:02
6

Replacing the sign in the format with a Z triggers the ISO 8601 behavior. Which is exactly time.RFC3339. If you are wanting the string output to end in 'Z' what you need to do is convert to the UTC zone.

package main    
import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05Z07:00"))
}
// this is the same format used by RFC3339. just a note on why. 
dustinevan
  • 918
  • 9
  • 21
  • This does not seem to return correct timezone value followed by Z – Amol Feb 27 '18 at 02:13
  • 1
    I'm sure that it does print the correct time stamp. This is the same format used in time.Time. See https://golang.org/src/time/format.go?s=15423:15465#L78 – dustinevan Mar 05 '18 at 18:45
  • 2
    Thanks a lot! I could format to ISO8601 basic (no colons) : `fmt.Println(time.Now().UTC().Format("20060102T150405Z"))` in fact it works the other directions as well: `tm, err := time.Parse("20060102T150405Z", "20220201T204925Z")` – Steven Varga Feb 01 '22 at 22:06