-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
50 lines (44 loc) · 1.19 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
package finding
import (
"time"
"unicode"
)
var UTC, _ = time.LoadLocation("UTC")
// FromEbayDateTime converts eBay datetime format to time.Time
// By default eBay date-time values are recorded in Universal Coordinated Time (UTC)
func FromEbayDateTime(ebayDT string) (time.Time, error) {
return time.ParseInLocation("2006-01-02T15:04:05.000Z", ebayDT, UTC)
}
// ToEbayDateTime converts given time to eBay format
// Given datetime has to be casted to UTC location.
func ToEbayDateTime(datetime time.Time) string {
return datetime.Format("2006-01-02T15:04:05.000Z")
}
// FromEbayDuration converts eBay duration to Golang duration
// eBay format is PnYnMnDTnHnMnS (e.g., P2DT23H32M51S)
func FromEbayDuration(ebayDuration string) time.Duration {
d := time.Second * 0
var cd int
for _, b := range ebayDuration {
if unicode.IsNumber(b) {
cd = cd*10 + int(b) - '0' // convert rune to int
continue
}
switch b {
case 'D':
d = d + time.Hour*24*time.Duration(cd)
case 'H':
d = d + time.Hour*time.Duration(cd)
case 'M':
d = d + time.Minute*time.Duration(cd)
case 'S':
d = d + time.Second*time.Duration(cd)
case 'P', 'T':
// empty
default:
continue
}
cd = 0
}
return d
}