whilst

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: MIT Imports: 7 Imported by: 0

README

Whilst

Go Reference Go Report Card Coverage Status

Purpose

Library that extends time.Duration from standard library with days, months and years

Features

Without approximations

Allows for presence of spaces in a string representation

Compatible with time.Duration in terms of parsing and conversion to string

Usage

Example:

package main

import (
    "fmt"
    "time"

    "github.com/akramarenkov/whilst"
)

func main() {
    from := time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)

    whl, err := whilst.Parse("2y")
    if err != nil {
        panic(err)
    }

    fmt.Println(whl)
    fmt.Println(whl.When(from))
    fmt.Println(whl.Duration(from))
    // Output:
    // 2y
    // 2025-04-01 00:00:00 +0000 UTC
    // 17544h0m0s
}

Documentation

Overview

Library that extends time.Duration from standard library with days, months and years.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCharDotAgain      = errors.New("dot character was specified again")
	ErrCharSignAgain     = errors.New("sign character was specified again")
	ErrInputEmpty        = errors.New("input string is empty")
	ErrNumberUnspecified = errors.New("number was not specified")
	ErrOnlyInteger       = errors.New("years, months and days can only be integer")
	ErrUnexpectedChar    = errors.New("unexpected character was specified")
	ErrUnexpectedUnit    = errors.New("unexpected unit was specified")
	ErrUnitUnspecified   = errors.New("unit was not specified")
)

Functions

This section is empty.

Types

type Whilst

type Whilst struct {
	Nano time.Duration

	Days   uint16
	Months uint16
	Years  uint16

	Negative bool
}

Time duration with days, months and years.

func Parse

func Parse(input string) (Whilst, error)

Parses a string representation of the duration.

A duration string consists of several decimal numbers supplemented with an unit of measurement. There may be spaces between a numbers supplemented with an unit, but there must not be spaces between a number and an unit. One of a signs - or + can be specified at a beginning of a string.

A value of days, months and years can only be an integer and cannot be greater than 65535 for each.

Remaining values ​​must not be greater than 9223372036854775807 for positive duration and 9223372036854775808 for negative duration and may have a fractional part.

List of valid units:

  • y - year
  • mo - month
  • d - day
  • h - hour
  • m - minute
  • s - second
  • ms - millisecond
  • µs | μs | us - microsecond
  • ns - nanosecond

Example of strings:

  • 2y3mo10d 24h30m28.02006002s
  • - 2y3mo10d24h30m28.02006002s
  • + 2y 3mo 10d 24h 30m 28.02006002s
Example
package main

import (
	"fmt"
	"time"

	"github.com/akramarenkov/whilst"
)

func main() {
	from := time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)

	whl, err := whilst.Parse("2y")
	if err != nil {
		panic(err)
	}

	fmt.Println(whl)
	fmt.Println(whl.When(from))
	fmt.Println(whl.Duration(from))
}
Output:
2y
2025-04-01 00:00:00 +0000 UTC
17544h0m0s
Example (Compound)
package main

import (
	"fmt"

	"github.com/akramarenkov/whilst"
)

func main() {
	whl, err := whilst.Parse("2y 3mo 10d 23.5h 59.5m 58.01003001s 10ms 30µs 10ns")
	if err != nil {
		panic(err)
	}

	fmt.Println(whl)
}
Output:
2y3mo10d24h30m28.02006002s

func (Whilst) Duration

func (whl Whilst) Duration(from time.Time) time.Duration

Returns a time.Duration representation of the duration.

Time from is necessary because shift by days, months and years is not deterministic and depends on the time relative to which it occurs.

func (Whilst) IsZero

func (whl Whilst) IsZero() bool

Reports whether the duration is zero.

func (Whilst) String

func (whl Whilst) String() string

Returns a string representation of the duration.

func (Whilst) When

func (whl Whilst) When(from time.Time) time.Time

Returns a time shifted by the duration.

Directories

Path Synopsis
internal
ascii
Internal package used to work with 7-bit US-ASCII characters.
Internal package used to work with 7-bit US-ASCII characters.
consts
Internal package with constants common to this module.
Internal package with constants common to this module.
credible
Internal package used for integer calculations with overflow checking.
Internal package used for integer calculations with overflow checking.

Jump to

Keyboard shortcuts

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