clock

package module
v0.0.0-...-dd65420 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2021 License: MIT Imports: 7 Imported by: 0

README

clock

godoc License Go Report Card

Go package useful for dealing with time (hours, minutes, and seconds). The native time.Time struct built-in to Go also deals with dates and timezones. This package is for the more narrow and simpler use case for when all you care about is clock time.

Quick Start

package main

import (
    "fmt"

    "github.com/hypnobrando/clock"
)

func main() {
    noon := clock.NewTime(12, 0, 0)

    now := clock.Now("America/Los_Angeles")

    if now.After(noon) {
        fmt.Println("It is afternoon!")
    }
}

Installation / Usage

To install clock, use go get:

go get github.com/hypnobrando/clock

Import the hypnobrando/clock package into your code:

import "github.com/hypnobrando/clock"

Staying Up to Date

To update clock to the latest version, use go get -u github.com/hypnobrando/clock.

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DurationBetween

func DurationBetween(start Time, end Time) time.Duration

DurationBetween returns the duration between the two times. If start occurs after end, then the returned duration assumes that end refers to the following day.

func TimePointer

func TimePointer(t time.Time) *time.Time

TimePointer returns a pointer reference to the input Time object.

Types

type Time

type Time struct {
	// contains filtered or unexported fields
}

Time deals only with hours, minutes, and seconds. As opposed to the native time.Time struct that deals also with dates & timezones.

var (
	// StartOfDayTime is the time referring to 00:00:00
	StartOfDayTime Time

	// EndOfDayTime is the time referring to 23:59:59
	EndOfDayTime Time

	// ErrInvalidTimeFormat indicates that the input string is in
	// an invalid format.
	ErrInvalidTimeFormat = errors.New("invalid time format")
)

func NewTime

func NewTime(h, m, s int) Time

NewTime returns a new Time object given hours, minutes, and seconds.

func Now

func Now(timezone string) Time

Now returns the current Time at the sepcified timezone. If an invalid timezone is given, then UTC is used.

func ParseTime

func ParseTime(str string) (*Time, error)

ParseTime takes in a string of the format: hh:mm:ss and returns a parsed Time object. If the string is not in a valid format ErrInvalidTimeFormat is returned.

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add increments the Time by the given input duration.

func (Time) After

func (t Time) After(comparison Time) bool

After returns true fo the Time object occurs after the input Time.

func (Time) Before

func (t Time) Before(comparison Time) bool

Before returns true fo the Time object occurs before the input Time.

func (Time) HoursMinutesSeconds

func (t Time) HoursMinutesSeconds() (int, int, int)

HoursMinutesSeconds returns the given hours, minutes, and seconds within the Time object.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Time) Scan

func (t *Time) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (*Time) String

func (t *Time) String() string

String returns the string representation of Time: hh:mm:ss

func (Time) Sub

func (t Time) Sub(d time.Duration) Time

Sub decrements the Time by the given input duration.

func (*Time) Today

func (t *Time) Today(timezone string) time.Time

Today converts the Time object into a time.Time at the current date given a timezone.

func (Time) TotalSeconds

func (t Time) TotalSeconds() int

TotalSeconds returns the total amount of seconds into the day that this Time object is.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Time) Value

func (t *Time) Value() (driver.Value, error)

Value implements the sql.Valuer interface so that Time can be used in conjunction with the time type in databases.

func (Time) Within

func (t Time) Within(start Time, end Time) bool

Within returns true if the Time occurs within the start and end range. If start occurs after end, then the returned duration assumes that end refers to the following day.

Jump to

Keyboard shortcuts

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