1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:30:25 -07:00

Add RFC822 formats as named constants.

Make sure to print a time zone when formatting even if none is defined.
Add a comment introducing lookupTimezone (not lookupTimeZone).

Fixes isse 577.

R=rsc
CC=golang-dev
https://golang.org/cl/196090
This commit is contained in:
Rob Pike 2010-02-04 15:39:27 +11:00
parent 188b2ac839
commit 2bcca5d951
3 changed files with 38 additions and 5 deletions

View File

@ -28,6 +28,9 @@ const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 1504 MST"
// RFC822 with Zulu time.
RFC822Z = "02 Jan 06 1504 -0700"
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
Kitchen = "3:04PM"
@ -209,7 +212,7 @@ func (t *Time) Format(layout string) string {
case stdISO8601TZ, stdNumTZ:
// Ugly special case. We cheat and take "Z" to mean "the time
// zone as formatted for ISO 8601".
zone := t.ZoneOffset / 60 // conver to minutes
zone := t.ZoneOffset / 60 // convert to minutes
if p == stdISO8601TZ && t.ZoneOffset == 0 {
p = "Z"
} else {
@ -248,7 +251,21 @@ func (t *Time) Format(layout string) string {
p = "am"
}
case stdTZ:
if t.Zone != "" {
p = t.Zone
} else {
// No time zone known for this time, but we must print one.
// Use the -0700 format.
zone := t.ZoneOffset / 60 // convert to minutes
if zone < 0 {
p = "-"
zone = -zone
} else {
p = "+"
}
p += zeroPad(zone / 60)
p += zeroPad(zone % 60)
}
}
b.WriteString(p)
}

View File

@ -133,6 +133,7 @@ var formatTests = []FormatTest{
FormatTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010"},
FormatTest{"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010"},
FormatTest{"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010"},
FormatTest{"RFC822", RFC822, "04 Feb 10 2100 PST"},
FormatTest{"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST"},
FormatTest{"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST"},
FormatTest{"ISO8601", ISO8601, "2010-02-04T21:00:57-0800"},
@ -286,6 +287,20 @@ func TestParseErrors(t *testing.T) {
}
}
// Check that a time without a Zone still produces a (numeric) time zone
// when formatted with MST as a requested zone.
func TestMissingZone(t *testing.T) {
time, err := Parse(RubyDate, "Tue Feb 02 16:10:03 -0500 2006")
if err != nil {
t.Fatal("error parsing date:", err)
}
expect := "Tue Feb 2 16:10:03 -0500 2006" // -0500 not EST
str := time.Format(UnixDate) // uses MST as its time zone
if str != expect {
t.Errorf("expected %q got %q", expect, str)
}
}
func BenchmarkSeconds(b *testing.B) {
for i := 0; i < b.N; i++ {
Seconds()

View File

@ -221,6 +221,7 @@ func setupZone() {
}
}
// Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone)
if len(zones) == 0 {