1

I have one problem: I need to know the difference/duration between two timestamps in golang. Therefore, I use the time library (https://golang.org/pkg/time/).

If I have two timestamps of type "time.time", it is easy to get the difference using "time.Sub()". My problem is that one of my timestamps comes from another function and it is only possible to transfer it as a string:

t1 := "2009-11-10 23:00:00 +0000 UTC m=+0.000000001" //type: string

t2 := time.Now() //type: time.time

Now I have to basic possibilities:

A) Convert t2 to a string as well and try to find out the difference between two strings (UGLY)

B) Convert t1 from type "string" to type "time.time" and then apply "time.Sub()"

I want to go way B). Therefore, I found out that

time.Parse(format, timestring)

should be able to do so. So I tried to convert t1 using

t1_time, err := time.Parse(time.RFC3339, t1)

but the result was not as expected! Instead I got this

0001-01-01 00:00:00 +0000 UTC

and the error saying "error parsing time "2009-11-10 23:00:02 +0000 UTC m=+2.000000001" as "2006-01-02T15:04:05Z07:00": cannot parse " 23:00:02 +0000 UTC m=+2.000000001" as "T" ".

If I use my own timeformat which is the same as t1

timeformat := "2009-11-10 23:00:00 +0000 UTC m=+0.000000001"

t1_time, err := time.Parse(timeformat , t1)

the result stays wrong and I get the error saying"error parsing time "2009-11-10 23:00:02 +0000 UTC m=+2.000000001" as "2009-11-10 23:00:00 +0000 UTC m=+0.000000001": cannot parse "9-11-10 23:00:02 +0000 UTC m=+2.000000001" as "009-" ".

I also tried to go over UNIX time but the time library does not allow me to convert a string into unix.

What do I do wrong??! Why is the parsing not working? Thanks for any help!

Community
  • 1
  • 1
Chris
  • 45
  • 8
  • go uses a static point in time as the reference for formatting. See https://golang.org/pkg/time/#pkg-constants specifically: this point in time `Mon Jan 2 15:04:05 MST 2006` or in RFC3339 `"2006-01-02T15:04:05Z07:00"` – colm.anseo May 21 '19 at 15:00
  • 1
    You should not be using `"2009-11-10 23:00:00 +0000 UTC m=+0.000000001"` as a time format. Not only is it less convenient to parse, it's not a stable format. From the documentation: "The returned string is meant for *debugging*; for a stable serialized representation, use `t.MarshalText`, `t.MarshalBinary`, or `t.Format` with an explicit format string." – JimB May 21 '19 at 15:00
  • 1
    so you want your time (the one in 2009) in `t1`. But you want your *format* to be the `2006` string format – colm.anseo May 21 '19 at 15:01
  • @Volker and @ Flimzy I'm actual curious on this one. The duplicate reference handles the base time components, but does not cover the monotonic component. If someone does time adds/subtracts that part can be quite significant. I wonder if this question is actually unique in that regard? – colm.anseo May 21 '19 at 22:58
  • @colminator: Are you suggesting that the monotonic portion is relevant to the OPs answer? I don't see that being mentioned. If the question is how to parse monotonic times, and/or how to compare those parts, it may be unique. But as I see it, the question is only: How to marshal/umharshal times, and how to calculate the differences. The duplicates should answer that. – Jonathan Hall May 22 '19 at 06:18
  • Clarification from the OP would be very welcome. – Jonathan Hall May 22 '19 at 06:18

1 Answers1

0

Basically you want:

    t1_raw := "2009-11-10 23:00:00 +0000 UTC m=+0.000000001"
    format := "2006-01-02 15:04:05 -0700 MST"

    // for simplicity t1_raw[:29] discards time's monotonic delta
    // i.e. the " m=+0.000000001" suffix

    t1, err := time.Parse(format, t1_raw[:29])
    if err != nil {
            log.Fatal(err)
    }

    log.Println("Duration ->", t2.Sub(t1))

If you really want the monotonic delta included, that number would have to be parsed manually and the delta applied to t1.

Playground version.

Note: playground version will show a duration of zero - as the playground's clock starts on Nov 11 2009 - the seminal birthdate of go.

colm.anseo
  • 19,337
  • 4
  • 43
  • 52
  • Thank you @colminator! That works for me. I don't need the monotonic delta but it is inside the reference code I use so I now handle it using [:29]. Thank your for your help! – Chris May 22 '19 at 06:49