|
@@ -22,7 +22,7 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-//随机数种子
|
|
|
+// 随机数种子
|
|
|
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
func GetRandString(size int) string {
|
|
@@ -59,13 +59,13 @@ func StringsToJSON(str string) string {
|
|
|
return jsons
|
|
|
}
|
|
|
|
|
|
-//序列化
|
|
|
+// 序列化
|
|
|
func ToString(v interface{}) string {
|
|
|
data, _ := json.Marshal(v)
|
|
|
return string(data)
|
|
|
}
|
|
|
|
|
|
-//md5加密
|
|
|
+// md5加密
|
|
|
func MD5(data string) string {
|
|
|
m := md5.Sum([]byte(data))
|
|
|
return hex.EncodeToString(m[:])
|
|
@@ -93,7 +93,7 @@ func GetToday(format string) string {
|
|
|
return today
|
|
|
}
|
|
|
|
|
|
-//获取今天剩余秒数
|
|
|
+// 获取今天剩余秒数
|
|
|
func GetTodayLastSecond() time.Duration {
|
|
|
today := GetToday(FormatDate) + " 23:59:59"
|
|
|
end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
|
|
@@ -115,7 +115,7 @@ func GetBrithDate(idcard string) string {
|
|
|
return GetToday(FormatDate)
|
|
|
}
|
|
|
|
|
|
-//处理性别
|
|
|
+// 处理性别
|
|
|
func WhichSexByIdcard(idcard string) string {
|
|
|
var sexs = [2]string{"女", "男"}
|
|
|
length := len(idcard)
|
|
@@ -129,7 +129,7 @@ func WhichSexByIdcard(idcard string) string {
|
|
|
return "男"
|
|
|
}
|
|
|
|
|
|
-//截取小数点后几位
|
|
|
+// 截取小数点后几位
|
|
|
func SubFloatToString(f float64, m int) string {
|
|
|
n := strconv.FormatFloat(f, 'f', -1, 64)
|
|
|
if n == "" {
|
|
@@ -148,14 +148,14 @@ func SubFloatToString(f float64, m int) string {
|
|
|
return newn[0] + "." + newn[1][:m]
|
|
|
}
|
|
|
|
|
|
-//截取小数点后几位
|
|
|
+// 截取小数点后几位
|
|
|
func SubFloatToFloat(f float64, m int) float64 {
|
|
|
newn := SubFloatToString(f, m)
|
|
|
newf, _ := strconv.ParseFloat(newn, 64)
|
|
|
return newf
|
|
|
}
|
|
|
|
|
|
-//获取相差时间-年
|
|
|
+// 获取相差时间-年
|
|
|
func GetYearDiffer(start_time, end_time string) int {
|
|
|
t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
|
|
|
t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
|
|
@@ -166,7 +166,7 @@ func GetYearDiffer(start_time, end_time string) int {
|
|
|
return age
|
|
|
}
|
|
|
|
|
|
-//获取相差时间-秒
|
|
|
+// 获取相差时间-秒
|
|
|
func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
|
|
|
diff := end_time.Unix() - start_time.Unix()
|
|
|
return diff
|
|
@@ -193,7 +193,7 @@ func StrListToString(strList []string) (str string) {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
-//Token
|
|
|
+// Token
|
|
|
func GetToken() string {
|
|
|
randStr := GetRandString(64)
|
|
|
token := MD5(randStr + Md5Key)
|
|
@@ -201,30 +201,30 @@ func GetToken() string {
|
|
|
return strings.ToUpper(token + GetRandString(tokenLen))
|
|
|
}
|
|
|
|
|
|
-//数据没有记录
|
|
|
+// 数据没有记录
|
|
|
func ErrNoRow() string {
|
|
|
return "<QuerySeter> no row found"
|
|
|
}
|
|
|
|
|
|
-//校验邮箱格式
|
|
|
+// 校验邮箱格式
|
|
|
func ValidateEmailFormatat(email string) bool {
|
|
|
reg := regexp.MustCompile(RegularEmail)
|
|
|
return reg.MatchString(email)
|
|
|
}
|
|
|
|
|
|
-//验证是否是手机号
|
|
|
+// 验证是否是手机号
|
|
|
func ValidateMobileFormatat(mobileNum string) bool {
|
|
|
reg := regexp.MustCompile(RegularMobile)
|
|
|
return reg.MatchString(mobileNum)
|
|
|
}
|
|
|
|
|
|
-//判断文件是否存在
|
|
|
+// 判断文件是否存在
|
|
|
func FileIsExist(filePath string) bool {
|
|
|
_, err := os.Stat(filePath)
|
|
|
return err == nil || os.IsExist(err)
|
|
|
}
|
|
|
|
|
|
-//获取图片扩展名
|
|
|
+// 获取图片扩展名
|
|
|
func GetImgExt(file string) (ext string, err error) {
|
|
|
var headerByte []byte
|
|
|
headerByte = make([]byte, 8)
|
|
@@ -267,7 +267,7 @@ func GetImgExt(file string) (ext string, err error) {
|
|
|
return ext, nil
|
|
|
}
|
|
|
|
|
|
-//保存图片
|
|
|
+// 保存图片
|
|
|
func SaveImage(path string, img image.Image) (err error) {
|
|
|
//需要保持的文件
|
|
|
imgfile, err := os.Create(path)
|
|
@@ -277,7 +277,7 @@ func SaveImage(path string, img image.Image) (err error) {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-//保存base64数据为文件
|
|
|
+// 保存base64数据为文件
|
|
|
func SaveBase64ToFile(content, path string) error {
|
|
|
data, err := base64.StdEncoding.DecodeString(content)
|
|
|
if err != nil {
|
|
@@ -407,7 +407,7 @@ func GetWilsonScore(p, n float64) float64 {
|
|
|
return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
|
|
|
}
|
|
|
|
|
|
-//将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
|
|
|
+// 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
|
|
|
func ChangeWordsToNum(str string) (numStr string) {
|
|
|
words := ([]rune)(str)
|
|
|
num := 0
|
|
@@ -554,7 +554,7 @@ func ConvertToFormatDay(excelDaysString string) string {
|
|
|
return resultTime
|
|
|
}
|
|
|
|
|
|
-//人民币小写转大写
|
|
|
+// 人民币小写转大写
|
|
|
func ConvertNumToCny(num float64) (str string, err error) {
|
|
|
strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
|
|
|
sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
|
|
@@ -735,7 +735,7 @@ func Implode(idIntList []int) (str string) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//字符串类型时间转周几
|
|
|
+// 字符串类型时间转周几
|
|
|
func StrDateTimeToWeek(strTime string) string {
|
|
|
var WeekDayMap = map[string]string{
|
|
|
"Monday": "周一",
|
|
@@ -752,7 +752,7 @@ func StrDateTimeToWeek(strTime string) string {
|
|
|
return WeekDayMap[staweek_int]
|
|
|
}
|
|
|
|
|
|
-//字符串转换为time
|
|
|
+// 字符串转换为time
|
|
|
func StrTimeToTime(strTime string) time.Time {
|
|
|
timeLayout := "2006-01-02 15:04:05" //转化所需模板
|
|
|
loc, _ := time.LoadLocation("Local") //重要:获取时区
|
|
@@ -813,4 +813,133 @@ func JoinStr2IntArr(str, sep string) (arr []int) {
|
|
|
arr = append(arr, v)
|
|
|
}
|
|
|
return
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+// GetNextWeekMonday 获取下一周周一的时间
|
|
|
+func GetNextWeekMonday() time.Time {
|
|
|
+ nowMonday := GetNowWeekMonday()
|
|
|
+ return nowMonday.AddDate(0, 0, 7)
|
|
|
+}
|
|
|
+
|
|
|
+// GetNextWeekLastDay 获取下一周最后一天的时间
|
|
|
+func GetNextWeekLastDay() time.Time {
|
|
|
+ nowSunday := GetNowWeekLastDay()
|
|
|
+ return nowSunday.AddDate(0, 0, 7)
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowWeekMonday 获取本周周一的时间
|
|
|
+func GetNowWeekMonday() time.Time {
|
|
|
+ offset := int(time.Monday - time.Now().Weekday())
|
|
|
+ if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
|
|
|
+ offset = -6
|
|
|
+ }
|
|
|
+ mondayTime := time.Now().AddDate(0, 0, offset)
|
|
|
+ mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
|
|
|
+ return mondayTime
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowWeekLastDay 获取本周最后一天的时间
|
|
|
+func GetNowWeekLastDay() time.Time {
|
|
|
+ offset := int(time.Monday - time.Now().Weekday())
|
|
|
+ if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
|
|
|
+ offset = -6
|
|
|
+ }
|
|
|
+ firstDayTime := time.Now().AddDate(0, 0, offset)
|
|
|
+ firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
|
|
|
+ lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
|
|
|
+
|
|
|
+ return lastDayTime
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowMonthFirstDay 获取本月第一天的时间
|
|
|
+func GetNowMonthFirstDay() time.Time {
|
|
|
+ nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ return nowMonthFirstDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowMonthLastDay 获取本月最后一天的时间
|
|
|
+func GetNowMonthLastDay() time.Time {
|
|
|
+ nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
|
|
|
+ nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
|
|
|
+ return nowMonthLastDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowQuarterFirstDay 获取本季度第一天的时间
|
|
|
+func GetNowQuarterFirstDay() time.Time {
|
|
|
+ month := int(time.Now().Month())
|
|
|
+ var nowQuarterFirstDay time.Time
|
|
|
+ if month >= 1 && month <= 3 {
|
|
|
+ //1月1号
|
|
|
+ nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ } else if month >= 4 && month <= 6 {
|
|
|
+ //4月1号
|
|
|
+ nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ } else if month >= 7 && month <= 9 {
|
|
|
+ nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ } else {
|
|
|
+ nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ }
|
|
|
+ return nowQuarterFirstDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowQuarterLastDay 获取本季度最后一天的时间
|
|
|
+func GetNowQuarterLastDay() time.Time {
|
|
|
+ month := int(time.Now().Month())
|
|
|
+ var nowQuarterLastDay time.Time
|
|
|
+ if month >= 1 && month <= 3 {
|
|
|
+ //03-31 23:59:59
|
|
|
+ nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ } else if month >= 4 && month <= 6 {
|
|
|
+ //06-30 23:59:59
|
|
|
+ nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ } else if month >= 7 && month <= 9 {
|
|
|
+ //09-30 23:59:59
|
|
|
+ nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ } else {
|
|
|
+ //12-31 23:59:59
|
|
|
+ nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ }
|
|
|
+ return nowQuarterLastDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowHalfYearFirstDay 获取当前半年的第一天的时间
|
|
|
+func GetNowHalfYearFirstDay() time.Time {
|
|
|
+ month := int(time.Now().Month())
|
|
|
+ var nowHalfYearLastDay time.Time
|
|
|
+ if month >= 1 && month <= 6 {
|
|
|
+ //03-31 23:59:59
|
|
|
+ nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ } else {
|
|
|
+ //12-31 23:59:59
|
|
|
+ nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ }
|
|
|
+ return nowHalfYearLastDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowHalfYearLastDay 获取当前半年的最后一天的时间
|
|
|
+func GetNowHalfYearLastDay() time.Time {
|
|
|
+ month := int(time.Now().Month())
|
|
|
+ var nowHalfYearLastDay time.Time
|
|
|
+ if month >= 1 && month <= 6 {
|
|
|
+ //03-31 23:59:59
|
|
|
+ nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ } else {
|
|
|
+ //12-31 23:59:59
|
|
|
+ nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ }
|
|
|
+ return nowHalfYearLastDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowYearFirstDay 获取当前年的最后一天的时间
|
|
|
+func GetNowYearFirstDay() time.Time {
|
|
|
+ //12-31 23:59:59
|
|
|
+ nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
+ return nowYearFirstDay
|
|
|
+}
|
|
|
+
|
|
|
+// GetNowYearLastDay 获取当前年的最后一天的时间
|
|
|
+func GetNowYearLastDay() time.Time {
|
|
|
+ //12-31 23:59:59
|
|
|
+ nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
|
|
|
+ return nowYearLastDay
|
|
|
+}
|