-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
71 lines (61 loc) · 2.06 KB
/
helpers.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
// Copyright (c) 2024 Neomantra Corp
package dbn
import (
"bytes"
"strings"
"time"
)
// / The denominator of fixed prices in DBN.
const FIXED_PRICE_SCALE float64 = 1000000000.0
func Fixed9ToFloat64(fixed int64) float64 {
return float64(fixed) / FIXED_PRICE_SCALE
}
// TrimNullBytes removes trailing nulls from a byte slice and returns a string.
func TrimNullBytes(b []byte) string {
return string(bytes.TrimRight(b, "\x00"))
}
// TimestampToSecNanos converts a DBN timestamp to seconds and nanoseconds.
func TimestampToSecNanos(dbnTimestamp uint64) (int64, int64) {
secs := int64(dbnTimestamp / 1e9)
nano := int64(dbnTimestamp) - int64(secs*1e9)
return secs, nano
}
// TimestampToTime converts a DBN timestamp to time.Time
func TimestampToTime(dbnTimestamp uint64) time.Time {
secs := int64(dbnTimestamp / 1e9)
nano := int64(dbnTimestamp) - int64(secs*1e9)
return time.Unix(secs, nano)
}
// TimeToYMD returns the YYYYMMDD for the time.Time in that Time's location.
// A zero time returns a 0 value.
// From https://github.com/neomantra/ymdflag/blob/main/ymdflag.go#L49
func TimeToYMD(t time.Time) uint32 {
if t.IsZero() {
return 0
} else {
return uint32(10000*t.Year() + 100*int(t.Month()) + t.Day())
}
}
// YMDtoTime returns the Time corresponding to the YYYYMMDD in the specified location, without validating the argument.`
// A value of 0 returns a Zero Time, independent of location.
// A nil location implies local time.
// https://github.com/neomantra/ymdflag/blob/main/ymdflag.go#L34
func YMDToTime(yyyymmdd int, loc *time.Location) time.Time {
if yyyymmdd == 0 {
return time.Time{}
}
var year int = yyyymmdd / 10000
var month int = (yyyymmdd % 10000) / 100
var day int = yyyymmdd % 100
if loc == nil {
loc = time.Local
}
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc)
}
// DatasetToHostname returns the dataset string into the hostname formnat.
// This is the dataset in lowercase with . replaced with -.
func DatasetToHostname(dataset string) string {
str := strings.ToLower(dataset)
str = strings.ReplaceAll(str, ".", "-")
return str
}