6

I need to format a timestamp with RFC2616, the standard for HTTP dates. However, the standard says:

All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception.

From a little digging GMT and UTC are not the same thing. Is there a proper way to convert a timestamp to GMT in Go?

Elliot Chance
  • 5,526
  • 10
  • 49
  • 80

2 Answers2

12

Use the http.TimeFormat layout to format times for HTTP headers. This layout assumes a time in the UTC location.

 s := t.UTC().Format(http.TimeFormat)

If the time is known to be in UTC, then the call to UTC() can be skipped:

 s := t.Format(http.TimeFormat)
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
2

If I'm reading this right, the UTC() approach is problematic, since if the timezone is shown as text (e.g. format time.RFC1123) it will end in "UTC". The request was how to have it be "GMT".

I think THIS is what is needed:

 gmtTimeLoc := time.FixedZone("GMT", 0)
 s := t.In(gmtTimeLoc).Format(http.TimeFormat)

In my case, using format time.RFC1123, I get:

Sat, 09 Jun 2022 21:01:08 GMT

(in my impl, I created gmtTimeLoc once and reuse it)

BrianT.
  • 394
  • 1
  • 6