2019-05-09 01:07:38 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
2019-11-08 00:56:06 +03:00
|
|
|
"strconv"
|
2019-05-11 18:49:56 +03:00
|
|
|
"strings"
|
2019-05-09 01:07:38 +03:00
|
|
|
"time"
|
2019-05-11 18:49:56 +03:00
|
|
|
|
|
|
|
"github.com/n1try/wakapi/models"
|
2019-05-09 01:07:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseDate(date string) (time.Time, error) {
|
|
|
|
return time.Parse("2006-01-02 15:04:05", date)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDate(date time.Time) string {
|
|
|
|
return date.Format("2006-01-02 15:04:05")
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:39:56 +03:00
|
|
|
func FormatDateHuman(date time.Time) string {
|
|
|
|
return date.Format("Mon, 02 Jan 2006 15:04")
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:07:38 +03:00
|
|
|
func ParseUserAgent(ua string) (string, string, error) {
|
|
|
|
re := regexp.MustCompile(`^wakatime\/[\d+.]+\s\((\w+).*\)\s.+\s(\w+)\/.+$`)
|
|
|
|
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
|
|
|
|
}
|
2019-05-11 18:49:56 +03:00
|
|
|
|
|
|
|
func MakeConnectionString(config *models.Config) string {
|
2019-10-10 19:09:50 +03:00
|
|
|
location, _ := time.LoadLocation("Local")
|
2019-05-11 18:49:56 +03:00
|
|
|
str := strings.Builder{}
|
|
|
|
str.WriteString(config.DbUser)
|
|
|
|
str.WriteString(":")
|
|
|
|
str.WriteString(config.DbPassword)
|
|
|
|
str.WriteString("@tcp(")
|
|
|
|
str.WriteString(config.DbHost)
|
2019-11-08 00:56:06 +03:00
|
|
|
str.WriteString(":")
|
|
|
|
str.WriteString(strconv.Itoa(int(config.DbPort)))
|
2019-05-11 18:49:56 +03:00
|
|
|
str.WriteString(")/")
|
|
|
|
str.WriteString(config.DbName)
|
2019-10-10 19:09:50 +03:00
|
|
|
str.WriteString("?charset=utf8&parseTime=true&loc=")
|
|
|
|
str.WriteString(location.String())
|
2019-05-11 18:49:56 +03:00
|
|
|
return str.String()
|
|
|
|
}
|