65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package stime
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func GetTimestampMillisecond() int64 {
|
|
now := time.Now()
|
|
return now.UnixNano() / 1e6
|
|
}
|
|
|
|
func StringToTime(strTime string) (*time.Time, error) {
|
|
const TIME_LAYOUT = "2006-01-02 15:04:05" //此时间不可更改
|
|
timeobj, err := time.ParseInLocation(TIME_LAYOUT, strTime, Loc.Shanghai())
|
|
return &timeobj, err
|
|
}
|
|
|
|
func StringToTimeWithFormat(strTime string, timeFormat string) (*time.Time, error) {
|
|
timeobj, err := time.ParseInLocation(timeFormat, strTime, Loc.Shanghai())
|
|
return &timeobj, err
|
|
}
|
|
|
|
// 去除精确时间后面的小数点
|
|
func NowTimeToTime(layout string) *time.Time {
|
|
otime := time.Now().Format(layout) //"2006-01-02 15:04:05" and so on
|
|
tt, _ := StringToTime(otime)
|
|
return tt
|
|
}
|
|
|
|
// 时间之间的转换
|
|
func TimeStampToString(value interface{}, after_type string) {
|
|
switch value.(type) {
|
|
case string:
|
|
fmt.Println(value.(string))
|
|
case int64:
|
|
fmt.Println(value.(int64))
|
|
case int32:
|
|
fmt.Println(value.(int32))
|
|
}
|
|
}
|
|
|
|
func GetAge(birthday time.Time) int {
|
|
if birthday.IsZero() {
|
|
return 0
|
|
}
|
|
now := time.Now()
|
|
year, month, day := now.Date()
|
|
if year == 0 || month == 0 || day == 0 {
|
|
return 0
|
|
}
|
|
age := year - birthday.Year() - 1
|
|
//判断年龄
|
|
if birthday.Month() < month || birthday.Month() == month && birthday.Day() <= day {
|
|
age++
|
|
}
|
|
return age
|
|
}
|
|
func ParseTimeStamp(unix int64) (result time.Time) {
|
|
if unix == 0 {
|
|
return
|
|
}
|
|
return time.Unix(unix, 0)
|
|
}
|