Using Lua to format 0 seconds as 00:00:00

流过昼夜 提交于 2019-11-30 03:44:07

问题


I'm trying to format a duration (in seconds) as a time and I'm getting results indicating that I'm supposed to account for an epoch somewhere. I expected os.date("%X", 0) to produce "00:00:00" but it is returning "20:00:00" as well as a date value of "12/31/69" (I don't need a calendar date though).

Is there a standard way of getting a time duration string that causes 0 seconds to produce a clock representing a total of zero seconds? I can't seem to find an example anywhere of what I'm trying to do.

Thanks


回答1:


In most systems (ie, POSIX), os.date("%X",0) gives you the time of the epoch, which is 00:00:00 (Coordinated Universal Time, UTC), 1 January 1970. You get 20:00:00 because you're in a different time zone.

To force UTC instead of your time zone, start the format with !. This is mentioned in the manual.

So, use os.date("!%X",0) to get 00:00:00 as desired. It'll work with any number of seconds less than a day (86400). For instance, os.date("!%X",70) gives 00:01:10: 1 minute and 10 seconds.




回答2:


Lua does not have a vast standard library:

string.format("%.2d:%.2d:%.2d", s/(60*60), s/60%60, s%60)


来源:https://stackoverflow.com/questions/17474312/using-lua-to-format-0-seconds-as-000000

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!