@@ -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,46 @@ 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
+ b , err := t .AppendBinary (make ([]byte , 0 , 16 ))
1488
+ if err != nil {
1489
+ return nil , err
1490
+ }
1491
+ return b , nil
1484
1492
}
1485
1493
1486
- // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
1494
+ // UnmarshalBinary implements the [ encoding.BinaryUnmarshaler] interface.
1487
1495
func (t * Time ) UnmarshalBinary (data []byte ) error {
1488
1496
buf := data
1489
1497
if len (buf ) == 0 {
@@ -1576,19 +1584,26 @@ func (t *Time) UnmarshalJSON(data []byte) error {
1576
1584
return err
1577
1585
}
1578
1586
1579
- // MarshalText implements the [encoding.TextMarshaler ] interface.
1587
+ // AppendText implements the [encoding.TextAppender ] interface.
1580
1588
// The time is formatted in RFC 3339 format with sub-second precision.
1581
1589
// If the timestamp cannot be represented as valid RFC 3339
1582
- // (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 ))
1590
+ // (e.g., the year is out of range), then an error is returned.
1591
+ func (t Time ) AppendText (b []byte ) ([]byte , error ) {
1585
1592
b , err := t .appendStrictRFC3339 (b )
1586
1593
if err != nil {
1587
1594
return nil , errors .New ("Time.MarshalText: " + err .Error ())
1588
1595
}
1589
1596
return b , nil
1590
1597
}
1591
1598
1599
+ // MarshalText implements the [encoding.TextMarshaler] interface. The output
1600
+ // matches that of calling the [Time.AppendText] method.
1601
+ //
1602
+ // See [Time.AppendText] for more information.
1603
+ func (t Time ) MarshalText () ([]byte , error ) {
1604
+ return t .AppendText (make ([]byte , 0 , len (RFC3339Nano )))
1605
+ }
1606
+
1592
1607
// UnmarshalText implements the [encoding.TextUnmarshaler] interface.
1593
1608
// The time must be in the RFC 3339 format.
1594
1609
func (t * Time ) UnmarshalText (data []byte ) error {
0 commit comments