mirror of
https://github.com/golang/go
synced 2024-11-24 22:27:57 -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:
parent
188b2ac839
commit
2bcca5d951
@ -28,6 +28,9 @@ const (
|
|||||||
ANSIC = "Mon Jan _2 15:04:05 2006"
|
ANSIC = "Mon Jan _2 15:04:05 2006"
|
||||||
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
|
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
|
||||||
RubyDate = "Mon Jan 02 15:04:05 -0700 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"
|
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
|
||||||
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
|
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
|
||||||
Kitchen = "3:04PM"
|
Kitchen = "3:04PM"
|
||||||
@ -209,7 +212,7 @@ func (t *Time) Format(layout string) string {
|
|||||||
case stdISO8601TZ, stdNumTZ:
|
case stdISO8601TZ, stdNumTZ:
|
||||||
// Ugly special case. We cheat and take "Z" to mean "the time
|
// Ugly special case. We cheat and take "Z" to mean "the time
|
||||||
// zone as formatted for ISO 8601".
|
// 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 {
|
if p == stdISO8601TZ && t.ZoneOffset == 0 {
|
||||||
p = "Z"
|
p = "Z"
|
||||||
} else {
|
} else {
|
||||||
@ -248,7 +251,21 @@ func (t *Time) Format(layout string) string {
|
|||||||
p = "am"
|
p = "am"
|
||||||
}
|
}
|
||||||
case stdTZ:
|
case stdTZ:
|
||||||
|
if t.Zone != "" {
|
||||||
p = 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)
|
b.WriteString(p)
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,7 @@ var formatTests = []FormatTest{
|
|||||||
FormatTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010"},
|
FormatTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010"},
|
||||||
FormatTest{"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010"},
|
FormatTest{"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010"},
|
||||||
FormatTest{"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 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{"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST"},
|
||||||
FormatTest{"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST"},
|
FormatTest{"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST"},
|
||||||
FormatTest{"ISO8601", ISO8601, "2010-02-04T21:00:57-0800"},
|
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) {
|
func BenchmarkSeconds(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
Seconds()
|
Seconds()
|
||||||
|
@ -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) {
|
func lookupTimezone(sec int64) (zone string, offset int) {
|
||||||
once.Do(setupZone)
|
once.Do(setupZone)
|
||||||
if len(zones) == 0 {
|
if len(zones) == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user