times

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: BSD-3-Clause Imports: 15 Imported by: 1

Documentation

Overview

Package times provides functionality for measuring and displaying time.

This package should keep much fewer dependencies with other packages.

Index

Constants

View Source
const (
	D  = 24 * time.Hour
	H  = time.Hour
	M  = time.Minute
	S  = time.Second
	MS = time.Millisecond
	US = time.Microsecond
	NS = time.Nanosecond
)

Variables

This section is empty.

Functions

func Date

func Date() string

Date returns current date in string like "2006-01-02".

func Datetime

func Datetime() string

Datetime returns current datetime in string like "2006-01-02 15:04:05".

func FuncCost

func FuncCost(f func()) time.Duration

FuncCost calculates the cost time of function `f` in nanoseconds.

func ISO8601

func ISO8601() string

ISO8601 returns current datetime in ISO8601 format like "2006-01-02T15:04:05-07:00".

func ParseDuration

func ParseDuration(s string) (duration time.Duration, err error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h", "1d" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d".

Very note that it supports unit "d" more than function time.ParseDuration.

func RFC822

func RFC822() string

RFC822 returns current datetime in RFC822 format like "Mon, 02 Jan 06 15:04 MST".

func SetTimeZone

func SetTimeZone(zone string) (err error)

SetTimeZone sets the time zone for current whole process. The parameter `zone` is an area string specifying corresponding time zone, eg: Asia/Ho_Chi_Minh.

PLEASE VERY NOTE THAT: 1. This should be called before package "time" import. 2. This function should be called once. 3. Please refer to issue: https://github.com/golang/go/issues/34814

func Timestamp

func Timestamp() int64

Timestamp retrieves and returns the timestamp in seconds.

func TimestampMicro

func TimestampMicro() int64

TimestampMicro retrieves and returns the timestamp in microseconds.

func TimestampMicroStr

func TimestampMicroStr() string

TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.

func TimestampMilli

func TimestampMilli() int64

TimestampMilli retrieves and returns the timestamp in milliseconds.

func TimestampMilliStr

func TimestampMilliStr() string

TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.

func TimestampNano

func TimestampNano() int64

TimestampNano retrieves and returns the timestamp in nanoseconds.

func TimestampNanoStr

func TimestampNanoStr() string

TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.

func TimestampStr

func TimestampStr() string

TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.

Types

type Time

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

Time is a wrapper for time.Time for additional features.

func ConvertZone

func ConvertZone(strTime string, toZone string, fromZone ...string) (*Time, error)

ConvertZone converts time in string `strTime` from `fromZone` to `toZone`. The parameter `fromZone` is unnecessary, it is current time zone in default.

func New

func New(param ...interface{}) *Time

New creates and returns a Time object with given parameter. The optional parameter is the time object which can be type of: time.Time/*time.Time, string or integer. Example: New("2024-10-29") New(1390876568) New(t) // The t is type of time.Time.

func NewFromStr

func NewFromStr(str string) *Time

NewFromStr creates and returns a Time object with given string. Note that it returns nil if there's error occurs.

func NewFromStrFormat

func NewFromStrFormat(str string, format string) *Time

NewFromStrFormat creates and returns a Time object with given string and custom format like: Y-m-d H:i:s. Note that it returns nil if there's error occurs.

func NewFromStrLayout

func NewFromStrLayout(str string, layout string) *Time

NewFromStrLayout creates and returns a Time object with given string and stdlib layout like: 2006-01-02 15:04:05. Note that it returns nil if there's error occurs.

func NewFromTime

func NewFromTime(t time.Time) *Time

NewFromTime creates and returns a Time object with given time.Time object.

func NewFromTimeStamp

func NewFromTimeStamp(timestamp int64) *Time

NewFromTimeStamp creates and returns a Time object with given timestamp, which can be in seconds to nanoseconds. Eg: 1600443866 and 1600443866199266000 are both considered as valid timestamp number.

func Now

func Now() *Time

Now creates and returns a time object of now.

func ParseTimeFromContent

func ParseTimeFromContent(content string, format ...string) *Time

ParseTimeFromContent retrieves time information for content string, it then parses and returns it as *Time object. It returns the first time information if there are more than one time string in the content. It only retrieves and parses the time information with given first matched `format` if it's passed.

func StrToTime

func StrToTime(str string, format ...string) (*Time, error)

StrToTime converts string to *Time object. It also supports timestamp string. The parameter `format` is unnecessary, which specifies the format for converting like "Y-m-d H:i:s". If `format` is given, it acts as same as function StrToTimeFormat. If `format` is not given, it converts string as a "standard" datetime string. Note that, it fails and returns error if there's no date string in `str`.

func StrToTimeFormat

func StrToTimeFormat(str string, format string) (*Time, error)

StrToTimeFormat parses string `str` to *Time object with given format `format`. The parameter `format` is like "Y-m-d H:i:s".

func StrToTimeLayout

func StrToTimeLayout(str string, layout string) (*Time, error)

StrToTimeLayout parses string `str` to *Time object with given format `layout`. The parameter `layout` is in stdlib format like "2006-01-02 15:04:05".

func (*Time) Add

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

Add adds the duration to current time.

func (*Time) AddDate

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

AddDate adds year, month and day to the time.

func (*Time) AddStr

func (t *Time) AddStr(duration string) (*Time, error)

AddStr parses the given duration as string and adds it to current time.

func (*Time) After

func (t *Time) After(u *Time) bool

After reports whether the time instant t is after u.

func (*Time) Before

func (t *Time) Before(u *Time) bool

Before reports whether the time instant t is before u.

func (*Time) Clone

func (t *Time) Clone() *Time

Clone returns a new Time object which is a clone of current time object.

func (*Time) DayOfYear

func (t *Time) DayOfYear() int

DayOfYear checks and returns the position of the day for the year.

func (*Time) DaysInMonth

func (t *Time) DaysInMonth() int

DaysInMonth returns the day count of current month.

func (*Time) DeepCopy

func (t *Time) DeepCopy() interface{}

DeepCopy implements interface for deep copy of current type.

func (*Time) EndOfDay

func (t *Time) EndOfDay(withNanoPrecision ...bool) *Time

EndOfDay clones and returns a new time which is the end of day the and its time is set to 23:59:59.

func (*Time) EndOfHalf

func (t *Time) EndOfHalf(withNanoPrecision ...bool) *Time

EndOfHalf clones and returns a new time which is the end of the half year and its time is set to 23:59:59.

func (*Time) EndOfHour

func (t *Time) EndOfHour(withNanoPrecision ...bool) *Time

EndOfHour clones and returns a new time of which the minutes and seconds are both set to 59.

func (*Time) EndOfMinute

func (t *Time) EndOfMinute(withNanoPrecision ...bool) *Time

EndOfMinute clones and returns a new time of which the seconds is set to 59.

func (*Time) EndOfMonth

func (t *Time) EndOfMonth(withNanoPrecision ...bool) *Time

EndOfMonth clones and returns a new time which is the end of the month and its time is set to 23:59:59.

func (*Time) EndOfQuarter

func (t *Time) EndOfQuarter(withNanoPrecision ...bool) *Time

EndOfQuarter clones and returns a new time which is end of the quarter and its time is set to 23:59:59.

func (*Time) EndOfWeek

func (t *Time) EndOfWeek(withNanoPrecision ...bool) *Time

EndOfWeek clones and returns a new time which is the end of week and its time is set to 23:59:59.

func (*Time) EndOfYear

func (t *Time) EndOfYear(withNanoPrecision ...bool) *Time

EndOfYear clones and returns a new time which is the end of the year and its time is set to 23:59:59.

func (*Time) Equal

func (t *Time) Equal(u *Time) 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 CEST and 4:00 UTC are Equal. See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

func (*Time) Format

func (t *Time) Format(format string) string

Format formats and returns the formatted result with custom `format`. Refer method Layout, if you want to follow stdlib layout.

func (*Time) FormatNew

func (t *Time) FormatNew(format string) *Time

FormatNew formats and returns a new Time object with given custom `format`.

func (*Time) FormatTo

func (t *Time) FormatTo(format string) *Time

FormatTo formats `t` with given custom `format`.

func (*Time) ISO8601

func (t *Time) ISO8601() string

ISO8601 formats the time as ISO8601 and returns it as string.

func (*Time) IsLeapYear

func (t *Time) IsLeapYear() bool

IsLeapYear checks whether the time is leap year.

func (*Time) IsZero

func (t *Time) IsZero() bool

IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

func (*Time) Layout

func (t *Time) Layout(layout string) string

Layout formats the time with stdlib layout and returns the formatted result.

func (*Time) LayoutNew

func (t *Time) LayoutNew(layout string) *Time

LayoutNew formats the time with stdlib layout and returns the new Time object.

func (*Time) LayoutTo

func (t *Time) LayoutTo(layout string) *Time

LayoutTo formats `t` with stdlib layout.

func (*Time) Local

func (t *Time) Local() *Time

Local converts the time to local timezone.

func (Time) MarshalJSON

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

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that, DO NOT use `(t *Time) MarshalJSON() ([]byte, error)` as it looses interface implement of `MarshalJSON` for struct of Time.

func (*Time) Microsecond

func (t *Time) Microsecond() int

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

func (*Time) Millisecond

func (t *Time) Millisecond() int

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

func (*Time) Month

func (t *Time) Month() int

Month returns the month of the year specified by t.

func (*Time) Nanosecond

func (t *Time) Nanosecond() int

Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].

func (*Time) NoValidation

func (t *Time) NoValidation()

NoValidation marks this struct object will not be validated by package gvalid.

func (*Time) RFC822

func (t *Time) RFC822() string

RFC822 formats the time as RFC822 and returns it as string.

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) Scan

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

Scan implements interface used by Scan in package database/sql for Scanning value from database to local golang variable.

func (*Time) Second

func (t *Time) Second() int

Second returns the second offset within the minute specified by t, in the range [0, 59].

func (*Time) StartOfDay

func (t *Time) StartOfDay() *Time

StartOfDay clones and returns a new time which is the start of day, its time is set to 00:00:00.

func (*Time) StartOfHalf

func (t *Time) StartOfHalf() *Time

StartOfHalf clones and returns a new time which is the first day of the half year and its time is set to 00:00:00.

func (*Time) StartOfHour

func (t *Time) StartOfHour() *Time

StartOfHour clones and returns a new time of which the hour, minutes and seconds are set to 0.

func (*Time) StartOfMinute

func (t *Time) StartOfMinute() *Time

StartOfMinute clones and returns a new time of which the seconds is set to 0.

func (*Time) StartOfMonth

func (t *Time) StartOfMonth() *Time

StartOfMonth clones and returns a new time which is the first day of the month and its is set to 00:00:00

func (*Time) StartOfQuarter

func (t *Time) StartOfQuarter() *Time

StartOfQuarter clones and returns a new time which is the first day of the quarter and its time is set to 00:00:00.

func (*Time) StartOfWeek

func (t *Time) StartOfWeek() *Time

StartOfWeek clones and returns a new time which is the first day of week and its time is set to 00:00:00.

func (*Time) StartOfYear

func (t *Time) StartOfYear() *Time

StartOfYear clones and returns a new time which is the first day of the year and its time is set to 00:00:00.

func (*Time) String

func (t *Time) String() string

String returns current time object as string.

func (*Time) Sub

func (t *Time) Sub(u *Time) 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) Timestamp

func (t *Time) Timestamp() int64

Timestamp returns the timestamp in seconds.

func (*Time) TimestampMicro

func (t *Time) TimestampMicro() int64

TimestampMicro returns the timestamp in microseconds.

func (*Time) TimestampMicroStr

func (t *Time) TimestampMicroStr() string

TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.

func (*Time) TimestampMilli

func (t *Time) TimestampMilli() int64

TimestampMilli returns the timestamp in milliseconds.

func (*Time) TimestampMilliStr

func (t *Time) TimestampMilliStr() string

TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.

func (*Time) TimestampNano

func (t *Time) TimestampNano() int64

TimestampNano returns the timestamp in nanoseconds.

func (*Time) TimestampNanoStr

func (t *Time) TimestampNanoStr() string

TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.

func (*Time) TimestampStr

func (t *Time) TimestampStr() string

TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.

func (*Time) ToLocation

func (t *Time) ToLocation(location *time.Location) *Time

ToLocation converts current time to specified location.

func (*Time) ToZone

func (t *Time) ToZone(zone string) (*Time, error)

ToZone converts current time to specified zone like: Asia/Shanghai.

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 converts current time to UTC timezone.

func (*Time) UnmarshalJSON

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

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*Time) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface. Note that it overwrites the same implementer of `time.Time`.

func (*Time) Value

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

Value is the interface providing the Value method for package database/sql/driver for retrieving value from golang variable to database.

func (*Time) WeeksOfYear

func (t *Time) WeeksOfYear() int

WeeksOfYear returns the point of current week for the year.

Jump to

Keyboard shortcuts

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