Finally, simple, familiar, locale-based datetime formatting.
go get -u github.com/variadico/lctime
// Default locale is based on env vars or en_US if none are set. fmt.Println(lctime.Strftime("%c", time.Now())) // prints: Mon 14 Dec 2015 10:31:56 PM PST lctime.SetLocale("es_MX") fmt.Println(lctime.Strftime("%c", time.Now())) // prints: lun 14 dic 2015 22:31:56 PST
The problem with the Go standard library
Go's standard library
time is fine most of the time. However, it's currently
impossible for the Go standard library to format dates based on a user's locale
or langauge setting. Unfortunately, that means code like this, doesn't work.
d := time.Date(2015, 12, 25, 3, 2, 1, 0, time.UTC) fmt.Println(d.Format("lunes, 02 de enero de 2006")) // got: lunes, 25 de enero de 2015 // want: viernes, 25 de diciembre de 2015
More importantly, it also means that non-English speakers will either be stuck with purely numeric dates or dates in a language they don't prefer.
The solution offered by lctime
lctime brings the familiar
strftime functions found in other
programming languages like C, Python, or PHP. The formats and translations used
for this package are loosely based on glibc locale files, with close to 300
Locale data is just Go code, generated by go-bindata. This means you don't
have to worry about shipping anything extra. Just import and use
any other package and
go build like normal. Everything will just work.