Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a timestamp as GMT in Go?

Tags:

time

go

gmt

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?

like image 767
Elliot Chance Avatar asked Jan 02 '23 21:01

Elliot Chance


1 Answers

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)
like image 168
Bayta Darell Avatar answered Jan 05 '23 14:01

Bayta Darell