units

package module
v0.0.0-...-1ec5e51 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	B  Bytes = 1
	KB       = B * 1024
	MB       = KB * 1024
	GB       = MB * 1024
	TB       = GB * 1024
)

Binary/IEC byte size constants based on powers of 1024.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bytes

type Bytes uint64

Bytes represents a data size in bytes as an unsigned 64-bit integer.

func (Bytes) String

func (bs Bytes) String() string

String implements the fmt.Stringer interface. It formats the Bytes value back into a human-readable string rounded to two decimal places (e.g., "1.50MB", "450B").

func (*Bytes) UnmarshalJSON

func (bs *Bytes) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string containing human-readable data sizes (e.g., "10mb", "500K", "1.5GB") into a Bytes integer value.

type TimeDuration

type TimeDuration struct {
	time.Duration
}

TimeDuration extends standard time.Duration to add custom JSON serialization and support for day-based representations (e.g., "7d").

func (TimeDuration) MarshalJSON

func (o TimeDuration) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It serializes the TimeDuration into its string representation.

func (TimeDuration) String

func (o TimeDuration) String() string

String returns a string representation of the duration. If the duration is an exact multiple of 24 hours, it formats it using the "d" suffix (e.g., "2d"). Otherwise, it falls back to time.Duration.String().

func (*TimeDuration) UnmarshalJSON

func (o *TimeDuration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string into a TimeDuration, extending time.ParseDuration to natively support the "d" suffix for days (1d = 24h).

Jump to

Keyboard shortcuts

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