File: marshal.go

package info (click to toggle)
golang-github-rickb777-date 1.20.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 460 kB
  • sloc: sh: 63; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package clock

import (
	"errors"
)

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (c Clock) MarshalBinary() ([]byte, error) {
	enc := []byte{
		byte(c >> 24),
		byte(c >> 16),
		byte(c >> 8),
		byte(c),
	}
	return enc, nil
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (c *Clock) UnmarshalBinary(data []byte) error {
	if len(data) == 0 {
		return errors.New("Clock.UnmarshalBinary: no data")
	}
	if len(data) != 4 {
		return errors.New("Clock.UnmarshalBinary: invalid length")
	}

	*c = Clock(data[3]) | Clock(data[2])<<8 | Clock(data[1])<<16 | Clock(data[0])<<24
	return nil
}

// MarshalText implements the encoding.TextMarshaler interface.
func (c Clock) MarshalText() ([]byte, error) {
	return []byte(c.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (c *Clock) UnmarshalText(data []byte) (err error) {
	clock, err := Parse(string(data))
	if err == nil {
		*c = clock
	}
	return err
}