Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set System date and time using C++ in Linux

I am in the middle of developing a cross platform application that changes the system date and time to a specified value. I have completed the part for Windows.

How can I set the system date and time from a C++ program in Linux? I am looking for a function similar to SetSystemTime(SYSTEMTIME &x).

As far as I understood settimeofday() does nothing with the date and I am not sure about the usage of function stime(). I hope mktime() has nothing to do with my need.

Can anybody help me.

like image 212
Jackzz Avatar asked Feb 04 '15 06:02

Jackzz


People also ask

How get system date time with C?

In C language, we can display the current date and time by using the built-in time() and ctime() functions.

How to adjust time Linux?

How to Adjust the Time on Linux. To set or change the time, use the timedatectl command together with the set-time subcommand. Note: You need to have elevated privileges to adjust the time or date. In the aforementioned command, the hh stands for hours, mm for minutes, and ss for seconds.

How to date in C?

getdate() and setdate() function in C with Examplesstruct date dt; getdate(&dt); Parameter: This function accepts a single parameter dt which is the object of structure date. Return Value: This method does not return anything. It just gets the system date and sets it in the specified structure.


2 Answers

You understand wrongly. settimeofday(2) is setting the Epoch time. which is both date and time. Read time(7)

So you if you start from a string expressing a date, convert that string with strptime(3) to a struct tm then convert that to a Unix time with mktime(3) then feed that to settimeofday (i.e. the tv_sec field).

However, settimeofday requires root privilege and I believe you usually should avoid calling it (at least on usual, Internet-connected, computers). Better set some NTP client service on your Linux PC (e.g. run ntpd or chrony and more generally read the sysadmin chapter on keeping time...). See also adjtimex(2)

BTW, changing abruptly the system time on a multi-tasking system -like Linux or Windows- is a very dangerous operation (since it will upset and disturb a lot of system tasks depending or using the time). There are few good reasons to do that (it is a very bad idea in general). If you do that, do that with very few programs & services running (e.g. single user mode Linux). You should not do that in ordinary application code.

like image 81
Basile Starynkevitch Avatar answered Oct 23 '22 04:10

Basile Starynkevitch


I write this piece of code to set Date and Time under Linux.

#include <time.h>

struct tm time = { 0 };

time.tm_year = Year - 1900;
time.tm_mon  = Month - 1;
time.tm_mday = Day;
time.tm_hour = Hour;
time.tm_min  = Minute;
time.tm_sec  = Second;

if (time.tm_year < 0)
{
    time.tm_year = 0;
}

time_t t = mktime(&time);

if (t != (time_t) -1)
{
    stime(&t);
}

Note that stime requires root privileges.

like image 20
guan boshen Avatar answered Oct 23 '22 04:10

guan boshen