You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gotime/today.go

142 lines
3.0 KiB

2 years ago
package gotime
import (
"log"
"strconv"
"strings"
"time"
)
// Current 获取当前的时间
func Current() Pro {
var err error
2 years ago
p := NewPro()
p.loc, err = time.LoadLocation("Asia/Shanghai")
if err != nil {
log.Printf("【gotime】时区错误%v\n", err)
2 years ago
p.Time = time.Now().Add(time.Hour * 8)
} else {
p.Time = time.Now().In(p.loc)
}
2 years ago
return p
}
// SetCurrent 设置当前的时间
func SetCurrent(sTime time.Time) Pro {
p := NewPro()
p.Time = sTime
return p
}
// SetCurrentParse 设置当前的时间
func SetCurrentParse(str string) Pro {
var err error
2 years ago
p := NewPro()
p.loc, err = time.LoadLocation("Asia/Shanghai")
if err != nil {
log.Printf("【gotime】时区错误%v\n", err)
p.Time = time.Now().Add(time.Hour * 8)
}
2 years ago
layout := DateTimeFormat
if str == "" || str == "0" || str == "0000-00-00 00:00:00" || str == "0000-00-00" || str == "00:00:00" {
return p
}
if len(str) == 10 && strings.Count(str, "-") == 2 {
layout = DateFormat
}
if strings.Index(str, "T") == 10 {
layout = RFC3339Format
}
if _, err := strconv.ParseInt(str, 10, 64); err == nil {
switch len(str) {
case 8:
layout = ShortDateFormat
case 14:
layout = ShortDateTimeFormat
}
}
location, _ := time.ParseInLocation(layout, str, p.loc)
p.Time = location
return p
}
// SetCurrentUnix 设置当前的时间 Unix时间戳
func SetCurrentUnix(ts int64) Pro {
p := NewPro()
p.Time = time.Unix(ts, 0)
return p
}
// SetCurrentMillisecondUnix 设置当前的时间 毫秒Unix时间戳
func SetCurrentMillisecondUnix(ts int64) Pro {
p := NewPro()
p.Time = time.Unix(ts/1000, 0)
return p
}
2 years ago
// Now 今天此刻
func (p Pro) Now() time.Time {
return p.Time
}
// Format 今天此刻格式化
func (p Pro) Format() string {
return p.Time.Format(DateTimeFormat)
}
// FormatFilter 今天此刻格式化 带 过滤无效时间
func (p Pro) FormatFilter() string {
if p.Time.Format(DateTimeFormat) == "0001-01-01 00:00:00" || p.Time.Format(DateTimeFormat) == "0001-01-01 08:05:43" {
return ""
} else {
return p.Time.Format(DateTimeFormat)
}
}
2 years ago
// ToDateFormat 今天此刻日期
func (p Pro) ToDateFormat() string {
return p.Time.Format(DateFormat)
}
// ToDateFormatTime 今天此刻日期
func (p Pro) ToDateFormatTime() time.Time {
return SetCurrentParse(p.Time.Format(DateFormat)).Time
}
2 years ago
// ToTimeFormat 今天此刻时间
func (p Pro) ToTimeFormat() string {
return p.Time.Format(TimeFormat)
}
// Timestamp 今天此刻时间戳
func (p Pro) Timestamp() int64 {
return p.Time.Unix()
}
// TimestampWithSecond 今天此刻时间戳
func (p Pro) TimestampWithSecond() int64 {
return p.Time.Unix()
}
// TimestampWithMillisecond 今天毫秒级时间戳
func (p Pro) TimestampWithMillisecond() int64 {
return p.Time.UnixNano() / int64(time.Millisecond)
}
// TimestampWithMicrosecond 今天微秒级时间戳
func (p Pro) TimestampWithMicrosecond() int64 {
return p.Time.UnixNano() / int64(time.Microsecond)
}
// TimestampWithNanosecond 今天纳秒级时间戳
func (p Pro) TimestampWithNanosecond() int64 {
return p.Time.UnixNano()
}