最近使用Go语言在写一个数据后台系统,要求输入两个日期时间,出现按周查询的数据,网上找了找,没有发现用 Go 语言实现的博客文章,就自己写了,开始觉得很简单,写着写着发现我的方法好麻烦,算了 也写好了,如果有兴趣的小伙伴可以换另一种方法试试(我还没有试试呢)——循环所有日期 得出星期几,把周一和周日按一组分。这里只展示我已经实现的方法。
import (
"fmt"
"log"
"time"
)var WeekDayMap = map[string]int64{
"Monday":1,
"Tuesday":2,
"Wednesday": 3,
"Thursday":4,
"Friday":5,
"Saturday":6,
"Sunday":7,
}// 获取输入的两个日期分别是星期几
func GetWeekDay(startime, endtim string) (int64, int64) {
startday, _ := time.Parse("2006-01-02", startime)
endday, _ := time.Parse("2006-01-02", endtim)
staweek_int := startday.Weekday().String()
endweek_int := endday.Weekday().String()
return WeekDayMap[staweek_int], WeekDayMap[endweek_int]
}// 字符串转时间戳
func StringToTimeStamp(strTime string) int64 {
timeLayout := "2006-01-02"
//timeLayout := "2006-01-02 15:04:05"
loc, _ := time.LoadLocation("Local")
the_time, err := time.ParseInLocation(timeLayout, strTime, loc)
if err != nil {
log.Println("StringToTimeStamp出现异常:", err)
}
unix_time := the_time.Unix()
return unix_time
}// 时间戳转 字符串
func TimeStampToString(timeStp int64) string {
//转化所需模板
//timeLayout := "2006-01-02 15:04:05"
timeLayout := "2006-01-02"
//进行格式化
datetime := time.Unix(timeStp, 0).Format(timeLayout)
return datetime
}// 时间转化为周日期列表
func ChangeToWeek(startime, endtim string) []map[string]string { staweek_int, endweek_int := GetWeekDay(startime, endtim)
// 获取时间戳
start_stamp := StringToTimeStamp(startime)
end_stamp := StringToTimeStamp(endtim)
fmt.Println("start_stamp==",start_stamp,"end_stamp==", end_stamp) var week_list = make([]map[string]string, 0)
if (end_stamp-start_stamp)/604800 <= 1 && endweek_int-staweek_int >= 0 {
if end_stamp-start_stamp < 604800 && endweek_int-staweek_int > 0 {
one_map := map[string]string{}
mon_one := TimeStampToString(start_stamp - (staweek_int-1)*86400)
sun_one := TimeStampToString(start_stamp + (7-staweek_int)*86400)
one_map["mon"] = mon_one
one_map["sun"] = sun_one
week_list = append(week_list, one_map)
return week_list
}
one_map := map[string]string{}
mon_one := TimeStampToString(start_stamp - (staweek_int-1)*86400)
sun_one := TimeStampToString(start_stamp + (7-staweek_int)*86400)
one_map["mon"] = mon_one
one_map["sun"] = sun_one
week_list = append(week_list, one_map)
tow_map := map[string]string{}
mon_tow := TimeStampToString(end_stamp - (endweek_int-1)*86400)
sun_tow := TimeStampToString(end_stamp + (7-endweek_int)*86400)
tow_map["mon"] = mon_tow
tow_map["sun"] = sun_tow
week_list = append(week_list, tow_map)
return week_list
}
week_n := (end_stamp - start_stamp) / 604800
one_map := map[string]string{}
mon_one := TimeStampToString(start_stamp - (staweek_int-1)*86400)
sun_one := TimeStampToString(start_stamp + (7-staweek_int)*86400)
one_map["mon"] = mon_one
one_map["sun"] = sun_one
week_list = append(week_list, one_map)
for i := 1;
i <= int(week_n);
i++ {
week_map := map[string]string{}
mon_day := TimeStampToString(start_stamp - (staweek_int-1)*86400 + int64(i)*604800)
sun_day := TimeStampToString(start_stamp + (7-staweek_int)*86400 + int64(i)*604800)
week_map["mon"] = mon_day
week_map["sun"] = sun_day
week_list = append(week_list, week_map)
}
if endweek_int-staweek_int >= 0 {
return week_list
}
tow_map := map[string]string{}
mon_tow := TimeStampToString(end_stamp - (endweek_int-1)*86400)
sun_tow := TimeStampToString(end_stamp + (7-endweek_int)*86400)
tow_map["mon"] = mon_tow
tow_map["sun"] = sun_tow
week_list = append(week_list, tow_map)
return week_list}func main(){ fmt.Println(ChangeToWeek("2020-01-01","2020-01-28"))}
// 结果:
//start_stamp== 1577808000end_stamp== 1580140800
//[ map[mon:2019-12-30 sun:2020-01-05]
//map[mon:2020-01-06 sun:2020-01-12]
//map[mon:2020-01-13 sun:2020-01-19]
//map[mon:2020-01-20 sun:2020-01-26]
//map[mon:2020-01-27 sun:2020-02-02] ]
推荐阅读
- 【golang】leetcode中级-字母异位词分组&无重复字符的最长子串
- 彻底理解Golang Map
- kratos线上开源年会它来啦~
- 深入浅出 Golang 资源嵌入方案(go-bindata篇)
- 深入浅出 Golang 资源嵌入方案(前篇)
- golang 经典案例总结
- Go实战 | 基于有向无环图的并发执行流的实现
- Golang 数组和切片
- Go JSON编码与解码()
- golang map基础知识