Import path:
gitlab.soludian.com/soludian/fountain/libs/base/time_util
time_util
import "gitlab.soludian.com/soludian/fountain/libs/base/time_util"Package time_util provides an enhanced time.Time implementation. Add more commonly used functional methods.
such as: DayStart(), DayAfter(), DayAgo(), DateFormat() and more.
Index
- Constants
- Variables
- func AddDay(t time.Time, day int) time.Time
- func AddHour(t time.Time, hour int) time.Time
- func AddMinutes(t time.Time, minutes int) time.Time
- func AddSec(t time.Time, seconds int) time.Time
- func AddSeconds(t time.Time, seconds int) time.Time
- func Date(t time.Time, template ...string) string
- func DateFormat(t time.Time, template string) string
- func Datetime(t time.Time, template ...string) string
- func DayEnd(t time.Time) time.Time
- func DayStart(t time.Time) time.Time
- func Elapsed(start, end time.Time) string
- func ElapsedNow(start time.Time) string
- func Format(t time.Time) string
- func FormatBy(t time.Time, layout string) string
- func FormatByTpl(t time.Time, template string) string
- func FormatUnix(sec int64, layout ...string) string
- func FormatUnixBy(sec int64, layout string) string
- func FormatUnixByTpl(sec int64, template ...string) string
- func FromNow(t time.Time) string
- func FromNowWith(u time.Time, tms []TimeMessage) string
- func HourEnd(t time.Time) time.Time
- func HourStart(t time.Time) time.Time
- func HowLongAgo(diffSec int64) string
- func HowLongAgo2(diffSec int64, tms []TimeMessage) string
- func InRange(dst, start, end time.Time) bool
- func IsDuration(s string) bool
- func MustToDur(s string) time.Duration
- func MustToDuration(s string) time.Duration
- func NowAddDay(day int) time.Time
- func NowAddHour(hour int) time.Time
- func NowAddMinutes(minutes int) time.Time
- func NowAddSec(seconds int) time.Time
- func NowAddSeconds(seconds int) time.Time
- func NowHourEnd() time.Time
- func NowHourStart() time.Time
- func NowUnix() int64
- func NowUnix32() int32
- func ParseRange(expr string, opt *ParseRangeOpt) (start, end time.Time, err error)
- func SafeToDur(s string) time.Duration
- func SafeToDuration(s string) time.Duration
- func SetLocalByName(tzName string) error
- func ToDur(s string) (time.Duration, error)
- func ToDuration(s string) (time.Duration, error)
- func ToLayout(template string) string
- func ToTime(s string, layouts ...string) (time.Time, error)
- func TodayEnd() time.Time
- func TodayStart() time.Time
- func TryToTime(s string, bt time.Time) (time.Time, error)
- type ParseRangeOpt
- type Time
- func FromDate(s string, template ...string) (*Time, error)
- func FromString(s string, layouts ...string) (*Time, error)
- func FromTime(t time.Time) *Time
- func FromUnix(sec int64) *Time
- func Local() *Time
- func LocalByName(tzName string) *Time
- func New(t time.Time) *Time
- func Now() *Time
- func Wrap(t time.Time) *Time
- func (t *Time) AddDay(day int) *Time
- func (t *Time) AddDur(dur time.Duration) *Time
- func (t *Time) AddHour(hours int) *Time
- func (t *Time) AddMinutes(minutes int) *Time
- func (t *Time) AddSeconds(seconds int) *Time
- func (t *Time) AddString(dur string) *Time
- func (t *Time) CustomHMS(hour, min, sec int) *Time
- func (t *Time) DateFormat(template string) string
- func (t *Time) Datetime() string
- func (t *Time) DayAfter(day int) *Time
- func (t *Time) DayAgo(day int) *Time
- func (t *Time) DayEnd() *Time
- func (t *Time) DayStart() *Time
- func (t Time) Diff(u time.Time) time.Duration
- func (t Time) DiffSec(u time.Time) int
- func (t Time) DiffUnix(u int64) int
- func (t *Time) Format(layout string) string
- func (t *Time) HourEnd() *Time
- func (t *Time) HourStart() *Time
- func (t Time) HowLongAgo(before time.Time) string
- func (t *Time) IsAfter(u time.Time) bool
- func (t *Time) IsAfterUnix(ux int64) bool
- func (t *Time) IsBefore(u time.Time) bool
- func (t *Time) IsBeforeUnix(ux int64) bool
- func (t *Time) SubDay(day int) *Time
- func (t *Time) SubHour(hours int) *Time
- func (t *Time) SubMinutes(minutes int) *Time
- func (t *Time) SubSeconds(seconds int) *Time
- func (t Time) SubUnix(u time.Time) int
- func (t Time) T() time.Time
- func (t Time) Timestamp() int64
- func (t *Time) Tomorrow() *Time
- func (t *Time) TplFormat(template string) string
- func (t *Time) UnmarshalJSON(data []byte) error
- func (t *Time) UnmarshalText(data []byte) error
- func (t *Time) Yesterday() *Time
- type TimeMessage
- type TimeX
Constants
some common date time templates
const (
KDefaultTemplate = "Y-m-d H:i:s"
KTemplateWithMs3 = "Y-m-d H:i:s.v" // end with ".000"
KTemplateWithMs6 = "Y-m-d H:i:s.u" // end with ".000000"
)const (
KDateTimeLayout = "2006-01-02 15:04:05"
KLayoutWithMs3 = "2006-01-02 15:04:05.000"
KLayoutWithMs6 = "2006-01-02 15:04:05.000000"
KDateOnlyLayout = "2006-01-02"
KTimeOnlyLayout = "15:04:05"
KDateTimeAtFullLayout = "Monday, 02 January 2006 15:04:05 Z07:00"
// ZeroUnix zero unix timestamp
KZeroUnix int64 = -62135596800
)provide some commonly time consts
const (
KOneSecond = 1
KOneMinSec = 60
KOneHourSec = 3600
KOneDaySec = 86400
KOneWeekSec = 7 * 86400
KOneMonthSec = 30 * 86400
KMicrosecond = time.Microsecond
KMillisecond = time.Millisecond
KSecond = time.Second
KOneMin = time.Minute
KMinute = time.Minute
KOneHour = time.Hour
KHour = time.Hour
KOneDay = 24 * time.Hour
KDay = KOneDay
KOneWeek = 7 * 24 * time.Hour
KWeek = KOneWeek
KMonth = 30 * 24 * time.Hour
)Variables
var (
// DefaultLayout template for format time
DefaultLayout = KDateTimeLayout
// ZeroTime zero time instance
ZeroTime = time.Time{}
)TimeMessages time message list.
NOTE: last item.Seconds[0] is min boundary value.
var TimeMessages = []TimeMessage{
{"< 1 sec ago", []int{0}},
{"1 sec ago", []int{1}},
{"%d secs ago", []int{45, 1}},
{"1 min ago", []int{89}},
{"%d mins ago", []int{44 * 60, 60}},
{"1 hour ago", []int{89 * 60}},
{"%d hours ago", []int{21 * 3600, 3600}},
{"1 day ago", []int{35 * 3600}},
{"%d days ago", []int{30 * 86400, 86400}},
{"1 month ago", []int{45 * 86400}},
{"%d months ago", []int{319 * 86400, 2592000}},
{"1 year ago", []int{547 * 86400}},
{"%d years ago", []int{547 * 86400, 12 * 2592000}},
}func AddDay
func AddDay(t time.Time, day int) time.TimeAddDay add some day time for given time
func AddHour
func AddHour(t time.Time, hour int) time.TimeAddHour add some hour time for given time
func AddMinutes
func AddMinutes(t time.Time, minutes int) time.TimeAddMinutes add some minutes time for given time
func AddSec
func AddSec(t time.Time, seconds int) time.TimeAddSec add some seconds time for given time. alias of AddSeconds()
func AddSeconds
func AddSeconds(t time.Time, seconds int) time.TimeAddSeconds add some seconds time for given time
func Date
func Date(t time.Time, template ...string) stringDate format time by given date template. see ToLayout() for template parse.
func DateFormat
func DateFormat(t time.Time, template string) stringDateFormat format time by given date template. see ToLayout()
func Datetime
func Datetime(t time.Time, template ...string) stringDatetime convert time to string use template. see ToLayout() for template parse.
func DayEnd
func DayEnd(t time.Time) time.TimeDayEnd time for given time
func DayStart
func DayStart(t time.Time) time.TimeDayStart time for given time
func Elapsed
func Elapsed(start, end time.Time) stringElapsed calc elapsed time from start time to end time.
func ElapsedNow
func ElapsedNow(start time.Time) stringElapsedNow calc elapsed time from start time to now.
func Format
func Format(t time.Time) stringFormat convert time to string use default layout
func FormatBy
func FormatBy(t time.Time, layout string) stringFormatBy given default layout
func FormatByTpl
func FormatByTpl(t time.Time, template string) stringFormatByTpl format time by given date template. see ToLayout()
func FormatUnix
func FormatUnix(sec int64, layout ...string) stringFormatUnix time seconds use default layout
func FormatUnixBy
func FormatUnixBy(sec int64, layout string) stringFormatUnixBy format time seconds use given layout
func FormatUnixByTpl
func FormatUnixByTpl(sec int64, template ...string) stringFormatUnixByTpl format time seconds use given date template. see ToLayout()
func FromNow
func FromNow(t time.Time) stringFromNow format time from now, returns like: 1 hour ago, 2 days ago
refer: https://gist.github.com/davidrleonard/259fe449b1ec13bf7d87cde567ca0fde
func FromNowWith
func FromNowWith(u time.Time, tms []TimeMessage) stringFromNowWith format time from now with custom TimeMessage list
func HourEnd
func HourEnd(t time.Time) time.TimeHourEnd time for given time
func HourStart
func HourStart(t time.Time) time.TimeHourStart time for given time
func HowLongAgo
func HowLongAgo(diffSec int64) stringHowLongAgo format diff time seconds to string. alias of HowLongAgo2()
func HowLongAgo2
func HowLongAgo2(diffSec int64, tms []TimeMessage) stringHowLongAgo2 format diff time seconds with custom TimeMessage list
func InRange
func InRange(dst, start, end time.Time) boolInRange check the dst time is in the range of start and end.
if start is zero, only check dst < end, if end is zero, only check dst > start.
func IsDuration
func IsDuration(s string) boolIsDuration check the string is a valid duration string. alias of com_func.IsDuration()
func MustToDur
func MustToDur(s string) time.DurationMustToDur parse a duration string. alias of SafeToDuration()
func MustToDuration
func MustToDuration(s string) time.DurationSafeToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". Panic if error
func NowAddDay
func NowAddDay(day int) time.TimeNowAddDay add some day time from now
func NowAddHour
func NowAddHour(hour int) time.TimeNowAddHour add some hour time from now
func NowAddMinutes
func NowAddMinutes(minutes int) time.TimeNowAddMinutes add some minutes time from now
func NowAddSec
func NowAddSec(seconds int) time.TimeNowAddSec add some seconds time from now. alias of NowAddSeconds()
func NowAddSeconds
func NowAddSeconds(seconds int) time.TimeNowAddSeconds add some seconds time from now
func NowHourEnd
func NowHourEnd() time.TimeNowHourEnd time
func NowHourStart
func NowHourStart() time.TimeNowHourStart time
func NowUnix
func NowUnix() int64NowUnix is short of time.Now().Unix()
func NowUnix32
func NowUnix32() int32NowUnix32 is short of time.Now().Unix() with int32 rounding
func ParseRange
func ParseRange(expr string, opt *ParseRangeOpt) (start, end time.Time, err error)ParseRange parse time range expression string to time.Time range.
- "0" will use opt.BaseTime.
Expression format:
"-5h~-1h" => 5 hours ago to 1 hour ago
"1h~5h" => 1 hour after to 5 hours after
"-1h~1h" => 1 hour ago to 1 hour after
"-1h" => 1 hour ago to feature. eq "-1h~"
"-1h~0" => 1 hour ago to now.
"< -1h" OR "~-1h" => 1 hour ago.
"> 1h" OR "1h" => 1 hour after to feature
// keyword: now, today, yesterday, tomorrow
"today" => today start to today end
"yesterday" => yesterday start to yesterday end
"tomorrow" => tomorrow start to tomorrow endUsage:
start, end, err := ParseRange("-1h~1h", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(start, end)func SafeToDur
func SafeToDur(s string) time.DurationSafeToDur parse a duration string. alias of SafeToDuration()
func SafeToDuration
func SafeToDuration(s string) time.DurationSafeToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func SetLocalByName
func SetLocalByName(tzName string) errorSetLocalByName set local by tz name. eg: UTC, PRC
func ToDur
func ToDur(s string) (time.Duration, error)ToDur parse a duration string. alias of ToDuration()
func ToDuration
func ToDuration(s string) (time.Duration, error)ToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func ToLayout
func ToLayout(template string) stringToLayout convert chars date template to Go date layout.
template chars see time_util.charMap
func ToTime
func ToTime(s string, layouts ...string) (time.Time, error)ToTime parse a datetime string. alias of string_util.ToTime()
func TodayEnd
func TodayEnd() time.TimeTodayEnd time
func TodayStart
func TodayStart() time.TimeTodayStart time
func TryToTime
func TryToTime(s string, bt time.Time) (time.Time, error)TryToTime parse a date string or duration string to time.Time.
if s is empty, return zero time.
type ParseRangeOpt
ParseRangeOpt is the option for ParseRange
type ParseRangeOpt struct {
// BaseTime is the base time for relative time string.
// if is zero, use time.Now() as base time.
BaseTime time.Time
// OneAsEnd is the option for one time range.
// - False: "-1h" => "-1h,0"; "1h" => "+1h, feature"
// - True: "-1h" => "zero,-1h"; "1h" => "zero,1h"
OneAsEnd bool
// AutoSort is the option for sort the time range.
AutoSort bool
// SepChar is the separator char for time range string. default is '~'
SepChar byte
// BeforeFn hook for before parse time string.
BeforeFn func(string) string
// KeywordFn is the function for parse keyword time string.
KeywordFn func(string) (time.Time, time.Time, error)
}type Time
Time an enhanced time.Time implementation.
type Time struct {
time.Time
// Layout set the default date format layout. default use DefaultLayout
Layout string
}func FromDate
func FromDate(s string, template ...string) (*Time, error)FromDate create from datetime string.
func FromString
func FromString(s string, layouts ...string) (*Time, error)FromString create from datetime string. see string_util.ToTime()
func FromTime
func FromTime(t time.Time) *TimeFromTime new instance form given time.Time. alias of the New()
func FromUnix
func FromUnix(sec int64) *TimeFromUnix create from unix time
func Local
func Local() *TimeLocal time for now
func LocalByName
func LocalByName(tzName string) *TimeLocalByName time for now
func New
func New(t time.Time) *TimeNew instance form given time
func Now
func Now() *TimeNow time instance
func Wrap
func Wrap(t time.Time) *TimeWrap the go time instance. alias of the New()
func (*Time) AddDay
func (t *Time) AddDay(day int) *TimeAddDay add some day time for the time
func (*Time) AddDur
func (t *Time) AddDur(dur time.Duration) *TimeAddDur some duration time
func (*Time) AddHour
func (t *Time) AddHour(hours int) *TimeAddHour add some hour time
func (*Time) AddMinutes
func (t *Time) AddMinutes(minutes int) *TimeAddMinutes add some minutes time for the time
func (*Time) AddSeconds
func (t *Time) AddSeconds(seconds int) *TimeAddSeconds add some seconds time the time
func (*Time) AddString
func (t *Time) AddString(dur string) *TimeAddString add duration time string.
Example:
tn := time_util.Now() // example as "2019-01-01 12:12:12"
nt := tn.AddString("1h")
nt.Datetime() // Output: 2019-01-01 13:12:12func (*Time) CustomHMS
func (t *Time) CustomHMS(hour, min, sec int) *TimeCustomHMS custom change the hour, minute, second for create new time.
func (*Time) DateFormat
func (t *Time) DateFormat(template string) stringDateFormat use input template format time to date.
Example:
tn := time_util.Now()
tn.DateFormat("Y-m-d H:i:s") // Output: 2019-01-01 12:12:12
tn.DateFormat("Y-m-d H:i") // Output: 2019-01-01 12:12
tn.DateFormat("Y-m-d") // Output: 2019-01-01
tn.DateFormat("Y-m") // Output: 2019-01
tn.DateFormat("y-m-d") // Output: 19-01-01
tn.DateFormat("ymd") // Output: 190101see ToLayout() for convert template to layout.
func (*Time) Datetime
func (t *Time) Datetime() stringDatetime use DefaultLayout format time to date. see Format()
func (*Time) DayAfter
func (t *Time) DayAfter(day int) *TimeDayAfter get some day after time for the time. alias of Time.AddDay()
func (*Time) DayAgo
func (t *Time) DayAgo(day int) *TimeDayAgo get some day ago time for the time
func (*Time) DayEnd
func (t *Time) DayEnd() *TimeDayEnd get time at 23:59:59
func (*Time) DayStart
func (t *Time) DayStart() *TimeDayStart get time at 00:00:00
func (Time) Diff
func (t Time) Diff(u time.Time) time.DurationDiff calc diff duration for t - u. alias of time.Time.Sub()
func (Time) DiffSec
func (t Time) DiffSec(u time.Time) intDiffSec calc diff seconds for t - u
func (Time) DiffUnix
func (t Time) DiffUnix(u int64) intDiffUnix calc diff seconds for t.Unix() - u
func (*Time) Format
func (t *Time) Format(layout string) stringFormat returns a textual representation of the time value formatted according to the layout defined by the argument.
see time.Time.Format()
func (*Time) HourEnd
func (t *Time) HourEnd() *TimeHourEnd time
func (*Time) HourStart
func (t *Time) HourStart() *TimeHourStart time
func (Time) HowLongAgo
func (t Time) HowLongAgo(before time.Time) stringHowLongAgo format diff time to string.
func (*Time) IsAfter
func (t *Time) IsAfter(u time.Time) boolIsAfter the given time
func (*Time) IsAfterUnix
func (t *Time) IsAfterUnix(ux int64) boolIsAfterUnix the given unix timestamp
func (*Time) IsBefore
func (t *Time) IsBefore(u time.Time) boolIsBefore the given time
func (*Time) IsBeforeUnix
func (t *Time) IsBeforeUnix(ux int64) boolIsBeforeUnix the given unix timestamp
func (*Time) SubDay
func (t *Time) SubDay(day int) *TimeSubDay add some day time for the time
func (*Time) SubHour
func (t *Time) SubHour(hours int) *TimeSubHour minus some hour time
func (*Time) SubMinutes
func (t *Time) SubMinutes(minutes int) *TimeSubMinutes minus some minutes time for the time
func (*Time) SubSeconds
func (t *Time) SubSeconds(seconds int) *TimeSubSeconds minus some seconds time the time
func (Time) SubUnix
func (t Time) SubUnix(u time.Time) intSubUnix calc diff seconds for t - u
func (Time) T
func (t Time) T() time.TimeT returns the t.Time
func (Time) Timestamp
func (t Time) Timestamp() int64Timestamp value. alias of t.Unix()
func (*Time) Tomorrow
func (t *Time) Tomorrow() *TimeTomorrow time. get tomorrow time for the time
func (*Time) TplFormat
func (t *Time) TplFormat(template string) stringTplFormat use input template format time to date.
alias of DateFormat()
func (*Time) UnmarshalJSON
func (t *Time) UnmarshalJSON(data []byte) errorUnmarshalJSON implements the json.Unmarshaler interface.
Tip: will auto match a format by string_util.ToTime()
func (*Time) UnmarshalText
func (t *Time) UnmarshalText(data []byte) errorUnmarshalText implements the encoding.TextUnmarshaler interface.
Tip: will auto match a format by string_util.ToTime()
func (*Time) Yesterday
func (t *Time) Yesterday() *TimeYesterday get day ago time for the time
type TimeMessage
TimeMessage struct for HowLongAgo2(), FromNowWith()
type TimeMessage struct {
// Message string or format string
Message string
// Seconds time range.
// first elem is max boundary value, second elem is divisor(unit).
Seconds []int
}type TimeX
TimeX alias of Time Deprecated: use Time instead
type TimeX = TimeGenerated by gomarkdoc