Set date and time in Linux Operating System using C++ program

Learn: How to define/set date and time in Linux Operating System using C++ program? Here, we are using system() function to set date time.

There is a command in Linux date which is used to get date and date - s can be used to set date and time in Linux Operating System.

In this program, we are setting date and time in Linux O.S. by using system() function, it is used to execute Linux terminal commands using C++ program.

Consider the program:

using namespace std;

//function to set date and time
void setDateTime(int date, int month, int year,int hour, int min, int sec)
	//buffer to format command
	unsigned char buff[32]={0};
	//formatting command with the given parameters
	sprintf((char*)buff,(const char *)"date -s \"%02d/%02d/%04d %02d:%02d:%02d\"",month,date,year,hour,min,sec);
	//execute formatted command using system()
	system((const char *)buff);

//main funcion
int main()
	//calling function by passing date and time
	setDateTime(25,6 , 2017,10, 10,10);
	return 0;


Sun Jun 25 10:10:10 UTC 2017

Related Programs


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.