summary refs log tree commit diff
path: root/vendor/go.mau.fi/util/jsontime/string.go
blob: c3729d58d158286db25ac5fd92de3023e189b492 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2023 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package jsontime

import (
	"encoding/json"
	"strconv"
	"time"
)

func parseTimeString(data []byte, unixConv func(int64) time.Time, into *time.Time) error {
	var strVal string
	err := json.Unmarshal(data, &strVal)
	if err != nil {
		return err
	}
	val, err := strconv.ParseInt(strVal, 10, 64)
	if err != nil {
		return err
	}
	if val == 0 {
		*into = time.Time{}
	} else {
		*into = unixConv(val)
	}
	return nil
}

type UnixMilliString struct {
	time.Time
}

func (um UnixMilliString) MarshalJSON() ([]byte, error) {
	if um.IsZero() {
		return []byte{'"', '0', '"'}, nil
	}
	return json.Marshal(strconv.FormatInt(um.UnixMilli(), 10))
}

func (um *UnixMilliString) UnmarshalJSON(data []byte) error {
	return parseTimeString(data, time.UnixMilli, &um.Time)
}

type UnixMicroString struct {
	time.Time
}

func (um UnixMicroString) MarshalJSON() ([]byte, error) {
	if um.IsZero() {
		return []byte{'"', '0', '"'}, nil
	}
	return json.Marshal(strconv.FormatInt(um.UnixMicro(), 10))
}

func (um *UnixMicroString) UnmarshalJSON(data []byte) error {
	return parseTimeString(data, time.UnixMicro, &um.Time)
}

type UnixNanoString struct {
	time.Time
}

func (um UnixNanoString) MarshalJSON() ([]byte, error) {
	if um.IsZero() {
		return []byte{'"', '0', '"'}, nil
	}
	return json.Marshal(strconv.FormatInt(um.UnixNano(), 10))
}

func (um *UnixNanoString) UnmarshalJSON(data []byte) error {
	return parseTimeString(data, func(i int64) time.Time {
		return time.Unix(0, i)
	}, &um.Time)
}

type UnixString struct {
	time.Time
}

func (u UnixString) MarshalJSON() ([]byte, error) {
	if u.IsZero() {
		return []byte{'"', '0', '"'}, nil
	}
	return json.Marshal(strconv.FormatInt(u.Unix(), 10))
}

func (u *UnixString) UnmarshalJSON(data []byte) error {
	return parseTimeString(data, func(i int64) time.Time {
		return time.Unix(i, 0)
	}, &u.Time)
}