golang timestamp, date string, time type data conversion

golang timestamp, date string, time type data conversion

time to date string

const (
YearMonthDay = “2006-01-02”
HourMinuteSecond = “15:04:05”
DefaultLayout = YearMonthDay + ” ” + HourMinuteSecond
)

// time is converted to a date string in the default format
func TimeToDefaultStr(t time.Time) string {
return TimeToStr(t, DefaultLayout)
}

// convert time to date string in specified format
func TimeToStr(t time.Time, layout string) string {
return t.Format(layout)
}

time to timestamp

// time is converted to a second timestamp
func TimeToUnixSecond(t time.Time) int64 {
return t.Unix()
}

// convert time to millisecond timestamp
func TimeToUnixMilli(t time.Time) int64 {
return TimeToUnixNano(t) / 1000
}

// convert time to nanosecond timestamp
func TimeToUnixNano(t time.Time) int64 {
return t.UnixNano()
}

Timestamp to time

// second timestamp to time
func UnixSecondToTime(second int64) time.Time {
return time.Unix(second, 0)
}

// Millisecond timestamp to time
func UnixMilliToTime(milli int64) time.Time {
return time.Unix(milli/1000, (milli%1000)(10001000))
}

// nanosecond timestamp to time
func UnixNanoToTime(nano int64) time.Time {
return time.Unix(nano/(100010001000), nano%(100010001000))
}

Timestamp to date string

const (
YearMonthDay = “2006-01-02”
HourMinuteSecond = “15:04:05”
DefaultLayout = YearMonthDay + ” ” + HourMinuteSecond
)

// Second-level timestamp to default format date string
func UnixSecondToDefaultTimeStr(second int64) string {
return UnixSecondToTimeStr(second, DefaultLayout)
}

// Millisecond timestamp to default format date string
func UnixMilliToDefaultTimeStr(milli int64) string {
return UnixMilliToTimeStr(milli, DefaultLayout)
}

// Convert nanosecond timestamp to default format date string
func UnixNanoToDefaultTimeStr(nano int64) string {
return UnixNanoToTimeStr(nano, DefaultLayout)
}

// Convert second-level timestamp to date string in specified format
func UnixSecondToTimeStr(second int64, layout string) string {
return TimeToStr(UnixSecondToTime(second), layout)
}

// Convert millisecond timestamp to date string in specified format
func UnixMilliToTimeStr(second int64, layout string) string {
return TimeToStr(UnixMilliToTime(second), layout)
}

// Convert nanosecond timestamp to date string in specified format
func UnixNanoToTimeStr(nano int64, layout string) string {
return TimeToStr(UnixNanoToTime(nano), layout)
}

date string to time

const (
YearMonthDay = “2006-01-02”
HourMinuteSecond = “15:04:05”
DefaultLayout = YearMonthDay + ” ” + HourMinuteSecond
)

// Default format date string to time
func TimeStrToTimeDefault(str string) time.Time {
parseTime, _ := time.ParseInLocation(DefaultLayout, str, time.Local)
return parseTime
}

// Specify the format date string to time
func TimeStrToTime(str, layout string) time.Time {
parseTime, _ := time.ParseInLocation(layout, str, time.Local)
return parseTime
}

date string to timestamp

const (
YearMonthDay = “2006-01-02”
HourMinuteSecond = “15:04:05”
DefaultLayout = YearMonthDay + ” ” + HourMinuteSecond
)

// Default format date string to seconds timestamp
func TimeStrToUnixSecondDefault(str string) int64 {
return TimeStrToTimeDefault(str).Unix()
}

// Default format date string to millisecond timestamp
func TimeStrToUnixMilliDefault(str string) int64 {
return TimeStrToUnixNanoDefault(str) / 1000
}

// Default format date string to nanosecond timestamp
func TimeStrToUnixNanoDefault(str string) int64 {
return TimeStrToTimeDefault(str).UnixNano()
}

// Specify the format date string to seconds timestamp
func TimeStrToUnixSecond(str, layout string) int64 {
return TimeStrToTime(str, layout).Unix()
}

// Convert the specified format date string to millisecond timestamp
func TimeStrToUnixMilli(str, layout string) int64 {
return TimeStrToUnixNano(str, layout) / (1000 * 1000)
}

// Convert date string in specified format to nanosecond timestamp
func TimeStrToUnixNano(str, layout string) int64 {
return TimeStrToTime(str, layout).UnixNano()
}

// Default format date string to time
func TimeStrToTimeDefault(str string) time.Time {
parseTime, _ := time.ParseInLocation(DefaultLayout, str, time.Local)
return parseTime
}

// Specify the format date string to time
func TimeStrToTime(str, layout string) time.Time {
parseTime, _ := time.ParseInLocation(layout, str, time.Local)
return parseTime
}

Leave a Reply

Your email address will not be published.

en_USEnglish