zeit/z/helpers.go

165 lines
3.8 KiB
Go
Raw Permalink Normal View History

2020-10-11 14:12:41 +00:00
package z
import (
"os/user"
2020-10-17 12:16:06 +00:00
"os/exec"
"bytes"
2020-10-11 14:12:41 +00:00
"regexp"
"strconv"
2020-10-17 00:32:31 +00:00
"strings"
2020-10-11 14:12:41 +00:00
"time"
"math"
2020-10-11 14:12:41 +00:00
"errors"
2022-09-05 00:40:00 +00:00
"github.com/jinzhu/now"
2020-10-11 14:12:41 +00:00
)
const (
TFAbsTwelveHour int = 0
2022-09-05 00:40:00 +00:00
TFRelHourMinute int = 1
TFRelHourFraction int = 2
2020-10-11 14:12:41 +00:00
)
func TimeFormats() []string {
return []string{
`^\d{1,2}:\d{1,2}(am|pm)$`, // Absolute twelve hour format
`^([+-])(\d{1,2}):(\d{1,2})$`, // Relative hour:minute format
`^([+-])(\d{1,2})\.(\d{1,2})$`, // Relative hour.fraction format
}
}
func GetCurrentUser() (string) {
user, err := user.Current()
if err != nil {
return "unknown"
}
return user.Username
}
func GetTimeFormat(timeStr string) (int) {
var matched bool
var regerr error
for timeFormatId, timeFormat := range TimeFormats() {
matched, regerr = regexp.MatchString(timeFormat, timeStr)
if regerr != nil {
return -1
}
if matched == true {
return timeFormatId
}
}
return -1
}
// TODO: Use https://golang.org/pkg/time/#ParseDuration
2020-10-11 14:12:41 +00:00
func RelToTime(timeStr string, ftId int) (time.Time, error) {
var re = regexp.MustCompile(TimeFormats()[ftId])
gm := re.FindStringSubmatch(timeStr)
if len(gm) < 4 {
return time.Now(), errors.New("No match")
}
var hours int = 0
var minutes int = 0
if ftId == TFRelHourFraction {
f, _ := strconv.ParseFloat(gm[2] + "." + gm[3], 32)
minutes = int(f * 60.0)
} else {
hours, _ = strconv.Atoi(gm[2])
minutes, _ = strconv.Atoi(gm[3])
}
var t time.Time
switch gm[1] {
case "+":
t = time.Now().Local().Add(time.Hour * time.Duration(hours) + time.Minute * time.Duration(minutes))
case "-":
t = time.Now().Local().Add((time.Hour * time.Duration(hours) + time.Minute * time.Duration(minutes)) * -1)
}
return t, nil
}
func ParseTime(timeStr string) (time.Time, error) {
tfId := GetTimeFormat(timeStr)
t:= time.Now()
2020-10-11 14:12:41 +00:00
switch tfId {
case TFAbsTwelveHour:
tadj, err := time.Parse("3:04pm", timeStr)
tnew := time.Date(t.Year(), t.Month(), t.Day(), tadj.Hour(), tadj.Minute(), t.Second(), t.Nanosecond(), t.Location())
return tnew, err
2020-10-11 14:12:41 +00:00
case TFRelHourMinute, TFRelHourFraction:
return RelToTime(timeStr, tfId)
default:
2022-09-05 00:40:00 +00:00
return now.Parse(timeStr)
2020-10-11 14:12:41 +00:00
}
}
2020-10-16 16:02:41 +00:00
2020-10-23 19:51:51 +00:00
func GetIdFromName(name string) string {
2020-10-17 00:32:31 +00:00
reg, regerr := regexp.Compile("[^a-zA-Z0-9]+")
if regerr != nil {
return ""
}
2020-10-23 19:51:51 +00:00
id := strings.ToLower(reg.ReplaceAllString(name, ""))
2020-10-17 00:32:31 +00:00
2020-10-23 19:51:51 +00:00
return id
2020-10-17 00:32:31 +00:00
}
func GetISOCalendarWeek(date time.Time) (int) {
var _, cw = date.ISOWeek()
return cw
}
func GetISOWeekInMonth(date time.Time) (month int, weeknumber int) {
if date.IsZero() {
return -1, -1
2020-10-16 16:02:41 +00:00
}
newDay := (date.Day() - int(date.Weekday()) + 1)
addDay := (date.Day() - newDay) * -1
changedDate := date.AddDate(0, 0, addDay)
return int(changedDate.Month()), int(math.Ceil(float64(changedDate.Day()) / 7.0));
2020-10-16 16:02:41 +00:00
}
2020-10-17 12:16:06 +00:00
func GetGitLog(repo string, since time.Time, until time.Time) (string, string, error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command("git", "-C", repo, "config", "user.name")
2020-10-17 12:16:06 +00:00
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", "", err
}
gitUserStr, gitUserErrStr := string(stdout.Bytes()), string(stderr.Bytes())
if gitUserStr == "" && gitUserErrStr != "" {
return gitUserStr, gitUserErrStr, errors.New(gitUserErrStr)
}
stdout.Reset()
stderr.Reset()
cmd = exec.Command("git", "-C", repo, "log", "--author", gitUserStr, "--since", since.Format("2006-01-02T15:04:05-0700"), "--until", until.Format("2006-01-02T15:04:05-0700"), "--pretty=oneline")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return "", "", err
}
2020-10-17 12:16:06 +00:00
stdoutStr, stderrStr := string(stdout.Bytes()), string(stderr.Bytes())
return stdoutStr, stderrStr, nil
}