timex

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 5 Imported by: 0

README

timex

Go Reference Go Report Card codecov

📅 A Go package for working with date.

Why use timex?

  • Self-contained type Date, focusing on date-specific operations.
  • Don't rely on type time.Time, avoid timezone-related issues.
  • Represent date format like YYYY-MM-DD instead of 2006-01-02.
  • Working with date type of MySQL or PostgreSQL directly.
  • Fast methods implementation of Date relative to time.Time.
Method package timex package time
OrdinalDate 4.07 ns/op 6.28 ns/op
Date 6.81 ns/op 8.04 ns/op
WeekDay 0.31 ns/op 2.80 ns/op
ISOWeek 7.52 ns/op 9.80 ns/op
Add 12.23 ns/op 24.18 ns/op
AddDays 0.31 ns/op 3.12 ns/op
Sub 0.31 ns/op 6.53 ns/op
Parse 34.72 ns/op 55.47 ns/op
Format 28.05 ns/op 59.46 ns/op
MarshalJSON 27.53 ns/op 41.42 ns/op
UnmarshalJSON 10.89 ns/op 52.11 ns/op

Features

  • Fully-implemented type Date.
    • Zero value: January 1, year 1. Align with type time.Time.
    • Working with standard library: conversion with type time.Time.
    • Parsing & Formatting: conversion with formatted strings.
    • Getter: get year, quarter, month, day of year, day of month, day of week.
    • Manipulation: addition and subtraction with years, month, days.
    • Comparison: comparing dates with Before, After, Equal.
    • Serialization: JSON and database.

Getting Started

go get github.com/invzhi/timex

Reference

Documentation

Index

Constants

View Source
const (
	RFC3339 = "YYYY-MM-DD"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

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

Date represents a date.

The zero value of type Date is January 1 of year 1.

swagger:strfmt date

func DateFromOrdinalDate

func DateFromOrdinalDate(year, dayOfYear int) (Date, error)

DateFromOrdinalDate returns the date corresponding to year, day of year.

func DateFromTime

func DateFromTime(t time.Time) Date

DateFromTime returns the date specified by t.

func MustDateFromOrdinalDate

func MustDateFromOrdinalDate(year, dayOfYear int) Date

MustDateFromOrdinalDate is like DateFromOrdinalDate but panics if the date cannot be created.

func MustNewDate

func MustNewDate(year, month, day int) Date

MustNewDate is like NewDate but panics if the date cannot be created.

func NewDate

func NewDate(year, month, day int) (Date, error)

NewDate returns the date corresponding to year, month, and day.

func ParseDate

func ParseDate(layout, value string) (Date, error)

ParseDate parses a formatted string and returns the date it represents.

YY       01             Two-digit year
YYYY   2001             Four-digit year
M      1-12             Month, beginning at 1
MM    01-12             Month, 2-digits
MMM   Jan-Dec           The abbreviated month name
MMMM  January-December  The full month name
D      1-31             Day of month
DD    01-31             Day of month, 2-digits

func Today

func Today(location *time.Location) Date

Today returns the current date in the given location.

func (Date) Add

func (d Date) Add(years, months, days int) Date

Add returns the date corresponding to adding the given number of years, months, and days to d.

func (Date) AddDays

func (d Date) AddDays(days int) Date

AddDays returns the date corresponding to adding the given number of days to d.

func (Date) After

func (d Date) After(dd Date) bool

After reports whether the date d is after dd.

func (Date) Before

func (d Date) Before(dd Date) bool

Before reports whether the date d is before dd.

func (Date) Date

func (d Date) Date() (year, month, day int)

Date returns the date specified by d.

func (Date) Day

func (d Date) Day() int

Day returns the day of month specified by d.

func (Date) DayOfYear

func (d Date) DayOfYear() int

DayOfYear returns the day of year specified by d.

func (Date) Equal

func (d Date) Equal(dd Date) bool

Equal reports whether the date d and dd is the same date.

func (Date) Format

func (d Date) Format(layout string) string

Format returns a textual representation of the date.

YY       01             Two-digit year
YYYY   2001             Four-digit year
M      1-12             Month, beginning at 1
MM    01-12             Month, 2-digits
MMM   Jan-Dec           The abbreviated month name
MMMM  January-December  The full month name
D      1-31             Day of month
DD    01-31             Day of month, 2-digits

func (Date) GoString

func (d Date) GoString() string

GoString returns the Go syntax of the date.

func (Date) ISOWeek

func (d Date) ISOWeek() (year, week int)

ISOWeek returns the ISO 8601 year and week number specified by d.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether the date d is the zero value, January 1 of year 1.

func (Date) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface. The date is a quoted string in RFC 3339 format.

func (Date) Month

func (d Date) Month() int

Month returns the month specified by d.

func (Date) OrdinalDate

func (d Date) OrdinalDate() (year, dayOfYear int)

OrdinalDate returns the ordinal date specified by d.

func (Date) Quarter

func (d Date) Quarter() int

Quarter returns the quarter specified by d.

func (*Date) Scan

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

Scan implements the sql.Scanner interface.

func (Date) String

func (d Date) String() string

String returns the textual representation of the date.

func (Date) Sub

func (d Date) Sub(dd Date) int

Sub returns the number of days since the date d to dd. If the result exceeds the integer scope, the maximum (or minimum) integer will be returned.

func (Date) Time

func (d Date) Time(location *time.Location) time.Time

Time returns the time.Time specified by d in the given location.

func (*Date) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface. The date is expected to be a quoted string in RFC 3339 format.

func (Date) Value

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

Value implements the driver.Valuer interface.

func (Date) Weekday

func (d Date) Weekday() time.Weekday

Weekday returns the day of week specified by d.

func (Date) Year

func (d Date) Year() int

Year returns the year specified by d.

type NullDate

type NullDate struct {
	Date  Date
	Valid bool // Valid is true if Date is not NULL
}

NullDate represents a Date that may be null. NullDate implements the sql.Scanner interface, so it can be used as a scan destination, similar to sql.NullString.

swagger:strfmt date

func (*NullDate) Scan

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

Scan implements the sql.Scanner interface.

func (NullDate) Value

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

Value implements the driver.Valuer interface.

type ParseError

type ParseError struct {
	Layout     string
	Value      string
	LayoutElem string
	ValueElem  string
}

ParseError describes a problem parsing a date string.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns the string representation of a ParseError.

Jump to

Keyboard shortcuts

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