×
Namespaces

Variants
Actions

One-second timer implementation using Open C++

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.


Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition, FP2
Article
Keywords: time_t, time(), difftime()
Created: aknyman (05 Sep 2008)
Last edited: lpvalente (26 Oct 2012)

Contents

Overview

This code snippet shows how to create a simple one-second timer class using Open C++. This implementation is based on the time() and difftime() functions which are declared in the 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 the 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

A simple example application 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 application prompts the user to enter menu selections between 0 and 5. Based on the selections, the example timer is started, stopped, or asked to show the current state until 0 is entered and the program exits.


This page was last modified on 26 October 2012, at 20:03.
80 page views in the last 30 days.
×