How do I format a timestamp as GMT in Go?

Issue

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?

Solution

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)

Answered By – Bayta Darell

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.