duration

package
v1.40.2 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: Apache-2.0 Imports: 3 Imported by: 19

Documentation

Overview

Package duration provides enhanced duration parsing and formatting with support for extended time units beyond the standard library.

This package extends Go's standard time.Duration to support larger units like days, weeks, months, and years, making it more suitable for human-readable time representations and configuration files.

Key features:

  • Parse durations with extended units: "d" (days), "w" (weeks), "y" (years)
  • Human-readable string formatting: "3d12h" instead of "84h"
  • Automatic precision adjustment based on magnitude
  • Compatible with standard time.Duration

Supported time units:

  • ns: nanoseconds
  • us/µs/μs: microseconds
  • ms: milliseconds
  • s: seconds
  • m: minutes
  • h: hours
  • d: days (24 hours)
  • w: weeks (7 days)
  • y: years (365 days, approximation)

Basic usage:

// Parse duration strings
d, err := duration.ParseDuration("3d12h30m")
if err != nil {
	log.Fatal(err)
}
fmt.Println(d) // Output: 3d12h30m

// Create from standard units
d = duration.Day * 7 + duration.Hour * 6
fmt.Println(d) // Output: 1w6h

// Convert to standard time.Duration
td := time.Duration(d)

// Get components
fmt.Printf("Hours: %.2f, Days: %.2f\n", d.Hours(), d.Days())

The formatting automatically adjusts precision based on the duration magnitude:

  • Durations over a week: formatted as weeks, days, hours (1w2d3h)
  • Durations over a day: formatted as days, hours, minutes (2d3h45m)
  • Smaller durations: include seconds and sub-second precision

Index

Constants

This section is empty.

Variables

View Source
var (
	Nanosecond  = Duration(time.Nanosecond)
	Microsecond = Duration(time.Microsecond)
	Millisecond = Duration(time.Millisecond)
	Second      = Duration(time.Second)
	Minute      = Duration(time.Minute)
	Hour        = Duration(time.Hour)
	Day         = Hour * 24
	Week        = Day * 7
	Fortnight   = Week * 2
	Month       = Day * 30    // Approximation
	Year        = Day * 365   // Approximation
	Decade      = Year * 10   // Approximation
	Century     = Year * 100  // Approximation
	Millennium  = Year * 1000 // Approximation
)

Standard unit of time.

Functions

This section is empty.

Types

type Duration

type Duration time.Duration

Duration represents a time duration with support for extended units. It embeds time.Duration and can be used anywhere a time.Duration is expected, but provides enhanced parsing and formatting capabilities.

func ParseDuration

func ParseDuration(s string) (Duration, error)

ParseDuration parses a duration string with support for extended time units. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h", "2h45m", or "3d12h".

Valid time units are:

  • "ns": nanoseconds
  • "us" (or "µs"/"μs"): microseconds
  • "ms": milliseconds
  • "s": seconds
  • "m": minutes
  • "h": hours
  • "d": days (24 hours)
  • "w": weeks (7 days)
  • "y": years (365 days)

Examples:

ParseDuration("2h30m")      // 2 hours 30 minutes
ParseDuration("1.5d")       // 1.5 days (36 hours)
ParseDuration("3w2d")       // 3 weeks 2 days
ParseDuration("-30m")       // negative 30 minutes
ParseDuration("1d12h30m")   // 1 day 12 hours 30 minutes

func (Duration) Days

func (d Duration) Days() float64

Days returns the duration as a floating point number of days.

func (Duration) Hours

func (d Duration) Hours() float64

Hours returns the duration as a floating point number of hours.

func (Duration) Minutes

func (d Duration) Minutes() float64

Minutes returns the duration as a floating point number of minutes.

func (Duration) Nanoseconds

func (d Duration) Nanoseconds() int64

Nanoseconds returns the duration as an integer nanosecond count.

func (Duration) Seconds

func (d Duration) Seconds() float64

Seconds returns the duration as a floating point number of seconds.

func (Duration) String

func (d Duration) String() string

String returns a string representing the duration in the form "3d1h3m". Leading zero units are omitted. As a special case, durations less than one second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure that the leading digit is non-zero. Duration more than a day or more than a week lose granularity and are truncated to resp. days-hours-minutes and weeks-days-hours. The zero duration formats as 0s.

func (Duration) Weeks

func (d Duration) Weeks() float64

Weeks returns the duration as a floating point number of days.

Jump to

Keyboard shortcuts

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