timeutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DateLayout  = "2006-01-02"
	ClockLayout = "15:04:05"
	TimeLayout  = DateLayout + " " + ClockLayout

	DefaultTimeLocationName = "Asia/Shanghai"
)
View Source
const (
	DT14                = "20060102150405"
	DT8                 = "20060102"
	DT8MDY              = "01022006"
	DT6                 = "200601"
	MonthDay            = "1/2"
	DIN5008FullDate     = "02.01.2006" // German DIN 5008 standard
	DIN5008Date         = "02.01.06"
	RFC3339FullDate     = time.DateOnly
	RFC3339Milli        = "2006-01-02T15:04:05.999Z07:00"
	RFC3339Dash         = "2006-01-02T15-04-05Z07-00"
	ISO8601             = "2006-01-02T15:04:05Z0700"
	ISO8601TZHour       = "2006-01-02T15:04:05Z07"
	ISO8601NoTZ         = "2006-01-02T15:04:05"
	ISO8601MilliNoTZ    = "2006-01-02T15:04:05.999"
	ISO8601Milli        = "2006-01-02T15:04:05.999Z0700"
	ISO8601CompactZ     = "20060102T150405Z0700"
	ISO8601CompactNoTZ  = "20060102T150405"
	ISO8601YM           = "2006-01"
	ISO9075             = time.DateTime                    // ISO/IEC 9075 used by MySQL, BigQuery, etc.
	ISO9075MicroTZ      = "2006-01-02 15:04:05.999999-07"  // ISO/IEC 9075 used by PostgreSQL
	RFC5322             = "Mon, 2 Jan 2006 15:04:05 -0700" // RFC5322             = "Mon Jan 02 15:04:05 -0700 2006"
	SQLTimestamp        = ISO9075
	SQLTimestampMinutes = "2006-01-02 15:04"
	Ruby                = "2006-01-02 15:04:05 -0700" // Ruby Time.now.to_s
	InsightlyAPIQuery   = "_1/_2/2006 _3:04:05 PM"
	DateMDY             = "1/2/2006" // an underscore results in a space.
	DateMDYSlash        = "01/02/2006"
	DateDMYDash         = "_2-01-2006"     // Jira XML Date format
	DateDMYHM2          = "02:01:06 15:04" // GMT time in format dd:mm:yy hh:mm
	DateYMD             = RFC3339FullDate
	DateTextUS          = "January 2, 2006"
	DateTextUSAbbr3     = "Jan 2, 2006"
	DateTextEU          = "2 January 2006"
	DateTextEUAbbr3     = "2 Jan 2006"
	MonthAbbrYear       = "Jan 2006"
	MonthYear           = "January 2006"
)

More predefined layouts for use in Time.Format and time.Parse.

View Source
const (
	RFC3339Min         = "0000-01-01T00:00:00Z"
	RFC3339Max         = "9999-12-31T23:59:59Z"
	RFC3339Zero        = "0001-01-01T00:00:00Z" // Golang zero value
	RFC3339ZeroUnix    = "1970-01-01T00:00:00Z"
	RFC3339YMDZeroUnix = int64(-62135596800)
)
View Source
const (
	LayoutNameDT4  = "dt4"
	LayoutNameDT6  = "dt6"
	LayoutNameDT8  = "dt8"
	LayoutNameDT14 = "dt14"
)

Variables

View Source
var FormatMap = map[string]string{
	"RFC3339":    time.RFC3339,
	"RFC3339YMD": RFC3339FullDate,
	"ISO8601YM":  ISO8601YM,
}
View Source
var ReferenceTimeValue time.Time = time.Date(2006, 1, 2, 15, 4, 5, 999999999, time.FixedZone("MST", -7*60*60))

Functions

func DayDifferenceHours

func DayDifferenceHours(startDate, endDate string) float64

DayDifferenceHours 两天之间相差了多少小时

func DaySecondsDifferenceHours

func DaySecondsDifferenceHours(startSecond, endSecond int64) float64

func DayTimeDifferenceHours

func DayTimeDifferenceHours(startDate, endDate time.Time) float64

func DurationHMS

func DurationHMS(d time.Duration) (int, int, int)

DurationHMS Extracts the hours, minutes, and seconds from the given duration.

func DurationToDurationpb

