Ecere SDK/eC Forums • View topic - GetTime()获取的时间单位是什么?

Ecere SDK/eC Forums

Ecere Developers Community


Help and discussions in Chinese.


Postby janksenhu » Sat Feb 02, 2013 6:51 pm

是从1970年到现在的秒数吗? :D

time = GetTime();
Print("hello world") ;
time2 = GetTime();
Print((double)time-(double)time2) ;
Posts: 6
Joined: Sat Feb 02, 2013 2:45 am

Re: GetTime()获取的时间单位是什么?

Postby janksenhu » Sat Feb 02, 2013 7:48 pm

来源于网络:使用常规旧 timeGetTime() 做计时不可靠许多基于 Windows 的操作系统上,因为系统计时器的粒度可高达 10-15 毫秒,也就是说, timeGetTime() 只是精确到 10-15 毫秒为单位)。[注高粒度像 Windows NT 中,基于 NT 的运作系统出现于 2000 年和 XP。Windows 95 和 98 往往有很多好的粒度,约 1 5 毫秒。]

public Time GetTime(void)
#if defined(__WIN32__)
return timeGetTime() / 1000.0;
#elif defined(__unix__) || defined(__APPLE__)
struct timeval tp;
struct timezone tzp;
static int secbase = 0;

gettimeofday(&tp, &tzp);

secbase = tp.tv_sec;
return tp.tv_usec / 1000000.0;
return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
Posts: 6
Joined: Sat Feb 02, 2013 2:45 am

Re: GetTime()获取的时间单位是什么?

Postby jerome » Sat Feb 02, 2013 8:37 pm

Hi janksenhu,

GetTime() returns the time in seconds, from no particular moment (In Windows it happens to be the time since Windows was started, wrapping around every 49.71 days). It is meant to be used as delta only, from an earlier moment.

For getting the current time, use:

Code: Select all
DateTime now;

I just noticed, this supports only second precision (no milliseconds).

To convert this into seconds since 1970, use:

Code: Select all
SecSince1970 t;
t = now;

For cases requiring high precision, try setting the GuiApplication's timerResolution property.

If you have:

Code: Select all
define app = (GuiApplication)__thisModule;
app.timerResolution = 60; // 60 hertz

This ends up calling timeSetEvent() on Windows, which I think should increase the precision.
At some point in the past we were using timeBeginPeriod() to change that resolution, but that code is commented out right now.

Please let me know if you run into a limitation or if you find a way to improve on this :)


Site Admin
Posts: 614
Joined: Sat Jan 16, 2010 11:16 pm

Return to 欢迎使用中文讨论

Who is online

Users browsing this forum: No registered users and 7 guests