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

I am trying to format the duration (in seconds) as time, and I get results indicating that I should take the era into account somewhere. I expected os.date("%X", 0) create "00:00:00", but it will return "20:00:00" and also the date value is "12/31/69" (I don't need calendar date).

Is there a standard way to get a length of time string that calls 0 seconds to create a clock representing a total of zero seconds? I cannot find an example wherever I try.

thanks

+8
time lua duration
source share
2 answers

On most systems (i.e. POSIX), os.date("%X",0) gives you an epoch time of 00:00:00 (coordinated universal time, UTC) on January 1, 1970. You get 20:00:00 because you are in a different time zone.

To configure UTC instead of your time zone, run the format ! . This is indicated in manual .

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

+16
source share

Lua does not have an extensive standard library:

 string.format("%.2d:%.2d:%.2d", s/(60*60), s/60%60, s%60) 
+5
source share

All Articles