@@ -119,9 +119,9 @@ import (
119
119
// these methods does not change the actual instant it represents, only the time
120
120
// zone in which to interpret it.
121
121
//
122
- // Representations of a Time value saved by the [Time.GobEncode], [Time.MarshalBinary],
123
- // [Time.MarshalJSON], and [Time.MarshalText ] methods store the [Time.Location]'s offset, but not
124
- // the location name. They therefore lose information about Daylight Saving Time.
122
+ // Representations of a Time value saved by the [Time.GobEncode], [Time.MarshalBinary], [Time.AppendBinary],
123
+ // [Time.MarshalJSON], [Time.MarshalText] and [Time.AppendText ] methods store the [Time.Location]'s offset,
124
+ // but not the location name. They therefore lose information about Daylight Saving Time.
125
125
//
126
126
// In addition to the required “wall clock” reading, a Time may contain an optional
127
127
// reading of the current process's monotonic clock, to provide additional precision
@@ -1435,8 +1435,8 @@ const (
1435
1435
timeBinaryVersionV2 // For LMT only
1436
1436
)
1437
1437
1438
- // MarshalBinary implements the encoding.BinaryMarshaler interface.
1439
- func (t Time ) MarshalBinary ( ) ([]byte , error ) {
1438
+ // AppendBinary implements the [ encoding.BinaryAppender] interface.
1439
+ func (t Time ) AppendBinary ( b [] byte ) ([]byte , error ) {
1440
1440
var offsetMin int16 // minutes east of UTC. -1 is UTC.
1441
1441
var offsetSec int8
1442
1442
version := timeBinaryVersionV1
@@ -1452,38 +1452,42 @@ func (t Time) MarshalBinary() ([]byte, error) {
1452
1452
1453
1453
offset /= 60
1454
1454
if offset < - 32768 || offset == - 1 || offset > 32767 {
1455
- return nil , errors .New ("Time.MarshalBinary: unexpected zone offset" )
1455
+ return b , errors .New ("Time.MarshalBinary: unexpected zone offset" )
1456
1456
}
1457
1457
offsetMin = int16 (offset )
1458
1458
}
1459
1459
1460
1460
sec := t .sec ()
1461
1461
nsec := t .nsec ()
1462
- enc := [] byte {
1463
- version , // byte 0 : version
1464
- byte (sec >> 56 ), // bytes 1-8: seconds
1465
- byte (sec >> 48 ),
1466
- byte (sec >> 40 ),
1467
- byte (sec >> 32 ),
1468
- byte (sec >> 24 ),
1469
- byte (sec >> 16 ),
1470
- byte (sec >> 8 ),
1462
+ b = append ( b ,
1463
+ version , // byte 0 : version
1464
+ byte (sec >> 56 ), // bytes 1-8: seconds
1465
+ byte (sec >> 48 ),
1466
+ byte (sec >> 40 ),
1467
+ byte (sec >> 32 ),
1468
+ byte (sec >> 24 ),
1469
+ byte (sec >> 16 ),
1470
+ byte (sec >> 8 ),
1471
1471
byte (sec ),
1472
- byte (nsec >> 24 ), // bytes 9-12: nanoseconds
1473
- byte (nsec >> 16 ),
1474
- byte (nsec >> 8 ),
1472
+ byte (nsec >> 24 ), // bytes 9-12: nanoseconds
1473
+ byte (nsec >> 16 ),
1474
+ byte (nsec >> 8 ),
1475
1475
byte (nsec ),
1476
- byte (offsetMin >> 8 ), // bytes 13-14: zone offset in minutes
1476
+ byte (offsetMin >> 8 ), // bytes 13-14: zone offset in minutes
1477
1477
byte (offsetMin ),
1478
- }
1478
+ )
1479
1479
if version == timeBinaryVersionV2 {
1480
- enc = append (enc , byte (offsetSec ))
1480
+ b = append (b , byte (offsetSec ))
1481
1481
}
1482
+ return b , nil
1483
+ }
1482
1484
1483
- return enc , nil
1485
+ // MarshalBinary implements the [encoding.BinaryMarshaler] interface.
1486
+ func (t Time ) MarshalBinary () ([]byte , error ) {
1487
+ return t .AppendBinary (make ([]byte , 0 , 16 ))
1484
1488
}
1485
1489
1486
- // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
1490
+ // UnmarshalBinary implements the [ encoding.BinaryUnmarshaler] interface.
1487
1491
func (t * Time ) UnmarshalBinary (data []byte ) error {
1488
1492
buf := data
1489
1493
if len (buf ) == 0 {
@@ -1576,15 +1580,27 @@ func (t *Time) UnmarshalJSON(data []byte) error {
1576
1580
return err
1577
1581
}
1578
1582
1579
- // MarshalText implements the [encoding.TextMarshaler ] interface.
1583
+ // AppendText implements the [encoding.TextAppender ] interface.
1580
1584
// The time is formatted in RFC 3339 format with sub-second precision.
1581
1585
// If the timestamp cannot be represented as valid RFC 3339
1582
1586
// (e.g., the year is out of range), then an error is reported.
1583
- func (t Time ) MarshalText () ([]byte , error ) {
1584
- b := make ([]byte , 0 , len (RFC3339Nano ))
1587
+ func (t Time ) AppendText (b []byte ) ([]byte , error ) {
1585
1588
b , err := t .appendStrictRFC3339 (b )
1586
1589
if err != nil {
1587
- return nil , errors .New ("Time.MarshalText: " + err .Error ())
1590
+ return b , errors .New ("Time.MarshalText: " + err .Error ())
1591
+ }
1592
+ return b , nil
1593
+ }
1594
+
1595
+ // MarshalText implements the [encoding.TextMarshaler] interface. The output
1596
+ // matches that of calling the [Time.AppendText] method.
1597
+ //
1598
+ // See [Time.AppendText] for more information.
1599
+ func (t Time ) MarshalText () ([]byte , error ) {
1600
+ // This is semantically similar to t.AppendText(nil), and has better performance.
1601
+ b , err := t .AppendText (make ([]byte , 0 , len (RFC3339Nano )))
1602
+ if err != nil {
1603
+ return nil , err
1588
1604
}
1589
1605
return b , nil
1590
1606
}
0 commit comments