dtime

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

Easy Date/Time Formats with Duration Spans

WIP GoDoc License Go Report Card License

Returns one or two *time.Time pointers, one for the first second for given time, and a bounding second of the end of the duration (second after last second). This allows easy checks for give times within that duration.

+20m  - in 20 minutes
+1h   - in an hour
-2.5d - two and half days ago
.h    - this hour 
.d    - today (this day)
t     - tomorrow 
y     - yesterday
ly    - last year
nw    - next week
nm    - next month
lw    - last week
lmon  - last monday
ly+4w - last year for four weeks (24 days)

See the test data for hundreds of examples and the PEG grammar for specifics.

Motivation

When using a mobile device the only characters available on the default keyboard are alpha-numeric and the comma (,) and period (.). While it is only a minor convenience to shift to the character keyboard why not create a set of formats that worth with the least amount of trouble. Therefore, these formats use the shortest, best format possible to convey the most common references to dates and times.

This also makes these time formats particularly useful to add to applications with a terse command-line interface.

TODO

  • Add the dtime command (with tab completion) to go with the package.

See Also

TJ Holowaychuk's go-naturaldate Package

TJ's go-naturaldate package came out while I was developing this one. I noted his use of PEG and reworked the internals of my package to also use it.

TJ's package is far better for conversational UIs.

Mine started with emphasis on the least amount of typing possible and no spaces so that queries can easily be added as singular command-line arguments.

Mine also comes with the dtime command for easy integration into shell scripts or while editing files with vi/m using "wand" syntax (!!,!},!G}.

I also focus mostly on time spans rather than specific dates.

Andrew Snodgrass' PEG Golang Package

This PEG package is truly amazing. My days of writing ABNF are likely over.

Design Decisions

  • Lowercase for all. Since the primary motivation is efficiency both in inputting and parsing I decided to keep everything to lowercase which a few exceptions where uppercase has a different meaning.

  • Just one. No need for more than one way to refer to things. Easier to memorize.

Documentation

Overview

Package dtime enables easy human input of times, dates, and durations. It also includes many convenience functions for rouding time duration boundaries as is frequently needed for scheduling and time-based search applications.

Pointers to time.Time are used throughout the package since <nil> is usually a more desirable zero value than that for time.Time without a pointer. */

Index

Examples

Constants

View Source
const (
	SECOND float64 = 1000000000
	MINUTE float64 = 60000000000
	HOUR   float64 = 3600000000000
	DAY    float64 = 86400000000000
	WEEK   float64 = 604800000000000
	YEAR   float64 = 31536000000000000
)

Variables

View Source
var DefaultTime *time.Time

DefaultTime can be set instead of time.Now().

Functions

func AprilOf

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

AprilOf returns the beginning of the first day of April of the given year.

func AugustOf

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

AugustOf returns the beginning of the first day of August of the given year.

func DayOf

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

DayOf returns the start of the given day.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	t, _ := time.Parse("2006-01-02 15:04:05 -0700", "2020-05-13 14:34:56 -0500")
	fmt.Println(dtime.DayOf(&t))

}
Output:

2020-05-13 00:00:00 -0500 -0500

func DayOfWeek

func DayOfWeek(t *time.Time, day string) *time.Time

DayOfWeek returns the day of the week passed rounded to the beginning of the weekday indicated.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	then, _ := time.Parse("2006-01-02 15:04 -0700", "2020-05-13 14:34 -0500")
	fmt.Println(then)
	fmt.Println(dtime.DayOfWeek(&then, "tue"))
	fmt.Println(dtime.DayOfWeek(&then, "fri"))
	again, _ := time.Parse("2006-01-02 15:04 -0700", "2020-05-11 14:34 -0500")
	fmt.Println(again)
	fmt.Println(dtime.DayOfWeek(&again, "tue"))
	fmt.Println(dtime.DayOfWeek(&again, "fri"))
}
Output:

2020-05-13 14:34:00 -0500 -0500
2020-05-12 00:00:00 -0500 -0500
2020-05-15 00:00:00 -0500 -0500
2020-05-11 14:34:00 -0500 -0500
2020-05-12 00:00:00 -0500 -0500
2020-05-15 00:00:00 -0500 -0500

