1

I am trying to get milliseconds with the Lua date time This is how I am getting os time.

time = os.date("%Y-%m-%d %H:%M:%S")

which returns me the date and time as 2022-08-15 08:30:40 I also want to add milliseconds(00 to 999) with it like the expected output is 2022-08-15 08:30:40.786

I tried to find it but had no success even here at Lua date time page there is no info about this https://www.lua.org/pil/22.1.html

Any help would be appreciated, Thanks

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
Zain
  • 49
  • 6
  • It is impossible: `os.date` internally invokes C function `strftime` which knows only integer amount of seconds. See the most upvoted comment under [this answer](https://stackoverflow.com/a/3673305/1847592) for a "workaround" :-) – Egor Skriptunoff Aug 15 '22 at 09:33
  • @EgorSkriptunoff Thanks for comment, can you help me with this how can i format this `239657|20220815T082447.275Z` to date and time in lua? here after the symbol **|** is the date and time. can i get it as `2022-08-08 08:24:47.275`. Help would be appreciated. Thank you in advance. – Zain Aug 15 '22 at 10:13
  • Do you want just to beautify the string (insert dashes and colons)? Or do you want to pass it as time argument to `os.date()`? `os.date` accepts integer amount of seconds since 1970, so `.275` should be thrown away. – Egor Skriptunoff Aug 15 '22 at 11:18
  • @EgorSkriptunoff I just want dashes, like out put should be `2022-08-08 08:24:47.275` , no need for the datetime, need to beautify the string. – Zain Aug 15 '22 at 11:27
  • `str = str:match"|(.*)":gsub("^(%d%d%d%d)(%d%d)(%d%d)T(%d%d)(%d%d)(%d%d%.%d*)Z", "%1-%2-%3 %4:%5:%6")` – Egor Skriptunoff Aug 15 '22 at 11:57

1 Answers1

1

It is impossible to get sub-second time resolution by using os.date.
os.date internally invokes C function strftime which does not have fractional seconds data field in the data structure it's working with.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64