calendar

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2019 License: BSD-3-Clause Imports: 4 Imported by: 0

README

calendar

Go calendar Date (only), clock Time (only), and nullable versions of same, with sensible JSON and SQL behavior.

I wrote this because I needed:

  • calendar dates (with no associated time)
  • clock times (with no associated date)
  • with no time zone (time zone to be stored separately as an Olsen TZDB value)
  • with support for nullable dates/times
  • compatible with PostgreSQL DATE and TIME types, and
  • with sensible ISO-8601 bare-date and bare-time JSON representations

So I can define a structure with (say):

StartDate         calendar.Date     `json:"start_date"`
StartTime         calendar.NullTime `json:"start_time"`
EndDate           calendar.Date     `json:"end_date"`
EndTime           calendar.NullTime `json:"end_time"`
TimeZone          string            `json:"timezone"`	

and scan in values from a PostgreSQL database, and serialize them to JSON, and get out something like:

"start_date":"2019-02-14","start_time":"09:00:00","end_date":"2019-02-14","end_time":"17:00:00","timezone":"Americas/Chicago"

If you don't have those precise requirements, this is not the date/time library for you.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date time.Time

Date represents a date with no time or timezone information. It is compatible with PostgreSQL database DATE values when using the de facto standard lib/pq driver.

func DateFromTime

func DateFromTime(t time.Time) Date

DateFromTime constructs a new Date object from the provided time.Time value, throwing away all time and timezone information.

func NewDate

func NewDate(y, m, d int) Date

NewDate constructs a new Date object for the given year, month and day

func (Date) AddDate

func (d Date) AddDate(yy int, mm int, dd int) Date

AddDate adds the specified number of years, months and days to the Date, returning another Date.

func (Date) After

func (d Date) After(other Date) bool

After returns true if the first date (the reciever) is after the second date (the argument).

func (Date) Before

func (d Date) Before(other Date) bool

Before returns true if the first date (the reciever) is before the second date (the argument).

func (Date) Equal

func (d Date) Equal(other Date) bool

Equal returns true if the two dates are equal.

func (Date) MarshalJSON

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

MarshalJSON marshals a Date into JSON format. The date is formatted in RFC 3339 full-date format -- that is, yyyy-mm-dd.

func (*Date) Scan

func (d *Date) Scan(value interface{}) error

Scan implements the database/sql Scanner interface.

func (Date) String

func (d Date) String() string

String returns the value of the Date in ISO-8601 / RFC 3339 format yyyy-mm-dd.

func (*Date) UnmarshalJSON

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

UnmarshalJSON unmarshals a Date from JSON format. The date is expected to be in full-date format as per RFC 3339 -- that is, yyyy-mm-dd.

func (Date) Value

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

Value implements the database/sql Valuer interface.

type NullDate

type NullDate struct {
	Date  Date
	Valid bool
}

func NewNullDate

func NewNullDate(y, m, d int) NullDate

NewNullDate constructs a new NullDate object for the given year, month and day

func NullDateFromTime

func NullDateFromTime(t time.Time) NullDate

NullDateFromTime constructs a new NullDate object from the provided time.Time value, throwing away all time and timezone information.

func (NullDate) Equal

func (d NullDate) Equal(other NullDate) bool

Equal returns true if the two dates are equal.

func (NullDate) MarshalJSON

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

MarshalJSON marshals a NullDate into JSON format. The date is formatted in RFC 3339 full-date format -- that is, yyyy-mm-dd.

func (*NullDate) Scan

func (d *NullDate) Scan(value interface{}) error

Scan implements the database/sql Scanner interface.

func (NullDate) String

func (d NullDate) String() string

String returns the value of the NullDate in ISO-8601 / RFC 3339 format yyyy-mm-dd.

func (*NullDate) UnmarshalJSON

func (d *NullDate) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals a NullDate from JSON format. The date is expected to be in full-date format as per RFC 3339 -- that is, yyyy-mm-dd.

func (NullDate) Value

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

Value implements the database/sql Valuer interface.

type NullTime

type NullTime struct {
	Time  Time
	Valid bool
}

func NewNullTime

func NewNullTime(h, m, s int) NullTime

NewNullTime constructs a new NullTime object for the given hours, minutes and seconds

func NullTimeFromTime

func NullTimeFromTime(t time.Time) NullTime

NullTimeFromTime constructs a new NullTime object from the provided time.Time value, throwing away all time and timezone information.

func (NullTime) Equal

func (t NullTime) Equal(other NullTime) bool

Equal returns true if the two dates are equal.

func (NullTime) MarshalJSON

func (t NullTime) MarshalJSON() ([]byte, error)

MarshalJSON marshals a NullTime into JSON format. The date is formatted in RFC 3339 full-date format -- that is, yyyy-mm-dd.

func (*NullTime) Scan

func (t *NullTime) Scan(value interface{}) error

Scan implements the database/sql Scanner interface.

func (NullTime) String

func (t NullTime) String() string

String returns the value of the NullTime in ISO-8601 / RFC 3339 format yyyy-mm-dd.

func (*NullTime) UnmarshalJSON

func (t *NullTime) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals a NullTime from JSON format. The date is expected to be in full-date format as per RFC 3339 -- that is, yyyy-mm-dd.

func (NullTime) Value

func (t NullTime) Value() (driver.Value, error)

Value implements the database/sql Valuer interface.

type Time

type Time time.Time

Time represents a time with no date or timezone information. It is compatible with PostgreSQL database TIME values when using the de facto standard lib/pq driver.

func NewTime

func NewTime(h, m, s int) Time

NewDate constructs a new Date object for the given year, month and day

func (Time) Equal

func (ti Time) Equal(other Time) bool

func (Time) MarshalJSON

func (ti Time) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Time into JSON format. The date is formatted in RFC 3339 format -- that is, hh:mm:ss in 24 hour clock

func (*Time) Scan

func (ti *Time) Scan(value interface{}) error

Scan implements the database/sql Scanner interface.

func (Time) String

func (ti Time) String() string

String returns the value of the Time in hh:mm:ss format.

func (*Time) UnmarshalJSON

func (ti *Time) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals a Time from JSON format. The date is expected to be in RFC 3339 format -- that is, hh:mm:ss in 24 hour clock

func (Time) Value

func (ti Time) Value() (driver.Value, error)

Value implements the database/sql Valuer interface.

Jump to

Keyboard shortcuts

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