-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.go
82 lines (73 loc) · 1.84 KB
/
funcs.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 jalaali
import (
"regexp"
"strconv"
"strings"
"time"
ptime "github.com/yaa110/go-persian-calendar"
)
// TehranTz get tehran time zone
func TehranTz() *time.Location {
return time.FixedZone("Asia/Tehran", 12600)
}
// KabulTz get kabul time zone
func KabulTz() *time.Location {
l, err := time.LoadLocation("Asia/Kabul")
if err != nil {
return nil
}
return l
}
// Parse parse jalaali date from string
//
// this function return nil if invalid date passed
func Parse(str string) *time.Time {
return ParseForLocale(str, time.Now().Location())
}
// ParseForLocale parse jalaali date from string for locale
//
// this function return nil if invalid date passed
func ParseForLocale(str string, loc *time.Location) *time.Time {
isNumber := func(v string) bool { return regexp.MustCompile(`^[0-9]+$`).MatchString(v) }
pattern := regexp.MustCompile(`[^\d]+`)
_parts := strings.Split(pattern.ReplaceAllString(str, "-"), "-")
parts := make([]int, 6)
for i := 0; i < 6; i++ {
item := "0"
if len(_parts) > i && isNumber(_parts[i]) {
item = _parts[i]
}
t, _ := strconv.Atoi(item)
parts[i] = t
}
jDate := ptime.Date(parts[0], ptime.Month(parts[1]), parts[2], parts[3], parts[4], parts[5], 0, loc)
if jDate.Year() == parts[0] && int(jDate.Month()) == parts[1] && jDate.Day() == parts[2] {
res := jDate.Time()
return &res
}
return nil
}
// New create new jalaali date from time
//
// this function use current time zone
func New(t time.Time) Jalaali {
var jd Jalaali
jd.SetTime(t)
return jd
}
// New create new jalaali date from time
//
// this function use tehran time zone
func NewTehran(t time.Time) JalaaliTehran {
var jd JalaaliTehran
jd.SetTime(t)
return jd
}
// New create new jalaali date from time
//
// this function use kabul time zone
func NewKabul(t time.Time) JalaaliKabul {
var jd JalaaliKabul
jd.SetTime(t)
return jd
}