Parsing HTTP header Date in RFC1123

3,133 views
Skip to first unread message

Ihsan Junaidi Ibrahim

unread,
Oct 13, 2013, 4:24:37 AM10/13/13
to golan...@googlegroups.com
Hi,

http.TimeFormat returns RFC1123-formatted current time but hardcodes the timezone to GMT. Is there a way to change the timezone to current location without altering the time?

If I use time.Local(), the time changes to the GMT offset which is undesirable.

Thanks.
ihsan
Message has been deleted

Ihsan Junaidi Ibrahim

unread,
Oct 13, 2013, 1:39:41 PM10/13/13
to peterGo, golan...@googlegroups.com
When used with http.ParseTime(), it didn't work but it works correctly if I'm using time.Now() as your example showed.

t, _ := http.ParseTime(r.Header.Get("Date")
fmt.Println(t.Format(http.TimeFormat))
fmt.Println(t.Format(RFC1123))

Output:

Mon, 14 Oct 2013 01:38:32 GMT
Mon, 14 Oct 2013 01:38:32 UTC

On Oct 13, 2013, at 9:05 PM, peterGo <go.pe...@gmail.com> wrote:

> Use the time.RFC1123 format.
>
> For example,
>
> package main
>
> import (
> "fmt"
> "net/http"
> "time"
> )
>
> func main() {
> t := time.Now()
> fmt.Println(t.Format(http.TimeFormat))
> fmt.Println(t.Format(time.RFC1123))
> }
>
> Output:
>
> Sun, 13 Oct 2013 09:02:17 GMT
> Sun, 13 Oct 2013 09:02:17 EDT
> Peter
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1fa96ece-ce4f-45bd-b226-12ee4cc66483%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Ihsan Junaidi Ibrahim

unread,
Oct 14, 2013, 1:29:13 PM10/14/13
to golan...@googlegroups.com

Tamás Gulácsi

unread,
Oct 14, 2013, 3:53:18 PM10/14/13
to golan...@googlegroups.com
2013. október 14., hétfő 19:29:13 UTC+2 időpontban Ihsan Junaidi Ibrahim a következőt írta:
anyone?
See http://play.golang.org/p/3P_Xde4oAX

The easiest is to strip "GMT" from the end and replace it with your zone before parse.

Ihsan Junaidi Ibrahim

unread,
Oct 15, 2013, 1:41:57 PM10/15/13
to golan...@googlegroups.com
Sounds like a hack for what seems to be a trivial matter.

Is there a good reason why the local timezone is ignored when posting HTTP response? The HTTP1.1 spec mandates RFC1123 so why not just follow it?
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4e47dab9-c8de-4dff-8b24-c4cc023355f5%40googlegroups.com.

minux

unread,
Oct 15, 2013, 1:55:57 PM10/15/13
to Ihsan Junaidi Ibrahim, golang-nuts


On Oct 15, 2013 1:42 PM, "Ihsan Junaidi Ibrahim" <ih...@grep.my> wrote:
>
> Sounds like a hack for what seems to be a trivial matter.
>
> Is there a good reason why the local timezone is ignored when posting HTTP response? The HTTP1.1 spec mandates RFC1123 so why not just follow it?

because RFC actually requires it to be in GMT.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

Ihsan Junaidi Ibrahim

unread,
Oct 15, 2013, 2:40:11 PM10/15/13
to minux, golang-nuts
Thanks for pointing that out.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Ihsan Junaidi Ibrahim

unread,
Oct 15, 2013, 3:02:58 PM10/15/13
to golang-nuts
If the date is represented in GMT, shouldn't it change the time to reflect the actual time in GMT timezone.

I'm in +0800 but the the HTTP response header took the current server time and just replaced the timezone with GMT instead of shifting the time back to GMT.

So when I read back the response header on the client, the timestamp is all messed up.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/DB29A0C0-4C64-4075-B1F2-A6D5D7F5A50E%40grep.my.

minux

unread,
Oct 15, 2013, 4:03:09 PM10/15/13
to Ihsan Junaidi Ibrahim, golang-nuts


On Oct 15, 2013 3:03 PM, "Ihsan Junaidi Ibrahim" <ih...@grep.my> wrote:
>
> If the date is represented in GMT, shouldn't it change the time to reflect the actual time in GMT timezone.
>
> I'm in +0800 but the the HTTP response header took the current server time and just replaced  the timezone with GMT instead of shifting the time back to GMT.
>
> So when I read back the response header on the client, the timestamp is all messed up.

this was a real bug that has been fixed in tip (Go 1.2).

Ihsan Junaidi Ibrahim

unread,
Oct 16, 2013, 2:50:47 AM10/16/13
to minux, golang-nuts
Do you have the bug ID because I'm on tip +050f1f96c25c and the server is still sending the wrong timestamp.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA%2Bdb%3Dn1wJcVgS%2BJg%2B7-fRMQdSXmn%3D7157xE-CKAiUHt2_iHdsg%40mail.gmail.com.

minux

unread,
Oct 16, 2013, 10:53:37 AM10/16/13
to Ihsan Junaidi Ibrahim, golang-nuts
On Wed, Oct 16, 2013 at 2:50 AM, Ihsan Junaidi Ibrahim <ih...@grep.my> wrote:
Do you have the bug ID because I'm on tip +050f1f96c25c and the server is still sending the wrong timestamp.
there isn't an issue filed for that bug.

this is the CL that fixed the issue: https://codereview.appspot.com/13386047 

Jesse McNelis

unread,
Oct 16, 2013, 8:51:40 PM10/16/13
to Ihsan Junaidi Ibrahim, minux, golang-nuts
On Wed, Oct 16, 2013 at 5:50 PM, Ihsan Junaidi Ibrahim <ih...@grep.my> wrote:
Do you have the bug ID because I'm on tip +050f1f96c25c and the server is still sending the wrong timestamp.

Note that http.TimeFormat isn't a valid format for the time package.
The time package only recognises 'MST' as a timezone in a format pattern, anything else is just additional text in the format that it can ignore(like spaces, colons, dashes etc.).
If you take a time with a timezone other than GMT and format it as http.TimeFormat then you'll get a time string that has the wrong timezone.
But you shouldn't be setting the http date header to anything other than a GMT time so the fact that the format hardcodes it shouldn't be a problem.

--
=====================
http://jessta.id.au

Tamás Gulácsi

unread,
Oct 17, 2013, 8:17:03 AM10/17/13
to golan...@googlegroups.com
No, the http package shoul send GMT time, not local time tagged with GMT timezone.
Reply all
Reply to author
Forward
0 new messages