惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Vercel News
Vercel News
F
Fortinet All Blogs
月光博客
月光博客
G
Google Developers Blog
博客园 - Franky
GbyAI
GbyAI
The Cloudflare Blog
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
小众软件
小众软件
腾讯CDC
B
Blog
量子位
V
V2EX
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News

博客园 - 雨V幕

使用chromedp 来做人工模拟操作爬取数据方法 遍历redis按照前缀给未设置过期时间的数据添加过期时间 使用rabbitmq 进行任务调度 使用trace进行排查网络瓶颈 使用vscode 调试 Python 使用power shell 拆分 csv文件 将大文件拆分成小文件。 使用postman 添加预处理验签。 go 使用pprof 进行问题排查 Mysql无主键删除重复数据的快速方法 解决mysql 事务死锁的方法 go在处理批量下载时候出现fatal error: runtime: out of memory AnalyticDB 创建db clickhouse 进行建表期间的一些优化 kraots2.0 在windows 环境搭建开发环境 Sql Server使用函数获取拼音码 关于async 和await关键字 使用kubespray 一键部署 containerd 的安装和熟悉 VMware 配置双网卡实现上网和固定ip
go 序列化反序列化之后时区信息丢失
雨V幕 · 2024-03-11 · via 博客园 - 雨V幕
  •  编写了获取当前datetime 时间的方法如下
    // GetCurrentDateString 获取当天的时间date
    func GetCurrentDateString() time.Time {
        // 获取当前时间
        currentTime := time.Now().Local()
        // 格式化日期为字符串
        dateString := currentTime.Format(time.DateTime)
        resultTime, _ := time.Parse(time.DateTime, dateString)
        return resultTime
    }

    插入db之后发现时间+8了。

  • 造成以上原因是因为反序列化之后时区信息丢失了。所以没有时区的时候time到db用的是国际时间。所以跟北京/上海时间大致会相差8h
  • 解决方案 反序列化的时候代码改成 : 
    // GetCurrentDateString 获取当天的时间date
    func GetCurrentDateString() time.Time {
        // 获取当前时间
        currentTime := time.Now().Local()
        // 格式化日期为字符串
        dateString := currentTime.Format(time.DateTime)
        resultTime, _ := time.ParseInLocation(time.DateTime, dateString, time.Local)
        return resultTime
    }
  • 如果时间字符串中带了时区信息才去使用 time.Parse,否则使用 time.ParseInLocation
  • 排查db 和本地时间对不上的思路方案:
    • 检查链接字符串是否添加时区信息  例如 : parseTime=true&loc=Local
    • 检查服务器当前时区 是否正确: timedatectl | grep "Time zone"
    • 检查mysql 时区设置:SHOW VARIABLES LIKE '%time_zone%';
    • 阿里云AnalyticDB实例Datatime 类型最多精确到秒。使用TIMESTAMP可以存储'2024-03-11 15:22:11.4688249' 。 使用起来和Mysql实例有差距