-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptime.go
61 lines (50 loc) · 1.07 KB
/
ptime.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
package ptime
import (
"fmt"
"time"
"github.com/blackchip-org/zc/v5/pkg/ptime/locale"
)
type P struct {
Locale *locale.Locale
Parser *Parser
}
func For(loc *locale.Locale) *P {
return &P{
Locale: loc,
Parser: NewParser(loc),
}
}
func ForLocale(name string) (*P, error) {
loc, ok := locale.Lookup(name)
if !ok {
return nil, fmt.Errorf("unknown locale: %v", name)
}
return For(loc), nil
}
func (p *P) Parse(text string) (Parsed, error) {
return p.Parser.Parse(text)
}
func (p *P) ParseDate(text string) (Parsed, error) {
return p.Parser.ParseDate(text)
}
func (p *P) ParseTime(text string) (Parsed, error) {
return p.Parser.ParseTime(text)
}
func (p *P) Time(parsed Parsed, now time.Time) (time.Time, error) {
return Time(p.Locale, parsed, now)
}
func (p *P) Format(layout string, t time.Time) string {
return Format(p.Locale, layout, t)
}
func FormatOffset(offset int, sep string) string {
sign := "+"
if offset < 0 {
sign = "-"
}
h := offset / 3600
if h < 0 {
h = h * -1
}
m := offset / 60 % 60
return fmt.Sprintf("%v%02d%v%02d", sign, h, sep, m)
}