-
Notifications
You must be signed in to change notification settings - Fork 19
/
time.go
60 lines (52 loc) · 2.17 KB
/
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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package vegatime
import (
"time"
types "code.vegaprotocol.io/vega/protos/vega"
)
// Unix create a new time from sec and nsec in UTC timezone.
func Unix(sec int64, nsec int64) time.Time {
return time.Unix(sec, nsec).UTC()
}
// UnixNano equivalent to time.Unix(sec, nsec) but to be used with the
// result of time.Time{}.UnixNano() in UTC timezone.
func UnixNano(nsec int64) time.Time {
return time.Unix(nsec/int64(time.Second), nsec%int64(time.Second)).UTC()
}
// Parse parse a string expected to be a time in the time.RFC3339Nano format.
func Parse(t string) (time.Time, error) {
return time.ParseInLocation(time.RFC3339Nano, t, time.UTC)
}
// Format format the time using the time.RFC3339Nano formatting.
func Format(t time.Time) string {
return t.In(time.UTC).Format(time.RFC3339Nano)
}
// RoundToNearest round an actual time to the nearest interval.
func RoundToNearest(t time.Time, interval types.Interval) time.Time {
switch interval {
case types.Interval_INTERVAL_I1M:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, time.UTC)
case types.Interval_INTERVAL_I5M:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), (t.Minute()/5)*5, 0, 0, time.UTC)
case types.Interval_INTERVAL_I15M:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), (t.Minute()/15)*15, 0, 0, time.UTC)
case types.Interval_INTERVAL_I1H:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, time.UTC)
case types.Interval_INTERVAL_I6H:
return time.Date(t.Year(), t.Month(), t.Day(), (t.Hour()/6)*6, 0, 0, 0, time.UTC)
case types.Interval_INTERVAL_I1D:
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
default:
return t
}
}