Skip to content

Commit 86cca11

Browse files
time: revert CL 78735 (was: space padding using underscore)
CL 78735 description: time: add space padding layout strings(using underscore) for not only day but others As mentioned in golang#22802, only day component of layout string has space padding(represented by one underscore before its placeholder). This commit expands the rule for month, hour, minute and second. Updates golang#22802 (maybe fixes it) Revert this CL because it breaks currently working formats that happen to use underscores. Fixes golang#23259 Change-Id: I64acaaca9b5b74785ee0f0be7910574e87daa649 Reviewed-on: https://go-review.googlesource.com/85998 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org>
1 parent 74d8340 commit 86cca11

File tree

2 files changed

+10
-96
lines changed

2 files changed

+10
-96
lines changed

src/time/format.go

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ const (
9090
stdLongMonth = iota + stdNeedDate // "January"
9191
stdMonth // "Jan"
9292
stdNumMonth // "1"
93-
stdUnderMonth // "_1"
9493
stdZeroMonth // "01"
9594
stdLongWeekDay // "Monday"
9695
stdWeekDay // "Mon"
@@ -99,13 +98,10 @@ const (
9998
stdZeroDay // "02"
10099
stdHour = iota + stdNeedClock // "15"
101100
stdHour12 // "3"
102-
stdUnderHour12 // "_3"
103101
stdZeroHour12 // "03"
104102
stdMinute // "4"
105-
stdUnderMinute // "_4"
106103
stdZeroMinute // "04"
107104
stdSecond // "5"
108-
stdUnderSecond // "_5"
109105
stdZeroSecond // "05"
110106
stdLongYear = iota + stdNeedDate // "2006"
111107
stdYear // "06"
@@ -191,24 +187,13 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
191187
}
192188
return layout[0:i], stdDay, layout[i+1:]
193189

194-
case '_': // _1, _2, _2006, _3, _4, _5
195-
if len(layout) >= i+2 {
196-
switch layout[i+1] {
197-
case '1':
198-
return layout[0:i], stdUnderMonth, layout[i+2:]
199-
case '2':
200-
//_2006 is really a literal _, followed by stdLongYear
201-
if len(layout) >= i+5 && layout[i+1:i+5] == "2006" {
202-
return layout[0 : i+1], stdLongYear, layout[i+5:]
203-
}
204-
return layout[0:i], stdUnderDay, layout[i+2:]
205-
case '3':
206-
return layout[0:i], stdUnderHour12, layout[i+2:]
207-
case '4':
208-
return layout[0:i], stdUnderMinute, layout[i+2:]
209-
case '5':
210-
return layout[0:i], stdUnderSecond, layout[i+2:]
190+
case '_': // _2, _2006
191+
if len(layout) >= i+2 && layout[i+1] == '2' {
192+
//_2006 is really a literal _, followed by stdLongYear
193+
if len(layout) >= i+5 && layout[i+1:i+5] == "2006" {
194+
return layout[0 : i+1], stdLongYear, layout[i+5:]
211195
}
196+
return layout[0:i], stdUnderDay, layout[i+2:]
212197
}
213198

214199
case '3':
@@ -559,11 +544,6 @@ func (t Time) AppendFormat(b []byte, layout string) []byte {
559544
b = append(b, m...)
560545
case stdNumMonth:
561546
b = appendInt(b, int(month), 0)
562-
case stdUnderMonth:
563-
if month < 10 {
564-
b = append(b, ' ')
565-
}
566-
b = appendInt(b, int(month), 0)
567547
case stdZeroMonth:
568548
b = appendInt(b, int(month), 2)
569549
case stdWeekDay:
@@ -589,16 +569,6 @@ func (t Time) AppendFormat(b []byte, layout string) []byte {
589569
hr = 12
590570
}
591571
b = appendInt(b, hr, 0)
592-
case stdUnderHour12:
593-
// Noon is 12PM, midnight is 12AM.
594-
hr := hour % 12
595-
if hr == 0 {
596-
hr = 12
597-
}
598-
if hr < 10 {
599-
b = append(b, ' ')
600-
}
601-
b = appendInt(b, hr, 0)
602572
case stdZeroHour12:
603573
// Noon is 12PM, midnight is 12AM.
604574
hr := hour % 12
@@ -608,20 +578,10 @@ func (t Time) AppendFormat(b []byte, layout string) []byte {
608578
b = appendInt(b, hr, 2)
609579
case stdMinute:
610580
b = appendInt(b, min, 0)
611-
case stdUnderMinute:
612-
if min < 10 {
613-
b = append(b, ' ')
614-
}
615-
b = appendInt(b, min, 0)
616581
case stdZeroMinute:
617582
b = appendInt(b, min, 2)
618583
case stdSecond:
619584
b = appendInt(b, sec, 0)
620-
case stdUnderSecond:
621-
if sec < 10 {
622-
b = append(b, ' ')
623-
}
624-
b = appendInt(b, sec, 0)
625585
case stdZeroSecond:
626586
b = appendInt(b, sec, 2)
627587
case stdPM:
@@ -886,10 +846,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
886846
case stdLongMonth:
887847
month, value, err = lookup(longMonthNames, value)
888848
month++
889-
case stdNumMonth, stdUnderMonth, stdZeroMonth:
890-
if std == stdUnderMonth && len(value) > 0 && value[0] == ' ' {
891-
value = value[1:]
892-
}
849+
case stdNumMonth, stdZeroMonth:
893850
month, value, err = getnum(value, std == stdZeroMonth)
894851
if month <= 0 || 12 < month {
895852
rangeErrString = "month"
@@ -913,26 +870,17 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
913870
if hour < 0 || 24 <= hour {
914871
rangeErrString = "hour"
915872
}
916-
case stdHour12, stdUnderHour12, stdZeroHour12:
917-
if std == stdUnderHour12 && len(value) > 0 && value[0] == ' ' {
918-
value = value[1:]
919-
}
873+
case stdHour12, stdZeroHour12:
920874
hour, value, err = getnum(value, std == stdZeroHour12)
921875
if hour < 0 || 12 < hour {
922876
rangeErrString = "hour"
923877
}
924-
case stdMinute, stdUnderMinute, stdZeroMinute:
925-
if std == stdUnderMinute && len(value) > 0 && value[0] == ' ' {
926-
value = value[1:]
927-
}
878+
case stdMinute, stdZeroMinute:
928879
min, value, err = getnum(value, std == stdZeroMinute)
929880
if min < 0 || 60 <= min {
930881
rangeErrString = "minute"
931882
}
932-
case stdSecond, stdUnderSecond, stdZeroSecond:
933-
if std == stdUnderSecond && len(value) > 0 && value[0] == ' ' {
934-
value = value[1:]
935-
}
883+
case stdSecond, stdZeroSecond:
936884
sec, value, err = getnum(value, std == stdZeroSecond)
937885
if sec < 0 || 60 <= sec {
938886
rangeErrString = "second"

src/time/format_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,6 @@ func TestFormatShortYear(t *testing.T) {
116116
}
117117
}
118118

119-
// issue 22802.
120-
func TestFormatSpacePadding(t *testing.T) {
121-
for i := 9; i <= 10; i++ {
122-
time := Date(2001, Month(i), i, i, i, i, 700000000, UTC)
123-
result := time.Format("2006-_1-_2 _3:_4:_5")
124-
want := fmt.Sprintf("2001-%2d-%2d %2d:%2d:%2d", i, i, i, i, i)
125-
if result != want {
126-
t.Errorf("SpacePadding expected %q got %q", want, result)
127-
}
128-
}
129-
}
130-
131119
type ParseTest struct {
132120
name string
133121
format string
@@ -639,25 +627,3 @@ func TestUnderscoreTwoThousand(t *testing.T) {
639627
t.Errorf("Incorrect minute, got %d", m)
640628
}
641629
}
642-
643-
// issue 22802.
644-
func TestParseSpacePadding(t *testing.T) {
645-
format := "2006-_1-_2 _3:_4:_5"
646-
input := "2017- 9- 6 8: 4: 2"
647-
time, err := Parse(format, input)
648-
if err != nil {
649-
t.Error(err)
650-
}
651-
if y, m, d := time.Date(); y != 2017 || m != 9 || d != 6 {
652-
t.Errorf("Incorrect y/m/d, got %d/%d/%d", y, m, d)
653-
}
654-
if h := time.Hour(); h != 8 {
655-
t.Errorf("Incorrect hour, got %d", h)
656-
}
657-
if m := time.Minute(); m != 4 {
658-
t.Errorf("Incorrect minute, got %d", m)
659-
}
660-
if s := time.Second(); s != 2 {
661-
t.Errorf("Incorrect second, got %d", s)
662-
}
663-
}

0 commit comments

Comments
 (0)