func DurationToDurationpb(duration *time.Duration) *durationpb.Duration

func DurationpbToDuration

func DurationpbToDuration(duration *durationpb.Duration) *time.Duration

func DurationpbToFloat

func DurationpbToFloat(duration *durationpb.Duration, timePrecision time.Duration) *float64

func DurationpbToNumber

func DurationpbToNumber[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](duration *durationpb.Duration, timePrecision time.Duration) *T

func DurationpbToSecond

func DurationpbToSecond(duration *durationpb.Duration) *float64

func DurationpbToString

func DurationpbToString(in *durationpb.Duration) *string

func Float64ToDurationpb

func Float64ToDurationpb(d float64) *durationpb.Duration

func FloatToDurationpb

func FloatToDurationpb(duration *float64, timePrecision time.Duration) *durationpb.Duration

func FormatTimeMulti

func FormatTimeMulti(dateFormat string, v any) string

FormatTimeMulti formats a `time.Time` object or an epoch number. It is adapted from `github.com/wcharczuk/go-chart`.

func FormatTimeToString

func FormatTimeToString(format string) func(time.Time) string

func FormatTimer

func FormatTimer(d time.Duration) string

FormatTimer Formats the given duration in a colon-separated timer format in the form [HH:]MM:SS.

func FormatTimerf

func FormatTimerf(format string, d time.Duration) string

FormatTimerf Formats the given duration using the given format string. The string follows the same formatting rules as described in the fmt package, and will receive three integer arguments: hours, minutes, and seconds.

func FromTo

func FromTo(value, fromLayout, toLayout string) (string, error)

Reformat a time string from one format to another Deprecated...

func FromTo2

func FromTo2(fromLayout, toLayout, value string) (string, error)

Reformat a time string from one format to another

func FromToFirstValue

func FromToFirstValue(fromLayout, toLayout string, values []string) (string, error)

func FromToFirstValueOrEmpty

func FromToFirstValueOrEmpty(fromLayout, toLayout string, values []string) string

func GetCurrentMonthRangeDateString

func GetCurrentMonthRangeDateString() (string, string)

GetCurrentMonthRangeDateString 获取区间时间内的日期字符串(比如:2023-05-23 2023-05-23) - 本月

func GetCurrentMonthRangeTime

func GetCurrentMonthRangeTime() (time.Time, time.Time)

GetCurrentMonthRangeTime 获取区间时间 - 本月

func GetCurrentMonthRangeTimeString

func GetCurrentMonthRangeTimeString() (string, string)

GetCurrentMonthRangeTimeString 获取区间时间内的时间字符串(比如:2023-05-23 00:00:00 2023-05-23 23:59:59) - 本月

func GetCurrentYearRangeDateString

func GetCurrentYearRangeDateString() (string, string)

GetCurrentYearRangeDateString 获取区间时间内的日期字符串(比如:2023-05-23 2023-05-23) - 今年

func GetCurrentYearRangeTime

func GetCurrentYearRangeTime() (time.Time, time.Time)

GetCurrentYearRangeTime 获取区间时间 - 今年

func GetCurrentYearRangeTimeString

func GetCurrentYearRangeTimeString() (string, string)

GetCurrentYearRangeTimeString 获取区间时间内的时间字符串(比如:2023-05-23 00:00:00 2023-05-23 23:59:59) - 今年

func GetDefaultTimeLocation

func GetDefaultTimeLocation() *time.Location

func GetFormat

func GetFormat(formatName string) (string, error)

func GetLastMonthRangeDateString

func GetLastMonthRangeDateString() (string, string)

GetLastMonthRangeDateString 获取区间时间内的日期字符串(比如:2023-05-23 2023-05-23) - 上个月

func GetLastMonthRangeTime

func GetLastMonthRangeTime() (time.Time, time.Time)

GetLastMonthRangeTime 获取区间时间 - 上个月

func GetLastMonthRangeTimeString

func GetLastMonthRangeTimeString() (string, string)

GetLastMonthRangeTimeString 获取区间时间内的时间字符串(比如:2023-05-23 00:00:00 2023-05-23 23:59:59) - 上个月

func GetLastYearRangeDateString

func GetLastYearRangeDateString() (string, string)

