Azs*_*sgy 11 time lua datetime timestamp strftime
API返回一个时间戳作为UTC时间戳的UNIX时间戳,我想知道这个时间戳是否超过x几秒钟.正如预期的那样,这os.time() - x > timestamp在UTC中运行良好,但在其他时区爆炸.
不幸的是我找不到一个好方法来解决这个问题.
os.date有用地具有!前缀(例如os.date("!%H:%M:%S"))以返回UTC的时间,但似乎尽管文档声明它支持所有strftime选项,但这不支持该%s选项.我听说有人提到这是由类似问题的Lua编译时选项引起的,但由于解释器是由用户提供的,因此无法更改这些选项.
Joe*_*Joe 10
您可以使用
os.time(os.date("!*t"))
获得当前的UNIX时代.
好的,所以你想要 UTC 时间。请记住,os.time实际上对 timezones 一无所知,例如:
os.time(os.date("!*t"))
Run Code Online (Sandbox Code Playgroud)
所以你实际上会得到你的 UNIX_TIME - TIMEZONE_OFFSET。如果您在 GMT+5,您将获得 UTC-5 的时间戳。
在lua中进行时间转换的正确方法是:
os.time() - get current epoch value
os.time{ ... } - get epoch value for local date/time values
os.date("*t"),os.date("%format") - get your local date/time
os.date("!*t") or os.date("!%format") - get UTC date/time
os.date("*t", timestamp),os.date("%format", timestamp) - get your local date/time for given timestamp
os.date("!*t", timestamp) or os.date("!%format", timestamp) - get UTC date/time for given timestamp
Run Code Online (Sandbox Code Playgroud)
在https://gist.github.com/ichramm/5674287 上向Mons致敬。
如果您确实需要将任何 UTC 日期转换为时间戳,那么在这个问题中有一个关于如何执行此操作的很好的描述:Convert a string date to a timestamp
| 归档时间: |
|
| 查看次数: |
9256 次 |
| 最近记录: |