rawdate

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package rawdate provides a simple date handling utility without time. It uses zero-based values for year, month, and day to maintain consistency with time.Time, where an uninitialized instance defaults to the year 0001-01-01.

Index

Constants

View Source
const ISODate = time.DateOnly

ISODate represents the format for a date-only value in ISO standard.

View Source
const MSSQLDateFmt = "20060102"
View Source
const MSSQLZeroDate = "19000101"

Variables

View Source
var SQLDateFormat = "2006-01-02" // This is the format for the DB when Parse uses a string (default)
View Source
var SQLTimeZone = time.UTC // This is the timezone that gets used for the time.Time value
View Source
var SQLValueTime = false // Setting this to true uses a time.Time when unmarshalling for SQL
View Source
var SQLZeroValue = "0000-01-01" // This is what is used as Zero value in the DB (only with Parse using string)
View Source
var Zero = RawDate{0, 0, 0}

Zero represents a zero value for RawDate corresponding to the date 0001-01-01.

Functions

func Compare

func Compare(a, b RawDate) int

Compare compares two RawDates. Returns 1 if a > b, -1 if a < b, and 0 if a == b.

Types

type RawDate

type RawDate struct {
	Year0  int
	Month0 int8
	Day0   int8
}

RawDate represents a date without time information. Year0, Month0, and Day0 are zero-based values for year, month, and day respectively. These fields are exported for marshaling purposes and should be used with the understanding that they are zero-based.

func FromTime

func FromTime(t time.Time) RawDate

FromTime creates a RawDate from a time.Time value, ignoring the time portion.

func MustNew

func MustNew(y int, m time.Month, d int) RawDate

MustNew creates a new RawDate given the year, month, and day. Panics if the provided values do not form a valid date.

func New

func New(y int, m time.Month, d int) (RawDate, error)

New creates a new RawDate given the year, month, and day. Returns an error if the provided values do not form a valid date.

func Parse

func Parse(layout, s string) (RawDate, error)

Parse parses a string representing a date and returns the corresponding RawDate. An error is returned if the string does not represent a valid date.

func Today added in v0.7.0

func Today() RawDate

Today creates a new RawDate with the current date (in local time)

func (RawDate) AddDate added in v0.3.3

func (r RawDate) AddDate(years, months, days int) RawDate

AddDate adds the specified number of years, months, and days to the RawDate. Returns a new RawDate with the updated values.

func (RawDate) After added in v0.7.0

func (a RawDate) After(b RawDate) bool

After reports whether the RawDate a is after RawDate b.

func (RawDate) Before added in v0.7.0

func (a RawDate) Before(b RawDate) bool

Before reports whether the RawDate a is before RawDate b.

func (RawDate) Compare

func (r RawDate) Compare(a RawDate) int

Compare compares the RawDate with another RawDate. Returns 1 if r > a, -1 if r < a, and 0 if r == a.

func (RawDate) Day

func (r RawDate) Day() int

Day returns the day of the month for the RawDate.

func (RawDate) Equal added in v0.7.1

func (a RawDate) Equal(b RawDate) bool

Equal reports whether the RawDate a is equal RawDate b.

func (RawDate) Format

func (r RawDate) Format(format string) string

Format formats the RawDate according to the provided layout string.

func (RawDate) GoString added in v0.6.0

func (d RawDate) GoString() string

GoString implements `fmt.GoStringer`.

func (RawDate) IsValid

func (r RawDate) IsValid() bool

IsValid checks whether the RawDate represents a valid date.

func (RawDate) IsZero added in v0.3.2

func (r RawDate) IsZero() bool

IsZero checks if the RawDate is a zero date.

func (RawDate) MarshalJSON added in v0.6.0

func (d RawDate) MarshalJSON() ([]byte, error)

MarshalJSON implements `json.Marshaler` using YYYY-MM-DD

func (RawDate) MarshalText added in v0.6.0

func (d RawDate) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler

func (RawDate) Month

func (r RawDate) Month() time.Month

Month returns the month of the year for the RawDate.

func (RawDate) MonthEnd added in v0.5.0

func (r RawDate) MonthEnd() RawDate

MonthEnd returns a new RawDate that represents the last day of the month for the given RawDate.

func (RawDate) MonthStart added in v0.5.0

func (r RawDate) MonthStart() RawDate

MonthStart returns a new RawDate that represents the first day of the month for the given RawDate.

func (RawDate) NextWeekday added in v0.5.0

func (r RawDate) NextWeekday(weekday time.Weekday, orToday bool) RawDate

NextWeekday returns the next date that falls on the given weekday. If orToday is true and the given weekday is today, it returns the current date.

func (RawDate) PreviousWeekday added in v0.5.0

func (r RawDate) PreviousWeekday(weekday time.Weekday, orToday bool) RawDate

PreviousWeekday returns the previous date that falls on the given weekday. If orToday is true and the given weekday is today, it returns the current date.

func (*RawDate) Scan added in v0.6.0

func (d *RawDate) Scan(src any) error

Scan implements `sql.Scanner` by unmarshalling from `time.Time`

func (RawDate) String

func (r RawDate) String() string

String returns a string representation of the RawDate in YYYY-MM-DD format.

func (RawDate) Time

func (r RawDate) Time(location *time.Location) time.Time

Time converts the RawDate to a time.Time value, with the provided location and zero time.

func (*RawDate) UnmarshalJSON added in v0.6.0

func (d *RawDate) UnmarshalJSON(data []byte) error

UnmarshalJSON implements `json.Unmarshaler`; parses the date as time.DateOnly (YYYY-MM-DD).

func (*RawDate) UnmarshalText added in v0.6.0

func (d *RawDate) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler for YYYY-MM-DD

func (RawDate) Value added in v0.6.0

func (d RawDate) Value() (driver.Value, error)

Value implements `driver.Valuer`; marshals to a string

func (RawDate) ValueTime added in v0.7.0

func (d RawDate) ValueTime() (driver.Value, error)

This implementation will use the time.Time serialisation With test fields as destination this creates something like "2024-03-20 00:00:00+01:00"

func (RawDate) Weekday added in v0.4.0

func (r RawDate) Weekday() time.Weekday

Weekday calculates and returns the day of the week for the RawDate.

func (RawDate) Year

func (r RawDate) Year() int

Year returns the year for the RawDate.

Jump to

Keyboard shortcuts

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