【问题标题】:Get hours from unix timestamp从 unix 时间戳获取小时数
【发布时间】:2012-12-09 11:02:24
【问题描述】:

如何从 linux 时间戳中获取当前时间?我可以得到年、月、日、分和秒,但不能得到小时。有什么帮助吗?

当前代码:

local secondsPassed = os ~= nil and os.time() or tick()
local year = 1970 + math.floor(secondsPassed / (86400 * 365.25))
local days = math.floor((secondsPassed % (365.25 * 86400)) / 86400)
days = days + (year - 2011)
local minutes = math.floor((secondsPassed % 3600) / 60)
local seconds = math.floor(secondsPassed % 60)

破碎:

local hours = math.floor((secondsPassed % 86400) / 3600)-- +1

【问题讨论】:

  • 如果你的日子是正确的,那么你的小时/分钟/秒怎么能被打破,因为它们都依赖于 secondsPassed?
  • 唯一损坏的是几个小时

标签: lua timestamp


【解决方案1】:

这里有几个问题:

  • UNIX 时间相对于格林威治标准时间 1970 年 1 月 1 日午夜。如果您不在格林威治标准时间,您将看到取决于您所在时区的偏移量。如果您所在的地区采用夏令时,则此偏移量将根据日期以一种可能相当复杂的方式发生变化。

  • 年份不是 365.25 天。根据年份,它们有 365 天或 366 天长。 (这甚至不平均为 365.25;由于特殊情况下可被 100 和 400 整除的年份,平均为 365.2425。)

除非出于某种原因您不能使用 Lua date and time modules,否则我强烈建议您这样做,而不是尝试自己重新创建它们。

【讨论】:

  • 所以如果我不加/减我的时区,会不会看起来不对?我会使用 os 日期和时间模块,但 ROBLOX 不允许你使用它们。
  • 正确;不考虑时区肯定会给你错误的时间。不过,我提到的其他问题也将适用。
【解决方案2】:

你非常亲密。到目前为止,我的时间正确:

当地小时 = math.floor((secondsPassed % 86400) / 1440) + 1

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 2019-06-10
    • 2018-10-17
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多