Skip to content

We can't change time.ParseDuration, but can you guys add time.DurationParser related issues [ #69114 #11473 ] #69504

@go-shafaq

Description

@go-shafaq

What is the URL of the page with the issue?

"time"

What is your user agent?

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Screenshot

Hello golang Devs!
I simply want to ParseDuration with day and month.
I understand why you can't add day or month.
I'm asking can you guys add something like time.DurationParser
like this:

// ==========================================
// package time
// adding some futures without changing old code

type durationParser struct {
	unitMap map[string]uint64
}

func DurationParser(unitMap map[string]uint64) (*durationParser, error) {
	//  checking keys [not containing dig]
	for key, _ := range unitMap {
		if strings.ContainsAny(key, "0123456789") {
			return nil, errors.New("key [" + key + "] contains dig")
		}
	}

	return &durationParser{unitMap: unitMap}, nil
}

func (p *durationParser) ParseDuration(s string) (Duration, error) {
	// old logic
	return 0, nil
}

func BaseUnitMap() map[string]uint64 {
	return maps.Clone(unitMap)
}

// ==========================================
//package main

func main() {
	const day = uint64(24 * time.Hour)

	um := time.BaseUnitMap()
	um["d"] = day
	um["w"] = 7 * day
	um["M"] = 30 * day

	dp, _ := time.DurationParser(um)
	fmt.Println(dp.ParseDuration("1M2w3d4h5m"))
}

// ==========================================

Main idea is making it a bit flexible so devs like me can use it easily.
I hope you guys do something about it.
Thank you for your Attention

What did you do?

.

What did you see happen?

.

What did you expect to see?

.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions