This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
/
date_time.go
94 lines (77 loc) · 2.67 KB
/
date_time.go
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
// Copyright 2017 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
// Package datetime implements marshalling of Go DateTime values into Noms structs
// with type DateTimeType.
package datetime
import (
"math"
"time"
"github.com/attic-labs/noms/go/marshal"
"github.com/attic-labs/noms/go/types"
)
const (
datetypename = "DateTime"
hrsEncodingName = "noms-datetime"
)
// DateTime implements marshaling of time.Time to and from Noms.
type DateTime struct {
time.Time
}
// DateTimeType is the Noms type used to represent date time objects in Noms.
// The field secSinceEpoch may contain fractions in cases where seconds are
// not sufficient.
var DateTimeType = types.MakeStructTypeFromFields(datetypename, types.FieldMap{
"secSinceEpoch": types.NumberType,
})
var dateTimeTemplate = types.MakeStructTemplate(datetypename, []string{"secSinceEpoch"})
// Epoch is the unix Epoch. This time is very consistent,
// which makes it useful for testing or checking for uninitialized values
var Epoch = DateTime{time.Unix(0, 0)}
func init() {
RegisterHRSCommenter(time.Local)
}
// Now is an alias for a DateTime initialized with time.Now()
func Now() DateTime {
return DateTime{time.Now()}
}
// MarshalNoms makes DateTime implement marshal.Marshaler and it makes
// DateTime marshal into a Noms struct with type DateTimeType.
func (dt DateTime) MarshalNoms(vrw types.ValueReadWriter) (types.Value, error) {
return dateTimeTemplate.NewStruct([]types.Value{types.Number(float64(dt.Unix()) + float64(dt.Nanosecond())*1e-9)}), nil
}
// MarshalNomsType makes DateTime implement marshal.TypeMarshaler and it
// allows marshal.MarshalType to work with DateTime.
func (dt DateTime) MarshalNomsType() (*types.Type, error) {
return DateTimeType, nil
}
// UnmarshalNoms makes DateTime implement marshal.Unmarshaler and it allows
// Noms struct with type DateTimeType able to be unmarshaled onto a DateTime
// Go struct
func (dt *DateTime) UnmarshalNoms(v types.Value) error {
strct := struct {
SecSinceEpoch float64
}{}
err := marshal.Unmarshal(v, &strct)
if err != nil {
return err
}
s, frac := math.Modf(strct.SecSinceEpoch)
*dt = DateTime{time.Unix(int64(s), int64(frac*1e9))}
return nil
}
type DateTimeCommenter struct {
tz *time.Location
}
func (c DateTimeCommenter) Comment(v types.Value) string {
if !types.IsValueSubtypeOf(v, DateTimeType) {
return ""
}
var dt DateTime
marshal.MustUnmarshal(v, &dt)
return dt.In(c.tz).Format(time.RFC3339)
}
func RegisterHRSCommenter(tz *time.Location) {
hrsCommenter := DateTimeCommenter{tz: tz}
types.RegisterHRSCommenter(datetypename, hrsEncodingName, hrsCommenter)
}