增加vendor
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
package xtime
|
||||
|
||||
const (
|
||||
TimeLayout = "2006-01-02 15:04:05"
|
||||
TimeLayout_1 = "2006-01-02 15:04:05.000"
|
||||
TimeLayout_2 = "20060102150405"
|
||||
DateLayout = "2006-01-02"
|
||||
)
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package xtime
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var daysBefore = [...]int32{
|
||||
0,
|
||||
31,
|
||||
31 + 28,
|
||||
31 + 28 + 31,
|
||||
31 + 28 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
|
||||
}
|
||||
|
||||
func MonthDays(m time.Month, year int) int {
|
||||
if m == time.February && isLeap(year) {
|
||||
return 29
|
||||
}
|
||||
return int(daysBefore[m] - daysBefore[m-1])
|
||||
}
|
||||
|
||||
func isLeap(year int) bool {
|
||||
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||
}
|
||||
|
||||
// 获取最近 7天 日期
|
||||
func GetRecentSevenDay() (sevenDays []string) {
|
||||
now := time.Now()
|
||||
nowDay := time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local)
|
||||
for i := 0; i < 7; i++ {
|
||||
date := nowDay.AddDate(0, 0, i)
|
||||
sevenDays = append(sevenDays, date.Format(DateLayout))
|
||||
}
|
||||
return sevenDays
|
||||
}
|
||||
|
||||
// 获取最近 30天 日期
|
||||
func GetRecentThirtyDay() (thirtyDays []string) {
|
||||
now := time.Now()
|
||||
nowDay := time.Date(now.Year(), now.Month(), now.Day()-30, 0, 0, 0, 0, time.Local)
|
||||
for i := 0; i < 30; i++ {
|
||||
date := nowDay.AddDate(0, 0, i)
|
||||
thirtyDays = append(thirtyDays, date.Format(DateLayout))
|
||||
}
|
||||
return thirtyDays
|
||||
}
|
||||
|
||||
// 获取本周 7天 日期
|
||||
func GetCurWeekDays() (curWeekDays []string) {
|
||||
now := time.Now()
|
||||
offset := int(time.Monday - now.Weekday())
|
||||
if offset > 0 {
|
||||
offset = -6
|
||||
}
|
||||
weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
|
||||
for i := 0; i < 7; i++ {
|
||||
date := weekStartDate.AddDate(0, 0, i)
|
||||
curWeekDays = append(curWeekDays, date.Format(DateLayout))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 获取本月 日期
|
||||
func GetCurMonthDays() (curMonthDays []string) {
|
||||
now := time.Now()
|
||||
year := now.Year()
|
||||
month := now.Month()
|
||||
days := MonthDays(month, year)
|
||||
|
||||
monthFirstDay := time.Date(year, month, 01, 0, 0, 0, 0, time.Local)
|
||||
for i := 0; i < days; i++ {
|
||||
date := monthFirstDay.AddDate(0, 0, i)
|
||||
curMonthDays = append(curMonthDays, date.Format(DateLayout))
|
||||
}
|
||||
return curMonthDays
|
||||
}
|
||||
|
||||
// 获取上一个月 日期
|
||||
func GetLastMonthDays() (monthDays []string) {
|
||||
now := time.Now()
|
||||
year := now.Year()
|
||||
month := now.Month()
|
||||
days := MonthDays(month-1, year)
|
||||
|
||||
monthFirstDay := time.Date(year, month-1, 01, 0, 0, 0, 0, time.Local)
|
||||
for i := 0; i < days; i++ {
|
||||
date := monthFirstDay.AddDate(0, 0, i)
|
||||
monthDays = append(monthDays, date.Format(DateLayout))
|
||||
}
|
||||
return monthDays
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package xtime
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-pay/gopay/pkg/util"
|
||||
)
|
||||
|
||||
//解析时间
|
||||
// 时间字符串格式:2006-01-02 15:04:05
|
||||
func ParseDateTime(timeStr string) (datetime time.Time) {
|
||||
datetime, _ = time.ParseInLocation(TimeLayout, timeStr, time.Local)
|
||||
return
|
||||
}
|
||||
|
||||
//解析日期
|
||||
// 日期字符串格式:2006-01-02
|
||||
func ParseDate(timeStr string) (date time.Time) {
|
||||
date, _ = time.ParseInLocation(DateLayout, timeStr, time.Local)
|
||||
return
|
||||
}
|
||||
|
||||
//格式化Datetime字符串
|
||||
// 格式化前输入样式:2019-01-04T15:40:00Z 或 2019-01-04T15:40:00+08:00
|
||||
// 格式化后返回样式:2019-01-04 15:40:00
|
||||
func FormatDateTime(timeStr string) (formatTime string) {
|
||||
if timeStr == "" {
|
||||
return ""
|
||||
}
|
||||
replace := strings.Replace(timeStr, "T", " ", 1)
|
||||
formatTime = replace[:19]
|
||||
return
|
||||
}
|
||||
|
||||
//格式化Date成字符串
|
||||
// 格式化前输入样式:2019-01-04T15:40:00Z 或 2019-01-04T15:40:00+08:00
|
||||
// 格式化后返回样式:2019-01-04
|
||||
func FormatDate(dateStr string) (formatDate string) {
|
||||
if dateStr == "" {
|
||||
return ""
|
||||
}
|
||||
split := strings.Split(dateStr, "T")
|
||||
formatDate = split[0]
|
||||
return
|
||||
}
|
||||
|
||||
func DurationToUnit(duration time.Duration) string {
|
||||
var (
|
||||
t string
|
||||
intNs = int64(duration)
|
||||
)
|
||||
if intNs >= 0 && intNs < int64(time.Second) {
|
||||
t = util.Int642String(intNs/int64(time.Millisecond)) + "ms"
|
||||
}
|
||||
|
||||
// 大于等于 1秒,小于 1分钟
|
||||
if intNs >= int64(time.Second) && intNs < int64(time.Minute) {
|
||||
s := intNs / int64(time.Second)
|
||||
ms := (intNs - s*int64(time.Second)) / int64(time.Millisecond)
|
||||
t = util.Int642String(s) + "s"
|
||||
if ms > 0 {
|
||||
t += util.Int642String(ms) + "ms"
|
||||
}
|
||||
}
|
||||
// 大于等于 1分钟,小于 1小时
|
||||
if intNs >= int64(time.Minute) && intNs < int64(time.Hour) {
|
||||
m := intNs / int64(time.Minute)
|
||||
s := (intNs - m*int64(time.Minute)) / int64(time.Second)
|
||||
t = util.Int642String(m) + "m"
|
||||
if s > 0 {
|
||||
t += util.Int642String(s) + "s"
|
||||
}
|
||||
}
|
||||
// 大于等于 1小时,小于 1天
|
||||
if intNs >= int64(time.Hour) && intNs < 24*int64(time.Hour) {
|
||||
h := intNs / int64(time.Hour)
|
||||
m := (intNs - h*int64(time.Hour)) / int64(time.Minute)
|
||||
s := (intNs - h*int64(time.Hour) - m*int64(time.Minute)) / int64(time.Second)
|
||||
t = util.Int642String(h) + "h"
|
||||
if m > 0 {
|
||||
t += util.Int642String(m) + "m"
|
||||
}
|
||||
if s > 0 {
|
||||
t += util.Int642String(s) + "s"
|
||||
}
|
||||
}
|
||||
// 大于等于 1天
|
||||
if intNs >= 24*int64(time.Hour) {
|
||||
d := intNs / (24 * int64(time.Hour))
|
||||
h := (intNs - d*24*int64(time.Hour)) / int64(time.Hour)
|
||||
m := (intNs - d*24*int64(time.Hour) - h*int64(time.Hour)) / int64(time.Minute)
|
||||
s := ((intNs - m*int64(time.Minute)) % int64(time.Minute)) / int64(time.Second)
|
||||
|
||||
t = util.Int642String(d) + "d"
|
||||
if h > 0 {
|
||||
t += util.Int642String(h) + "h"
|
||||
}
|
||||
if m > 0 {
|
||||
t += util.Int642String(m) + "m"
|
||||
}
|
||||
if s > 0 {
|
||||
t += util.Int642String(s) + "s"
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package xtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Time be used to MySql timestamp converting.
|
||||
type Time int64
|
||||
|
||||
// Scan scan time.
|
||||
func (t *Time) Scan(src interface{}) (err error) {
|
||||
switch sc := src.(type) {
|
||||
case time.Time:
|
||||
if sc.IsZero() {
|
||||
return
|
||||
}
|
||||
*t = Time(sc.Unix())
|
||||
case string:
|
||||
var i int64
|
||||
i, err = strconv.ParseInt(sc, 10, 64)
|
||||
*t = Time(i)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Value get time value.
|
||||
func (t Time) Value() (driver.Value, error) {
|
||||
return time.Unix(int64(t), 0), nil
|
||||
}
|
||||
|
||||
// Time get time.
|
||||
func (t Time) Time() time.Time {
|
||||
return time.Unix(int64(t), 0)
|
||||
}
|
||||
|
||||
func (t *Time) FromDB(bs []byte) error {
|
||||
timeStr := string(bs)
|
||||
ti, err := time.ParseInLocation("2006-01-02T15:04:05", timeStr[:19], time.Local)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ti.IsZero() {
|
||||
return nil
|
||||
}
|
||||
*t = Time(ti.Unix())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Time) ToDB() ([]byte, error) {
|
||||
unix := time.Unix(int64(t), 0)
|
||||
return []byte(unix.String()), nil
|
||||
}
|
||||
|
||||
// Duration be used json unmarshal string time, like 1s, 500ms.
|
||||
type Duration time.Duration
|
||||
|
||||
// UnmarshalText unmarshal text to duration.
|
||||
func (d *Duration) UnmarshalText(text []byte) error {
|
||||
tmp, err := time.ParseDuration(string(text))
|
||||
if err == nil {
|
||||
*d = Duration(tmp)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnitTime duration parse to unit, such as "300ms", "1h30m" or "2h10s".
|
||||
func (d *Duration) UnitTime() string {
|
||||
return DurationToUnit(time.Duration(*d))
|
||||
}
|
||||
|
||||
// Shrink will decrease the duration by comparing with context's timeout duration and return new timeout\context\CancelFunc.
|
||||
func (d Duration) Shrink(c context.Context) (Duration, context.Context, context.CancelFunc) {
|
||||
if deadline, ok := c.Deadline(); ok {
|
||||
if ctimeout := time.Until(deadline); ctimeout < time.Duration(d) {
|
||||
// deliver small timeout
|
||||
return Duration(ctimeout), c, func() {}
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c, time.Duration(d))
|
||||
return d, ctx, cancel
|
||||
}
|
||||
Reference in New Issue
Block a user