Skip to content

Instantly share code, notes, and snippets.

@nootanghimire
Last active November 8, 2019 21:29
Show Gist options
  • Save nootanghimire/8605164 to your computer and use it in GitHub Desktop.
Save nootanghimire/8605164 to your computer and use it in GitHub Desktop.
Basic Stopwatch in C++ using ctime (or time.h)
/************************************************************************
* @author Nootan Ghimire <nootan.ghimire@gmail.com>
* @file a.stopwatch.basic.cpp
* @includes conioLinux.h (Modified the code from StackOverflow)
*
* @license Creative Commons: Attribution-ShareAlike 4.0 International
(CC BY-SA 4.0)
* @license http://creativecommons.org/licenses/by-sa/4.0/deed.en_US
*
* @desc A Simple Stopwatch.
*
* MAY NOT RUN IN TURBO C/C++ .
* DO NOT USE TURBO C/C++ . IT DOES NOT FOLLOW THE ANSI C/C++ STANDARD
*/
#include <iostream> /* Include the header file*/
#include <ctime> /* To get System Time*/
#define Linux /* Remove this line if running on windows*/
/* #define CodeBlocksinWindows */
/* Remove the above comment if
running on Code::Blocks in Windows with MinGW
*/
#include "conioLinux.h" /* My Header for using getch() on Linux */
/* Use 'std' namespace*/
using namespace std;
class myTime; /* Class Definition: myTime */
/* Function Declarations */
unsigned getCurrentSysTime(); /* Gets current System time in seconds */
myTime getTimefromSeconds(unsigned); /* Convers seconds to myTime class */
/**
* class myTime
* desc provides functions for managing time
* Data
*
** private
**
*** unsigned h : for storing 'hour' information
*** unsigned m : for storing 'minute' information
*** unsigned s : for storing 'second' information
*
* Functions
*
** public
**
*** unsigned getSeconds : @param none
* @return unsigned : total seconds
* @desc get the total seconds
*
*** void setTime : @param unsigned hr : hour
* @param unsigned mi : minute
* @param unsigned se : second
* @return none
* @desc sets the time to current object
*
*** myTime operator- : @param myTime t2
* @return myTime : Time after substraction
* @desc overloading - operator.
returns substraction result.
*
*** void display : @param none
* @return none
* @desc displays current object's stored time.
*
*/
class myTime{
private:
unsigned h, m, s;
public:
myTime(unsigned hr=0, unsigned mi=0, unsigned se=0):h(hr),m(mi),s(se){}
unsigned getSeconds(){
return(h*3600+m*60+s);
}
void setTime(unsigned hr, unsigned mi, unsigned se){
h = hr;
m = mi;
s = se;
}
myTime operator-(myTime t2){
unsigned sec1 = getSeconds();
unsigned sec2 = t2.getSeconds();
return getTimefromSeconds(sec2 > sec1 ? sec2 - sec1 : sec1 - sec2);
}
void display(){
cout<<h<<":"<<m<<":"<<s<<endl;
}
};
/* End Class */
/* Main starts */
int main(){
char s; /* Character s to hold the response */
myTime t1, t2, t3; /* Two to get start and end time,
one to store their difference */
myTime t_inter; /* Calculate middle time (impossible)*/
do { /* Infinite Loop */
clrscr(); /* Clears Screen */
#ifdef Linux
cout<<"Press \x1b[32;1ms\x1b[0m to start!";
#else
cout<<"Press s to start!";
#endif
s = getch(); /* gets one character from input */
/* If user presses s: then store current time, break from the loop */
if(s=='s') {t1 = getTimefromSeconds(getCurrentSysTime()); break;}
}while(1); /* End do...while loop */
while(1){ /* Another infinite loop */
clrscr();
#ifdef Linux
cout<<"Stopwatch Running! Press \x1b[32;1ms\x1b[0m to stop!";
cout<<" or\x1b[32;1m any other key\x1b[0m";
cout<<" to update the time :D\n\n\t\x1b[32;1m";
#else
cout<<"Stopwatch Running! Press s to stop!";
cout<<" or any other key to update the time :D\n\n\t";
#endif
t_inter = getTimefromSeconds(getCurrentSysTime());
(t_inter-t1).display();
#ifdef Linux
cout<<"\x1b[0m\n";
#else
cout<<"\n";
#endif
s=getch();
/* If user presses s: then store current time, break from the loop */
if(s=='s'){
t2 = getTimefromSeconds(getCurrentSysTime());
break;
}
} /* End while loop */
t3 = t2 - t1; /* Subtraction: See overloaded operator */
clrscr();
/* Now, t3 contains the differnce in time. i.e., stopwatch output.
* We are showing it in colorful thing :D
* \x1b[32;1m is a special escape code which causes
* the text after it to be bold and with foreground color 32 (green)
* \x1b[0m resets the color and weight to normal.
*/
#ifdef Linux
cout<<"The total time is: \x1b[32;1m";
#else
cout<<"The total time is: ";
#endif
t3.display();
#ifdef Linux
cout<<"\x1b[0m\n";
#else
cout<<"\n";
#endif
return 0; /* 0: Safe Exit Status */
}
unsigned getCurrentSysTime(){
time_t timev; /* time_t is data type
defined in ctime or time.h */
time(&timev); /* using the time function,
we store the number of seconds
from Jan 1 1990 (or any other reference)
*/
return timev; /* Returns the time */
}
/* Converts Seconds to the myTime class. Pretty Straightforward. */
myTime getTimefromSeconds(unsigned seconds=0){
unsigned hour, minute;
hour = seconds / 3600 ;
seconds = seconds % 3600 ;
minute = seconds / 60 ;
seconds = seconds % 60;
return myTime(hour, minute, seconds);
}
/* End of Code */
/* Coded in Sublime Text 3 */
/* Released under Public Domain: Do whatever you like with this file :D */
#ifdef Linux
#include <stdlib.h>
#define clrscr() system("clear")
#include <termios.h>
#include <stdio.h>
static struct termios old, xnew;
/* Initialize xnew terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
xnew = old; /* make xnew settings same as old settings */
xnew.c_lflag &= ~ICANON; /* disable buffered i/o */
xnew.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
tcsetattr(0, TCSANOW, &xnew); /* use these xnew terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void)
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);
}
#else
#include <conio.h>
#endif
#ifdef CodeBlocksinWindows
#ifndef Linux
#include <stdlib.h>
#define clrscr() system("cls")
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment