2

i want to convert seconds to iso 8601 format in Lua, But i'm not able to find it, can anyone help? ex: 60.001 secs --> PT1M0.001S

sam
  • 25
  • 4

2 Answers2

0

Start with

t = os.date("*t",os.time({day=1, month=1, year=2000, hour=0, min=0, sec=60.001}))

Then build the string from the fields in t, subtracting 1 from t.day and t.month and 2000 from t.year. The call to os.time takes care of the arithmetic, which is the boring part, but unfortunately does not support fractions of seconds.

The code above worked before Lua 5.3 but it now raises an error when sec does not contain an integer. A better solution is

sec = 60.001
fsec = sec%1
sec = math.floor(sec)
t = os.date("*t",os.time({day=1, month=1, year=2000, hour=0, min=0, sec=sec}))
t.sec = t.sec + fsec
lhf
  • 70,581
  • 9
  • 108
  • 149
-1

You want use only such time periods as 60s / 180 s.

If you will use timestamps, maybe that solution can help?

Timestamp to ISO 8601 in Lua

A. Denis
  • 562
  • 7
  • 15