Skip to content

Commit 0e9cc11

Browse files
committed
update docs
1 parent 7186a6d commit 0e9cc11

File tree

4 files changed

+60
-32
lines changed

4 files changed

+60
-32
lines changed

docs/.vuepress/navbar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export const myNavbar: ThemeNavItem[] = defineNavbarConfig([
125125
items: [
126126
{
127127
text: '知识星球',
128+
badge: { text: '重制中', type: 'warning' },
128129
icon: 'ph:planet-fill',
129130
link: '/planet'
130131
},

docs/.vuepress/sidebar.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ export const mySidebar: ThemeSidebarMulti = {
3636
{ text: 'CRUD', link: 'CRUD' },
3737
{ text: '分页', link: 'pagination' },
3838
{ text: '接口响应', link: 'response' },
39-
{ text: '国际化', link: 'i18n', badge: 'NEW' },
39+
{ text: '国际化', link: 'i18n' },
4040
{ text: 'JWT', link: 'jwt' },
4141
{ text: 'RBAC', link: 'RBAC' },
42-
{ text: 'OAuth 2.0', link: 'oauth2' },
43-
{ text: '自定义异常', link: '/planet', badge: { text: '专业版', type: 'warning' } },
44-
{ text: '节流', link: 'limit' },
4542
{ text: '跨域', link: 'CORS' },
46-
{ text: 'SQLA 事务', link: 'transaction' },
43+
{ text: '时区', link: 'timezone' },
44+
{ text: '节流', link: 'limit' },
4745
{ text: '切换主键', link: 'pk' },
4846
{ text: '切换数据库', link: 'db' },
47+
{ text: '事务', link: 'transaction' },
48+
{ text: 'OAuth 2.0', link: 'oauth2' },
4949
{ text: '数据权限', link: '/planet', badge: { text: '专业版', type: 'warning' } },
5050
{ text: '代码生成', link: 'code-generation' },
5151
{ text: 'SocketIo', link: '/planet', badge: { text: '专业版', type: 'warning' } },

docs/backend/reference/timezone.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: 时区
3+
---
4+
5+
我们为全局精心设计了统一时区,现在,这是一件非常轻松的工作,只需修改 `backend/core/conf.py` 中的时区配置即可改变全局时区
6+
7+
::: caution
8+
时区一旦确定,强烈建议不要后期修改,否则可能造成持久化数据时间信息紊乱!
9+
:::
10+
11+
## 架构应用
12+
13+
无论在架构何处调用时间模块,我们都应使用 `backend/utils/timezone.py` 中提供的现有方法,而不是直接调用 datetime 相关模块
14+
15+
## 数据库
16+
17+
在数据库中处理时区是一件令人头疼的事,常见的方式有以下 3 种:
18+
19+
- 全部存读为 UTC,前端转化(利于国际化管理)
20+
- 全部存读当前时区时间,根据前端传入的时区进行转换(利于本地化管理)
21+
- 全部存储为数值时间戳,前端转化(极其不易管理,但易操作)
22+
23+
让我们来看一个经典案例:
24+
25+
::: chat title="群聊"
26+
{:2025-08-26 12:44:00}
27+
28+
{王}
29+
请教大佬,为啥我查询的时间用的不同的时区和时间戳,返回的数据却是一样的?
30+
31+
![question_db_timezone](/images/question_db_timezone.png)
32+
33+
数据库用的是 mysql,原则上这两个 datetime 的时间戳是不一样的,但是查出来的数据是一样的结果;
34+
35+
{王}
36+
我直接写 sql 查询,这个两个是符合预期结果的,第一个有数据,第二个查不到;
37+
38+
![question_sql_timezone](/images/question_sql_timezone.png)
39+
40+
这个切换到pg数据库后查询符合预期结果的;
41+
42+
{.}
43+
**timezone**: not used by the MySQL dialect.
44+
45+
sqlalchemy 和所有 python mysql 驱动默认都不处理 mysql 时区信息,通常是直接丢弃,即便使用 TIMESTAMP 类型
46+
47+
{.}
48+
更具体的:[sqlalchemy/1985](https://github.com/sqlalchemy/sqlalchemy/issues/1985)
49+
:::
50+
51+
为此,我们使用了第 2 种解决方案,并创建了自定义 TimeZone 类型,位于 `backend/common/model.py` 文件中,我们可以通过 IDE
52+
点击类名快捷查看使用方式

docs/questions.md

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,5 @@ return await self.select_order( # [!code word:noload]
6363

6464
## 数据库时区陷阱
6565

66-
MySQL 不支持时区类型(数据库驱动设计便是如此),而 PostgreSQL 拥有完美的时区类型,以下是一个时区相关的经典案例
67-
68-
::: chat title="群聊"
69-
{:2025-08-26 12:44:00}
70-
71-
{王}
72-
请教大佬,为啥我查询的时间用的不同的时区和时间戳,返回的数据却是一样的?
73-
74-
![question_db_timezone](/images/question_db_timezone.png)
75-
76-
数据库用的是 mysql,原则上这两个 datetime 的时间戳是不一样的,但是查出来的数据是一样的结果;
77-
78-
{王}
79-
我直接写sql查询,这个两个是符合预期结果的,第一个有数据,第二个查不到;
80-
81-
![question_sql_timezone](/images/question_sql_timezone.png)
82-
83-
这个切换到pg数据库后查询符合预期结果的;
84-
85-
{.}
86-
**timezone**: not used by the MySQL dialect.
87-
88-
sqla 不处理 mysql 时区,即便换成 TIMESTAMP
89-
90-
{.}
91-
更具体的:[sqlalchemy/1985](https://github.com/sqlalchemy/sqlalchemy/issues/1985)
92-
:::
66+
MySQL 不支持时区存储类型,而 PostgreSQL 拥有完美的时区类型,所以在数据库中存储时间列确实是一件令人头疼的事情,不过我们已经实现完美方案,兼容
67+
pgsql 和 mysql,[查看详情](./backend/reference/timezone.md)

0 commit comments

Comments
 (0)