forked from capnspacehook/taskmaster
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
72 lines (56 loc) · 1.27 KB
/
utils.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
// +build windows
package taskmaster
import (
"errors"
"math"
"strings"
"time"
"github.com/rickb777/date/period"
)
var taskDateFormat = "2006-01-02T15:04:05"
var taskDateFormatWTimeZone = "2006-01-02T15:04:05-07:00"
var taskDateFormatUTC = "2006-01-02T15:04:05Z"
func IntToDayOfMonth(dayOfMonth int) (DayOfMonth, error) {
if dayOfMonth < 1 || dayOfMonth > 32 {
return 0, errors.New("invalid day of month")
}
return DayOfMonth(math.Exp2(float64(dayOfMonth - 1))), nil
}
func TimeToTaskDate(t time.Time) string {
defaultTime := time.Time{}
if t == defaultTime {
return ""
}
return t.Format(taskDateFormat)
}
func TaskDateToTime(s string) (time.Time, error) {
if s == "" {
return time.Time{}, nil
}
var t time.Time
var err error
if strings.Count(s, "-") == 3 || strings.Contains(s, "+") {
t, err = time.Parse(taskDateFormatWTimeZone, s)
} else if s[len(s)-1] == 'Z' {
t, err = time.Parse(taskDateFormatUTC, s)
} else {
t, err = time.Parse(taskDateFormat, s)
}
if err != nil {
return time.Time{}, err
}
return t, nil
}
func StringToPeriod(s string) (period.Period, error) {
if s == "" {
return period.Period{}, nil
}
return period.Parse(s)
}
func PeriodToString(p period.Period) string {
s := p.String()
if s == "P0D" {
return ""
}
return s
}