period

package
v1.21.7 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package period holds the subscription module's calendar rules: the time units that size a subscription term and the traffic reset cycles of a plan. It is the one place these rules live; the calendar reset, renewals, the refund quote and the owner's next-reset display all ask it.

A Calendar evaluates the rules in one time zone whatever zone a time value carries (a value read from the database comes in the driver's zone), so "the 15th" is the same day for every caller. Production code uses App(), the application zone of pkg/timeutil.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownUnit = errors.New("unknown subscription time unit")

ErrUnknownUnit rejects a time unit no rule knows. A term computed from one used to end where it started: a subscription expired on arrival.

Functions

This section is empty.

Types

type Calendar

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

Calendar evaluates the rules in one time zone.

func App

func App() Calendar

App returns the calendar of the application time zone.

func In

func In(loc *time.Location) Calendar

In returns the calendar of loc (UTC when nil).

func (Calendar) CycleAt

func (c Calendar) CycleAt(cycle Cycle, start, now time.Time) (from, to time.Time, ok bool)

CycleAt returns the reset cycle containing now: from the last reset at or before now up to the next one. ok is false when the cycle never resets.

func (Calendar) DayStart

func (c Calendar) DayStart(t time.Time) time.Time

DayStart returns midnight of t's day.

func (Calendar) LastReset

func (c Calendar) LastReset(cycle Cycle, start, at time.Time) (last time.Time, ok bool)

LastReset returns the most recent reset of cycle at or before at, at the start of its day, for a subscription that started at start. ok is false when the cycle never resets. It is the reset a subscription is owed when no run has reset it for that day yet.

func (Calendar) Location

func (c Calendar) Location() *time.Location

Location returns the calendar's time zone.

func (Calendar) NextReset

func (c Calendar) NextReset(cycle Cycle, start, now time.Time) (next time.Time, ok bool)

NextReset returns the first reset of cycle after now, at the start of its day, for a subscription that started at start. ok is false when the cycle never resets.

func (Calendar) ResetBetween

func (c Calendar) ResetBetween(cycle Cycle, start, from, to time.Time) bool

ResetBetween reports whether cycle has a reset day on any date from from's day through to's day, both included.

func (Calendar) ResetsOn

func (c Calendar) ResetsOn(cycle Cycle, start, day time.Time) bool

ResetsOn reports whether day, any instant of it, is a reset day of cycle for a subscription that started at start.

func (Calendar) TermEnd

func (c Calendar) TermEnd(unit Unit, quantity int64, start time.Time) (time.Time, error)

TermEnd returns the end of a term of quantity units beginning at start. Calendar units follow time.AddDate, so a month from Jan 31 ends on Mar 2 or 3; a term without limit ends at the no-limit sentinel.

type Cycle

type Cycle int64

Cycle is a plan's calendar traffic reset rule, its reset_cycle.

const (
	// CycleNone has no calendar reset: the traffic quota lasts the term.
	CycleNone Cycle = 0
	// CycleFirstOfMonth resets on the 1st of every month.
	CycleFirstOfMonth Cycle = 1
	// CycleMonthly resets every month on the start day, or on the month's
	// last day when the month is shorter.
	CycleMonthly Cycle = 2
	// CycleYearly resets every year on the start date; a Feb 29 start resets
	// on Feb 28 in common years.
	CycleYearly Cycle = 3
)

func (Cycle) Valid

func (c Cycle) Valid() bool

Valid reports whether c is a known cycle.

type Unit

type Unit string

Unit is a purchasable subscription time unit, a plan's unit_time.

const (
	UnitYear   Unit = "Year"
	UnitMonth  Unit = "Month"
	UnitDay    Unit = "Day"
	UnitHour   Unit = "Hour"
	UnitMinute Unit = "Minute"
	// UnitNoLimit is a term without end.
	UnitNoLimit Unit = "NoLimit"
)

func ParseUnit

func ParseUnit(name string) (Unit, error)

ParseUnit validates a stored or requested unit name.

Jump to

Keyboard shortcuts

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