Author Topic: function time()  (Read 779 times)

lost_mind

  • Newbie
  • *
  • Posts: 8
    • View Profile
function time()
« on: September 11, 2016 »
can i find an example with time() funct heavily described?
I would like to know how to retrieve the precise time of my system(e.g do a clock)
Or increment a var every second.
if i understand well time() return milsec so if i divide by 1000 it give me second.
But other than that i am a bit confused.

Thanks and see you

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: function time()
« Reply #1 on: September 11, 2016 »
Timer ?

Code: [Select]
set window 100, 40, 600, 400

t = time()
'wait .5 sec or 5 sec comment out 5000
delay = 500
delay = 5000
x = 0
while (time() - t < delay)
x = x + 1
wend
wln x/1000000, " Time's up! press any..."
wait keydown

EDIT: I changed above code.

I don't know why time() returns negative numbers, don't know how to relate it to time of day.
« Last Edit: September 11, 2016 by B+ »
B+

cvirus

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Re: function time()
« Reply #2 on: September 11, 2016 »
You can use an extension names ksink. http://www.naalaa.com/forum/index.php?topic=332.0

lost_mind

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: function time()
« Reply #3 on: September 11, 2016 »
ok thanks
 this stuff seem a bit confusing.
I will try another way maybe.
I would be curious marcus explain what does time() function return, and maybe naalaa itself could format it.

Great thank anyway

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: function time()
« Reply #4 on: September 11, 2016 »
ok thanks
 this stuff seem a bit confusing.
I will try another way maybe.
I would be curious marcus explain what does time() function return, and maybe naalaa itself could format it.

Great thank anyway

Are you aware of strict adherence to floats (versus integers) yet?
B+

lost_mind

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: function time()
« Reply #5 on: September 12, 2016 »
No not aware of that.
I am not a math man anyway...
But sure thing is Naalaa is very friendly user stuff...
Wait and see if marcusgot some time to light up stuff about time() function.
thanks and see you

and i have tried that:
Code: [Select]
'test 11092016

visible :
var = 0

set window 0, 0, 320, 240, false, 2
set redraw off

do

set color 0,0,0
cls
set color 255, 255, 255
set caret 0,0
wln "Press space bar to exit ..."

set caret 50,50
randomize time()
wln time()/1000
var = var + time()
wln var
redraw
until keydown(" ", true)

and the var = var + time sometimes give negative number it is very strange
« Last Edit: September 12, 2016 by lost_mind »

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: function time()
« Reply #6 on: September 12, 2016 »
Hi lost_mind,

From your code, I see something you might be after that I missed.

I have converted my example of timer to using floats and show seconds elapsed until time's up.
This should give you a taste of what is needed to do math with floats. Maybe you know already?

The thing I did not fully realize was that the absolute value of time() actually decreases as time progresses!!!
How NOT intuitive is that!?!?

Code: [Select]
' timer.txt for Naalaa [B+=MGA] 2016-09-12

' 2016-09-12 mods
' what is with time() that time start - current time > 0 ?
' mod revise timer.txt with floats to show fractions of secs passing

' note: one trick with using floats is to remember number literals have
'       to look like floats too! with decimal and 0 eg
'       for .1 use 0.1
'       for 5  use 5. or 5.0
'          Also it helps to use # after first declared to remember float math
'       var# = 0.0
'       var# = 10.0 + 5.0 * var#

set window 100, 40, 600, 400
set redraw off

start# = float(time())
wln "float(time()) = start: ", start#

'to wait .5 sec comment out 2nd delay# = 5.0

'notice error if I use just .5 in next
delay# = 0.5

'notice error if I use just 5 in next
delay# = 5.0

wln "We will signal when ", delay#, " seconds have passed."

'notice errors if I use 0 or .0 in next, floats apply to number literals
t# = 0.
'I like
t# = 0.0

while t# < delay#

'!!!!!!!
' unlike with procedure wait ms, you can do things while "timer" is counting down

'blank out last reading
set colori 0
draw rect 0, 40, 600, 400 - 40, true

'show next
set caret 10, 40
set colori 0xffffff
current# = float(time())
wln "float(time()) = current time(): ", current#

'OH, the current time is decreasing in absolute size !!!!!!!!!!!!!!!!!!!!!!!

'OK, since current < start in absolute terms subtract current from start
'since both are negative multiply by -1 for show and compare to positive delay#
'divide by 1000 to convert into secs, approximately
t# = -1.0 * (start# - current#) / 1000.0
wln "Estimate secs passed: ", t#
redraw
wend
wln "Time's up! press any..."
redraw
wait keydown
« Last Edit: September 12, 2016 by B+ »
B+

lost_mind

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: function time()
« Reply #7 on: September 14, 2016 »
Ok
I have tried your example (BTW thanks for your time)
I understand better the stuff now.
I will try to adapt my code from your example.
But defintly not very intutive with all the respect that is due.
Great thank
see you

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
Re: function time()
« Reply #8 on: July 14, 2017 »
Oh god, I've missed some threads :D

The function time() works just like timeGetTime in Windows (C++) and returns the time elapsed since Windows started :)

In the next update I'll change it to return the time that's passed since the program started instead, makes more sense (atleast for me).




« Last Edit: July 14, 2017 by Marcus »
.\\\opz

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
Re: function time()
« Reply #9 on: July 28, 2017 »
Yeah, someone reported negative time() values. I guess that could happen if your computer had been turned on for very long. Naalaa only supports signed integers, and as C's timegettime returns a DWORD (an unsigned int) some weird stuff could occur. I've fixed it and it'll be in the next update, that will be released within two weeks (finishing up the GLOOM library).
.\\\opz