This repository has been archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
82 lines (70 loc) · 1.77 KB
/
format.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
const format_dateTime string = "2006-01-02 15:04:05"
func (s *Session) durationToStr() string {
return durationToStr(s.Duration)
}
// TODO(zacsh) dry up places where this is done by hand
func parseStampCommand(cmd string) (time.Time, error) {
stamp, e := strconv.ParseInt(strings.TrimSpace(cmd), 10, 64)
if e != nil {
return time.Time{}, fmt.Errorf("stamp arg: %s", e)
}
return time.Unix(stamp, 0 /*nanoseconds*/), nil
}
const durationToStrMaxLen = 20
func getTZContext() string {
return time.Now().Format("-0700 MST")
}
func durationToStr(d time.Duration) string {
daysStr := ""
days := int(d.Hours()) / 24
if days > 0 {
daysStr = fmt.Sprintf("%04d days ", days)
}
h, m, s := durationToHMS(d)
colonIf := func(q int) string {
if q > 0 {
return fmt.Sprintf("%02d:", q)
}
return ""
}
return fmt.Sprintf("%s%s%02d:%02d", daysStr, colonIf(h), m, s)
}
func durationToHMS(d time.Duration) (int, int, int) {
days := int(d.Hours()) / 24
h := int((d - time.Duration(days)*time.Hour*24).Hours()) % 24
m := int((d - time.Duration(days)*time.Hour*24 -
time.Duration(h)*time.Hour).Minutes())
s := int((d - time.Duration(days)*time.Hour*24 -
time.Duration(h)*time.Hour -
time.Duration(m)*time.Minute).Seconds())
return h, m, s
}
func fromStatus(status bool) string {
if status {
return "in"
}
return "out"
}
func fromNote(note string) string {
if len(note) == 0 {
return "n/a"
}
return note
}
func isValidClient(clientStr string) bool {
if len(clientStr) < 1 {
return false
}
alphaOrNumeric := "[[:alpha:]]|[[:digit:]]"
validRegexp := regexp.MustCompile(fmt.Sprintf(
"^(%s)+(-*%s)*(_*%s)*$", alphaOrNumeric, alphaOrNumeric, alphaOrNumeric))
return validRegexp.MatchString(clientStr)
}