2019-05-09 01:07:38 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-02-12 21:25:59 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2019-05-09 01:07:38 +03:00
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseDate(date string) (time.Time, error) {
|
2021-02-13 14:59:59 +03:00
|
|
|
return time.Parse(config.SimpleDateFormat, date)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseDateTime(date string) (time.Time, error) {
|
2021-02-12 21:25:59 +03:00
|
|
|
return time.Parse(config.SimpleDateTimeFormat, date)
|
2019-05-09 01:07:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDate(date time.Time) string {
|
2021-02-13 14:59:59 +03:00
|
|
|
return date.Format(config.SimpleDateFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDateTime(date time.Time) string {
|
2021-02-12 21:25:59 +03:00
|
|
|
return date.Format(config.SimpleDateTimeFormat)
|
2019-05-09 01:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:39:56 +03:00
|
|
|
func FormatDateHuman(date time.Time) string {
|
|
|
|
return date.Format("Mon, 02 Jan 2006 15:04")
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:26:52 +03:00
|
|
|
func Add(i, j int) int {
|
|
|
|
return i + j
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:07:38 +03:00
|
|
|
func ParseUserAgent(ua string) (string, string, error) {
|
2020-09-29 19:58:10 +03:00
|
|
|
re := regexp.MustCompile(`(?iU)^wakatime\/[\d+.]+\s\((\w+)-.*\)\s.+\s([^\/\s]+)-wakatime\/.+$`)
|
2019-05-09 01:07:38 +03:00
|
|
|
groups := re.FindAllStringSubmatch(ua, -1)
|
|
|
|
if len(groups) == 0 || len(groups[0]) != 3 {
|
2019-11-08 01:11:19 +03:00
|
|
|
return "", "", errors.New("failed to parse user agent string")
|
2019-05-09 01:07:38 +03:00
|
|
|
}
|
|
|
|
return groups[0][1], groups[0][2], nil
|
2020-11-06 19:20:26 +03:00
|
|
|
}
|