Skip to content
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

time.ParseDuration day, week, month, year -> d, w, M, Y; helper code to ParseDuration #69114

Closed
go-shafaq opened this issue Aug 28, 2024 · 5 comments

Comments

@go-shafaq
Copy link

go-shafaq commented Aug 28, 2024

I've simplified the code to make it easier to copy and use.
also same issue[/issues/17767#issue-187078548]

// ParseDuration parses a duration string.
// examples: "10d", "-1.5w" or "3Y4M5d".
// Add time units are "d"="D", "w"="W", "M", "y"="Y".
func ParseDuration(s string) (time.Duration, error) {
	neg := false
	if len(s) > 0 && s[0] == '-' {
		neg = true
		s = s[1:]
	}

	re := regexp.MustCompile(`(\d*\.\d+|\d+)[^\d]*`)
	unitMap := map[string]time.Duration{
		"d": 24,
		"D": 24,
		"w": 7 * 24,
		"W": 7 * 24,
		"M": 30 * 24,
		"y": 365 * 24,
		"Y": 365 * 24,
	}

	strs := re.FindAllString(s, -1)
	var sumDur time.Duration
	for _, str := range strs {
		var _hours time.Duration = 1
		for unit, hours := range unitMap {
			if strings.Contains(str, unit) {
				str = strings.ReplaceAll(str, unit, "h")
				_hours = hours
				break
			}
		}

		dur, err := time.ParseDuration(str)
		if err != nil {
			return 0, err
		}

		sumDur += dur * _hours
	}

	if neg {
		sumDur = -sumDur
	}
	return sumDur, nil
}
@ianlancetaylor
Copy link
Member

Thanks, but we intentionally do not use any time unit larger than hour because they are ambiguous. Days are sometimes 24 hours, sometimes 23 or 25 at daylight savings time transitions. Similarly for the other larger units.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2024
@timkuijsten
Copy link

timkuijsten commented Sep 17, 2024

I understand the reasoning, but it does look a bit unpractical when used with flag.Duration, i.e. with a 30 day default:

  -whiteexp duration
        duration before whitelists expire (default 720h0m0s)

From poolpOrg/filter-spfgreylist#5

Maybe we should adapt flag.Duration (not time.Duration) to parse days when used as input from the command line?
Previous discussion: #11473

@go-shafaq
Copy link
Author

I understand the reasoning, but it does look a bit unpractical when used with flag.Duration, i.e. with a 30 day default:

  -whiteexp duration
        duration before whitelists expire (default 720h0m0s)

From poolpOrg/filter-spfgreylist#5

Maybe we should adapt flag.Duration (not time.Duration) to parse days when used as input from the command line? Previous discussion: #11473

I didnt get you
I made my pkg github.com/go-shafaq/timep
what do you think

@timkuijsten
Copy link

I didnt get you

Sorry, I was replying to ianlancetaylor.

I made my pkg github.com/go-shafaq/timep
what do you think

It's nice, although I would prefer to copy paste if it's just a little bit of code. For me just adding days and weeks would help with flags to command line tools and it's less ambiguous than the length of a month.

@go-shafaq
Copy link
Author

I didnt get you

Sorry, I was replying to ianlancetaylor.

I made my pkg github.com/go-shafaq/timep
what do you think

It's nice, although I would prefer to copy paste if it's just a little bit of code. For me just adding days and weeks would help with flags to command line tools and it's less ambiguous than the length of a month.

I just copied code from org time pkg and added some flexibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants