timeutil

package
v0.2.56 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package now is a time toolkit for golang.

More details README here: https://github.com/jinzhu/now

import "github.com/jinzhu/now"

now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon
now.BeginningOfDay()    // 2013-11-18 00:00:00 Mon
now.EndOfDay()          // 2013-11-18 23:59:59.999999999 Mon

Index

Constants

View Source
const (
	// DefaultFormat       = "2006-01-02 15:04:05"
	Format              = "2006-01-02 15:04:05"
	GoFormat            = "2006-01-02 15:04:05.999999999"
	DateFormat          = "2006-01-02"
	FormattedDateFormat = "Jan 2, 2006"
	HourMinuteFormat    = "15:04"
	HourFormat          = "15"
	DayDateTimeFormat   = "Mon, Aug 2, 2006 3:04 PM"
	CookieFormat        = "Monday, 02-Jan-2006 15:04:05 MST"
	RFC822Format        = "Mon, 02 Jan 06 15:04:05 -0700"
	RFC1036Format       = "Mon, 02 Jan 06 15:04:05 -0700"
	RFC2822Format       = "Mon, 02 Jan 2006 15:04:05 -0700"
	RFC3339Format       = "2006-01-02T15:04:05-07:00"
	RSSFormat           = "Mon, 02 Jan 2006 15:04:05 -0700"
	WeekStartDay        = time.Sunday
)

GoTime the different string formats for go dates

Variables

