LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2006, 10:58 AM   #1
bpalmer
Member
 
Registered: Mar 2005
Distribution: Debian stable, OpenBSD, FreeBSD
Posts: 30

Rep: Reputation: 15
Simple C/C++ time question


Hi,

Recently I started putting together some simple OpenGL code. I'm up to the part where I want to do smooth time-based movement. Unfortunately, I had no clue how to work with time/timers on linux.

I looked at the linux port of this tutorial: http://nehe.gamedev.net/data/lessons....asp?lesson=21 , and it used the following code:

Code:
#include <sys/timeb.h>
#include <stdio.h>

unsigned int getMilliSeconds()
{
    struct timeb tb;
    ftime(&tb);
    return tb.time * 1000 + tb.millitm;
}
However, simply displaying the result of a call to getMilliSeconds() shows a negative number! Is that supposed to be the number of milliseconds since the system was booted? What is the linux equivalent of GetTickCount() on Windows?

Thank you reading this
 
Old 03-09-2006, 11:04 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Time is never negative, its just because you must be storing the value in some signed variable, and if the value is large then it will set the sign bit and when you display the result, it will give you the negative number....

like if you are using
long t = getMilliSeconds();
try using
unsigned long t = getMilliSeconds();
 
Old 03-09-2006, 11:06 AM   #3
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
And i missed one thing...
Sorry there is no alternative to GetTickCount() in linux... You have to do the work some other way... But linux will give you more precision with the timer...
 
Old 03-09-2006, 11:14 AM   #4
bpalmer
Member
 
Registered: Mar 2005
Distribution: Debian stable, OpenBSD, FreeBSD
Posts: 30

Original Poster
Rep: Reputation: 15
Ok, I tried storing it into a unsigned long, and it works great!

One more simple question, what is the code for displaying a long in printf() ?

When I do printf("Time: %i\n", timer); it shows a negative number, but cout << timer; shows it correctly.
 
Old 03-09-2006, 11:27 AM   #5
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Printf use following formats...

%d for double
%u for unsigned long
%c for char
%x for displaying HEX
and so on.....

So to print unsigned long you have to use...

printf("Time: %u\n", timer);
 
Old 03-09-2006, 11:39 AM   #6
bpalmer
Member
 
Registered: Mar 2005
Distribution: Debian stable, OpenBSD, FreeBSD
Posts: 30

Original Poster
Rep: Reputation: 15
Excellent, thanks alot for the assistance!
 
Old 03-09-2006, 11:52 AM   #7
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Quote:
Originally Posted by bpalmer
Excellent, thanks alot for the assistance!
No problem. Always there...
Just wanted to say that, like my post helped you... So you also try if you can help anyone... That's make a community...

All the best....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
simple time problem drigz Linux - Software 2 06-26-2004 05:31 PM
Simple scripting question - running a command for a specified time FluoroAlien Programming 8 03-06-2004 12:33 AM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration