封装使用

先看看封装后的使用

注意输出:

Source date time data :USA_datetime_str : 2020-11-20 15:30:10USA_UTC_offset_hours : -5
Tranform to :CHINA_BeiJing_datetime_str : 2020-11-21 4:30:10CHINA_BeiJing_UTC_offset_hours : 8

Source Code

Const = {}
LuaUtil = {}Const.EnableTimeZoneSummer = true
Const.EnableTimeZoneFall = false--[[@desc: 可替换包含{xxx}的内容author: jave.lintime:2020-12-09 14:30:58--@format_str: 带占位格式的字符串 "{year}-{month}-{day} {hour}:{min}:{sec}"--@tbl: 包含{xxx}的内容:{ year = 2020, month = 12, day = 9, hour = 14, min = 30, sec = 58}--@p: nil or "{(%w+)}@return: 替换了 {xxx} 的内容
]]
table.format = function(format_str, tbl, p)p = p or "{(%w+)}"return tostring(string.gsub(format_str, p, tbl))
end--[[@desc: 获取 timestamp 时间戳对应的时间日期的表现方式author: jave.lintime:2020-12-09 15:32:31--@timestamp: 时间戳,单位:秒--@format_str: 带占位格式的字符串 "{year}-{month}-{day} {hour}:{min}:{sec}"--@p: nil or "{(%w+)}@return: 返回 timestamp 对应的日期时间的表示字符串
]]
function LuaUtil.getDateTimeStr(timestamp, format_str, p)format_str = format_str or "{year}-{month}-{day} {hour}:{min}:{sec}"return table.format(format_str, os.date("*t", timestamp), p)
end
function LuaUtil.getDateTimeStrByTbl(datetime_tbl, format_str, p)format_str = format_str or "{year}-{month}-{day} {hour}:{min}:{sec}"return table.format(format_str, datetime_tbl, p)
end--[[@desc: 获取本地时区相对 UTC 的时差值,单位:秒,如:北京时差 +8,那么返回:8 * 3600 秒author: jave.lintime:2020-12-24 09:44:02@return: 返回相对 UTC 时区的时差值:秒
]]
function LuaUtil.getLocalUtcOffsetSeconds()if LuaUtil.__localUtcOffsetSeconds == nil thenlocal now_tick = os.time()local date_local = os.date("*t", now_tick) -- locallocal date_utc = os.date("!*t", now_tick) -- utclocal local_utc_offset = os.time(date_local) - os.time(date_utc)LuaUtil.__localUtcOffsetSeconds = local_utc_offsetendreturn LuaUtil.__localUtcOffsetSeconds
end--[[@desc: 获取本地时区相对 UTC 的时差值,单位:小时,如:北京时差 +8,那么返回:8 小时author: jave.lintime:2020-12-24 09:44:02@return: 返回相对 UTC 时区的时差值:小时
]]
function LuaUtil.getLocalUtcOffsetHours()if LuaUtil.__localUtcOffsetHours == nil thenLuaUtil.__localUtcOffsetHours = LuaUtil.getLocalUtcOffsetSeconds() / 3600endreturn LuaUtil.__localUtcOffsetHours
end-- src_zone_timestamp src zone 的时间戳
-- src_zone_offset_hour src zone 的相对 utc 的时间戳
-- return 相对当前 local(本地)的时间戳
--[[@desc: 将 src 指定的时间戳(秒)、时区值(小时),转为相对本地相对 utc 的偏差后的时间戳author: jave.lintime:2020-12-24 09:48:07--@src_zone_timestamp: src 指定的时间戳(秒)--@src_zone_offset_hour: src 时区值(小时)@return: 返回相对本地相对 utc 的偏差后的时间戳
]]
function LuaUtil.srcToLocalUtcOffsetTimestamp(src_zone_timestamp, src_zone_offset_hour)local src_utc_timestamp = (src_zone_timestamp - (src_zone_offset_hour * 3600))local to_local_utc_timestamp = src_utc_timestamp + LuaUtil.getLocalUtcOffsetSeconds()-- 3-5月就是春季,6-8就是夏季,如果是9-11就是秋季,如果是12-2就是冬季-- local date_time_tbl = os.date("*t", to_local_utc_timestamp)-- 夏令时、冬令时我们不需要额外处理,lua 底层有处理-- 冬令时:就是标准时间,按理不需要调整,所以下面的 - 3600 操作是有问题的-- os.date 返回的 table 数据中 date_time_tbl.isdst 就是是否夏令时的一个标记,内部有处理,但是显示的话,要是按标准的时间来显示,所以不需要处理-- if -- Const.EnableTimeZoneSummer and -- date_time_tbl.month >= 3 and -- date_time_tbl.month <= 5 then--     -- 夏令时,在春季调整--     to_local_utc_timestamp  = to_local_utc_timestamp + 3600-- elseif -- Const.EnableTimeZoneFall and-- date_time_tbl.month >= 9 and-- date_time_tbl.month <= 11 then--     -- 冬令时,在秋季调整--     to_local_utc_timestamp  = to_local_utc_timestamp - 3600-- endreturn to_local_utc_timestamp
end--[[@desc: 获取 timestamp 时间戳、utc_zone_offset UTC 时差对应的时间日期的表现方式author: jave.lintime:2020-12-09 15:32:31--@timestamp: 目标时区的时间戳,单位:秒--@utc_zone_ofset: 目标时区时差,单位:小时,如:美国纽约:-5,中国北京:+8--@format_str: 带占位格式的字符串 "{year}-{month}-{day} {hour}:{min}:{sec}"--@p: nil or "{(%w+)}@return: 返回 timestamp 对应的日期时间的表示字符串
]]
function LuaUtil.getUtcOffsetDateTimeStr(timestamp, utc_zone_ofset, format_str, p)local ts = LuaUtil.srcToLocalUtcOffsetTimestamp(timestamp, utc_zone_ofset)return LuaUtil.getDateTimeStr(ts, format_str, p)
end-- USA 时间
local USA_datetime_tbl = { hour=15, min=30, sec=10, year=2020, month=11, day=20 }
-- USA 相对的时间戳
local USA_timestamp = os.time(USA_datetime_tbl)
-- USA 相对 UTC 的时区时差值:小时
local USA_UTC_offset_hours = -5-- 将 USA 时间戳、USA 相对 UTC 是差值,转换为中国北京的时间戳
local CHINA_BeiJing_timestamp = LuaUtil.srcToLocalUtcOffsetTimestamp(USA_timestamp, USA_UTC_offset_hours)
-- 将时间戳格式化显示字符
local CHINA_BeiJing_datetime_str = LuaUtil.getDateTimeStr(CHINA_BeiJing_timestamp)
-- 也可以使用另一个 API 直接转为格式化的时间日期
-- local CHINA_BeiJing_datetime_str = LuaUtil.getUtcOffsetDateTimeStr(USA_timestamp, USA_UTC_offset_hours)-- 打印相关信息
print("Source date time data :")
print("  USA_datetime_str : ")
print("    " .. LuaUtil.getDateTimeStrByTbl(USA_datetime_tbl))
print("  USA_UTC_offset_hours : ")
print("    " .. USA_UTC_offset_hours)print("Tranform to :")
print("  CHINA_BeiJing_datetime_str : ")
print("    " .. CHINA_BeiJing_datetime_str)
print("  CHINA_BeiJing_UTC_offset_hours : ")
print("    " .. LuaUtil.getLocalUtcOffsetHours())

夏令时、冬令时

可以参考:


注意!


前面封装一堆代码

其实 lua 的 api 已经帮我们处理了

如果是有一个固定的时间戳,如服务器发的一个时间戳:

fixed_timestamp = xxx

那么我们客户端直接用来显示相对我们本地时区对应的时间日期

可以这么使用:

local date_local = os.date("*t", now_tick) -- local
local date_utc = os.date("!*t", now_tick) -- utc

date_local、date_utc 都是 table ,一般我们常用的有:
year、month、day、hour、min、sec,分别对应:年月日时分秒

date_local 就是相对带有 utc 时差后的时间
date_utc 就是时差为 0 的 utc 时间


References

  • 时间那点儿事儿 – 时间戳,时区,冬令时,夏令时
  • lua中的时区与时间

Lua - 从指定时区提供的时间戳、时区的 UTC 时差,转换为对应当前本地 UTC 时差后的时间相关推荐

  1. date javascript 时区_js Date 时间戳 时区等问题总结

    js 的Date 时间戳并没有时区的概念  在任何时区 打印new Date(1) 显示的都是相对时间 如下: var t = new Date(1); t // Thu Jan 01 1970 08 ...

  2. php 时间戳 时区,PHP时间函数 时间戳 设置时区

    目录: 什么是时间戳 设置时区的方式 与时间相关的函数 1.什么是时间戳 现实中如何表示时间? 中国:2020年10月9日 下午3点30分 外国:9/10/2020 世界各国时间表示方式不一样,导致计 ...

  3. php 的时间戳时区,[php]php时间戳当中关于时区的问题

    PHP_VERSION = 5.5.11 话说php函数 time() 的起始时间戳是从:GMT 1970-01-01 00:00:00 开始算起的 写了点测试代码: $gmt1 = strtotim ...

  4. android 时间戳 时区,三句话理解时区与时间戳

    从不浪费时间的人,没有工夫抱怨时间不够. -- 杰弗逊 第一句话:时间戳 时间不分东西南北.在地球的每一个角落都是相同的.他们都有一个相同的名字,叫时间戳.时间戳 指的就是Unix时间戳(Unix t ...

  5. PHP中关于时间,时间戳 时区的设置问题

    1,设置时区   date_default_timezone_set('PRC');//设置时区 2,time()获取当前时间的 时间戳 格式:1536751700时间格式:记住这两种就好: 2018 ...

  6. php 的时间戳时区,PHP中时间戳和时区

    时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. 时区 由于世界各国家与地区经度不同,地方时也有所不同,因此 ...

  7. PHP中时间戳和时区,PHP中时间戳和时区 - osc_3rll7emc的个人空间 - OSCHINA - 中文开源技术交流社区...

    时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. 时区 由于世界各国家与地区经度不同,地方时也有所不同,因此 ...

  8. CentOS 6.9时间与时间服务器相差1小时(时区问题,经过查看时区是蒙古时区,蒙古时区与亚洲上海时区相差1小时)

    项目上服务器与时间服务器同步时间后,总是比实际时间快一个小时 clock 英[klɒk] 美[klɑːk] n. 时钟; 钟; v. 达到(某时间或速度); 测-的速度; 注意到; 认出; 首先查看时 ...

  9. 日期格式转换成时间戳格式php,php日期转时间戳,指定日期转换成时间戳

    有朋友问php与mysql有没有办法把日期转时间戳或把指定日期转换成时间戳呢,其实这个是有并且还非常的简单,下面我来给大家介绍介绍. 一.在MySQL中完成 这种方式在MySQL查询语句中转换,优点是 ...

  10. java calendar 设置时区_设置calendar时区

    iOS - Swift NSTimeZone时区 前言 public class NSTimeZone : NSObject, NSCopying, NSSecureCoding NSTimeZone ...

最新文章

  1. 【jsp】jsp的内置对象(部分)
  2. 磨刀不误砍柴工!vs2010快捷键大全
  3. linux-``反引号
  4. AOE网(求关键路径)(c/c++)
  5. php curl 传输大文件,空白目录 · php下载大文件curl · 看云
  6. vue install 报错 This is a problem related to network connectivity.
  7. Android之FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
  8. 基于FlashPaper的文档播放器
  9. 发光的二次元克拉克拉 满足年轻用户个性化、碎片化的文娱需求
  10. strictmath_Java StrictMath cos()方法与示例
  11. 微信小程序|开发实战篇之二
  12. 测试低频噪音软件,设计制作并验证0.1Hz10Hz超低频微弱噪音检测放大器要点
  13. leetcode 1518 换酒问题
  14. 416. Partition Equal Subset Sum
  15. 思科又发紧急安全通告 IOS集群管理协议漏洞和Struts2漏洞 有影响产品列表及应对措施了...
  16. 阿里云云计算 13 OSS的优势和使用场景
  17. 单片微型计算机第三版课后习题答案,单片微型计算机原理与应用_课后习题答案_山东理工.docx...
  18. CAD中插入外部参照字体会变繁体_CAD发给客户没字体怎么办?快速打包外部参照、字体、打印样式...
  19. 查询结果按中文拼音顺序排序
  20. 模拟信号的采样定理MATLAB实现

热门文章

  1. vue-cli脚手架build目录中的webpack.base.conf.js配置文件
  2. foxmail连接163邮箱服务器,win10系统下foxmail绑定或添加163邮箱的方法
  3. macbook边缘磕碰如何修复
  4. 【通俗解释】P_value 假设检验
  5. android mt4 macd,超准确的4小时MACD交易策略
  6. python 输出后面多一个None
  7. 禅修程序员十诫 [译文]
  8. 原来微信还有隐藏代码,80%的用户还不知道!(附表白代码)
  9. python保存excel文件列宽自适应解决方案
  10. 博士申请 | 香港科技大学(广州)王林助理教授招收计算机视觉博士生