golang time.Now().Format with milliseconds without dot in string

3,227 views
Skip to first unread message

Vasiliy Tolstov

unread,
Jul 14, 2020, 7:00:02 AM7/14/20
to golan...@googlegroups.com
Hi! I'm read several times godoc about the time package but have now
way to deal with it.
I need to output strings with format YYYYMMDDHHMMSSMILLISEC without
dot in string. But go able to output milliseconds only in case of .000


--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru

Jake Montgomery

unread,
Jul 14, 2020, 8:16:55 AM7/14/20
to golang-nuts
Why not just remove the dot? You know where it is: https://play.golang.org/p/ZHqHTOeA7HQ
Perhaps there is a better way, but this seems to work.

Vasiliy Tolstov

unread,
Jul 14, 2020, 8:40:18 AM7/14/20
to Jake Montgomery, golang-nuts
вт, 14 июл. 2020 г. в 15:17, Jake Montgomery <jake...@gmail.com>:
>
> Why not just remove the dot? You know where it is: https://play.golang.org/p/ZHqHTOeA7HQ
> Perhaps there is a better way, but this seems to work.
>

Yes, thanks. My question mostly - why format time to some intermediate
string that needs to be preprocessed before usage =)
Format means that you get a needed sting and not that you must use
slice manipulation.

Jake Montgomery

unread,
Jul 14, 2020, 11:38:39 AM7/14/20
to golang-nuts
I agree, it seems like an unfortunate oversight. You can do it without the slice tricks: https://play.golang.org/p/tNAPOcQqApN
It does take an additional Sprintf() though.

Vasiliy Tolstov

unread,
Jul 16, 2020, 7:21:32 PM7/16/20
to Jake Montgomery, golang-nuts
вт, 14 июл. 2020 г. в 18:39, Jake Montgomery <jake...@gmail.com>:
>
> I agree, it seems like an unfortunate oversight. You can do it without the slice tricks: https://play.golang.org/p/tNAPOcQqApN
> It does take an additional Sprintf() though.
>

Thanks, looks good. But i think for go devs adding additional format
that can be used to specify milli/micro/nano seconds will be easy.

Justin Israel

unread,
Jul 16, 2020, 10:40:22 PM7/16/20
to golang-nuts
I asked the same question on reddit about a month ago, where I needed to match an existing timestamp format:

Seems string formatting was the best option available to me.
 

Vasiliy Tolstov

unread,
Jul 18, 2020, 6:49:23 PM7/18/20
to Justin Israel, golang-nuts
пт, 17 июл. 2020 г. в 05:40, Justin Israel <justin...@gmail.com>:
Yes, this is bad case for stuff that works with many messages and
want to avoid additional memory allocations

peterGo

unread,
Jul 19, 2020, 5:32:06 PM7/19/20
to golang-nuts
Vasiliy Tolstov,

"I need to output strings with format YYYYMMDDHHMMSSMILLISEC without dot in string. But Go able to output milliseconds only in case of .000" ... "that works with many messages and want to avoid additional memory allocations"

The Go standard library does not pretend to provide everything you might need. Sometimes you need to do some programming. For example,

    func formatMillisecond(t time.Time) string

    fmtms.go
    https://play.golang.org/p/dfackUTL9qw

    $ go run fmtms.go

    20091110230000.001
    20091110230000001


A specialized function is often faster that a generalized function.

    fmtms_test.go
    https://play.golang.org/p/wS7PqZjExDq

    $ go test fmtms_test.go fmtms.go -bench=. -benchmem

    BenchmarkFmtMs-4   11470974   101 ns/op   32 B/op   1 allocs/op
    BenchmarkNoDot-4    4385608   272 ns/op   64 B/op   2 allocs/op
    BenchmarkDot-4      5529204   215 ns/op   32 B/op   1 allocs/op


Peter
Reply all
Reply to author
Forward
0 new messages