GetLastYearRangeDateString 获取区间时间内的日期字符串(比如:2023-05-23 2023-05-23) - 去年

func GetLastYearRangeTime

func GetLastYearRangeTime() (time.Time, time.Time)

GetLastYearRangeTime 获取区间时间 - 去年

func GetLastYearRangeTimeString

func GetLastYearRangeTimeString() (string, string)

GetLastYearRangeTimeString 获取区间时间内的时间字符串(比如:2023-05-23 00:00:00 2023-05-23 23:59:59) - 去年

func GetTodayRangeDateString

func GetTodayRangeDateString() (string, string)

GetTodayRangeDateString 获取区间时间内的日期字符串(比如:2023-05-23 2023-05-23) - 今天

func GetTodayRangeTime

func GetTodayRangeTime() (time.Time, time.Time)

GetTodayRangeTime 获取区间时间 - 今天

func GetTodayRangeTimeString

func GetTodayRangeTimeString() (string, string)

GetTodayRangeTimeString 获取区间时间内的时间字符串(比如:2023-05-23 00:00:00 2023-05-23 23:59:59) - 今天

func GetYesterdayRangeDateString

func GetYesterdayRangeDateString() (string, string)

GetYesterdayRangeDateString 获取区间时间内的日期字符串(比如:2023-05-23 2023-05-23) - 昨天

func GetYesterdayRangeTime

func GetYesterdayRangeTime() (time.Time, time.Time)

GetYesterdayRangeTime 获取区间时间 - 昨天

func GetYesterdayRangeTimeString

func GetYesterdayRangeTimeString() (string, string)

GetYesterdayRangeTimeString 获取区间时间内的时间字符串(比如:2023-05-23 00:00:00 2023-05-23 23:59:59) - 昨天

func IsDTX

func IsDTX(d int) (string, error)

IsDTX returns the dtx format if conformant to various DTX values (dt4, dt6, dt8, dt14).

func MicrosecondsToTimestamp

func MicrosecondsToTimestamp(microseconds *int64) *timestamppb.Timestamp

func MillisecondsToTimestamp

func MillisecondsToTimestamp(milliseconds *int64) *timestamppb.Timestamp

func NanosecondsToTimestamp

func NanosecondsToTimestamp(nanoseconds *int64) *timestamppb.Timestamp

NanosecondsToTimestamp 将纳秒转换为 timestamppb.Timestamp

func NumberToDurationpb

func NumberToDurationpb[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](duration *T, timePrecision time.Duration) *durationpb.Duration

func OffsetFormat

func OffsetFormat(offset int, useColon, useZ bool) string

OffsetFormat converts an integer offset value to a string value to use in string time formats. Note: RFC-3339 times use colons and the UTC "Z" offset.

func ParseFirst

func ParseFirst(layouts []string, value string) (time.Time, error)

ParseFirst attempts to parse a string with a set of layouts.

func ParseFirstValue

func ParseFirstValue(layout string, values []string) (time.Time, error)

func ParseFirstValueOrZero

func ParseFirstValueOrZero(layout string, values []string) time.Time

func ParseOrZero

func ParseOrZero(layout, value string) time.Time

ParseOrZero returns a parsed time.Time or the RFC-3339 zero time.

func ParseSlice

func ParseSlice(layout string, strings []string) ([]time.Time, error)

func ParseTimeSQLTimestampUsingOffset

func ParseTimeSQLTimestampUsingOffset(timeStr string, offset int) (time.Time, error)

ParseTimeSQLTimestampUsingOffset converts a SQL timestamp without timezone adding in a manual integer timezone.

func ParseTimeUsingOffset

func ParseTimeUsingOffset(format, raw, sep string, offset int, useColon, useZ bool) (time.Time, error)

func RangeStringDateToTime

func RangeStringDateToTime(startDate, endDate string) (time.Time, time.Time)

func ReferenceTime

func ReferenceTime() time.Time

ReferenceTime Return the standard Golang reference time (2006-01-02T15:04:05.999999999Z07:00)

func RefreshDefaultTimeLocation

func RefreshDefaultTimeLocation(name string) *time.Location

func SecondToDurationpb

func SecondToDurationpb(seconds *float64) *durationpb.Duration

func SecondsDifferenceDays

func SecondsDifferenceDays(startSecond, endSecond int64) int

