hms

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 3 Imported by: 4

README

hms package

Build Status Go Report Card GoDoc Coverage Status

The hms package provides a simple time handling within a day.

Installation

To install the package, run:

go get github.com/axkit/hms

Usage

Creating a new HMS instance
package main

import (
	"fmt"
	"time"
	"github.com/axkit/hms"
)

func main() {
	h := hms.New(60*60 * time.Second)
	fmt.Println(h) // Output: 01:00:00
}
Adding durations
h := hms.New(23 * time.Hour)
newH, overMidnight := h.Add(time.Hour + time.Minute)
fmt.Println(newH) // Output: 00:01:00
fmt.Println(overMidnight) // Output: true
Parsing from string
h, err := hms.Parse("01:02:03")
if err != nil {
	fmt.Println("Error:", err)
} else {
	fmt.Println(h) // Output: 01:02:03
}
Converting to duration
h := hms.New(3661 * time.Second)
d := h.ToDuration()
fmt.Println(d) // Output: 1h1m1s

Errors

ErrParseFailed

Returned by parsing functions when an invalid format/character is encountered.

Running Tests

To run the tests, use:

go test ./...

License

This package is open-source and distributed under the MIT License. Contributions and feedback are welcome!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrParseFailed = errors.New("invalid string format. Expected HH:MM:SS")

Functions

This section is empty.

Types

type HMS added in v0.0.2

type HMS int64

HMS holds time in format HH:MM:SS with 24 hours limit. The value is stored in seconds.

func New added in v0.0.2

func New(d time.Duration) HMS

New builds HMS from time.Duration.

func Parse added in v0.0.2

func Parse(str string) (HMS, error)

Parse receives string in format HH:MM:SS, parses it and returns HMS.

func (HMS) Add added in v0.0.2

func (x HMS) Add(d time.Duration) (HMS, bool)

Add adds duration to HMS and returns new HMS. If the result of addition causes the time to go past 24:00, then 24 hours are subtracted from the result.

func (HMS) Hour added in v0.0.2

func (x HMS) Hour() int

Hour returns hours.

func (HMS) Interval added in v0.0.2

func (x HMS) Interval(y HMS) HMS

Interval returns time interval between two values.

func (HMS) Minute added in v0.0.2

func (x HMS) Minute() int

Minute returns minutes.

func (HMS) Second added in v0.0.2

func (x HMS) Second() int

Second returns seconds.

func (HMS) String added in v0.0.2

func (x HMS) String() string

String returns HMS as string in format HH:MM:SS.

func (HMS) Subtract added in v0.0.2

func (x HMS) Subtract(d time.Duration) (HMS, bool)

Subtract subtracts duration from HMS and returns new HMS. If the result of subtraction causes the time to go below 00:00, then 24 hours are added to the result.

func (HMS) ToDuration added in v0.0.2

func (x HMS) ToDuration() time.Duration

ToDuration returns value as time.Duration.

Jump to

Keyboard shortcuts

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