{"id":13412465,"url":"https://github.com/xhit/go-str2duration","last_synced_at":"2026-01-16T18:22:46.925Z","repository":{"id":40640472,"uuid":"237726800","full_name":"xhit/go-str2duration","owner":"xhit","description":"Convert string to duration in golang","archived":false,"fork":false,"pushed_at":"2023-10-14T00:55:51.000Z","size":23,"stargazers_count":100,"open_issues_count":1,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:50:56.495Z","etag":null,"topics":["convert","duration","golang","parser","string"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xhit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://paypal.me/hit"]}},"created_at":"2020-02-02T06:04:07.000Z","updated_at":"2024-07-09T08:54:55.000Z","dependencies_parsed_at":"2024-01-03T01:01:48.478Z","dependency_job_id":"8782b3f5-6624-4cc3-b623-6d43b5c049ad","html_url":"https://github.com/xhit/go-str2duration","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/xhit/go-str2duration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xhit%2Fgo-str2duration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xhit%2Fgo-str2duration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xhit%2Fgo-str2duration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xhit%2Fgo-str2duration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xhit","download_url":"https://codeload.github.com/xhit/go-str2duration/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xhit%2Fgo-str2duration/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28480636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T11:59:17.896Z","status":"ssl_error","status_checked_at":"2026-01-16T11:55:55.838Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["convert","duration","golang","parser","string"],"created_at":"2024-07-30T20:01:24.984Z","updated_at":"2026-01-16T18:22:46.903Z","avatar_url":"https://github.com/xhit.png","language":"Go","funding_links":["https://paypal.me/hit"],"categories":["Go","Date and Time","日期和时间`用于处理日期和时间的库`","日期和时间","Relational Databases"],"sub_categories":["Advanced Console UIs","Search and Analytic Databases","SQL 查询语句构建库","检索及分析资料库"],"readme":"# Go String To Duration (go-str2duration)\n\nThis package allows to get a time.Duration from a string. The string can be a string retorned for time.Duration or a similar string with weeks or days too!.\n\n\u003ca href=\"https://goreportcard.com/report/github.com/xhit/go-str2duration/v2\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/xhit/go-str2duration\" alt=\"Go Report Card\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pkg.go.dev/github.com/xhit/go-str2duration/v2?tab=doc\"\u003e\u003cimg src=\"https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\" alt=\"go.dev\"\u003e\u003c/a\u003e\n\n## Download\n\n```bash\ngo get github.com/xhit/go-str2duration/v2\n```\n\n## Features\n\nGo String To Duration supports this strings conversions to duration:\n- All strings returned in time.Duration String.\n- A string more readable like 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds).\n- `µs` and `us` are microsecond.\n\nIt's the same `time.ParseDuration` standard function in Go, but with days and week support.\n\n**Note**: a day is 24 hour.\n\nIf you don't need days and weeks, use [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration).\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tstr2duration \"github.com/xhit/go-str2duration/v2\"\n\t\"os\"\n\t\"time\"\n)\n\nfunc main() {\n\n    for i, tt := range []struct {\n            dur      string\n            expected time.Duration\n        }{\n            //This times are returned with time.Duration string\n            {\"1h\", time.Duration(time.Hour)},\n            {\"1m\", time.Duration(time.Minute)},\n            {\"1s\", time.Duration(time.Second)},\n            {\"1ms\", time.Duration(time.Millisecond)},\n            {\"1µs\", time.Duration(time.Microsecond)},\n            {\"1us\", time.Duration(time.Microsecond)},\n            {\"1ns\", time.Duration(time.Nanosecond)},\n            {\"4.000000001s\", time.Duration(4*time.Second + time.Nanosecond)},\n            {\"1h0m4.000000001s\", time.Duration(time.Hour + 4*time.Second + time.Nanosecond)},\n            {\"1h1m0.01s\", time.Duration(61*time.Minute + 10*time.Millisecond)},\n            {\"1h1m0.123456789s\", time.Duration(61*time.Minute + 123456789*time.Nanosecond)},\n            {\"1.00002ms\", time.Duration(time.Millisecond + 20*time.Nanosecond)},\n            {\"1.00000002s\", time.Duration(time.Second + 20*time.Nanosecond)},\n            {\"693ns\", time.Duration(693 * time.Nanosecond)},\n\n            //This times aren't returned with time.Duration string, but are easily readable and can be parsed too!\n            {\"1ms1ns\", time.Duration(time.Millisecond + 1*time.Nanosecond)},\n            {\"1s20ns\", time.Duration(time.Second + 20*time.Nanosecond)},\n            {\"60h8ms\", time.Duration(60*time.Hour + 8*time.Millisecond)},\n            {\"96h63s\", time.Duration(96*time.Hour + 63*time.Second)},\n\n            //And works with days and weeks!\n            {\"2d3s96ns\", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},\n            {\"1w2d3s96ns\", time.Duration(168*time.Hour + 48*time.Hour + 3*time.Second + 96*time.Nanosecond)},\n\n            {\"10s1us693ns\", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)},\n\n        } {\n            durationFromString, err := str2duration.ParseDuration(tt.dur)\n            if err != nil {\n                panic(err)\n\n            //Check if expected time is the time returned by the parser\n            } else if tt.expected != durationFromString {\n                 fmt.Println(fmt.Sprintf(\"index %d -\u003e in: %s returned: %s\\tnot equal to %s\", i, tt.dur, durationFromString.String(), tt.expected.String()))\n            }else{\n                fmt.Println(fmt.Sprintf(\"index %d -\u003e in: %s parsed succesfully\", i, tt.dur))\n            }\n        }\n}\n```\n\nAlso, you can convert to string the duration using `String(t time.Duration)` function. This support weeks and days and not return the ugly decimals from golang standard `t.String()` function. Units with 0 values aren't returned. For example: `1d1ms` means 1 day 1 millisecond.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxhit%2Fgo-str2duration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxhit%2Fgo-str2duration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxhit%2Fgo-str2duration/lists"}