123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package pkg
- import (
- "fmt"
- "strconv"
- "time"
- )
- func GetTodayZeroTime() time.Time {
- nowTime := time.Now()
- todayZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
- return todayZeroTime
- }
- func GetTomorrowZeroTime() time.Time {
- nowTime := time.Now()
- todayZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
- duration, _ := time.ParseDuration("24h")
- tomorrowZeroTime := todayZeroTime.Add(duration)
- return tomorrowZeroTime
- }
- func GetFormatTime(origin time.Time) string {
- if origin.Year() == 0001 { // 空值处理
- return ""
- }
- return origin.Format("2006-01-02 15:04:05")
- }
- func GetMySQLFormatTime(origin time.Time) string {
- if origin.Year() == 0001 { // 空值处理
- return ""
- }
- return origin.Format("2006_01_02")
- }
- func IsEmptyTime(origin time.Time) bool {
- if origin.Year() == 0001 { // 空值处理
- return true
- } else {
- return false
- }
- }
- func GetLastMonthFirstDay() time.Time {
- timeNow := time.Now()
- curMonthFirstDay := TimeParse(timeNow.Year(), timeNow.Month(), 1) // 获取本月第一天0点
- duration, _ := time.ParseDuration("-1h")
- lastMonthDay := curMonthFirstDay.Add(duration) // 获取到上个月的时间
- return TimeParse(lastMonthDay.Year(), lastMonthDay.Month(), 1)
- }
- func TimeParse(year int, month time.Month, day int) time.Time {
- location, err := time.LoadLocation("Asia/Shanghai")
- if err != nil {
- location = time.FixedZone("CST-8", 8*3600)
- }
- time, _ := time.ParseInLocation("2006-01-02 15:04:05", FormatDate(year, month, day), location)
- return time
- }
- func TimeParseFmt(timeStr string) time.Time {
- location, err := time.LoadLocation("Asia/Shanghai")
- if err != nil {
- location = time.FixedZone("CST-8", 8*3600)
- }
- time, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, location)
- return time
- }
- func FormatDate(year int, month time.Month, day int) string {
- return fmt.Sprintf("%d-%s-%s 00:00:00", year, monthToDate(month), intToDate(day))
- }
- func intToDate(num int) string {
- if num < 10 {
- return fmt.Sprintf("0%d", num)
- } else {
- return strconv.Itoa(num)
- }
- }
- func monthToDate(month time.Month) string {
- switch month {
- case time.January:
- return "01"
- case time.February:
- return "02"
- case time.March:
- return "03"
- case time.April:
- return "04"
- case time.May:
- return "05"
- case time.June:
- return "06"
- case time.July:
- return "07"
- case time.August:
- return "08"
- case time.September:
- return "09"
- case time.October:
- return "10"
- case time.November:
- return "11"
- case time.December:
- return "12"
- }
- return ""
- }
|