One second timer implementation using Open C++

__NOTOC__
__NOEDITSECTION__

{|style=”background:#eceff2″ width=”660px” border=”1″ cellpadding=”5″ cellspacing=”0″
|-
|”’ID”’ ||  
|”’Creation date”’ || September 5, 2008
|-
|”’Platform”’ || S60 3rd Edition, FP2
|”’Tested on devices”’ || Nokia 6220 Classic
|-
|”’Category”’ || Open C/C++
|”’Subcategory”’ || Files/Data
|}

{|style=”background:#eceff2″ width=”660px” border=”1″ cellpadding=”5″ cellspacing=”0″
|-
|”’Keywords (APIs, classes, methods, functions):”’ time_t, time(), difftime()
|}

==Overview==

This code snippet shows how to create a simple one second timer class using Open C++. This implementation is based on <tt>time()</tt> and <tt>difftime()</tt> functions which are declared in time.h header file. The ExampleTimer class has methods to start, stop and reset the timer. The timer class offers getter methods for time and state as well.

”’Note”’: In order to use this code, you need to install [http://www.forum.nokia.com/info/sw.nokia.com/id/91d89929-fb8c-4d66-bea0-227e42df9053/Open_C_SDK_Plug-In.html Open C/C++ plug-in].

This snippet can be self-signed.

==MMP file==

The following libraries are required:

<code>
LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib
</code>

==Header file==
<code cpp>
#ifndef EXAMPLETIMER_H_
#define EXAMPLETIMER_H_ // INCLUDES #include <time.h> // CLASS DECLARATION class ExampleTimer { public: ExampleTimer(); void Start(); void Stop(); void Reset(bool restart); int GetTime(); bool IsRunning() { return m_isrunning; } bool IsReset() { return m_isreset; } private: time_t m_starttime; time_t m_stoptime; bool m_isreset; bool m_isrunning; };
#endif // EXAMPLETIMER_H_
</code>

==Source file==

<code cpp>

#include “ExampleTimer.h”

ExampleTimer::ExampleTimer(): m_starttime(0),
m_stoptime(0),
m_isreset(false),
m_isrunning(false)
{
}

void ExampleTimer::Start()
{ if(!m_isrunning) { if(m_isreset) { time(&m_starttime); } else { time_t now = time(&now); time_t tmp = (time_t)difftime(m_stoptime, now); m_starttime = (time_t)difftime(m_starttime, tmp); } m_isrunning = true; m_isreset = false; }
}
void ExampleTimer::Stop()
{ if(m_isrunning) { m_isrunning = false; time(&m_stoptime); }
}
void ExampleTimer::Reset(bool restart)
{ Stop(); m_isreset = true; m_starttime = m_stoptime; if(restart) { Start(); }
}
int ExampleTimer::GetTime()
{ if(m_isrunning) { time_t now = time(&now); return (int)difftime(now, m_starttime); } else { return (int)difftime(m_stoptime, m_starttime); }
}
</code>

== Example Class Usage ==

Simple example program demonstrating the ExampleTimer class usage:

<code cpp>
#include <stdio.h> //atoi()
#include <string> //string, getline()
#include <iostream> //cout, cin

#include “ExampleTimer.h”

using namespace std;

void ShowMenu() { cout << ” 1 = Start/Stop Timer” << endl; cout << ” 2 = Reset Timer” << endl; cout << ” 3 = Reset Timer and Restart” << endl; cout << ” 4 = Show Timer State” << endl; cout << ” 5 = Show Timer Time” << endl; cout << ” 0 = Quit Test Program” << endl; cout << endl; cout << “>”; }

int main()
{ int choice = -1; string selection; bool exit = false;
ExampleTimer timer; while(!exit) { ShowMenu(); getline(cin, selection); // NOTE: atoi will convert also values e.g. “xyz” and “\n” to ‘0′ choice = atoi(selection.c_str());
switch(choice) { case 1: //Start/Stop if(timer.IsRunning()) { timer.Stop(); cout << “[timer stopped]” << endl; } else { timer.Start(); cout << “[timer started]” << endl; } break; case 2: //Reset timer.Reset(false); cout << “[timer reset]” << endl; break; case 3: //Reset and Restart timer.Reset(true); cout << “[timer reset and restart]” << endl; break; case 4: //Show State cout << “[timer state]” << endl; cout << “Running:” << timer.IsRunning() << endl; cout << “Reset:” << timer.IsReset() << endl; break; case 5: //Show Time cout << “[timer time]:” << timer.GetTime() << ” sec” << endl; break; case 0: //Quit exit = true; break; default: cout << “Please, enter only numeric values between 0 and 5!” << endl; } return 0; }
</code>

==Postconditions==

The example program prompts user to enter menu selections between 0 and 5. According to selections the example timer is started, stopped or asked to show the current state until 0 is entered and the program exits.

<!– ==See also== –>



Thank you for reading this post. You can now Leave A Comment (0) or Leave A Trackback.

Post Info

This entry was posted on Sunday, September 7th, 2008 and is filed under Insurance.

You can follow any responses to this entry through the Comments Feed. You can Leave A Comment, or A Trackback.



Previous Post: Nokia: Q3 2008 market share may drop »
Next Post: Pixelmator puts new shortcuts in 1.2.3, pitches 1.3 Tempo beta »

Read More

Related Reading:



Leave a Reply

Note: Any comments are permitted only because the site owner is letting you post, and any comments will be removed for any reason at the absolute discretion of the site owner.

You must be logged in to post a comment.

One second timer implementation using Open C++

ID  
Creation date September 5, 2008

Platform S60 3rd Edition, FP2
Tested on devices Nokia 6220 Classic

Category Open C/C++
Subcategory Files/Data

Keywords (APIs, classes, methods, functions): time_t, time(), difftime()

Overview
This code snippet shows how to create a simple one second timer class using Open C++. This implementation is based on time() and difftime() functions which are declared in time.h header file. The ExampleTimer class has methods to start, stop and reset the timer. The timer class offers getter methods for time and state as well.
Note: In order to use this code, you need to install Open C/C++ plug-in.
This snippet can be self-signed.

MMP file
The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib
Header file
#ifndef EXAMPLETIMER_H_
#define EXAMPLETIMER_H_
 
// INCLUDES
#include <time.h>
 
// CLASS DECLARATION
class ExampleTimer
{
public:
ExampleTimer();
void Start();
void Stop();
void Reset(bool restart);
int GetTime();
bool IsRunning() { return m_isrunning; }
bool IsReset() { return m_isreset; }
private:
time_t m_starttime;
time_t m_stoptime;

bool m_isreset;
bool m_isrunning;
};
 
#endif // EXAMPLETIMER_H_
Source file
#include "ExampleTimer.h"
 
ExampleTimer::ExampleTimer(): m_starttime(0),
m_stoptime(0),
m_isreset(false),
m_isrunning(false)
{
}
 
void ExampleTimer::Start()
{
if(!m_isrunning)
{
if(m_isreset)
{
time(&m_starttime);
}
else
{
time_t now = time(&now);
time_t tmp = (time_t)difftime(m_stoptime, now);
m_starttime = (time_t)difftime(m_starttime, tmp);
}
m_isrunning = true;
m_isreset = false;
}
}
void ExampleTimer::Stop()
{
if(m_isrunning)
{
m_isrunning = false;
time(&m_stoptime);
}
}
void ExampleTimer::Reset(bool restart)
{
Stop();
m_isreset = true;
m_starttime = m_stoptime;
if(restart)
{
Start();
}
}
int ExampleTimer::GetTime()
{
if(m_isrunning)
{
time_t now = time(&now);
return (int)difftime(now, m_starttime);
}
else
{
return (int)difftime(m_stoptime, m_starttime);
}
}
Example Class Usage
Simple example program demonstrating the ExampleTimer class usage:

#include <stdio.h> //atoi()
#include <string> //string, getline()
#include <iostream> //cout, cin
 
#include "ExampleTimer.h"
 
using namespace std;
 
void ShowMenu()
{
cout << " 1 = Start/Stop Timer" << endl;
cout << " 2 = Reset Timer" << endl;
cout << " 3 = Reset Timer and Restart" << endl;
cout << " 4 = Show Timer State" << endl;
cout << " 5 = Show Timer Time" << endl;
cout << " 0 = Quit Test Program" << endl;
cout << endl;
cout << ">";
}
 
 
int main()
{
int choice = -1;
string selection;
bool exit = false;
 
ExampleTimer timer;

while(!exit)
{
ShowMenu();
getline(cin, selection);

// NOTE: atoi will convert also values e.g. "xyz" and "\n" to ‘0′
choice = atoi(selection.c_str());
 
switch(choice)
{
case 1: //Start/Stop
if(timer.IsRunning())
{
timer.Stop();
cout << "[timer stopped]" << endl;
}
else
{
timer.Start();
cout << "[timer started]" << endl;
}
break;
case 2: //Reset
timer.Reset(false);
cout << "[timer reset]" << endl;
break;
case 3: //Reset and Restart
timer.Reset(true);
cout << "[timer reset and restart]" << endl;
break;
case 4: //Show State
cout << "[timer state]" << endl;
cout << "Running:" << timer.IsRunning() << endl;
cout << "Reset:" << timer.IsReset() << endl;
break;
case 5: //Show Time
cout << "[timer time]:" << timer.GetTime() << " sec" << endl;
break;
case 0: //Quit
exit = true;
break;
default:
cout << "Please, enter only numeric values between 0 and 5!" << endl;
}

return 0;

}

Postconditions
The example program prompts user to enter menu selections between 0 and 5. According to selections the example timer is started, stopped or asked to show the current state until 0 is entered and the program exits.



Thank you for reading this post. You can now Leave A Comment (0) or Leave A Trackback.

Post Info

This entry was posted on Sunday, September 7th, 2008 and is filed under Blogroll.

You can follow any responses to this entry through the Comments Feed. You can Leave A Comment, or A Trackback.



Previous Post: Israeli diggers uncover parts of ancient wall »
Next Post: TSS001089 - Canceling an asynchronous silent uninstallation takes a long time »

Read More

Related Reading:



Leave a Reply

Note: Any comments are permitted only because the site owner is letting you post, and any comments will be removed for any reason at the absolute discretion of the site owner.

You must be logged in to post a comment.

edwan