{"id":26455015,"url":"https://github.com/gosmos-space/sduration","last_synced_at":"2026-05-10T14:47:17.764Z","repository":{"id":282619337,"uuid":"949141412","full_name":"gosmos-space/sduration","owner":"gosmos-space","description":"A type-safe Go library providing string-serializable duration values. SDuration wraps time.Duration with full support for text, JSON, and SQL serialization, making it ideal for configuration files and databases where human-readable durations are preferred over nanosecond integers.","archived":false,"fork":false,"pushed_at":"2025-03-24T17:19:18.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T07:55:11.385Z","etag":null,"topics":["duration","go","golang","library","string","time"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gosmos-space.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-15T19:15:34.000Z","updated_at":"2025-03-15T20:08:44.000Z","dependencies_parsed_at":"2025-03-15T20:39:33.835Z","dependency_job_id":null,"html_url":"https://github.com/gosmos-space/sduration","commit_stats":null,"previous_names":["gosmos-space/sduration"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gosmos-space/sduration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosmos-space%2Fsduration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosmos-space%2Fsduration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosmos-space%2Fsduration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosmos-space%2Fsduration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gosmos-space","download_url":"https://codeload.github.com/gosmos-space/sduration/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosmos-space%2Fsduration/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32860226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-10T13:40:02.631Z","status":"ssl_error","status_checked_at":"2026-05-10T13:40:02.145Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["duration","go","golang","library","string","time"],"created_at":"2025-03-18T20:29:39.958Z","updated_at":"2026-05-10T14:47:17.741Z","avatar_url":"https://github.com/gosmos-space.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sduration\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/gosmos-space/sduration.svg)](https://pkg.go.dev/github.com/gosmos-space/sduration)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gosmos-space/sduration)](https://goreportcard.com/report/github.com/gosmos-space/sduration)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA Go package that provides a string-serializable duration type that wraps `time.Duration`.\n\n## Overview\n\nThe `sduration` package implements a custom duration type (`SDuration`) that wraps the standard `time.Duration` with serialization capabilities. It enables:\n\n- Text marshaling/unmarshaling (for encoding like YAML, TOML)\n- JSON marshaling/unmarshaling\n- sql package compatibility for database storage\n- Human-readable duration strings in configs and data formats\n\n## Installation\n\n```bash\ngo get github.com/gosmos-space/sduration\n```\n\n## Usage\n\n### Basic Usage\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"time\"\n    \n    \"github.com/gosmos-space/sduration\"\n)\n\nfunc main() {\n    // Create an SDuration from time.Duration\n    d := sduration.SDuration(5 * time.Second)\n    \n    // Convert back to time.Duration\n    timeDur := d.Duration()\n    \n    fmt.Printf(\"Duration: %v\\n\", timeDur) // Output: Duration: 5s\n}\n```\n\n### With Text Marshaling (YAML, TOML)\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"encoding/json\"\n    \"gopkg.in/yaml.v3\"\n    \n    \"github.com/gosmos-space/sduration\"\n)\n\ntype Config struct {\n    Timeout sduration.SDuration `yaml:\"timeout\" json:\"timeout\"`\n    Retry   sduration.SDuration `yaml:\"retry\" json:\"retry\"`\n}\n\nfunc main() {\n    // Parse config from YAML\n    yamlData := []byte(`\ntimeout: 30s\nretry: 5m\n`)\n    \n    var config Config\n    if err := yaml.Unmarshal(yamlData, \u0026config); err != nil {\n        panic(err)\n    }\n    \n    fmt.Printf(\"Timeout: %v\\n\", config.Timeout.Duration())\n    fmt.Printf(\"Retry: %v\\n\", config.Retry.Duration())\n    \n    // Convert back to YAML\n    out, _ := yaml.Marshal(config)\n    fmt.Println(string(out))\n}\n```\n\n### With JSON\n\n```go\npackage main\n\nimport (\n    \"encoding/json\"\n    \"fmt\"\n    \"time\"\n    \n    \"github.com/gosmos-space/sduration\"\n)\n\ntype APIConfig struct {\n    RequestTimeout sduration.SDuration `json:\"requestTimeout\"`\n    SessionTTL     sduration.SDuration `json:\"sessionTTL\"`\n}\n\nfunc main() {\n    // Create a config\n    config := APIConfig{\n        RequestTimeout: sduration.SDuration(30 * time.Second),\n        SessionTTL:     sduration.SDuration(24 * time.Hour),\n    }\n    \n    // Marshal to JSON\n    jsonData, _ := json.Marshal(config)\n    fmt.Println(string(jsonData))\n    // Output: {\"requestTimeout\":\"30s\",\"sessionTTL\":\"24h0m0s\"}\n    \n    // Unmarshal from JSON\n    jsonInput := []byte(`{\"requestTimeout\":\"45s\",\"sessionTTL\":\"12h\"}`)\n    var newConfig APIConfig\n    \n    if err := json.Unmarshal(jsonInput, \u0026newConfig); err != nil {\n        panic(err)\n    }\n    \n    fmt.Printf(\"Request Timeout: %v\\n\", newConfig.RequestTimeout.Duration())\n    fmt.Printf(\"Session TTL: %v\\n\", newConfig.SessionTTL.Duration())\n}\n```\n\n### With SQL Databases\n\n```go\npackage main\n\nimport (\n    \"database/sql\"\n    \"fmt\"\n    \"log\"\n    \"time\"\n    \n    \"github.com/gosmos-space/sduration\"\n    _ \"github.com/mattn/go-sqlite3\"\n)\n\ntype Job struct {\n    ID       int\n    Timeout  sduration.SDuration\n    Interval sduration.SDuration\n}\n\nfunc main() {\n    // Open database connection\n    db, err := sql.Open(\"sqlite3\", \":memory:\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    defer db.Close()\n    \n    // Create table\n    _, err = db.Exec(`CREATE TABLE jobs (\n        id INTEGER PRIMARY KEY,\n        timeout TEXT,\n        interval TEXT\n    )`)\n    if err != nil {\n        log.Fatal(err)\n    }\n    \n    // Insert job with durations\n    job := Job{\n        ID:       1,\n        Timeout:  sduration.SDuration(30 * time.Second),\n        Interval: sduration.SDuration(5 * time.Minute),\n    }\n    \n    _, err = db.Exec(\n        \"INSERT INTO jobs (id, timeout, interval) VALUES (?, ?, ?)\",\n        job.ID, job.Timeout, job.Interval,\n    )\n    if err != nil {\n        log.Fatal(err)\n    }\n    \n    // Query the data back\n    var retrievedJob Job\n    err = db.QueryRow(\"SELECT id, timeout, interval FROM jobs WHERE id = ?\", 1).Scan(\n        \u0026retrievedJob.ID, \u0026retrievedJob.Timeout, \u0026retrievedJob.Interval,\n    )\n    if err != nil {\n        log.Fatal(err)\n    }\n    \n    fmt.Printf(\"Job ID: %d\\n\", retrievedJob.ID)\n    fmt.Printf(\"Timeout: %v\\n\", retrievedJob.Timeout.Duration())\n    fmt.Printf(\"Interval: %v\\n\", retrievedJob.Interval.Duration())\n}\n```\n\n## Features\n\n- **String Serialization**: Durations are stored as human-readable strings (e.g., \"5m30s\") rather than nanosecond integers\n- **Compatible with time.Duration**: Easy conversion between SDuration and time.Duration\n- **Supports JSON**: Properly marshals/unmarshals to/from JSON as strings with units\n- **Supports Text Encoding**: Works with text-based encodings like YAML and TOML\n- **SQL Database Support**: Implements `sql.Scanner` and `driver.Valuer` for database storage\n- **Type Safety**: Maintains type safety while ensuring serialization works correctly\n\n## Supported Formats\n\nSDuration supports all the same duration formats as `time.ParseDuration`:\n\n- \"ns\" - nanoseconds\n- \"us\" or \"µs\" - microseconds\n- \"ms\" - milliseconds\n- \"s\" - seconds\n- \"m\" - minutes\n- \"h\" - hours\n\nExamples: \"300ms\", \"1.5h\", \"2h45m\", \"-30s\"\n\n## License\n\nMIT License\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgosmos-space%2Fsduration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgosmos-space%2Fsduration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgosmos-space%2Fsduration/lists"}