Introduction
This article solves a simple yet tricky problem! Time estimations !
Did you ever create a function to download a file from the web but you couldn't calculate the transfer rate , estimate the time ...If you tried you got the Division By Zero fatal message! Well this class can do many great things just by a few function calls.
CxProgress
typedef unsigned long u_long;
typedef unsigned int u_int;
class CXProgress
{
public:
CXProgress();
void SetRange(u_long prgmin, u_long prgmax);
const char * FormatLeftTime();
const char * FormatElapsedTime();
u_long ToComeSeconds();
bool StepIt(u_long steps=1);
bool SetStep(u_long step=1);
u_long RatePerHour();
u_long RatePerMinute();
u_long RatePerSecond();
u_long Rate(u_long milisecs=1000);
u_long ElapsedMilisecs();
u_long ElapsedDays();
u_long ElapsedHours();
u_long ElapsedMinutes();
bool IsTimeElapsed();
bool DecreaseMinPos(u_long num);
bool EncreaseMinPos(u_long num);
bool DecreaseMaxPos(u_long num);
bool IncreaseMaxPos(u_long num);
u_long ElapsedSeconds();
u_int GetPercentLeft();
u_int GetPercentElapsed();
u_long GetLeftPos();
u_long GetCurrentPos();
bool Can();
u_long LeftDays();
u_long LeftHours();
u_long LeftMinutes();
u_long LeftSeconds();
bool SetCurrentPos(u_long);
CXProgress(u_long, u_long);
virtual ~CXProgress();
private:
u_long startMom,nowMom;
long startTime,nowTime;
u_long prgMin;
u_long prgMax;
u_long curPos;
u_long numStep;
char buf[512];
};
Using the code
The code using method is most easy :
void main(void)
{
CXProgress prg(0,100);
int cur=0;
while(cur<100)
{
cur++;
prg.SetCurrentPos(cur);
Sleep(1000);
printf("%s -",prg.FormatElapsedTime());
printf(" %s - Rate: %d\r",prg.FormatLeftTime(),prg.RatePerSecond());
}
}
As you see it's ideal for internet transfers , for getting rate ...
Documentations
How to use each function :
void SetRange |
u_long prgmin - start range position=0
u_long prgmax - end range position
It can have values between the two values. |
const char * FormatLeftTime |
Returns 00:00:00 format of left time. |
const char * FormatElapsedTime |
Returns 00:00:00 format of elapsed time. |
u_long ToComeSeconds |
left time in seconds. |
bool StepIt |
u_long steps= 1 - increase current position with steps *step |
bool SetStep |
u_long step=1 - step size - usually 1 |
u_long RatePerHour |
Estimated progress rate per hour. |
u_long RatePerMinute |
Estimated progress rate per minute. |
u_long RatePerSecond |
Estimated progress rate per second. |
u_long Rate |
u_long milisecs =1000 - progress rate for intervals less a second. |
u_long ElapsedMilisecs |
Elapsed time in milliseconds. |
u_long ElapsedDays |
Elapsed time in days. |
u_long ElapsedHours |
Elapsed time in hours. |
u_long ElapsedMinutes. |
Elapsed time in milliseconds. |
bool IsTimeElapsed |
Did any time elapse from the beginning. |
bool DecreaseMinPos |
u_long num - make minPos =minPos -num |
bool EncreaseMinPos |
u_long num - make minPos =minPos +num |
bool DecreaseMaxPos |
u_long num - makes maxPos =maxPos -num |
bool IncreaseMaxPos |
u_long num - make maxPos =maxPos +num |
u_long ElapsedSeconds |
Elapsed time in seconds. |
u_int GetPercentLeft |
% to come. |
u_int GetPercentElapsed |
% over. |
u_long GetLeftPos |
Returns left positions. (maxPos -curPos ) |
u_long GetCurrentPos |
Returns current position. |
bool Can |
If it has any internal valid times to estimate. |
u_long LeftDays |
Left time in days. |
u_long LeftHours |
Left time in hours. |
u_long LeftMinutes |
Left time in minutes. |
u_long LeftSeconds |
Left time in seconds. |
bool SetCurrentPos |
u_long pos - set current pos to pos |
History
- Only release 6:31 AM 3/15/2003 (I think)
Last but not least
And if you did not understand anything look at the code. You can't miss it. You'll get it for sure! :~)
The source code will reveal its darkest secrets to your mortal eyes by only looking at it. And I'll let you in on another little secret of mine. You can use the very ancient but long forgotten copy-paste technique if the complexity of the source code overwhelms you. And if you need more information check the holy book of software engineers, the forbidden MSDN. Many words of wisdom will be discovered within its dirty and dusty pages.
And to find out the truth about our existence, humanity, universe and aliens mail me.