date

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 4 Imported by: 0

README

Date

test Go Report Card codecov Version Badge License Badge Go Reference

The extends of the Golang built-in time package.

Installation

Run the following command to install the library, which requires Go 1.20 and later versions.

go get -u github.com/ghosind/go-date

Getting Started

This library overwrites the built-in format layouts, the following example is a simple implementation of parsing a string:

str := "2024-01-10 23:59:30"
layout := "YYYY-MM-DD HH:mm:ss"
tm, err := date.Parse(layout, str)
if err != nil {
  // handle error
}
fmt.Print(tm) // 2024-01-10 23:59:30 +0000 CST

You can also use the Format method to format the Time to a string:

tm := Date(2024, time.January, 1, 23, 59, 30, 0)
fmt.Print(fm.Format("YYYY-MM-DD HH:mm:ss")) // 2024-01-10 23:59:30

Available Formats

Format Description Example
YYYY 4-digits year 2023
YY 2-digits year 23
MM 2-digits month 01-12
M Month, beginning at 1 1-12
MMMM The month name January-December
MMM The abbreviated month name Jan-Dec
DD The day of month, 2-digits 01-31
D The day of month, beginning at 1 1-31
dddd The day of week Sunday-Friday
ddd The abbreviated name of weekday Sun-Fri
d The day of week, beginning at 0 (Sunday) 0-6
HH The hour of 24-hour clock, 2-digits 00-23
H The hour of 24-hour clock, beginning at 1 0-23
hh The hour of 12-hour clock, 2-digits 01-12
h The hour of 12-hour clock, beginning at 1 1-12
mm The minutes, 2-digits 00-59
m The minutes 0-59
ss The seconds, 2-digits 00-59
s The seconds 0-59
SSS The milliseconds, 3-digits 000-999
SS The tens of milliseconds, 2-digits 00-99
S The hundreds of milliseconds, 1-digit 0-9
A Post or ante meridiem, in upper case AM, PM
a Post or ante meridiem, in lower case am, pm
Z Timezone offset from UTC, separate by colon -08:00
ZZ Timezone offset from UTC -0800

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotTime error = errors.New("not a Time")
)

Functions

func Since

func Since(t any) time.Duration

Since returns the time elapsed since t. It is shorthand for time.Now().Sub(t).

func Until

func Until(t any) time.Duration

Until returns the duration until t. It is shorthand for t.Sub(time.Now()).

Types

type ParseError

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

ParseError is the error that happens when parsing the time string by the layout.

func (*ParseError) Error

func (pe *ParseError) Error() string

type Time

type Time struct {
	time.Time
}

func Date

func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc ...*time.Location) Time

Date creates and returns a new Time by the specific values. The location is an optional parameter, default time.UTC.

func New

func New(t ...time.Time) Time

New creates and returns a new Time. It'll initialize by the parameter, or set the time to now.

func Now

func Now() Time

Now returns the current time.

func Parse

func Parse(layout, value string) (Time, error)

Parse parses a formatted string with the layout and returns the time value it represents.

func ParseInLocation

func ParseInLocation(layout, value string, loc *time.Location) (Time, error)

ParseInLocation parses a formatted string with the layout and the given location, and returns the time value it represents.

func ParseInLocationName

func ParseInLocationName(layout, value, name string) (Time, error)

ParseInLocationName tries to load the location with the given name, parses a formatted string with the layout and the location, and returns the time value it represents.

func Unix

func Unix(sec, nsec int64) Time

Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC. It is valid to pass nsec outside the range [0, 999999999]. Not all sec values have a corresponding time value. One such value is 1<<63-1 (the largest int64 value).

func UnixMicro

func UnixMicro(usec int64) Time

UnixMicro returns the local Time corresponding to the given Unix time, usec microseconds since January 1, 1970 UTC.

func UnixMilli

func UnixMilli(msec int64) Time

UnixMilli returns the local Time corresponding to the given Unix time, msec milliseconds since January 1, 1970 UTC.

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add returns the time t+d.

func (Time) AddDate

func (t Time) AddDate(years int, months int, days int) Time

AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

func (Time) After

func (t Time) After(u any) bool

After reports whether the time instant t is after u.

func (Time) AppendFormat

func (t Time) AppendFormat(b []byte, layout string) []byte

AppendFormat is like Format but appends the textual representation to b and returns the extended buffer.

func (Time) Before

func (t Time) Before(u any) bool

Before reports whether the time instant t is before u.

func (Time) Compare

func (t Time) Compare(u any) int

Compare compares the time instant t with u. If t is before u, it returns -1; if t is after u, it returns +1; if they're the same, it returns 0.

