This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.go
105 lines (86 loc) · 2.36 KB
/
functions.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
95
96
97
98
99
100
101
102
103
104
105
package asterisk
import (
"math"
"strings"
"time"
)
//-----------------------------------------------------------------------------
// Round .
func Round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
//-----------------------------------------------------------------------------
// Mod % operator for float64
func Mod(f float64, n float64) float64 {
lf := math.Abs(f)
for lf > n {
lf = lf - n
}
if f < 0 {
lf = -1. * lf
}
return lf
}
//-----------------------------------------------------------------------------
// DurationFromHour .
func DurationFromHour(hour float64) time.Duration {
return time.Duration(int64(hour*HourNanosecondFactor)) * time.Nanosecond
}
//-----------------------------------------------------------------------------
// DurationFromMinute .
func DurationFromMinute(minute float64) time.Duration {
return time.Duration(int64(minute*MinuteNanosecondFactor)) * time.Nanosecond
}
//-----------------------------------------------------------------------------
// DurationFromSecond .
func DurationFromSecond(second float64) time.Duration {
return time.Duration(int64(second*NanosecondFactor)) * time.Nanosecond
}
//-----------------------------------------------------------------------------
// DegreeString .
func DegreeString(d time.Duration) string {
sec := d.Seconds()
sec = Round(sec, .5, 0)
d = time.Duration(sec * 1000000000)
su := d.String()
s := strings.Replace(su, "h", "°", 1)
s = strings.Replace(s, "m", "'", 1)
s = strings.Replace(s, "s", "\"", 1)
return s
}
//-----------------------------------------------------------------------------
// FitIn .
func FitIn(x float64, inclusiveStart float64, exclusiveEnd float64) float64 {
d := x
dx := exclusiveEnd - inclusiveStart
for d >= exclusiveEnd {
d = d - dx
}
for d < inclusiveStart {
d = d + dx
}
return d
}
//-----------------------------------------------------------------------------
// PositiveMod .
func PositiveMod(l float64, factor float64) float64 {
var n = l
for n < 0.0 {
n = n + factor
}
for n >= factor {
n = n - factor
}
return n
}
//-----------------------------------------------------------------------------