-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
43 lines (37 loc) · 999 Bytes
/
helpers.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
func formatPubDate(isoDate string) (string, error) {
t, err := time.Parse(time.RFC3339Nano, isoDate)
if err != nil {
return "", err
}
return t.Format(time.RFC1123), nil
}
func formatDuration(isoDuration string) string {
re := regexp.MustCompile("P(\\d+D)?T(\\d+H)?(\\d+M)?(\\d+S)?")
matches := re.FindAllStringSubmatch(isoDuration, -1)
dayStr, hourStr, minuteStr, secondStr :=
matches[0][1], matches[0][2], matches[0][3], matches[0][4]
day, hour, minute, second :=
takeTimePart(dayStr, "D", "Day"),
takeTimePart(hourStr, "H", "Hour"),
takeTimePart(minuteStr, "M", "Minute"),
takeTimePart(secondStr, "S", "Second")
return fmt.Sprintf("%0.2d:%0.2d:%0.2d", hour+day*24, minute, second)
}
func takeTimePart(input string, tShort string, tLong string) int {
if input == "" {
return 0
}
output, err := strconv.Atoi(strings.Replace(input, tShort, "", -1))
if err != nil {
return 0
}
return output
}