Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to turn a timestamp (2017-09-17T19:25:27.9843202Z) into a os.time() value?

Asked by 6 years ago

So im trying to make a script that gets a timestamp from productinfo of a model and takes that value away from os.time() so i know how long ago the model was updated. I have no idea how i would do this and not sure if its even possible, if it is please tell me. Thanks!

2 answers

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
6 years ago

That timestamp you posted is in ISO-8601.

After some Googling, I found that someone made their own function to convert this into UST (Unix Standard Time), which is what os.time() returns time as.

-- https://forum.rainmeter.net/viewtopic.php?t=23486
function TimeStamp(dateStringArg)

   local inYear, inMonth, inDay, inHour, inMinute, inSecond, inZone =     
  string.match(dateStringArg, '^(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)(.-)$')

   local zHours, zMinutes = string.match(inZone, '^(.-):(%d%d)$')

   local returnTime = os.time({year=inYear, month=inMonth, day=inDay, hour=inHour, min=inMinute, sec=inSecond, isdst=false})

   if zHours then
      returnTime = returnTime - ((tonumber(zHours)*3600) + (tonumber(zMinutes)*60))
   end

   return returnTime
end

And you can use it like this:

local t = "2017-09-17T19:25:27.9843202Z"
print(TimeStamp(t)) -- 1505676327, which seems about right.
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This is really easy using multiplication.

function ConvertYear(value) --converts a year to seconds
    return value *3.154e+7
end

function ConvertMonth(value) --converts a month into seconds
    return value * 2.628e+6
end

and so on. Also, months may not be accurate as they can have 29 days, 30 days, or 31. Why would you want to convert this to seconds? It is harder to read when it is in seconds.

Hope this helps!

0
what would value be? outlook1234567890 115 — 6y
0
the year like 2017 hiimgoodpack 2009 — 6y
1
When you're trying to compare one date with another, converting it to seconds (typically UST) is easier than comparing each component separately. XAXA 1569 — 6y

Answer this question