func (Time) EndOfDay added in v0.2.0

func (t Time) EndOfDay() Time

EndOfDay returns the end time of the day.

func (Time) EndOfHalfYear added in v0.2.0

func (t Time) EndOfHalfYear() Time

EndOfHalfYear returns the end time of the half year.

func (Time) EndOfHour added in v0.2.0

func (t Time) EndOfHour() Time

EndOfHour returns the end time of the hour.

func (Time) EndOfMinute added in v0.2.0

func (t Time) EndOfMinute() Time

EndOfMinute returns the end time of the minute.

func (Time) EndOfMonth added in v0.2.0

func (t Time) EndOfMonth() Time

EndOfMonth returns the end time of the month.

func (Time) EndOfQuarter added in v0.2.0

func (t Time) EndOfQuarter() Time

EndOfQuarter returns the end time of the quarter.

func (Time) EndOfSecond added in v0.2.0

func (t Time) EndOfSecond() Time

EndOfSecond returns the end time of the second.

func (Time) EndOfYear added in v0.2.0

func (t Time) EndOfYear() Time

EndOfYear returns the end time of the year.

func (Time) Equal

func (t Time) Equal(u any) bool

Equal reports whether t and u represent the same time instant. Two times can be equal even if they are in different locations. For example, 6:00 +0200 and 4:00 UTC are Equal.

func (Time) Format

func (t Time) Format(layout string) string

Format returns a string of the time formatted by the layout from the parameter.

func (Time) Hour12

func (t Time) Hour12() int

Hour12 returns the 12-hours clock hour offset within the day specified by the time, in the range [1, 12]. See https://en.wikipedia.org/wiki/12-hour_clock for more details about the value.

func (Time) In

func (t Time) In(loc *time.Location) Time

In returns a copy of t representing the same time instant, but with the copy's location information set to loc for display purposes.

In panics if loc is nil.

func (Time) Local

func (t Time) Local() Time

Local returns t with the location set to local time.

func (Time) Microsecond

func (t Time) Microsecond() int

Microsecond returns the microsecond offset within the second specified by the time, in the range [0, 999999].

func (Time) Millisecond

func (t Time) Millisecond() int

Millisecond returns the millisecond offset within the second specified by the time, in the range [0, 999].

func (Time) Round

func (t Time) Round(d time.Duration) Time

Round returns the result of rounding t to the nearest multiple of d (since the zero time). The rounding behavior for halfway values is to round up. If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.

Round operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, Round(Hour) may return a time with a non-zero minute, depending on the time's Location.

func (Time) StartOfDay added in v0.2.0

func (t Time) StartOfDay() Time

StartOfDay returns the start time of the day.

func (Time) StartOfHalfYear added in v0.2.0

func (t Time) StartOfHalfYear() Time

StartOfHalfYear returns the start time of the half year.

func (Time) StartOfHour added in v0.2.0

func (t Time) StartOfHour() Time

StartOfHour returns the start time of the hour.

func (Time) StartOfMinute added in v0.2.0

func (t Time) StartOfMinute() Time

StartOfMinute returns the start time of the minute.

func (Time) StartOfMonth added in v0.2.0

func (t Time) StartOfMonth() Time

StartOfMonth returns the start time of the month.

func (Time) StartOfQuarter added in v0.2.0

func (t Time) StartOfQuarter() Time

StartOfQuarter returns the start time of the quarter.

func (Time) StartOfSecond added in v0.2.0

func (t Time) StartOfSecond() Time

StartOfSecond returns the start time of the second.

func (Time) StartOfYear added in v0.2.0

func (t Time) StartOfYear() Time

StartOfYear returns the start time of the year.

func (Time) Sub

func (t Time) Sub(u any) time.Duration

Sub returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute t-d for a duration d, use t.Add(-d).

func (Time) Truncate

func (t Time) Truncate(d time.Duration) Time

Truncate returns the result of rounding t down to a multiple of d (since the zero time). If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.

Truncate operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, Truncate(Hour) may return a time with a non-zero minute, depending on the time's Location.

func (Time) UTC

func (t Time) UTC() Time

UTC returns t with the location set to UTC.

func (Time) ZoneBounds

func (t Time) ZoneBounds() (Time, Time)

ZoneBounds returns the bounds of the time zone in effect at time t. The zone begins at start and the next zone begins at end. If the zone begins at the beginning of time, start will be returned as a zero Time. If the zone goes on forever, end will be returned as a zero Time. The Location of the returned times will be the same as t.

Jump to

Keyboard shortcuts

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