Skip to content

Commit

Permalink
feat : develop
Browse files Browse the repository at this point in the history
  • Loading branch information
kakabei committed Aug 9, 2024
1 parent 20b4606 commit 54028d8
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 3 deletions.
10 changes: 7 additions & 3 deletions _posts/develop-log/2024-08-01-develop-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ title: 2024年8月
date: 2024-08-01 16:12:15
categories: 日志
tags: 开发日志
excerpt:
excerpt: 8月了,秋天来了!
---

准备开始开发笔记~

2024年8月7日
## 2024年8月7日

今年把自己的笔记越整理越少,感觉现在的笔记比较以前有用多了。 这两年来一直在做减法,一点也不简单,自己的生活,工作都太臃肿了。

最近一直在整理 golang 的代码。把自己工作中,业务中比较有共性的服务抽出来。方便以后使用。 看来要整理的东西不少。

2024年8月8日
## 2024年8月8日

业务上做了一个排队的逻辑,是不是后面考虑做成一个排队系统。

Expand Down Expand Up @@ -53,6 +53,10 @@ zset 按时间大小排序,越早申请时间越小,越排在前面。 zset

其实,以前也有用 redis 的 zset 做过游戏的排行榜, 方法大同小异吧。

## 2024年8月9日

整理了一下公司的文档,工作中要记文档,但也别写太多的文档,把主要工作给淹没了。多写文档还有好处的,能正确清晰的表达出来。说明已经很好的理解了。




Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
127 changes: 127 additions & 0 deletions _posts/develop/2024-03-01-golang-time-adddate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

---
layout: post
title: golang time adddate
date: 2024-04-11 9:08:12
categories: 开发
tags: golang 开发小经验
excerpt: time 的 adddate 方法出现的一个问题
---

周末线上出现一个问题,3月份的订单没有算出来,从数据库表数据上看,订单数据已经是完成状态,但数据为空。

业务上的逻辑是,每天会计算上个月的订单和当月的订单和。一月的订单周期是一个有1日12点到下个月的1日12点。

如前当3月31日,上个月的周期是2月1日12点到3月1日12点;本月的周期是3月1点12到当前时间点。

逻辑在处理上个月的订单时用了 `AddDate`

```go
lastMonth := time.AddDate(0, -1, 0).Format("200601")
```

问题就出现在这里。 当前时间是3月31时,time.AddDate(0, -1, 0) 之后,是 20240302 ,还是3月份,并不符合预期。

如下代码:

```go
package main

import (
"fmt"
"time"
)

func main() {

today := time.Date(2024, 2, 29, 0, 0, 0, 0, time.Local)
nextDay := today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 3, 31, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 4, 30, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 5, 31, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 6, 30, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 7, 31, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 8, 31, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 9, 30, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 10, 31, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 11, 30, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

today = time.Date(2024, 12, 31, 0, 0, 0, 0, time.Local)
nextDay = today.AddDate(0, -1, 0)
fmt.Printf("today :%v nextDay :%+v \n", today.Format("20060102"), nextDay.Format("20060102"))

}

```

输出:

```bash
today :20240229 nextDay :20240129
today :20240331 nextDay :20240302
today :20240430 nextDay :20240330
today :20240531 nextDay :20240501
today :20240630 nextDay :20240530
today :20240731 nextDay :20240701
today :20240831 nextDay :20240731
today :20240930 nextDay :20240830
today :20241031 nextDay :20241001
today :20241130 nextDay :20241030
today :20241231 nextDay :20241201
```
其中,如 20240331、20240531、20240731、20241031 这一些时算的上月时间就出错,它依然是本月的时间。

解决这个问题最简单,最快的方式是引用库 [https://github.com/Andrew-M-C/go.timeconv](https://github.com/Andrew-M-C/go.timeconv)

```go
package main

import (
"fmt"
"time"
"github.com/Andrew-M-C/go.timeconv"
)

func main() {
t := time.Date(2019, 1, 31, 0, 0, 0, 0, time.UTC)
nt := t.AddDate(0, 1, 0) // Add one month
fmt.Printf("%v\n", nt) // 2019-03-03 00:00:00 +0000 UTC, not expected

nt = timeconv.AddDate(t, 0, 1, 0)
fmt.Printf("%v\n", nt) // 2019-02-28 00:00:00 +0000 UTC
}
```


----
1、令人困惑的 Go time.AddDate [https://learnku.com/articles/71760](https://learnku.com/articles/71760)

2、Go time 包中的 AddDate 的逻辑避坑指南 [https://cloud.tencent.com/developer/article/1803695](https://cloud.tencent.com/developer/article/1803695)
29 changes: 29 additions & 0 deletions _posts/develop/2024-03-20-python-mysql-connector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: post
title: python mysql 插入更新一些特殊的字符
date: 2024-03-09 9:08:12
categories: 开发
tags: python 开发小经验
excerpt: python mysql 使用参数化查询处理特殊字符
---

写了一个脚本,把一个mysql 表的中数据从一个表更新到另一个表中。其他有字段是路径,包含有 `/``'` 等字符。

第一次的做法是:

```python
sql = "update t_softname set icon=%s, start_cmd=%s where name=%s".format(icon, link, name)
cursor.execute(sql)
```

却发现了问题,就是在遇到特殊的字符会被转义。如:写入的路径后会没了斜杠 `\`。或遇到 `'` 出现 sql 解析异常。

这是字符串被转义导致的。

**使用参数化查询**才可以,如下:

```python

sql = "update t_softname set icon=%s, start_cmd=%s where name=%s"
cursor.execute(sql,(icon, link, name))
```
File renamed without changes.

0 comments on commit 54028d8

Please sign in to comment.