timeutil

package
v0.0.0-...-d280c18 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: GPL-3.0 Imports: 2 Imported by: 2

Documentation

Overview

Package timeutil contains some types and functions to help work with times and time spans.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DaysSinceMonday

func DaysSinceMonday(day time.Weekday) int

DaysSinceMonday returns the number of days has passed since last time it was a monday.

This function is a naïve implementation that assumes all weeks have the 7 weekdays. In reality, there are some weird edge cases where some regions have skipped some days, but those cases are left as "undefined behavior".

Types

type TimeSpan

type TimeSpan struct {
	Start *time.Time
	End   *time.Time
}

TimeSpan holds a start and end timestamp. Both the start and the end are optional, as represented by being set to nil.

func Day

func Day(now time.Time) TimeSpan

Day returns a TimeSpan from 00:00 - 23:59 for the same day as "now".

func Week

func Week(now time.Time) TimeSpan

Week returns a TimeSpan from 00:00 monday - 23:59 sunday for the same week as "now".

type TimeSpanShorthand

type TimeSpanShorthand byte

TimeSpanShorthand is an enumeration of different TimeSpan templates.

const (
	// TimeSpanNone represents a TimeSpan of nil - nil
	TimeSpanNone TimeSpanShorthand = iota
	// TimeSpanPast represents a TimeSpan of nil - now
	TimeSpanPast
	// TimeSpanFuture represents a TimeSpan of now - nil
	TimeSpanFuture
	// TimeSpanThisDay represents a TimeSpan of 00:00 today - 23:59 today
	TimeSpanThisDay
	// TimeSpanThisWeek represents a TimeSpan of 00:00 this monday - 23:59 this sunday
	TimeSpanThisWeek
	// TimeSpanPrevDay represents a TimeSpan of 00:00 yesterday - 23:59 yesterday
	TimeSpanPrevDay
	// TimeSpanPrevWeek represents a TimeSpan of 00:00 monday - 23:59 sunday last week
	TimeSpanPrevWeek
	// TimeSpanNextDay represents a TimeSpan of 00:00 tomorrow - 23:59 tomorrow
	TimeSpanNextDay
	// TimeSpanNextWeek represents a TimeSpan of 00:00 monday - 23:59 sunday next week
	TimeSpanNextWeek
)

func (TimeSpanShorthand) Span

func (s TimeSpanShorthand) Span(now time.Time) TimeSpan

Span returns a TimeSpan given a specific reference time of when "now" is.

func (TimeSpanShorthand) String

func (s TimeSpanShorthand) String() string

Jump to

Keyboard shortcuts

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