dengbiao 2 settimane fa
parent
commit
f031339662
2 ha cambiato i file con 20 aggiunte e 33 eliminazioni
  1. +12
    -12
      app/hdl/hdl_points_center.go
  2. +8
    -21
      app/utils/time.go

+ 12
- 12
app/hdl/hdl_points_center.go Vedi File

@@ -349,16 +349,16 @@ func GetPriceCurve(c *gin.Context) {
var xData []interface{}
switch kind {
case "1":
var date = now.AddDate(0, 0, -30).Format("2006-01-02")
var date = now.AddDate(0, 0, -6).Format("2006-01-02")
var sql = fmt.Sprintf("SELECT price,date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE >= \"%s\" AND DATE != \"%s\" ORDER BY DATE ASC ", date, now.Format("2006-01-02"))
results, _ := db.Db.QueryString(sql)
results, _ := db.Db.QueryString(db.Db, sql)
for _, v := range results {
tmpDate := utils.TimeParseDateStd(v["date"])
tmpDate := utils.String2TimeV2(v["date"])
yData = append(yData, v["price"])
xData = append(xData, tmpDate.Format("01-02"))
}
yData = append(yData, m.Price)
tmpDate := utils.TimeParseDateStd(m.Date)
tmpDate := utils.String2TimeV2(m.Date)
xData = append(xData, tmpDate.Format("01-02"))
break
case "2":
@@ -377,24 +377,24 @@ func GetPriceCurve(c *gin.Context) {
}
}
yData = append(yData, m.Price)
xData = append(xData, m.Date+" "+m.Hour+":00")
xData = append(xData, m.Hour+":00")
break
case "3":
var nums = 29
var nums = 6
for i := nums; i >= 1; i-- {
var date = now.AddDate(0, 0, -7*i).Format("2006-01-02")
var sql = "SELECT price, date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE = \"%s\" "
var sql = "SELECT price,date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE = \"%s\" "
sql = fmt.Sprintf(sql, date)
results, _ := db.Db.QueryString(sql)
results, _ := db.Db.QueryString(db.Db, sql)
if results != nil {
year, week, _, _ := utils.GetWeekInfoByDate(results[0]["date"])
tmpDate := utils.String2TimeV2(results[0]["date"])
yData = append(yData, results[0]["price"])
xData = append(xData, fmt.Sprintf("%s年%s周", year, week))
xData = append(xData, tmpDate.Format("01-02"))
}
}
yData = append(yData, m.Price)
year, week, _, _ := utils.GetWeekInfoByDate(m.Date)
xData = append(xData, fmt.Sprintf("%s年%s周", year, week))
tmpDate := utils.String2TimeV2(m.Date)
xData = append(xData, tmpDate.Format("01-02"))
break
}



+ 8
- 21
app/utils/time.go Vedi File

@@ -8,6 +8,14 @@ import (
"time"
)

func String2TimeV2(timeStr string) time.Time {
toTime, err := time.ParseInLocation("2006-01-02", timeStr, time.Local)
if err != nil {
return time.Now()
}
return toTime
}

func StrToTime(s string) (int64, error) {
// delete all not int characters
if s == "" {
@@ -71,11 +79,6 @@ func TimeParseStd(src string) time.Time {
return t
}

func TimeParseDateStd(src string) time.Time {
t, _ := TimeParse("2006-01-02", src)
return t
}

func TimeStdParseUnix(src string) int64 {
t, err := TimeParse("2006-01-02 15:04:05", src)
if err != nil {
@@ -242,19 +245,3 @@ func GetStartOfWeek(t time.Time) time.Time {
func GetZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}

// GetWeekInfoByDate 获取周数、开始时间、结束时间
func GetWeekInfoByDate(dateStr string) (string, string, string, string) {
date := TimeParseDateStd(dateStr)
year, week := date.ISOWeek()
location, _ := time.LoadLocation("Asia/Shanghai")
// 计算给定年份1月1日是星期几
startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
// 计算给定年份的第一周的开始日期
firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
// 计算给定周的开始日期
weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
weekEnd := weekStart.AddDate(0, 0, 6)
return IntToStr(year), IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
}

Caricamento…
Annulla
Salva