SecondsDifferenceDays 两天之间相差了多少天

func SecondsToTimestamp

func SecondsToTimestamp(seconds *int64) *timestamppb.Timestamp

SecondsToTimestamp 将秒转换为 timestamppb.Timestamp

func StringDateToTime

func StringDateToTime(str *string) *time.Time

StringDateToTime 字符串 -> 时间

func StringDifferenceDays

func StringDifferenceDays(startDate, endDate string) int

StringDifferenceDays 两天之间相差了多少天

func StringTimeToTime

func StringTimeToTime(str *string) *time.Time

StringTimeToTime 时间字符串 -> 时间

func StringToDurationpb

func StringToDurationpb(in *string) *durationpb.Duration

func StringToTime

func StringToTime(str *string, layout string) *time.Time

func StringToUnixMilliInt64Ptr

func StringToUnixMilliInt64Ptr(tm *string) *int64

StringToUnixMilliInt64Ptr 字符串 -> 毫秒时间戳

func TimeDifferenceDays

func TimeDifferenceDays(startDate, endDate time.Time) int

TimeDifferenceDays 两天之间相差了多少天

func TimeMinRFC3339

func TimeMinRFC3339() time.Time

func TimeToDateString

func TimeToDateString(tm *time.Time) *string

TimeToDateString 时间 -> 日期字符串

func TimeToString

func TimeToString(tm *time.Time, layout string) *string

func TimeToTimeString

func TimeToTimeString(tm *time.Time) *string

TimeToTimeString 时间 -> 时间字符串

func TimeToTimestamppb

func TimeToTimestamppb(tm *time.Time) *timestamppb.Timestamp

TimeToTimestamppb time.Time -> timestamppb.Timestamp

func TimeToUnixMilliInt64Ptr

func TimeToUnixMilliInt64Ptr(tm *time.Time) *int64

TimeToUnixMilliInt64Ptr 时间 -> 毫秒时间戳

func TimeToUnixSecondInt64Ptr

func TimeToUnixSecondInt64Ptr(tm *time.Time) *int64

TimeToUnixSecondInt64Ptr 时间 -> 秒时间戳

func TimeZeroRFC3339

func TimeZeroRFC3339() time.Time

func TimeZeroUnix

func TimeZeroUnix() time.Time

func TimestampToMicroseconds

func TimestampToMicroseconds(ts *timestamppb.Timestamp) int64

func TimestampToMilliseconds

func TimestampToMilliseconds(ts *timestamppb.Timestamp) int64

func TimestampToNanoseconds

func TimestampToNanoseconds(ts *timestamppb.Timestamp) int64

TimestampToNanoseconds 将 timestamppb.Timestamp 转换为纳秒

func TimestampToSeconds

func TimestampToSeconds(ts *timestamppb.Timestamp) int64

TimestampToSeconds 将 timestamppb.Timestamp 转换为秒

func TimestamppbToTime

func TimestamppbToTime(timestamp *timestamppb.Timestamp) *time.Time

TimestamppbToTime timestamppb.Timestamp -> time.Time

func UnixMilliToStringPtr

func UnixMilliToStringPtr(milli *int64) *string

UnixMilliToStringPtr 毫秒时间戳 -> 字符串

func UnixMilliToTimePtr

func UnixMilliToTimePtr(milli *int64) *time.Time

UnixMilliToTimePtr 毫秒时间戳 -> 时间

func UnixSecondToTimePtr

func UnixSecondToTimePtr(second *int64) *time.Time

UnixSecondToTimePtr 秒时间戳 -> 时间

Types

type ISO8601NoTzMilliTime

type ISO8601NoTzMilliTime struct{ time.Time }

func (ISO8601NoTzMilliTime) MarshalJSON

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

func (*ISO8601NoTzMilliTime) UnmarshalJSON

func (t *ISO8601NoTzMilliTime) UnmarshalJSON(buf []byte) error

type RFC3339YMDTime

type RFC3339YMDTime struct{ time.Time }

func (RFC3339YMDTime) MarshalJSON

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

func (*RFC3339YMDTime) UnmarshalJSON

func (t *RFC3339YMDTime) UnmarshalJSON(buf []byte) error

Jump to

Keyboard shortcuts

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