duration

package
v0.0.0-...-4e18fe8 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package duration provides utilities for interacting with durations.

In contrast to the functions provided by the time package, duration also supports the units Day, Week, Month and Year, but does not provide support for units smaller than a millisecond.

Index

Constants

View Source
const (
	Millisecond = time.Millisecond
	Second      = time.Second
	Minute      = time.Minute
	Hour        = time.Hour
	Day         = 24 * Hour
	Week        = 7 * Day
	Month       = 30 * Day
	Year        = 365 * Day
)

Variables

This section is empty.

Functions

func Format

func Format(d time.Duration) string

Format formats the passed duration rounded to milliseconds. Individual Values are separated.

For example: '1h 10s'.

func Parse

func Parse(s string) (time.Duration, error)

Parse parses the passed duration. A duration string is a sequence of decimal numbers and units. Both numbers and units may be followed by spaces or tabs.

Valid units are "ms", "s" or "sec", "min", "h" or "hr", "d", "w", "m" or "mon", and "y" or "yr".

If the passed string does not represent a valid duration, Parse will return a *ParseError.

Types

type ParseError

type ParseError struct {
	// Code is the error code describing further what error occurred.
	Code ParseErrorCode
	// Val is the faulty part of the raw duration.
	// This will be empty, unless the doc of the error code specifically
	// states it,
	Val string
	// RawDuration is the raw duration, as attempted to parse
	RawDuration string
}

ParseError is the error returned by Parse, if the passed duration is faulty.

func (ParseError) Error

func (p ParseError) Error() string

type ParseErrorCode

type ParseErrorCode string
const (
	// ErrSyntax indicates a syntax error.
	ErrSyntax ParseErrorCode = "invalid duration"
	// ErrSize indicates that the parsed duration exceeds the maximum size of
	// a duration.
	ErrSize ParseErrorCode = "the duration is too large"
	// ErrMissingUnit indicates that a number is missing its unit.
	ErrMissingUnit ParseErrorCode = "missing unit in duration"
	// ErrInvalidUnit indicates an unknown unit.
	// Val will contain the invalid unit.
	ErrInvalidUnit ParseErrorCode = "invalid unit '%s' in duration"
)

Jump to

Keyboard shortcuts

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