Tech Support Guy banner
Status
Not open for further replies.

C/C++ ,sample code to get current time of the system

15K views 3 replies 3 participants last post by  vikrant321 
#1 ·
can any body reply with the module for retriving current system time in C/C++. i tried with "sys/timeb.h" and "ftime " function but , could not get.
 
#2 ·
Code:
#include "time.h"

int main( int argc, char *argv[] )
{
	time_t tm;
	struct tm *ltime;
	
	time( &tm );
	
	ltime = localtime( &tm );
	ltime->tm_mon++;
	ltime->tm_year += 1900;
	
	printf( "%02i.%02i.%04i  %02i:%02i:%02i\n\0", ltime->tm_mon,
		ltime->tm_mday, ltime->tm_year, ltime->tm_hour, ltime->tm_min, ltime->tm_sec );
		
	return( 0 );
	
}
 
#3 ·
Here's the same thing, but just the time.

Code:
#include <iostream>
#include <ctime>

using namespace std;

int main() {
   const time_t curr = time(NULL);
   const tm* local = localtime(&curr);
   cout << endl << local->tm_hour << "h:" << local->tm_min << "m:" 
        << local->tm_sec << "s" << endl;
}
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top