~gloatingfiddle/exdura

7d917d399ef8cd5625ab1bde8793220b0a6a67d6 — David Hagerty 1 year, 4 months ago
Just write the package
2 files changed, 36 insertions(+), 0 deletions(-)

A exdura.go
A go.mod
A  => exdura.go +33 -0
@@ 1,33 @@
// Package exdura provides extra time intervals greater than one hour.
// It makes a few assumptions - first, that the user is using a Gregorian calendar.
// Second, it assumes that the user is in a context where exact precision for months and years is not needed.
// If you do need exact precision for months or years, it would be better to roll it yourself.
package exdura

import "time"

// Days takes a count and converts it into a [time.Duration] representing that count of days.
func Days(count int) time.Duration {
	return toDuration(count) * 24 * time.Hour
}

// Weeks takes a count and converts it into a [time.Duration] representing that count of weeks.
func Weeks(count int) time.Duration {
	return toDuration(count) * Days(7)
}

// Months takes a count and converts it into a [time.Duration] representing that count of months.
// It assumes that a month is equivalent to four-weeks.
func Months(count int) time.Duration {
	return toDuration(count) * Weeks(4)
}

// Years takes a count and converts it into a [time.Duration] representing that count of years.
// It assumes that a year is equivalent to 52 weeks.
func Years(count int) time.Duration {
	return toDuration(count) * Weeks(52)
}

func toDuration(number int) time.Duration {
	return time.Duration(number)
}

A  => go.mod +3 -0
@@ 1,3 @@
module dathagerty.com/go/exdura

go 1.20