Greetings,
I'm trying to set a response cookie in my program which works, but no matter what I try, I can't get the expires string to
1. Conform to the required format i.e. DAY, DD-MMM-YYYY HH:MM:SS GMT
2. Print in anything other than UTC
I've been stuck on this for hours hoping someone can help it's got to be something completely simple :/
Below is a snippet that illustrates the problem, output from running this is:
Thu, 18-Dec-2014 01:58:28 GMT
foo=bar; Path=/; Domain=
example.com; Expires=Thu, 18 Dec 2014 01:58:28 UTC
So top string is what I want, string shown after Expires= is what I end up with :/
I'm using go 1.2.2 on x64 thanks in advance for any help!
/* begin code */
package main
import (
"fmt"
"net/http"
"os"
"time"
)
func main() {
// Time format for cookie expires string
const layout = "Mon, 02-Jan-2006 15:04:05 GMT"
loc, err := time.LoadLocation("GMT")
if err != nil {
os.Exit(1)
}
expires := time.Now().In(loc)
expires = expires.AddDate(0, 0, 7)
// Date, format and time zone (gmt) print correctly
fmt.Println(expires.In(loc).Format(layout))
cookie := new(http.Cookie)
cookie.Name = "foo"
cookie.Value = "bar"
cookie.Path = "/"
cookie.Domain = "
example.com"
cookie.Expires = expires.In(loc)
cookie.RawExpires = expires.In(loc).Format(layout)
// Date correct, format and time zone (utc) incorrect
fmt.Println(cookie.String())
}