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
- Variables
- func Compare(a, b RawDate) int
- type RawDate
- func (r RawDate) AddDate(years, months, days int) RawDate
- func (a RawDate) After(b RawDate) bool
- func (a RawDate) Before(b RawDate) bool
- func (r RawDate) Compare(a RawDate) int
- func (r RawDate) Day() int
- func (a RawDate) Equal(b RawDate) bool
- func (r RawDate) Format(format string) string
- func (d RawDate) GoString() string
- func (r RawDate) IsValid() bool
- func (r RawDate) IsZero() bool
- func (d RawDate) MarshalJSON() ([]byte, error)
- func (d RawDate) MarshalText() ([]byte, error)
- func (r RawDate) Month() time.Month
- func (r RawDate) MonthEnd() RawDate
- func (r RawDate) MonthStart() RawDate
- func (r RawDate) NextWeekday(weekday time.Weekday, orToday bool) RawDate
- func (r RawDate) PreviousWeekday(weekday time.Weekday, orToday bool) RawDate
- func (d *RawDate) Scan(src any) error
- func (r RawDate) String() string
- func (r RawDate) Time(location *time.Location) time.Time
- func (d *RawDate) UnmarshalJSON(data []byte) error
- func (d *RawDate) UnmarshalText(data []byte) error
- func (d RawDate) Value() (driver.Value, error)
- func (d RawDate) ValueTime() (driver.Value, error)
- func (r RawDate) Weekday() time.Weekday
- func (r RawDate) Year() int
Constants ¶
const ISODate = time.DateOnly
ISODate represents the format for a date-only value in ISO standard.
const MSSQLDateFmt = "20060102"
const MSSQLZeroDate = "19000101"
Variables ¶
var SQLDateFormat = "2006-01-02" // This is the format for the DB when Parse uses a string (default)
var SQLTimeZone = time.UTC // This is the timezone that gets used for the time.Time value
var SQLValueTime = false // Setting this to true uses a time.Time when unmarshalling for SQL
var SQLZeroValue = "0000-01-01" // This is what is used as Zero value in the DB (only with Parse using string)
var Zero = RawDate{0, 0, 0}
Zero represents a zero value for RawDate corresponding to the date 0001-01-01.
Functions ¶
Types ¶
type RawDate ¶
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 MustNew ¶
MustNew creates a new RawDate given the year, month, and day. Panics if the provided values do not form a valid date.
func New ¶
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 ¶
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
AddDate adds the specified number of years, months, and days to the RawDate. Returns a new RawDate with the updated values.
func (RawDate) Compare ¶
Compare compares the RawDate with another RawDate. Returns 1 if r > a, -1 if r < a, and 0 if r == a.
func (RawDate) MarshalJSON ¶ added in v0.6.0
MarshalJSON implements `json.Marshaler` using YYYY-MM-DD
func (RawDate) MarshalText ¶ added in v0.6.0
MarshalText implements the encoding.TextMarshaler
func (RawDate) MonthEnd ¶ added in v0.5.0
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
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
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
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
Scan implements `sql.Scanner` by unmarshalling from `time.Time`
func (RawDate) 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
UnmarshalJSON implements `json.Unmarshaler`; parses the date as time.DateOnly (YYYY-MM-DD).
func (*RawDate) UnmarshalText ¶ added in v0.6.0
UnmarshalText implements the encoding.TextUnmarshaler for YYYY-MM-DD
func (RawDate) ValueTime ¶ added in v0.7.0
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"