func DecemberOf

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

DecemberOf returns the beginning of the first day of December of the given year.

func FebruaryOf

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

FebruaryOf returns the beginning of the first day of February of the given year.

func FridayOf

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

FridayOf returns the Friday of the week passed as time rounded to the beginning of the day.

func HourOf

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

HourOf returns the start of the given hour.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	t, _ := time.Parse("2006-01-02 15:04:05 -0700", "2020-05-13 14:34:56 -0500")
	fmt.Println(dtime.HourOf(&t))

}
Output:

2020-05-13 14:00:00 -0500 -0500

func JanuaryOf

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

JanuaryOf returns the beginning of the first day of January of the given year.

func JulyOf

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

JulyOf returns the beginning of the first day of July of the given year.

func JuneOf

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

JuneOf returns the beginning of the first day of June of the given year.

func MarchOf

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

MarchOf returns the beginning of the first day of March of the given year.

func MayOf

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

MayOf returns the beginning of the first day of May of the given year.

func MinuteOf

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

MinuteOf returns the start of the given minute.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	t, _ := time.Parse("2006-01-02 15:04:05 -0700", "2020-05-13 14:34:56 -0500")
	fmt.Println(dtime.MinuteOf(&t))

}
Output:

2020-05-13 14:34:00 -0500 -0500

func MondayOf

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

MondayOf returns the Monday of the week passed as time rounded to the beginning of the day.

func MonthOf

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

MonthOf returns the start of the month.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	t, _ := time.Parse("2006-01-02 15:04:05 -0700", "2020-05-13 14:34:56 -0500")
	fmt.Println(dtime.MonthOf(&t))

}
Output:

2020-05-01 00:00:00 -0500 -0500

func MonthOfYear

func MonthOfYear(t *time.Time, month string) *time.Time

MonthOfYear returns the beginning of the first day of the specified month of the given year.

func NextHourOf

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

NextHourOf returns the start of the given hour.

func NovemberOf

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

NovemberOf returns the beginning of the first day of November of the given year.

func OctoberOf

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

OctoberOf returns the beginning of the first day of October of the given year.

func Pretty

func Pretty(pretty bool) func(*spanParser) error

func SameTimeInAprilOf

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

SameTimeInAprilOf returns the exact same month day and time but for the month of April instead.

func SameTimeInAugustOf

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

SameTimeInAugustOf returns the exact same month day and time but for the month of August instead.

func SameTimeInDecemberOf

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

SameTimeInDecemberOf returns the exact same month day and time but for the month of December instead.

func SameTimeInFebruaryOf

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

SameTimeInFebruaryOf returns the exact same month day and time but for the month of February instead.

func SameTimeInJanuaryOf

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

SameTimeInJanuaryOf returns the exact same month day and time but for the month of January instead.

func SameTimeInJulyOf

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

SameTimeInJulyOf returns the exact same month day and time but for the month of July instead.

func SameTimeInJuneOf

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

SameTimeInJuneOf returns the exact same month day and time but for the month of June instead.

func SameTimeInMarchOf

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

SameTimeInMarchOf returns the exact same month day and time but for the month of March instead.

func SameTimeInMayOf

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

SameTimeInMayOf returns the exact same month day and time but for the month of May instead.

func SameTimeInMonthOfYear

func SameTimeInMonthOfYear(t *time.Time, month string) *time.Time

SameTimeInMonthOfYear returns the exact same month day and time but for the specified month instead.

func SameTimeInNovemberOf

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

SameTimeInNovemberOf returns the exact same month day and time but for the month of November instead.

func SameTimeInOctoberOf

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

SameTimeInOctoberOf returns the exact same month day and time but for the month of October instead.

func SameTimeInSeptemberOf

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

SameTimeInSeptemberOf returns the exact same month day and time but for the month of September instead.

func SameTimeOnDayOfWeek

func SameTimeOnDayOfWeek(t *time.Time, day string) *time.Time

