Skip to main content
GameDev.net gamedev.net
🔒 Locked

time(0) problems

Started by BladeStone Jun 28, 2009 at 9:27 PM 10 replies 2.6k views
Original Post
BladeStone
BladeStone
I'm working with time, and I can't get a current time on a second try. I'm working with std::tm, std::time_t, and my books don't make any sense. My start up time code is as follows: std::time_t startTime = *time(0); std::tm curTime = * std::localtime(&startTime); int tempCurTime[3]; tempCurTime[0] = curTime.tm_hour; tempCurTime[1] = curTime.tm_min; tempCurTime[2] = curTime.tm_sec; The above works fine, but I can't figure out how to get the current time from the system after the above code. Below is what I try and it fails to compile: startTime = *time(0); curTime = * std::localtime(&startTime); Although the compiler says: time cannot be used as a function I can't find any other code to get the current time. Thank you in advance, BladeStone
BladeStoneOwner, WolfCrown.com
fastcall22
fastcall22
Unless you are using this to display the system time, why not use the return value of std::time() as an integer?
BladeStone
BladeStone
Oh gaud, Here was the problem. The code doesn't like to be in a class-function. So I made a free floating command called GetTimeNow();, see below is exactly how I was able to get around the compiler issue;

std::time_t GetTimeNow(void) return time(0);

... // some where deep into class methods/functions

std::time_t bob = GetTimeNow();

... and poof no more compiler issues, on top of that, it does work :)
BladeStoneOwner, WolfCrown.com
BladeStone
BladeStone
std::time ... I haven't heard of it, that's why, but I'll look it up.

Thank you
BladeStoneOwner, WolfCrown.com
Zahlman
Zahlman
Quote:
Original post by BladeStone
Oh gaud, Here was the problem. The code doesn't like to be in a class-function.


Do you mean "There is some kind of problem that I don't understand, if I put the code into a member function of some class"?

If so - have you considered that if that class defines a member called 'time', it will be selected in preference to the global function std::time (because the member is in a tighter scope)?

That said, your free function is probably a good thing to keep around. :)
BladeStone
BladeStone
Yes I did find I was calling two differant things time
time(0);
time[3];

which to me are two differant things, but I changed time[3] to msgTime[3]; just to be more clear to myself and to any one who might also use it.

Yes, for some reason, if you put std::time_t bob = time(0); it a class member function the system would not compile, but move time(0) into it's own stand alone function and things work fine.

New Problem relating to time:

It looks like time(0); stores the first call to time, and then never updates. How do I get the current time? I've been looking through my code and manuals, and still no skill/luck.
BladeStoneOwner, WolfCrown.com
nobodynews
nobodynews
time(0) will always return the current time, in seconds, since the epoch (probably Jan. 1, 1970. I'm not sure if the standard dictates this, but I think it does). If you call the function more than once in the same second it will return the same value for that second over and over again. the standard function clock returns the number of ticks (where the number of ticks per second is implementation defined as CLOCKS_PER_SEC), but probably isn't better than 1 ms resolution and more likely far worse on most computers.

So, depending on what you meant, the problem you might be experiencing is that resolution of std::time is too low for your needs.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
BladeStone
BladeStone
My thought is that if it's say 03 seconds and 900 milliseconds, then if I call it four times before the new second, I'll be getting 03 seconds of the current minute, but when I call it say at 04:0050 sec:milliseconds that I would be getting 04 as seconds.

Now if 03:0900 resets to 03:0000 after I call it, then that would be a problem. I'm thinking that current time is exactly that, it checks the system clock.

....

Which might mean that current time(0) isn't making it into my varibles ... I'll go check.
BladeStoneOwner, WolfCrown.com
Fire Lancer
Fire Lancer
I cant remember if it rounds to the nearest second or just rounds down. However either way it just reads the time, doesn't change it. What exactly do you want it for though? Usually its much useful to a program in its integer form and you also depending on what your doing may want a time method with a higher precision.

I certainly don't see the value of sticking parts of the tm struct into an array.
BladeStone
BladeStone
It's for a 15 second fade timer on screen displayed messages.

12:00:00 new message is added for display
12:00:15 new message is going to fade out and be removed from the screen

so I get current time, add 15 seconds to it, and set it to be compaired with current time, it it's equal or less then current time, then we turn on the fade, and poof, it fades and removed.
BladeStoneOwner, WolfCrown.com
nobodynews
nobodynews
Whether it rounds or not it will behave the same in both circumstances, give-or-take 0.5 seconds. I'm assuming you will be 'polling' the system time much more frequently than once a second (say, at least 100 times a second or more) for that to be true. If you don't want it to be off by even that much (i.e., to occur exactly at 12:00:00 and last for exactly 15 seconds within a few dozen milliseconds) then you'll need a more accurate timing function than std::time, I believe.

That is to say, if you actually want the message to occur at a specific moment in real-time, you wish for more accuracy than std::localtime and std::time allows, and you are on windows consider the Win32 API functions GetLocalTime and GetTickCount (you can use them in console applications, but you will need to include the windows.h header and link to Kernel32.lib) for more accuracy than std::time allows. That said, you probably just want something like this:
bool IsTimeX(int hour, int minute, int second){  time_t rawtime;  struct tm * timeinfo;  time ( &rawtime );  timeinfo = localtime ( &rawtime );  if(timeinfo->tm_hour == hour && timeinfo->tm_min == minute && timeinfo->tm_sec == second)  {    return true;  }  return false;}void WaitFor12AndFadeAfter15Seconds(){    while(!IsTimeX(12, 0, 0));    time_t curTime = time(0);    curTime += 15;    while(curTime < time());    FadeFunction();}
I leave it as an exercise for the reader to allow other computation to occur instead of actually waiting for the event to occur. The IsTimeX function will of course fail if your application somehow doesn't receive a time slice for the one second you are looking for. I think a better implementation would figure out the time you are looking for and wait until time returns an integer greater than or equal to that moment when converted to seconds.

Yet another possibility is using the Win32 SetTimer function, but although you can specify a callback, I'm not sure if you can use this in a console program easily as it is meant to be used with a window procedure. I am curious about that, so I will investigate.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
nobodynews
nobodynews
Yes, it is possible.
#include <windows.h>#include <iostream>bool quit = false;VOID CALLBACK TimerProc ( HWND hParent, UINT uMsg, UINT uEventID, DWORD dwTimer ){	quit = true;}int main(){	UINT myTimer = SetTimer ( NULL, 0, 5000, TimerProc );	std::cout << "Starting clock #" << myTimer << "...\n";	MSG msg;          // message structure 	while( GetMessage(&msg, NULL, 0, 0) )	{		TranslateMessage(&msg);		DispatchMessage(&msg);		if(quit)		{			break;		}		// perform program actions here	}	std::cout << "5 seconds ellapsed!\n";	std::cout.flush();	KillTimer(NULL, myTimer);}
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.