2016-02-18 13 views

risposta

40
package main 

import (
    "time" 
    "fmt" 
) 

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

golang Time.Format

+0

Questa dovrebbe essere la risposta accettata. Gli altri potrebbero funzionare, ma sicuramente confondono qualcuno che non ha familiarità con il codice. – Shadoninja

+0

ISO 8601 e RFC3339 non sono tecnicamente la stessa cosa. https://stackoverflow.com/questions/522251/whats-the-difference-between-iso-8601-and-rfc-3339-date-formats – 425nesp

+1

Da quello che ho potuto dire, RFC3339 è una versione più rigorosa di ISO 8601. Quindi è probabilmente sicuro usare il formato RFC se un sistema si aspetta l'ISO. –

27
package main 

import (
    "fmt" 
    "time" 
) 

func main() { 
    fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700")) 
} 
4

Sostituzione del segno nel formato con una Z innesca il comportamento ISO 8601 di stampa Z al posto di un offset per l'orario UTC. Quindi la Z è importante.

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. 
+0

Sembra che non restituisca il valore corretto del fuso orario seguito da Z – Amol

+0

Sono sicuro che stampa il timestamp corretto. Questo è lo stesso formato utilizzato nel tempo. Tempo. Vedi https://golang.org/src/time/format.go?s=15423:15465#L78 – dustinevan

Problemi correlati