Skip to content

Adding new features from systemd.time #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/systemdtime/systemdtime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package systemdtime

import (
"errors"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -69,6 +70,8 @@ func ParseDuration(raw string) (time.Duration, error) {
return 0, fmt.Errorf("ParseDuration: incorrect format for raw input %s", raw)
}

isAgo := strings.Contains(raw, "ago")

reNegative, err := regexp.Compile(`^\s*-.*`)
if err != nil {
return 0, err
Expand Down Expand Up @@ -114,9 +117,29 @@ func ParseDuration(raw string) (time.Duration, error) {
totalDuration *= -1
}

if isAgo {
totalDuration *= -1
}

return totalDuration, nil
}

func TranslateWords(raw string) (time.Time, error) {
switch raw {
case "now":
return time.Now(), nil
case "today":
return time.Now().Truncate(time.Hour * 24), nil
case "yesterday":
return time.Now().Add(time.Hour * -24).Truncate(time.Hour * 24), nil
case "tomorrow":
return time.Now().Add(time.Hour * 24).Truncate(time.Hour * 24), nil
default:
var t time.Time
return t, errors.New("no matching words")
}
}

// AdjustTime takes a systemd time adjustment string and uses it to modify a time.Time
func AdjustTime(original time.Time, adjustment string) (time.Time, error) {
duration, err := ParseDuration(adjustment)
Expand Down
24 changes: 24 additions & 0 deletions pkg/systemdtime/systemdtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func TestToDuration(t *testing.T) {
" 2 min 23sec": 2*time.Minute + 23*time.Second,
"1sec": 1 * time.Second,
"1 hour": 1 * time.Hour,
"-1hour": -1 * time.Hour,
"-1hour ago": 1 * time.Hour,
"-2day": -2 * 24 * time.Hour,
"2day ago": -2 * 24 * time.Hour,
"-2day ago": 2 * 24 * time.Hour,
"10 minutes": 10 * time.Minute,
}

Expand All @@ -123,6 +127,26 @@ func TestToDuration(t *testing.T) {
}
}

func TestTranslatedWords(t *testing.T) {
testWord := func(t *testing.T, raw string, shouldSucceed time.Time) {
got, err := TranslateWords(raw)
if raw == "now" {
got = got.Truncate(time.Second)
}
if err != nil {
t.Fatalf("Input '%s' failed: err is '%v', and shouldSucceed set to %v", raw, err, shouldSucceed)
}
if !got.Equal(shouldSucceed) {
t.Fatalf("Input '%s' failed: got %v, and shouldSucceed set to %v", raw, got, shouldSucceed)
}
}

testWord(t, "now", time.Now().Truncate(time.Second))
testWord(t, "today", time.Now().Truncate(time.Hour*24))
testWord(t, "yesterday", time.Now().Add(time.Hour*-24).Truncate(time.Hour*24))
testWord(t, "tomorrow", time.Now().Add(time.Hour*24).Truncate(time.Hour*24))
}

func TestAdjustTime(t *testing.T) {
time1 := time.Date(2012, time.May, 12, 5, 0, 0, 0, time.UTC)
time1Mod, err := AdjustTime(time1, " 4 days 2 hr")
Expand Down