Documentation
¶
Overview ¶
Package timestring provides some helper functions for displaying time in a human readable way.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AbsoluteFormatter ¶ added in v0.5.0
type AbsoluteFormatter struct {
// contains filtered or unexported fields
}
AbsoluteFormatter is a Absolute Formatter. It provides a concise and precise representation of time.Duration, always using abbreviated units and omitting zero-value units.
func (AbsoluteFormatter) Option ¶ added in v0.5.0
func (s AbsoluteFormatter) Option(opts ...FormatterOption) Formatter
Option returns a Absolute Formatter with the applied options. For AbsoluteFormatter, Abbreviated is always true. ShowMSOnSeconds is not applicable.
func (AbsoluteFormatter) String ¶ added in v0.5.0
func (s AbsoluteFormatter) String(td time.Duration) string
String returns a human readable string using the Absolute Formatter. It always uses abbreviated units and omits zero-value units.
Example: "1d 2h 3m 4s", "2h 3m", "4s", "0s".
Example ¶
ExampleAbsoluteFormatter_String demonstrates the usage of AbsoluteFormatter's String method.
package main
import (
"fmt"
"time"
"github.com/na4ma4/go-timestring"
)
func main() {
d, _ := time.ParseDuration("49h15m30s")
fmt.Println(timestring.Absolute.String(d))
}
Output: 2d 1h 15m 30s
type Duration ¶ added in v0.2.0
type Duration struct {
Days int64
Hours int64
Minutes int64
Seconds int64
Milliseconds int64
Microseconds int64
Nanoseconds int64
}
Duration contains the absolute number of each field with all fields adding up to the total duration, so Hours is only the amount of hours to be displayed, unlike (time.Duration).Hours().
func TimeDurationToDuration ¶ added in v0.2.0
TimeDurationToDuration converts a time.Duration to the timestring.Duration for easier display of durations.
type Formatter ¶
type Formatter interface {
Option(...FormatterOption) Formatter
String(time.Duration) string
}
Formatter is the interface that any timestring formatter should match.
var Absolute Formatter = AbsoluteFormatter{/* contains filtered or unexported fields */}
Absolute is the ready-to-use Absolute Formatter.
var LongProcess Formatter = LongProcessFormatter{}
LongProcess is the ready-to-use Long Process Formatter.
var ShortProcess Formatter = ShortProcessFormatter{/* contains filtered or unexported fields */}
ShortProcess is the ready-to-use Short Process Formatter.
type FormatterOption ¶ added in v0.2.0
type FormatterOption uint
FormatterOption is a list of options that can be applied to the standard formatters.
const ( // NoSpaces is a FormatterOption that tells the formatter to ignore spaces between values. NoSpaces FormatterOption = iota // NoUnitSpaces is a FormatterOption that tells the formatter to ignore spaces betwee values and // units. NoUnitSpaces // Abbreviated is a FormatterOption that tells the formatter to abbreviate the units // (eg. "d" instead of "days"). Abbreviated // ShowMSOnSeconds is a FormatterOption that tells the formatter to show milliseconds when // the value is less than a minute (59 seconds or less). ShowMSOnSeconds )
type LongProcessFormatter ¶
type LongProcessFormatter struct {
// contains filtered or unexported fields
}
LongProcessFormatter is a Long Process Formatter.
It is a formatter that handles processes that would be considered long running, like displaying the uptime of a server or service.
func (LongProcessFormatter) Option ¶ added in v0.2.0
func (a LongProcessFormatter) Option(opts ...FormatterOption) Formatter
Option returns a Long Process Formatter with the applied options.
func (LongProcessFormatter) String ¶
func (a LongProcessFormatter) String(td time.Duration) string
String returns a human readable string using the Long Process Formatter. It formats the duration into days, hours, minutes, seconds, and milliseconds, with options for abbreviated output, no spaces, and showing milliseconds on seconds.
type ShortProcessFormatter ¶ added in v0.4.0
type ShortProcessFormatter struct {
// contains filtered or unexported fields
}
ShortProcessFormatter is a Short Process Formatter. It provides a concise representation of time.Duration, always using abbreviated units and omitting zero-value units.
func (ShortProcessFormatter) Option ¶ added in v0.4.0
func (s ShortProcessFormatter) Option(opts ...FormatterOption) Formatter
Option returns a Short Process Formatter with the applied options. For ShortProcessFormatter, Abbreviated is always true. ShowMSOnSeconds is not applicable.