Commit 78547ca1 authored by Russ Cox's avatar Russ Cox

time: do not segment time strings by character class.

instead use pure substring matching to find template values.

this makes stdZulu unnecessary and allows formats
like "20060102 030405" (used in some internet protocols).

this makes Parse not handle years < 0000 or > 9999 anymore.
that seems like an okay price to pay, trading hypothetical
functionality for real functionality.

also changed the comments on the Time struct to use the
same reference date as the format and parse routines.

R=r
CC=golang-dev
https://golang.org/cl/833045
parent 8fbe8bee
This diff is collapsed.
......@@ -43,12 +43,12 @@ const (
// Time is the struct representing a parsed time value.
type Time struct {
Year int64 // 2008 is 2008
Month, Day int // Sep-17 is 9, 17
Hour, Minute, Second int // 10:43:12 is 10, 43, 12
Weekday int // Sunday, Monday, ...
ZoneOffset int // seconds east of UTC
Zone string
Year int64 // 2006 is 2006
Month, Day int // Jan-2 is 1, 2
Hour, Minute, Second int // 15:04:05 is 15, 4, 5.
Weekday int // Sunday, Monday, ...
ZoneOffset int // seconds east of UTC, e.g. -7*60 for -0700
Zone string // e.g., "MST"
}
var nonleapyear = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
......
......@@ -169,8 +169,6 @@ var parseTests = []ParseTest{
ParseTest{"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1},
ParseTest{"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1},
ParseTest{"ISO8601", ISO8601, "2010-02-04T21:00:57-0800", true, false, 1},
// Negative year
ParseTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 -2010", false, true, -1},
// Amount of white space should not matter.
ParseTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1},
ParseTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1},
......@@ -239,6 +237,10 @@ func TestFormatAndParse(t *testing.T) {
const fmt = "Mon MST " + ISO8601 // all fields
f := func(sec int64) bool {
t1 := SecondsToLocalTime(sec)
if t1.Year < 1000 || t1.Year > 9999 {
// not required to work
return true
}
t2, err := Parse(fmt, t1.Format(fmt))
if err != nil {
t.Errorf("error: %s", err)
......@@ -270,7 +272,7 @@ type ParseErrorTest struct {
var parseErrorTests = []ParseErrorTest{
ParseErrorTest{ANSIC, "Feb 4 21:00:60 2010", "parse"}, // cannot parse Feb as Mon
ParseErrorTest{ANSIC, "Thu Feb 4 21:00:57 @2010", "format"},
ParseErrorTest{ANSIC, "Thu Feb 4 21:00:57 @2010", "parse"},
ParseErrorTest{ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"},
ParseErrorTest{ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"},
ParseErrorTest{ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment