-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
43 lines (38 loc) · 647 Bytes
/
util.go
File metadata and controls
43 lines (38 loc) · 647 Bytes
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
package main
import (
"math"
"os"
"path/filepath"
"time"
)
func clamp01(x float64) float64 {
if x < 0 {
return 0
}
if x > 1 {
return 1
}
return x
}
func ageSec(now, last time.Time) float64 {
if last.IsZero() {
return 0
}
a := now.Sub(last).Seconds()
if a < 0 {
return 0
}
return a
}
func writeFileAtomic(path string, b []byte, perm os.FileMode) error {
dir := filepath.Dir(path)
if dir != "." && dir != "" {
_ = os.MkdirAll(dir, 0o755)
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, b, perm); err != nil {
return err
}
return os.Rename(tmp, path)
}
var _ = math.Inf // to silence unused import if needed