View Source
var (
	TimeFormats = []string{"1/2/2006", "1/2/2006 15:4:5", "2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "1-2", "15:4:5", "15:4", "15", "15:4:5 Jan 2, 2006 MST", "2006-01-02 15:04:05.999999999 -0700 MST", "2006-01-02T15:04:05-07:00"}
)

Functions

func Add

func Add(begin time.Time, tm string) (end time.Time)

Add add some time

func BeginningOfDay

func BeginningOfDay() time.Time

BeginningOfDay beginning of day

func BeginningOfHour

func BeginningOfHour() time.Time

BeginningOfHour beginning of hour

func BeginningOfMinute

func BeginningOfMinute() time.Time

BeginningOfMinute beginning of minute

func BeginningOfMonth

func BeginningOfMonth() time.Time

BeginningOfMonth beginning of month

func BeginningOfQuarter

func BeginningOfQuarter() time.Time

BeginningOfQuarter beginning of quarter

func BeginningOfWeek

func BeginningOfWeek() time.Time

BeginningOfWeek beginning of week

func BeginningOfYear

func BeginningOfYear() time.Time

BeginningOfYear beginning of year

func Between

func Between(time1, time2 string) bool

Between check now between the begin, end time or not

func EndOfDay

func EndOfDay() time.Time

EndOfDay end of day

func EndOfHour

func EndOfHour() time.Time

EndOfHour end of hour

func EndOfMinute

func EndOfMinute() time.Time

EndOfMinute end of minute

func EndOfMonth

func EndOfMonth() time.Time

EndOfMonth end of month

func EndOfQuarter

func EndOfQuarter() time.Time

EndOfQuarter end of quarter

func EndOfSunday

func EndOfSunday() time.Time

EndOfSunday end of sunday

func EndOfWeek

func EndOfWeek() time.Time

EndOfWeek end of week

func EndOfYear

func EndOfYear() time.Time

EndOfYear end of year

func FormatTime

func FormatTime(tm time.Time, for_str string) string

func GetDay

func GetDay(time time.Time) int

return yyyyMMdd

func GetDay0

func GetDay0(timestamp int64) time.Time

获取给定日期的零点时间

func GetDayStr

func GetDayStr(tm time.Time) string

func GetHour

func GetHour(timestamp int64) int

当前时间向上取整点

func GetHour0

func GetHour0(timestamp int64) time.Time

获取向上整时时间

func GetLastDayCurrentTime

func GetLastDayCurrentTime(timestamp int64, offset int) time.Time

获取offset天的现在时间:注意时区

func GetLocalTime

func GetLocalTime(tm time.Time) time.Time

获取本地时间

func GetMonDay

func GetMonDay() int

func GetMonTS

func GetMonTS() (int64, error)

GetMonTS get monday timestamp

func GetMonth0

func GetMonth0(timestamp int64) time.Time

获取给定时间的当月1号零点时间

func GetSeveralDaysAgo

func GetSeveralDaysAgo(day int) (start, end int)

func GetSysTimeLayout

func GetSysTimeLayout() string

获取系统时间的格式

func GetSystemCurDate

func GetSystemCurDate() string

GetSystemCurDate 格式化日期时间 YYYY-MM-DD

func GetSystemCurTime

func GetSystemCurTime() int

GetSystemCurTime get system current time, nanoseconds value

func GetTimeStr

func GetTimeStr(tm time.Time) string

func GetTimeWeek

func GetTimeWeek(timestamp int64) int

获取给定时间的星期

func GetToday

func GetToday() (start time.Time, end time.Time)

func GetUtcDay0

func GetUtcDay0(now time.Time, timeZone *time.Location) int64

获取offset 0点时间

func GetUtcTime

func GetUtcTime(tm time.Time) int64

获取本地时间戳

func GetUtcWeek0

func GetUtcWeek0(timestamp int64) int64

获取最近上个星期天的零点日期

func GetWeek0

func GetWeek0(timestamp int64) time.Time

获取最近上个星期天的零点日期

func GetYesterday

func GetYesterday() (start, end int)

func Monday

func Monday() time.Time

Monday monday

func MustParse

func MustParse(strs ...string) time.Time

MustParse must parse string to time or will panic

func MustParseInLocation

func MustParseInLocation(loc *time.Location, strs ...string) time.Time

MustParseInLocation must parse string to time in location or will panic

func NowTimestamp

func NowTimestamp() int64

毫秒时间戳

func NowUnix

func NowUnix() int64

秒时间戳

func Parse

func Parse(strs ...string) (time.Time, error)

Parse parse string to time

func ParseInLocation

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

ParseInLocation parse string to time in location

func StrToTime

func StrToTime(tStr, format string, timeZone *time.Location) time.Time

字符串转时间

func StringTimetoUnix

func StringTimetoUnix(timestr string) int64

给定字符串时间转换成本地时间戳

func Sunday

func Sunday() time.Time

Sunday sunday

func TimeFormat

func TimeFormat(time time.Time, layout string) string

时间格式化

func TimeFromTimestamp

func TimeFromTimestamp(timestamp int64) time.Time

毫秒时间戳转时间

func TimeFromUnix

func TimeFromUnix(unix int64) time.Time

秒时间戳转时间

func TimeParse

func TimeParse(timeStr, layout string) (time.Time, error)

字符串时间转时间类型

func TimerByHour

func TimerByHour(f func())

整点执行操作

func Timestamp

func Timestamp(t time.Time) int64

毫秒时间戳

func TimestampNano

func TimestampNano() int64

TimestampNano get now timestamp

func UnixToTime

func UnixToTime(timestamp int64) time.Time

时间戳转换为time

func WithTimeAsStartOfDay

func WithTimeAsStartOfDay(t time.Time) time.Time

返回指定时间当天的开始时间

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

func (d Date) Before(d2 Date) bool

Before reports whether d1 occurs before d2.

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

type Now struct {
	time.Time
}

Now now struct

func New

func New(t time.Time) *Now

New initialize Now with time

func (*Now) BeginningOfDay

func (now *Now) BeginningOfDay() time.Time

BeginningOfDay beginning of day

func (*Now) BeginningOfHalf

func (now *Now) BeginningOfHalf() time.Time

BeginningOfHalf beginning of half year

func (*Now) BeginningOfHour

func (now *Now) BeginningOfHour() time.Time

BeginningOfHour beginning of hour

func (*Now) BeginningOfMinute

func (now *Now) BeginningOfMinute() time.Time

BeginningOfMinute beginning of minute

func (*Now) BeginningOfMonth

func (now *Now) BeginningOfMonth() time.Time

BeginningOfMonth beginning of month

func (*Now) BeginningOfQuarter

func (now *Now) BeginningOfQuarter() time.Time

BeginningOfQuarter beginning of quarter

func (*Now) BeginningOfWeek

func (now *Now) BeginningOfWeek() time.Time

BeginningOfWeek beginning of week

func (*Now) BeginningOfYear

func (now *Now) BeginningOfYear() time.Time

BeginningOfYear BeginningOfYear beginning of year

func (*Now) Between

func (now *Now) Between(begin, end string) bool

Between check time between the begin, end time or not

func (*Now) EndOfDay

func (now *Now) EndOfDay() time.Time

EndOfDay end of day

func (*Now) EndOfHalf

func (now *Now) EndOfHalf() time.Time

EndOfHalf end of half year

func (*Now) EndOfHour

func (now *Now) EndOfHour() time.Time

EndOfHour end of hour

func (*Now) EndOfMinute

func (now *Now) EndOfMinute() time.Time

EndOfMinute end of minute

func (*Now) EndOfMonth

func (now *Now) EndOfMonth() time.Time

EndOfMonth end of month

func (*Now) EndOfQuarter

func (now *Now) EndOfQuarter() time.Time

EndOfQuarter end of quarter

func (*Now) EndOfSunday

func (now *Now) EndOfSunday() time.Time

EndOfSunday end of sunday

func (*Now) EndOfWeek

func (now *Now) EndOfWeek() time.Time

EndOfWeek end of week

func (*Now) EndOfYear

func (now *Now) EndOfYear() time.Time

EndOfYear end of year

func (*Now) Monday

func (now *Now) Monday() time.Time

Monday monday

func (*Now) MustParse

func (now *Now) MustParse(strs ...string) (t time.Time)

MustParse must parse string to time or it will panic

func (*Now) Parse

func (now *Now) Parse(strs ...string) (t time.Time, err error)

Parse parse string to time

func (*Now) Sunday

func (now *Now) Sunday() time.Time

Sunday sunday

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