123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package util
- import (
- "fmt"
- "math"
- "time"
- )
- const (
- LocationName = "Asia/Shanghai"
- LayoutTime = "2006-01-02 15:04:05"
- Layout = "2006-01-02"
- LayoutMonth = "2006-01"
- )
- func TimeParseLocalUnix(DayTime string) int64 {
- value := DayTime
- if len(DayTime) <= 11 {
- value = fmt.Sprintf("%s 00:00:00", DayTime)
- }
- loc, _ := time.LoadLocation("Local")
- theTime, _ := time.ParseInLocation(LayoutTime, value, loc)
- return theTime.Unix()
- }
- func TimeParseLocalEndUnix(DayTime string) int64 {
- value := DayTime
- if len(DayTime) <= 11 {
- value = fmt.Sprintf("%s 23:59:59", DayTime)
- }
- loc, _ := time.LoadLocation("Local")
- theTime, _ := time.ParseInLocation(LayoutTime, value, loc)
- return theTime.Unix()
- }
- func ConvertParseLocalUnix(timeParse string) (int64, error) {
- loc, err := time.LoadLocation("Local")
- if err != nil {
- return 0, err
- }
- value := fmt.Sprintf("%s %s", time.Now().Format(Layout), timeParse)
- theTime, err := time.ParseInLocation(LayoutTime, value, loc)
- if err != nil {
- return 0, err
- }
- return theTime.Unix(), nil
- }
- func GetMonthRemainDay() int {
- now := time.Now()
- lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 0, 23, 59, 59, 999999999, now.Location())
- return int(lastDayOfMonth.Sub(now).Hours()/24) + 1
- }
- func Ceil(x float64) float64 {
-
-
-
-
- intPart := math.Floor(x)
- if x-intPart > 0 {
- return intPart + 1
- }
- return intPart
- }
- func GetLastDayOfMonth(month string) (string, error) {
-
- layout := "2006-01"
- t, err := time.Parse(layout, month)
- if err != nil {
- return "", err
- }
-
- nextMonth := t.AddDate(0, 1, 0)
-
- lastDay := nextMonth.AddDate(0, 0, -1)
- return lastDay.Format(Layout), nil
- }
- func GetMonthsInRange(startMonth, endMonth string) ([]string, error) {
-
- startLayout := "2006-01"
- startTime, err := time.Parse(startLayout, startMonth)
- if err != nil {
- return nil, err
- }
-
- endTime, err := time.Parse(startLayout, endMonth)
- if err != nil {
- return nil, err
- }
-
- var months []string
-
- for curr := startTime; curr.Before(endTime) || curr.Equal(endTime); curr = curr.AddDate(0, 1, 0) {
- months = append(months, curr.Format("2006-01"))
- }
- return months, nil
- }
|