Skip to content

Linux Ubuntu timer c++

by admin on May 18th, 2011

/*
* File:   main.cpp
* Author: r2d2
*
* Created on 20 Апрель 2011 г., 12:18
*/

#include <cstdlib>
#include <stdio.h>
#include <unistd.h>

#include "timer.h"

using namespace std;

int main(int argc, char** argv) {
struct TimerInterval TI, *pTI;
pTI=&TI;
pTI->enabled=1;
pTI->interval_hour=0;
pTI->interval_min=1;
int ok;
while(1){
ok=timer(pTI);
if(ok==0){
pTI->enabled=1;
printf("Timer elapsed!\n");
}
}

return 0;
}

#include <cstdlib>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "timer.h"

/********************************************************************
* Function name:  Timer
* Description  : compares predefined interval with system
*                time and returns flag 1 if time elapsed
*                                      0 if timer is running
*******************************************************************/
int timer(struct TimerInterval *TI){
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv,&tz);
usleep(1000000);
tm=localtime(&tv.tv_sec);
//printf("Linux Time: %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
printf("seconds%d\n",tv.tv_sec);
printf("minutes%d\n",tv.tv_sec/60);
printf("hours%d\n",tv.tv_sec/3600);
printf("days%d\n",tv.tv_sec/3600/24);
printf("year%d\n",1970+tv.tv_sec/3600/24/365);

if(TI->enabled){
TI->enabled=0;
TI->tm_hour=tm->tm_hour+TI->interval_hour;
TI->tm_min=tm->tm_min+TI->interval_min;
}

if((tm->tm_hour==TI->tm_hour)&&(tm->tm_min==TI->tm_min)){
return 0;
}else{
return 1;
}
}


From → Main()

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS