AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FUNCTION: Acuratime() ;Keeps precise time to .0001 seconds

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
philz



Joined: 05 Jun 2007
Posts: 87
Location: USA

PostPosted: Wed Dec 12, 2007 10:35 am    Post subject: FUNCTION: Acuratime() ;Keeps precise time to .0001 seconds Reply with quote

I was looking for a way to keep accurate, precise time in terms of AHK's YYYYMMDD24-HRMISS format and extending it to YYYYMMDD24-HRMISSMILISEC (which includes a decimal for tenths of miliseconds)

I have thought of three methods, each with different good and bad things.

Here is the code for the first method:
It uses urldownloadtovar() which can be found here.
Code:

acuratime()
{
   x := urldownloadtovar("http://132.163.4.101:14")
   secmilla := substr(x,33,6) = 1000 ? SubStr(x,23,2)+1 "0000.0" : SubStr(x,23,2) "0" substr(x,33,6)
   timestring := A_YYYY substr(x,11,2) substr(x,14,2) substr(x,17,2) SubStr(x,20,2) secmilla
   return,  %timestring%
}

This is precise to one tenth of a milisecond. The problem is that It will lag because of the need to download from a time server, so accuracy is an issue.

Here is the code for the second method:
Code:
SetWorkingDir, %a_scriptdir%

loop {
FileAppend, % precisetime() "`n",dump.txt
SplashTextOn,  520, 40, It is now, % precisetime()
}
;use this to test it.

precisetime()
{
;the logic behind this is that the last three digits of a_tickcount will
;have a certain value when the second of a_now changes. So by determining
;this value, we can use it alongside a_tickcount to determine miliseconds
;that fit out own nice timestamp.
   static offset
   startcount := A_tickcount
   starttime := A_now
   IF(offset = "") ;determine the value of offset
      loop
      {
         if( a_now - starttime > 0) {
            offset := substr(a_tickcount - startcount, -2)
            break
         }
      }
   if( substr(a_tickcount, -2) - offset < 0) {
      if( strlen(substr(a_tickcount,-2) + 1000 - offset)<4) {
         Loop, % 4 - strlen(substr(a_tickcount,-2) + 1000 - offset)
            milisec := "0" substr(a_tickcount,-2) + 1000 - offset
      }
      else milisec := substr(a_tickcount,-2) + 1000 - offset
   }
   else if( strlen(substr(a_tickcount,-2)- offset)<4) {
         Loop, % 4 - strlen(substr(a_tickcount,-2)- offset)
            milisec := "0" substr(a_tickcount,-2) - offset
   }
   Time := a_now milisec
   return, %time%
}

This has two issues.
One is that sometimes the milisecond part doesn't always turn out to be four digits long. I don't know why.
The other issue is that sometimes the miliseconds revert to zero when they should not. This makes times inaccurate to no more than one half of a second.

While posting I came up with a third Idea:
Code:

precisetime()
{
return, % a_now "0" substr(a_tickcount -140,-2) ;I subtract 140 to bring accuracy but it doesn't always work
}
;this one seems to work best because I noticed that an a_tickcount
;second changes almost perfectly in line with with a_now.

It is the simplest but the seconds revert in the wrong place sometimes.

Any Ideas on fixes????

I decided to put this into the Scripts & Functions forum because the acuratime works fine except for the lag in downloading from a time server. If you guys have qualms about my placement of the post It will be moved.
Back to top
View user's profile Send private message Send e-mail
BoBoĻ
Guest





PostPosted: Wed Dec 12, 2007 2:03 pm    Post subject: Reply with quote

[Brainfart]wouldn't it be possible to adjust the detected timestamp with getting&adding the time the download itself has needed? [/Brainfart]
Back to top
philz



Joined: 05 Jun 2007
Posts: 87
Location: USA

PostPosted: Wed Dec 12, 2007 8:03 pm    Post subject: Reply with quote

BoBoĻ wrote:
[Brainfart]wouldn't it be possible to adjust the detected timestamp with getting&adding the time the download itself has needed? [/Brainfart]


Are you suggesting I determine the download time via taking a_tickcount before and after then adding the difference to the time? It may add some accuracy. I still think there must be a better way to do this though.
Back to top
View user's profile Send private message Send e-mail
philz



Joined: 05 Jun 2007
Posts: 87
Location: USA

PostPosted: Thu Dec 13, 2007 6:36 pm    Post subject: Reply with quote

looks like the internet download method won't work anyway.
The time server doesn't include miliseconds.

Yhis seems to be the most accurate method. I cannot make autohotkey reach perfection on this. This is as good as I can get it.

Code:
precisetime()
{
   static offset, timecalled = 1
   if %Timecalled%
   {
      timecalled = 0
      starttime := A_now
      loop {
         if( A_now - starttime) {
            offset := a_tickcount - 5
            offset := substr(offset,-3)
            break
         }
      }
   }
   return, % a_now substr(a_tickcount - offset,-2)
}
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group