timeutil

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2025 License: BSD-3-Clause-Clear Imports: 3 Imported by: 1

Documentation

Overview

Package timeutil provides JSON-serializable time utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration struct {
	time.Duration
}

Duration is a wrapper around time.Duration that supports JSON marshaling/unmarshaling.

When marshaling to JSON, it outputs the duration as a string using time.Duration.String(). When unmarshaling from JSON, it accepts both:

  • String values that can be parsed by time.ParseDuration (e.g., "30s", "5m", "1h30m")
  • Numeric values that represent nanoseconds as a float64

This makes it compatible with configuration files and APIs that need to represent durations in a human-readable format.

Example usage:

type Config struct {
	Timeout timeutil.Duration `json:"timeout"`
}

// JSON: {"timeout": "30s"}
// or:   {"timeout": 30000000000}

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It marshals the duration as a string using time.Duration.String().

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler. It accepts both string values (parsed via time.ParseDuration) and numeric values (interpreted as nanoseconds).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL