I was programming an application, when I found a need to get milliseconds. How can I get realtime milliseconds? OS.clock on gives us CPU time, not realtime time. OS.time only gives me seconds. I see that this is Lua’s fault, but it would be nice if you could put this in the app.
Os.clock gets real time seconds since startup - if you grab elapsed time as the first thing in startup and do some math, you can derive it. Ugly, yes. I’d do a code sample, but I’m in a meeting and this is my spine responding.
Alright. Thanks. I From what I saw, it only returned seconds. If you could post an example, that would be great. Thanks.
Ok, I’ve officially lied - but I blame the docs!
os.time() on a POSIX system (which this is) should return seconds since epoch. But - it doesn’t, it returns (apparently) seconds since the start of the program run.
os.clock seems to return fractional seconds - but I have no idea what the first portion is.
We could presumably parse os.date(), but “meh”. (it wouldn’t be THAT bad - it returns a table, and you could do the math, but you’d have to account for leap years and that jazz).
Ooh. Hold the phone - looking closer, os.time() returns (for me):
I missed the “e+” in there. I blame a small font and old eyes. So - that’s 1325490000 roughly. I suspect the extra precision is there, it’s just not in print() output. Still digging.
Lua only uses floating point numbers - end of story.
This particular version of Lua uses 32 bit floating point numbers. See http://twolivesleft.com/Codea/Talk/discussion/comment/1244#Comment_1244
An Epoch timestamp is a 32 bit integer number (and I believe will roll over in 2038).
So - we probably do not have enough precision in a variable to hold epoch. Yucko.
Can we work around it? Presumably by parsing os.date() and keeping time in multiple variables? Yep.
Will it be pretty? Nope.
Can most of the ugly be encased in a Class so normal people don’t need to look at it every day? Probably.
Will I write that class? Maybe.
Why? Because I can.
Will I hope Andrew (or Nat or blanchot or ruilov or YOU or someone else entirely) beats me to it? Yes.
Will I count on that? No.
“Why? Because I can.” Your so humble but it’s true. I’m also glad you like talking to yourself. Here is the code I have:
local origin = vec2(WIDTH/2, HEIGHT/2) local timeAppStarted = os.clock() -- os.time() local x = -WIDTH/2 local y = 0 function timePassed() return os.clock() - timeAppStarted --local diff = os.difftime(os.time(),timeAppStarted) --return diff end function setup() print("This is a grapher.") backingMode( RETAINED ) end function f(x) local y = math.abs(x) y = x*x / 300 / 1.5 return y end local looptime = 5 local printCount = 100 function draw() background(52, 255, 0, 255) fill(92, 92, 92, 255) stroke(239, 255, 0, 255) strokeWidth(5) line(0,HEIGHT/2,WIDTH,HEIGHT/2) line(WIDTH/2,0,WIDTH/2,HEIGHT) translate(origin.x, origin.y) stroke(0, 84, 255, 255) --USE MATH.FMOD x = WIDTH*(math.fmod(timePassed(),looptime)/looptime)-(WIDTH/2) -- if timePassed() >= ttdraw then local y = f(x) -- end sprite("SpaceCute:Star",x,y) printCount = printCount - 1 if printCount < 0 then print(timePassed()) printCount = 100 end end
AHHG! Code isn’t working right!
I’m also away from my Mac and I can’t figure out how to put a .codea file into Textastic. I also haven’t signed up for that website where you can email files. This is good for now.
If it beats you @Bortels, it beats everybody. Our last resort is probably @Simeon. Thanks for looking at it Bortels.
Nah. There’s plenty of people here who could write this - what I lack in smarts I make up for with enthusiasm, and willingness to write nasty code.
Besides - @Simeon can cheat! (@Simeon - if we got a function to return epoch as a string, we could presumably chop it apart ourselves. Alternatively, is there a lua Bignum library? I could google for it I guess…)
Milliseconds isn’t possible in any lua programming environment. Look at the official lua documentation. I’m just curiouse, is all those functions useable in Codea? Not all are listed.
mmm - miliseconds we can (and do) get. os.clock returns fractional seconds, that go up on a sub-second basis. I don’t know how accurate they are, but you can get fractional seconds for sure.
Well you saw my code… Run it and Look at the output. It’s strange… Most of the other languages do this… (I’m waiting for Simeon for extra help)
edited your post - use three tildes (~) to start and stop code sections.
Thanks. That would have been nice to be mentioned in the Markdown syntax reference I looked at.
@Zoyt do you want the time since the program started, in milliseconds?
Perhaps I’m missing something, but can’t you just do the following?
function setup() milliseconds = 0 watch("milliseconds") end function draw() milliseconds = ElapsedTime * 1000 end
Oh. Thank you. I was looking under the Lua section for time. I guess I was missing something. This would be a great reason to add the search functions ability.
use three tildes (~) to start and stop code sections.
Thanks Mr. Bortels! I was trying to figure that out yesterday when I wanted to format some code and found the ‘code’ tag doesn’t work very well (other than disappear when you want to show it grrr…).
Perhaps a sticky or sidebar link to the forum’s markdown syntax is in order? It would save my own aged and sorely tried memory and maybe even save you and Andrew some work.
On that note since this forum has rapidly become the de facto home base for the Codea community might I also suggest a link from the main discussion page to that other wellspring of Codea goodness: the wiki? At the moment there is no clear way to get from here to there (or if there is I’m not seeing it… a couple of times I’ve clicked on the wiki tag to find a post with the link).
Edit: Just rooted around and saw that the wiki link is in the FAQ. Good… I’ll try to remember that in the future. A forum syntax/markdown link would also be welcome in the FAQ…
TLL/Andrew/Mr. Bortels: I would still consider promoting the wiki link to the main forum page (I noticed that @Andrew_Stacey has done some refactoring/made additions there recently. Thanks Andrew!) The wiki is important.
A sidebar - a floating one, that stayed on the screen when you scrolled the page - with wiki syntax and links to the app, to the wiki, and to the base lua docs - would be DAMN HANDY.
Agreed, but this is hosted by Vanilla, a sepreate company. I don’t know if the forum manager can change the HTML or CSS.