godate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2025 License: MIT Imports: 7 Imported by: 0

README

godate

godate is simple package for work with date. It similar to standard go's package time and use some its functions. You can use only ISO format (YYYY-MM-DD) for string present Date and store in databases.

Installation

go get github.com/Nemagu/godate

Documentation

Overview

Package godate providers functionality for working with dates. It follows the design of package time in the standard library.

Index

Constants

View Source
const Day = Duration(1)

Variables

This section is empty.

Functions

func ValidateDate

func ValidateDate(year int, month time.Month, day int) error

ValidateDate validate given year, month and day for exists.

Types

type Date

type Date uint32

A Date represent one date from range from "0001-01-01" to "9999-12-31"

A Date value can be used by multiple goroutines simultaneously except that the methods Date.UnmarshalJSON and Date.UnmarshalText are not concurrency-safe.

dates can be compared using the Date.Before, Date.After and Date.Equal methods. The Date.Sub method subtracts two dates, producing a Duration. The Date.Add method adds a Date and a Duration, producing a Date.

Representations of a Date value saved by the Date.MarshalJSON and Date.MarshalText methods.

func FromString

func FromString(s string) (Date, error)

FromString returns the date which corresponds given string. It function use time.Parse with time.DateOnly("2006-01-02") layout string so if it returns err FromString returns this error.

func FromTime

func FromTime(t time.Time) Date

FromTime returns Date which corresponds given time.

func MustNew

func MustNew(year int, month time.Month, day int) Date

MustNew returns date and error from given year, month and day. If year was out of range [1, 9999] and month was out of range [1, 12] and day was more than days count in month function called panic!

func New

func New(year int, month time.Month, day int) (Date, error)

New returns date and error from given year, month and day. If year was out of range [1, 9999] and month was out of range [1, 12] and day was more than days count in month function returned error.

func Today

func Today() Date

Today returns the date based on your UTC offset.

func TodayUTC

func TodayUTC() Date

TodayUTC returns the date based on zero UTC offset.

func ZeroDate

func ZeroDate() Date

ZeroDate returns date which corresponds zero value Date - "0001-01-01" or 0.

func (Date) Add

func (d Date) Add(duration Duration) Date

Add returns the date d+other.

func (Date) After

func (d Date) After(other Date) bool

After reports whether the date d is after other.

func (Date) Before

func (d Date) Before(other Date) bool

Before reports whether the date d is before other.

func (Date) Compare

func (d Date) Compare(other Date) int

Compare compares the date d with other. If t is before other, it returns -1; if t is after other, it returns +1; if they're the same, it returns 0.

func (Date) Date

func (d Date) Date() (int, time.Month, int)

Date returns (year, month, day) for date.

func (Date) Day

func (d Date) Day() int

Day returns day for date.

func (Date) Equal

func (d Date) Equal(other Date) bool

Equal reports whether d and other represent the same date.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether d represents the zero Date("0001-01-01").

func (Date) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface. The date must be in the ISO 8601 format.

func (Date) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface. The output matches that of calling the Date.String method.

func (Date) Month

func (d Date) Month() time.Month

Month returns month for date.

func (*Date) Scan

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

Scan implements the [sql.Scanner].

Scan parse some value. If the value is nil the date will be zero. If the value holds string or []byte it calls FromString method; if the value holds time.Time it calls FromTime method; else Scan returns error.

func (Date) String

func (d Date) String() string

String returns date formatted using the ISO standard("YYYY-MM-DD")

func (Date) Sub

func (d Date) Sub(other Date) Duration

Sub returns the duration d-other.

func (Date) ToTime

func (d Date) ToTime() time.Time

ToTime returns time.Time instant which corresponds Date with 0 minutes, 0 seconds etc and based on zero UTC offset.

func (*Date) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface. The date must be in the ISO 8601 format.

func (*Date) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface. The date must be in the ISO 8601 format.

func (Date) Value

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

Value implements the driver.Valuer.

If Date.IsZero is true Value return nil as value for database; else it is false Value returns Date.String method's result.

func (Date) Year

func (d Date) Year() int

Year returns year for date.

type Duration

type Duration int32

A Duration represents the elapsed date between two dates as an int32 days count.

func (Duration) Days

func (d Duration) Days() int

Days returns the duration as a integer number of days.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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