Skip to content

Commit 2b664d5

Browse files
apocelipesgopherbot
authored andcommitted
time: correct time.AppendText's error message
"time.AppendText" returns error messages that start with the prefix "time.MarshalText: " which seems confusion. Now correct the message prefix to "time.AppendText: " and add a test to prevent regression. Change-Id: I5742c9c3ed802eb79c65d459910deae4f3652ffd GitHub-Last-Rev: ce96559 GitHub-Pull-Request: #69914 Reviewed-on: https://go-review.googlesource.com/c/go/+/620597 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f15195a commit 2b664d5

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/time/time.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,24 +1584,28 @@ func (t *Time) UnmarshalJSON(data []byte) error {
15841584
return err
15851585
}
15861586

1587+
func (t Time) appendTo(b []byte, errPrefix string) ([]byte, error) {
1588+
b, err := t.appendStrictRFC3339(b)
1589+
if err != nil {
1590+
return nil, errors.New(errPrefix + err.Error())
1591+
}
1592+
return b, nil
1593+
}
1594+
15871595
// AppendText implements the [encoding.TextAppender] interface.
15881596
// The time is formatted in RFC 3339 format with sub-second precision.
15891597
// If the timestamp cannot be represented as valid RFC 3339
15901598
// (e.g., the year is out of range), then an error is returned.
15911599
func (t Time) AppendText(b []byte) ([]byte, error) {
1592-
b, err := t.appendStrictRFC3339(b)
1593-
if err != nil {
1594-
return nil, errors.New("Time.MarshalText: " + err.Error())
1595-
}
1596-
return b, nil
1600+
return t.appendTo(b, "Time.AppendText: ")
15971601
}
15981602

15991603
// MarshalText implements the [encoding.TextMarshaler] interface. The output
16001604
// matches that of calling the [Time.AppendText] method.
16011605
//
16021606
// See [Time.AppendText] for more information.
16031607
func (t Time) MarshalText() ([]byte, error) {
1604-
return t.AppendText(make([]byte, 0, len(RFC3339Nano)))
1608+
return t.appendTo(make([]byte, 0, len(RFC3339Nano)), "Time.MarshalText: ")
16051609
}
16061610

16071611
// UnmarshalText implements the [encoding.TextUnmarshaler] interface.

src/time/time_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,16 @@ func TestMarshalInvalidTimes(t *testing.T) {
915915
case err == nil || err.Error() != want:
916916
t.Errorf("(%v).MarshalText() error = %v, want %v", tt.time, err, want)
917917
}
918+
919+
buf := make([]byte, 0, 64)
920+
want = strings.ReplaceAll(tt.want, "MarshalJSON", "AppendText")
921+
b, err = tt.time.AppendText(buf)
922+
switch {
923+
case b != nil:
924+
t.Errorf("(%v).AppendText() = %q, want nil", tt.time, b)
925+
case err == nil || err.Error() != want:
926+
t.Errorf("(%v).AppendText() error = %v, want %v", tt.time, err, want)
927+
}
918928
}
919929
}
920930

0 commit comments

Comments
 (0)