Finally, simple, familiar, locale-based datetime formatting.
Go
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
internal/locales
.gitignore
CONTRIBUTING.md
LICENSE
README.md
directives.go
directives_test.go
lctime.go
lctime_test.go
parse.go
parse_test.go

README.md

lctime

Finally, simple, familiar, locale-based datetime formatting.

GoDoc Build Status Coverage Status

Install

go get -u github.com/variadico/lctime

Usage

// 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 setlocale and 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 locales.

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 lctime like any other package and go build like normal. Everything will just work.