Commit efb4f5c8db872b6cfd2c7755c843ac735583b1ba
1 parent
2ed2e73d
Exists in
master
and in
1 other branch
feat✨:排行榜结算方式改为每天结算
Showing
7 changed files
with
245 additions
and
37 deletions
Show diff stats
service/roomrank/dto-player.go
| ... | ... | @@ -9,11 +9,12 @@ import ( |
| 9 | 9 | |
| 10 | 10 | // Player 房间排行持久数据 |
| 11 | 11 | type Player struct { |
| 12 | - Uid int64 `gorm:"column:uid;primaryKey;comment:玩家唯一ID"` | |
| 13 | - TopType int `gorm:"comment:排行榜类型"` | |
| 14 | - Name string `gorm:"-"` // 玩家名字 | |
| 15 | - Icon string `gorm:"-"` // 玩家头像 | |
| 16 | - ActivityId int64 `gorm:"comment:活动ID"` | |
| 12 | + Uid int64 `gorm:"column:uid;primaryKey;comment:玩家唯一ID"` | |
| 13 | + TopType int `gorm:"comment:排行榜类型"` | |
| 14 | + Name string `gorm:"-"` // 玩家名字 | |
| 15 | + Icon string `gorm:"-"` // 玩家头像 | |
| 16 | + ActivityId int64 `gorm:"comment:活动ID"` | |
| 17 | + ActivityTime int64 `gorm:"comment:当前活动周期时间"` | |
| 17 | 18 | |
| 18 | 19 | UserType int `gorm:"comment:用户类型"` // 0=新手用户 1=优质用户 2=普通用户 3=垃圾用户 |
| 19 | 20 | UserScore int `gorm:"comment:用户评级分"` |
| ... | ... | @@ -23,11 +24,12 @@ type Player struct { |
| 23 | 24 | |
| 24 | 25 | RoomUid int64 `gorm:"comment:所在房间唯一ID"` |
| 25 | 26 | |
| 26 | - SettleActivityId int64 `gorm:"comment:上次结算的活动ID"` | |
| 27 | - SettleRank int `gorm:"comment:结算名次"` | |
| 28 | - SettleScore int64 `gorm:"comment:结算分数"` | |
| 29 | - SettleUserClass int `gorm:"comment:结算用户评级"` | |
| 30 | - SettleAward string `gorm:"comment:结算奖励内容"` | |
| 27 | + SettleActivityId int64 `gorm:"comment:上次结算的活动ID"` | |
| 28 | + SettleActivityTime int64 `gorm:"comment:上次结算的活动时间戳"` | |
| 29 | + SettleRank int `gorm:"comment:结算名次"` | |
| 30 | + SettleScore int64 `gorm:"comment:结算分数"` | |
| 31 | + SettleUserClass int `gorm:"comment:结算用户评级"` | |
| 32 | + SettleAward string `gorm:"comment:结算奖励内容"` | |
| 31 | 33 | |
| 32 | 34 | CreateTime int64 `gorm:"comment:创建时间戳"` |
| 33 | 35 | UpdateTime int64 `gorm:"comment:修改时间戳"` | ... | ... |
service/roomrank/dto-room.go
| ... | ... | @@ -11,10 +11,11 @@ import ( |
| 11 | 11 | |
| 12 | 12 | // Room 房间排行持久数据 |
| 13 | 13 | type Room struct { |
| 14 | - Id int64 `gorm:"column:id;primaryKey;comment:房间唯一ID"` | |
| 15 | - TopType int `gorm:"comment:排行榜类型"` | |
| 16 | - ActivityId int64 `gorm:"comment:活动ID"` | |
| 17 | - ConfigId int `gorm:"comment:房间配置ID"` | |
| 14 | + Id int64 `gorm:"column:id;primaryKey;comment:房间唯一ID"` | |
| 15 | + TopType int `gorm:"comment:排行榜类型"` | |
| 16 | + ActivityId int64 `gorm:"comment:活动ID"` | |
| 17 | + ActivityTime int64 `gorm:"comment:当前活动周期时间"` | |
| 18 | + ConfigId int `gorm:"comment:房间配置ID"` | |
| 18 | 19 | |
| 19 | 20 | Details *RoomDetails `gorm:"-"` // 详情 |
| 20 | 21 | DetailsText string `gorm:"comment:详情封装"` |
| ... | ... | @@ -29,9 +30,10 @@ type Room struct { |
| 29 | 30 | |
| 30 | 31 | func (d *Room) MysqlInfo(suffix string) *svmysql.MysqlInfo { |
| 31 | 32 | tableName := svconst.MYSQL_TABLE_S_ROOMRANK_ROOM |
| 33 | + tableName = fmt.Sprintf("%s_%s_%d_%d", tableName, suffix, d.TopType, d.ActivityId) | |
| 32 | 34 | return &svmysql.MysqlInfo{ |
| 33 | 35 | DbMysql: svconst.DbCommon, |
| 34 | - TableName: fmt.Sprintf("%s_%s_%d_%d", tableName, suffix, d.TopType, d.ActivityId), | |
| 36 | + TableName: tableName, | |
| 35 | 37 | } |
| 36 | 38 | } |
| 37 | 39 | ... | ... |
service/roomrank/handle.go
| ... | ... | @@ -52,8 +52,10 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 52 | 52 | } |
| 53 | 53 | defer lock.Release() |
| 54 | 54 | |
| 55 | + activityTime := GetActivityTime(config) | |
| 56 | + | |
| 55 | 57 | // 尝试判断结算 |
| 56 | - hasSettleChange := TrySettle(gameId, req.TopType, player, config) | |
| 58 | + hasSettleChange := TrySettle(gameId, req.TopType, player, config, activityTime) | |
| 57 | 59 | |
| 58 | 60 | if hasSettleChange { |
| 59 | 61 | SavePlayer(gameId, player) |
| ... | ... | @@ -75,9 +77,11 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 75 | 77 | SavePlayer(gameId, player) |
| 76 | 78 | } |
| 77 | 79 | }() |
| 78 | - if player.ActivityId != config.Id { | |
| 80 | + if player.ActivityId != config.Id && player.ActivityTime != activityTime { | |
| 79 | 81 | player.ActivityId = config.Id |
| 82 | + player.ActivityTime = activityTime | |
| 80 | 83 | player.RoomUid = 0 |
| 84 | + player.Score = 0 | |
| 81 | 85 | needSavePlayer = true |
| 82 | 86 | } |
| 83 | 87 | |
| ... | ... | @@ -88,7 +92,7 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 88 | 92 | } |
| 89 | 93 | |
| 90 | 94 | // 尝试加入房间 |
| 91 | - room, hasRoom, firstJoin := TryGetRoom(gameId, req.TopType, player, config, playerLevel) | |
| 95 | + room, hasRoom, firstJoin := TryGetRoom(gameId, req.TopType, player, config, playerLevel, activityTime) | |
| 92 | 96 | |
| 93 | 97 | if !hasRoom { |
| 94 | 98 | code = code_msg.RECODE_MERGE_CONFIG_ERROR |
| ... | ... | @@ -135,8 +139,10 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r |
| 135 | 139 | } |
| 136 | 140 | defer lock.Release() |
| 137 | 141 | |
| 142 | + activityTime := GetActivityTime(config) | |
| 143 | + | |
| 138 | 144 | // 尝试判断结算 |
| 139 | - _ = TrySettle(gameId, req.TopType, player, config) | |
| 145 | + _ = TrySettle(gameId, req.TopType, player, config, activityTime) | |
| 140 | 146 | if !player.SettleHas() { |
| 141 | 147 | code = code_msg.RECODE_MERGE_ROOMRANK_NOTHASSETTLE_ERROR |
| 142 | 148 | return |
| ... | ... | @@ -150,6 +156,7 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r |
| 150 | 156 | rsp.AwardText = player.SettleAward |
| 151 | 157 | player.SettleAward = "" |
| 152 | 158 | player.ActivityId = config.Id |
| 159 | + player.ActivityTime = 0 | |
| 153 | 160 | player.RoomUid = 0 |
| 154 | 161 | player.Score = 0 |
| 155 | 162 | SavePlayer(gameId, player) |
| ... | ... | @@ -192,8 +199,10 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 192 | 199 | } |
| 193 | 200 | defer lock.Release() |
| 194 | 201 | |
| 202 | + activityTime := GetActivityTime(config) | |
| 203 | + | |
| 195 | 204 | // 尝试判断结算 |
| 196 | - hasSettleChange := TrySettle(gameId, req.TopType, player, config) | |
| 205 | + hasSettleChange := TrySettle(gameId, req.TopType, player, config, activityTime) | |
| 197 | 206 | if hasSettleChange { |
| 198 | 207 | SavePlayer(gameId, player) |
| 199 | 208 | } |
| ... | ... | @@ -216,9 +225,11 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 216 | 225 | SavePlayer(gameId, player) |
| 217 | 226 | } |
| 218 | 227 | }() |
| 219 | - if player.ActivityId != config.Id { | |
| 228 | + if player.ActivityId != config.Id && player.ActivityTime != activityTime { | |
| 220 | 229 | player.ActivityId = config.Id |
| 230 | + player.ActivityTime = activityTime | |
| 221 | 231 | player.RoomUid = 0 |
| 232 | + player.Score = 0 | |
| 222 | 233 | needSavePlayer = true |
| 223 | 234 | } |
| 224 | 235 | |
| ... | ... | @@ -237,7 +248,7 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 237 | 248 | } |
| 238 | 249 | |
| 239 | 250 | // 尝试加入房间 |
| 240 | - room, hasRoom, firstJoin := TryGetRoom(gameId, req.TopType, player, config, playerLevel) | |
| 251 | + room, hasRoom, firstJoin := TryGetRoom(gameId, req.TopType, player, config, playerLevel, activityTime) | |
| 241 | 252 | |
| 242 | 253 | if !hasRoom { |
| 243 | 254 | code = code_msg.RECODE_MERGE_CONFIG_ERROR | ... | ... |
service/roomrank/logic.go
| ... | ... | @@ -3,33 +3,75 @@ package roomrank |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/configs/confroomrank" |
| 5 | 5 | "apigame/service-common/svconst" |
| 6 | + "apigame/util/util-lx/lxtime" | |
| 6 | 7 | "apigame/util/utstring" |
| 7 | - "apigame/util/zjson" | |
| 8 | + "apigame/util/ztime" | |
| 8 | 9 | "fmt" |
| 10 | + "time" | |
| 9 | 11 | ) |
| 10 | 12 | |
| 11 | 13 | func getLockKey(gameId string, topType int, activityId int64) string { |
| 12 | 14 | return fmt.Sprintf("%s:lock:roomrank:%s:%d:%d", svconst.REDIS_CACHEP_REFIX, gameId, topType, activityId) |
| 13 | 15 | } |
| 14 | 16 | |
| 17 | +// GetActivityTime 根据当前时间获取当前次的结算时间戳 | |
| 18 | +func GetActivityTime(config *confroomrank.ActivityConfig) int64 { | |
| 19 | + dtNow := time.Now() | |
| 20 | + nowYear, nowMonth, nowDay := dtNow.Date() | |
| 21 | + hour, min, sec := ztime.TimeClock(config.ReleaseTime) | |
| 22 | + dt := time.Date(nowYear, nowMonth, nowDay, hour, min, sec, 0, time.Local) | |
| 23 | + if dtNow.After(dt) { | |
| 24 | + dt = dt.AddDate(0, 0, 1) | |
| 25 | + } | |
| 26 | + dtYear, dtMonth, dtDay := dt.Date() | |
| 27 | + activityTime := int64(dtYear)*1_0000 + int64(dtMonth)*100 + int64(dtDay) | |
| 28 | + return activityTime | |
| 29 | +} | |
| 30 | + | |
| 31 | +func ActivityTimeToDate(activityTime int64) (year int, month time.Month, day int) { | |
| 32 | + year = int(activityTime / 1_0000) | |
| 33 | + month = time.Month((activityTime % 1_0000) / 100) | |
| 34 | + day = int(activityTime % 100) | |
| 35 | + return | |
| 36 | +} | |
| 37 | + | |
| 15 | 38 | // TrySettle 尝试判断结算 |
| 16 | -func TrySettle(gameId string, topType int, player *Player, config *confroomrank.ActivityConfig) (hasChange bool) { | |
| 39 | +func TrySettle(gameId string, topType int, player *Player, config *confroomrank.ActivityConfig, activityTime int64) (hasChange bool) { | |
| 17 | 40 | hasChange = false |
| 18 | - if player.SettleActivityId == config.Id { | |
| 41 | + if player.ActivityId == 0 { | |
| 19 | 42 | return |
| 20 | 43 | } |
| 21 | - if player.ActivityId == 0 { | |
| 44 | + if player.SettleActivityId == config.Id && player.SettleActivityTime == activityTime { | |
| 22 | 45 | return |
| 23 | 46 | } |
| 24 | - if player.ActivityId == config.Id { | |
| 47 | + if player.ActivityId == config.Id && player.ActivityTime == activityTime { | |
| 25 | 48 | return |
| 26 | 49 | } |
| 27 | - fmt.Println(zjson.Str(player)) | |
| 50 | + //if player.ActivityId == 0 { | |
| 51 | + // return | |
| 52 | + //} | |
| 53 | + //if player.ActivityId == config.Id { | |
| 54 | + // return | |
| 55 | + //} | |
| 56 | + //fmt.Println(zjson.Str(player)) | |
| 28 | 57 | // 查找玩家所在的房间 |
| 29 | 58 | room, hasRoom := LoadRoom(gameId, topType, player.ActivityId, player.RoomUid) |
| 30 | 59 | if !hasRoom { |
| 31 | 60 | return |
| 32 | 61 | } |
| 62 | + | |
| 63 | + if room.HasSettle { | |
| 64 | + return | |
| 65 | + } | |
| 66 | + // 未到结算时间不结算 | |
| 67 | + dtYear, dtMonth, dtDay := ActivityTimeToDate(room.ActivityTime) | |
| 68 | + hour, min, sec := ztime.TimeClock(config.ReleaseTime) | |
| 69 | + dt := time.Date(dtYear, dtMonth, dtDay, hour, min, sec, 0, time.Local) | |
| 70 | + dtNow := lxtime.NowUninx() | |
| 71 | + if dtNow < dt.Unix() { | |
| 72 | + return | |
| 73 | + } | |
| 74 | + | |
| 33 | 75 | confActivity, hasConfActivity := confroomrank.GetConfig(gameId, room.ActivityId) |
| 34 | 76 | if !hasConfActivity { |
| 35 | 77 | return |
| ... | ... | @@ -47,6 +89,7 @@ func TrySettle(gameId string, topType int, player *Player, config *confroomrank. |
| 47 | 89 | // 设置玩家奖励等数据 |
| 48 | 90 | hasChange = true |
| 49 | 91 | player.SettleActivityId = config.Id |
| 92 | + player.SettleActivityTime = activityTime | |
| 50 | 93 | confRoom, hasConfRoom := confActivity.Room[room.ConfigId] |
| 51 | 94 | if !hasConfRoom { |
| 52 | 95 | return | ... | ... |
service/roomrank/room.go
| ... | ... | @@ -49,14 +49,15 @@ func LoadRoom(gameId string, topType int, activityId int64, roomUid int64) (room |
| 49 | 49 | return |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | -func CreateRoom(gameId string, topType int, activityId int64, roomConfigId int) (room *Room, has bool) { | |
| 52 | +func CreateRoom(gameId string, topType int, config *confroomrank.ActivityConfig, roomConfigId int, activityTime int64) (room *Room, has bool) { | |
| 53 | 53 | room = new(Room) |
| 54 | 54 | room.TopType = topType |
| 55 | - room.ActivityId = activityId | |
| 55 | + room.ActivityId = config.Id | |
| 56 | 56 | room.ConfigId = roomConfigId |
| 57 | 57 | room.Closed = false |
| 58 | 58 | room.HasSettle = false |
| 59 | 59 | room.CreateTime = lxtime.NowUninx() |
| 60 | + room.ActivityTime = activityTime | |
| 60 | 61 | details := new(RoomDetails) |
| 61 | 62 | details.Players = make([]*RoomPlayer, 0) |
| 62 | 63 | details.IndexNames = make([]int, 0) |
| ... | ... | @@ -72,12 +73,12 @@ func CreateRoom(gameId string, topType int, activityId int64, roomConfigId int) |
| 72 | 73 | } |
| 73 | 74 | |
| 74 | 75 | // FindRoom 查找 |
| 75 | -func FindRoom(gameId string, topType int, activityId int64, roomConfigId int) (rooms []*Room, has bool) { | |
| 76 | - roomDemo := &Room{TopType: topType, ActivityId: activityId} | |
| 76 | +func FindRoom(gameId string, topType int, activityId int64, roomConfigId int, activityTime int64) (rooms []*Room, has bool) { | |
| 77 | + roomDemo := &Room{TopType: topType, ActivityId: activityId, ActivityTime: activityTime} | |
| 77 | 78 | tryInitRoom(gameId, roomDemo) |
| 78 | 79 | info := roomDemo.MysqlInfo(gameId) |
| 79 | 80 | |
| 80 | - info.DbMysql = info.DbMysql.Where("config_id = ? AND closed = false", roomConfigId) | |
| 81 | + info.DbMysql = info.DbMysql.Where("config_id = ? AND activity_time = ? AND closed = false", roomConfigId, activityTime) | |
| 81 | 82 | has, _ = svmysql.Find(&rooms, info) |
| 82 | 83 | for _, room := range rooms { |
| 83 | 84 | room.Decode() |
| ... | ... | @@ -87,7 +88,7 @@ func FindRoom(gameId string, topType int, activityId int64, roomConfigId int) (r |
| 87 | 88 | } |
| 88 | 89 | |
| 89 | 90 | // TryGetRoom 尝试获取房间 |
| 90 | -func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank.ActivityConfig, playerLevel int) (room *Room, hasRoom bool, firstJoin bool) { | |
| 91 | +func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank.ActivityConfig, playerLevel int, activityTime int64) (room *Room, hasRoom bool, firstJoin bool) { | |
| 91 | 92 | hasRoom = false |
| 92 | 93 | firstJoin = false |
| 93 | 94 | // 查找玩家所在的房间 |
| ... | ... | @@ -115,7 +116,7 @@ func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank |
| 115 | 116 | return |
| 116 | 117 | } |
| 117 | 118 | // 数据库查找合适条件的房间列表 |
| 118 | - rooms, has := FindRoom(gameId, topType, config.Id, roomConfigId) | |
| 119 | + rooms, has := FindRoom(gameId, topType, config.Id, roomConfigId, activityTime) | |
| 119 | 120 | if has { |
| 120 | 121 | for i := 0; i < len(rooms); i++ { |
| 121 | 122 | roomTemp := rooms[i] |
| ... | ... | @@ -138,7 +139,7 @@ func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank |
| 138 | 139 | firstJoin = true |
| 139 | 140 | } else { |
| 140 | 141 | // 如果没有合适条件的房间 创建一个 |
| 141 | - roomCreate, hasCreate := CreateRoom(gameId, topType, config.Id, roomConfigId) | |
| 142 | + roomCreate, hasCreate := CreateRoom(gameId, topType, config, roomConfigId, activityTime) | |
| 142 | 143 | if hasCreate { |
| 143 | 144 | room = roomCreate |
| 144 | 145 | |
| ... | ... | @@ -168,7 +169,7 @@ func TryCloseRoom(gameId string, room *Room, roomConfig confroomrank.RoomConfig) |
| 168 | 169 | return |
| 169 | 170 | } |
| 170 | 171 | secNow := lxtime.NowUninx() |
| 171 | - if secNow > room.CreateTime+RoomCloseSecond { | |
| 172 | + if secNow > room.ActivityTime-RoomCloseSecond { | |
| 172 | 173 | room.Closed = true |
| 173 | 174 | // 根据缺口自动填充机器人 |
| 174 | 175 | for i := 0; i < len(roomConfig.PlayerTypeCount); i++ { | ... | ... |
| ... | ... | @@ -0,0 +1,20 @@ |
| 1 | +package ztime | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "strconv" | |
| 5 | + "strings" | |
| 6 | +) | |
| 7 | + | |
| 8 | +func TimeClock(text string) (hour, min, sec int) { | |
| 9 | + list := strings.Split(text, ":") | |
| 10 | + if len(list) >= 1 { | |
| 11 | + hour, _ = strconv.Atoi(list[0]) | |
| 12 | + } | |
| 13 | + if len(list) >= 2 { | |
| 14 | + min, _ = strconv.Atoi(list[1]) | |
| 15 | + } | |
| 16 | + if len(list) >= 3 { | |
| 17 | + sec, _ = strconv.Atoi(list[2]) | |
| 18 | + } | |
| 19 | + return | |
| 20 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,129 @@ |
| 1 | +package ztime | |
| 2 | + | |
| 3 | +import "time" | |
| 4 | + | |
| 5 | +const ( | |
| 6 | + DtFormatDate = "2006-01-02" | |
| 7 | + DtFormatTime = "2006-01-02 15:04:05" | |
| 8 | + DtFormatMonth = "2006-01" | |
| 9 | +) | |
| 10 | + | |
| 11 | +// Unix 获取当前秒时间戳 | |
| 12 | +func Unix() int64 { | |
| 13 | + return time.Now().Unix() | |
| 14 | +} | |
| 15 | + | |
| 16 | +// UnixMilli 获取当前毫秒时间戳 | |
| 17 | +func UnixMilli() int64 { | |
| 18 | + return time.Now().UnixMilli() | |
| 19 | +} | |
| 20 | + | |
| 21 | +// TimeToDateString | |
| 22 | +// @Description: 时间转日期字符串 | |
| 23 | +// @param dt | |
| 24 | +// @return string | |
| 25 | +func TimeToDateString(dt time.Time) string { | |
| 26 | + return dt.Format(DtFormatDate) | |
| 27 | +} | |
| 28 | + | |
| 29 | +// TimeToTimeString | |
| 30 | +// @Description: 时间转时间字符串 | |
| 31 | +// @param dt | |
| 32 | +// @return string | |
| 33 | +func TimeToTimeString(dt time.Time) string { | |
| 34 | + return dt.Format(DtFormatTime) | |
| 35 | +} | |
| 36 | + | |
| 37 | +// TimeToMonthString | |
| 38 | +// @Description: 时间转月份字符串 | |
| 39 | +// @param dt | |
| 40 | +// @return string | |
| 41 | +func TimeToMonthString(dt time.Time) string { | |
| 42 | + return dt.Format(DtFormatMonth) | |
| 43 | +} | |
| 44 | + | |
| 45 | +// TimeToMonth | |
| 46 | +// @Description: 时间转月份字符串 | |
| 47 | +// @param dt | |
| 48 | +// @return string | |
| 49 | +func TimeToMonth(dt time.Time) int { | |
| 50 | + return dt.Year()*100 + int(dt.Month()) | |
| 51 | +} | |
| 52 | + | |
| 53 | +// DateStringToTimeE | |
| 54 | +// @Description: 日期字符串转时间 | |
| 55 | +// @param dt | |
| 56 | +// @return time.Time | |
| 57 | +// @return error | |
| 58 | +func DateStringToTimeE(dt string) (time.Time, error) { | |
| 59 | + return time.Parse(DtFormatDate, dt) | |
| 60 | +} | |
| 61 | + | |
| 62 | +// DateStringToTime | |
| 63 | +// @Description: 时间字符串转时间 | |
| 64 | +// @param dt | |
| 65 | +// @return time.Time | |
| 66 | +// @return error | |
| 67 | +func DateStringToTime(dt string) time.Time { | |
| 68 | + if t, err := time.Parse(DtFormatDate, dt); err == nil { | |
| 69 | + return t | |
| 70 | + } else { | |
| 71 | + return time.Now() | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | +// TimeStringToTimeE | |
| 76 | +// @Description: 时间字符串转时间 | |
| 77 | +// @param dt | |
| 78 | +// @return time.Time | |
| 79 | +// @return error | |
| 80 | +func TimeStringToTimeE(dt string) (time.Time, error) { | |
| 81 | + return time.Parse(DtFormatTime, dt) | |
| 82 | +} | |
| 83 | + | |
| 84 | +// TimeStringToTime | |
| 85 | +// @Description: 时间字符串转时间 | |
| 86 | +// @param dt | |
| 87 | +// @return time.Time | |
| 88 | +// @return error | |
| 89 | +func TimeStringToTime(dt string) time.Time { | |
| 90 | + if t, err := time.Parse(DtFormatTime, dt); err == nil { | |
| 91 | + return t | |
| 92 | + } else { | |
| 93 | + return time.Now() | |
| 94 | + } | |
| 95 | +} | |
| 96 | + | |
| 97 | +// TimeTextInt64 把时间转换成数字字符串 比如 20240510113130 | |
| 98 | +func TimeTextInt64(dt time.Time) int64 { | |
| 99 | + date := int64(dt.Year())*1_0000 + int64(dt.Month())*100 + int64(dt.Day()) | |
| 100 | + t := int64(dt.Hour())*1_0000 + int64(dt.Minute())*100 + int64(dt.Second()) | |
| 101 | + return date*1_000000 + t | |
| 102 | +} | |
| 103 | + | |
| 104 | +// SameMonth | |
| 105 | +// @Description: 判断同一月 | |
| 106 | +// @param dt1 | |
| 107 | +// @param dt2 | |
| 108 | +// @return bool | |
| 109 | +func SameMonth(dt1, dt2 time.Time) bool { | |
| 110 | + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() | |
| 111 | +} | |
| 112 | + | |
| 113 | +// SameDay | |
| 114 | +// @Description: 判断同一天 | |
| 115 | +// @param dt1 | |
| 116 | +// @param dt2 | |
| 117 | +// @return bool | |
| 118 | +func SameDay(dt1, dt2 time.Time) bool { | |
| 119 | + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() | |
| 120 | +} | |
| 121 | + | |
| 122 | +// SameHour | |
| 123 | +// @Description: 判断同一小时 | |
| 124 | +// @param dt1 | |
| 125 | +// @param dt2 | |
| 126 | +// @return bool | |
| 127 | +func SameHour(dt1, dt2 time.Time) bool { | |
| 128 | + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() && dt1.Hour() == dt2.Hour() | |
| 129 | +} | ... | ... |