1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/utils/common.go

56 lines
1.9 KiB
Go
Raw Normal View History

package utils
import (
"errors"
2021-02-12 21:25:59 +03:00
"github.com/muety/wakapi/config"
"regexp"
"time"
)
// ParseDateTimeTZ attempts to parse the given date string from multiple formats.
// First, a time-zoned date-time string (e.g. 2006-01-02T15:04:05+02:00) is tried
// Second, a non-time-zoned date-time string (e.g. 2006-01-02 15:04:05) is tried at the given zone
// Third, a non-time-zoned date string (e.g. 2006-01-02) is tried at the given zone
// Example:
// - Server runs in CEST (UTC+2), requesting user lives in PDT (UTC-7).
// - 2021-04-25T10:30:00Z, 2021-04-25T3:30:00-0100 and 2021-04-25T12:30:00+0200 are equivalent, they represent the same point in time
// - When user requests non-time-zoned range (e.g. 2021-04-25T00:00:00), but has their time zone properly configured, this will resolve to 2021-04-25T09:00:00
func ParseDateTimeTZ(date string, tz *time.Location) (time.Time, error) {
if t, err := time.Parse(time.RFC3339, date); err == nil {
return t, nil
}
if t, err := time.ParseInLocation(config.SimpleDateTimeFormat, date, tz); err == nil {
return t, nil
}
return time.ParseInLocation(config.SimpleDateFormat, date, tz)
}
func FormatDate(date time.Time) string {
return date.Format(config.SimpleDateFormat)
}
func FormatDateTime(date time.Time) string {
2021-02-12 21:25:59 +03:00
return date.Format(config.SimpleDateTimeFormat)
}
2021-04-30 17:20:08 +03:00
func FormatDateTimeHuman(date time.Time) string {
2020-02-20 17:39:56 +03:00
return date.Format("Mon, 02 Jan 2006 15:04")
}
2021-04-30 17:20:08 +03:00
func FormatDateHuman(date time.Time) string {
return date.Format("Mon, 02 Jan 2006")
}
func Add(i, j int) int {
return i + j
}
func ParseUserAgent(ua string) (string, string, error) {
re := regexp.MustCompile(`(?iU)^wakatime\/(?:v?[\d+.]+|unset)\s\((\w+)-.*\)\s.+\s([^\/\s]+)-wakatime\/.+$`)
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")
}
return groups[0][1], groups[0][2], nil
}