go-strftime
is a Golang library that implements the Python-like Time Format Function strftime()
.
According to this discussion Issue 444: Implement strftime, in the eyes of the Golang designer, the strftime()
is a bad interface and should be replaced by time.Format()
because the later one has a simpler format representation. However the formats could be used in time.Format()
is strictly limited to a few pre-defined constants. Sometimes it would become error-prone and tricky to use the format right to produce what format programmers want exactly other than the pre-defineds.
So this library is created to provide a convenient interface for those who like to use strftime()
in Golang.
Get the code down using the standard go tool:
go get github.com/hhkbp2/go-strftime
and write your code like
import "github.com/hhkbp2/go-strftime"
formatted := stftime.Format("%Y-%m-%d %H:%M:%S %9n", time.Now())
Please refer to the Python strftime
doc for all the supported format strings. Note that there are two differences:
- %c returns RFC1123 which is a bit different from what Python does
- %[1-9]n is added for fractional second. e.g. "%3n" means to print millisecond offset with this second. "%6n" means to print microsecond. "%9n" is for nanosecond.
This library is originally developed by Miki Tebeka and forked by Dylan Wen to add fractional format support.
The original repository could be found at https://bitbucket.org/tebeka/strftime.