SameTimeOnDayOfWeek returns the day of the week passed rounded to the beginning of the week day indicated.

func SameTimeOnFridayOf

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

SameTimeOnFridayOf returns the exact same time but on the Friday of the week indicated. For beginning of day use without SameTimeOn.

func SameTimeOnMondayOf

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

SameTimeOnMondayOf returns the exact same time but on the Monday of the week indicated. For beginning of day use without SameTimeOn.

func SameTimeOnSaturdayOf

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

SameTimeOnSaturdayOf returns the exact same time but on the Saturday of the week indicated. For beginning of day use without SameTimeOn.

func SameTimeOnSundayOf

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

SameTimeOnSundayOf returns the exact same time but on the Sunday of the week indicated. For beginning of day use without SameTimeOn.

func SameTimeOnThursdayOf

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

SameTimeOnThursdayOf returns the exact same time but on the Thursday of the week indicated. For beginning of day use without SameTimeOn.

func SameTimeOnTuesdayOf

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

SameTimeOnTuesdayOf returns the exact same time but on the Tuesday of the week indicated. For beginning of day use without SameTimeOn.

func SameTimeOnWednesdayOf

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

SameTimeOnWednesdayOf returns the exact same time but on the Wednesday of the week indicated. For beginning of day use without SameTimeOn.

func SaturdayOf

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

SaturdayOf returns the Saturday of the week passed as time rounded to the beginning of the day.

func SeptemberOf

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

SeptemberOf returns the beginning of the first day of September of the given year.

func Span

func Span(s string) (first *time.Time, last *time.Time)

Span parses the htime format string and returns one or two time pointers. The first is the start time, the second is the last time. If a date and time are detected in the string the first is set. If the offset is detected in the string the second will be set. If no date and time are detected DefaultTime or time.Now() is assumed.

Spans the main function in this package and provides a minimal format for entering date and time information in a practical way. It's primary use case is when a user needs to enter such data quickly and regularly from the command line or into mobile and other devices where human input speed is limited to what can be tapped out on the screen. The characters used in the formatting only characters that appear on most default keyboards without shifting or switching to symbolic input. The package provides no method of specifying timezone, which falls out of the scope of this package. All times are therefore assumed to be local. For a precise specification of the format see the htime.peg file included with the package source code.

func SundayOf

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

SundayOf returns the Sunday of the week passed as time rounded to the beginning of the day.

func ThursdayOf

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

ThursdayOf returns the Thursday of the week passed as time rounded to the beginning of the day.

func Today

func Today() *time.Time

Today returns the start of the current date.

func Tomorrow

func Tomorrow() *time.Time

Tomorrow returns the start of the next day.

func TuesdayOf

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

TuesdayOf returns the Tuesday of the week passed as time rounded to the beginning of the day.

func Until

func Until(fn func(*time.Time) *time.Time, t *time.Time) time.Duration

Until takes a time and a function that takes a time and returns a time and converts the result into a duration offset between them. Returns nil if unable to determine. This is useful when a duration is needed for any of the functions in this package that match that signature. The duration is positive or negative depending on the relation between the times.

func WednesdayOf

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

WednesdayOf returns the Wednesday of the week passed as time rounded to the beginning of the day.

func WeekOf

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

WeekOf returns the start of the given week.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	t, _ := time.Parse("2006-01-02 15:04:05 -0700", "2020-05-13 14:34:56 -0500")
	fmt.Println(dtime.WeekOf(&t))

}
Output:

2020-05-11 00:00:00 -0500 -0500

func YearOf

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

YearOf returns the start of the month.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rwxrob/bonzai/dtime"
)

func main() {
	t, _ := time.Parse("2006-01-02 15:04:05 -0700", "2020-05-13 14:34:56 -0500")
	fmt.Println(dtime.YearOf(&t))

}
Output:

2020-01-01 00:00:00 -0500 -0500

func Yesterday

func Yesterday() *time.Time

Yesterday returns the start of the previous day.

Types

This section is empty.

Jump to

Keyboard shortcuts

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