在 Linux 中使用 C++ 设置系统日期和时间
Set System date and time using C++ in Linux
我正在开发一个将系统日期和时间更改为指定值的跨平台应用程序。我已经完成 Windows
的部分。
如何从 Linux
中的 C++
程序设置系统日期和时间?我正在寻找类似于 SetSystemTime(SYSTEMTIME &x)
.
的函数
据我了解 settimeofday()
对日期没有任何作用,我不确定函数 stime()
的用法。我希望mktime()
与我的需求无关。
谁能帮帮我。
你理解错了。 settimeofday(2) is setting the Epoch time. which is both date and time. Read time(7)
因此,如果您从表示日期的字符串开始,请使用 strptime(3) to a struct tm
then convert that to a Unix time with mktime(3) 转换该字符串,然后将其提供给 settimeofday
(即 tv_sec
字段)。
然而,settimeofday
需要 root 权限,我相信你通常应该避免调用它(至少在平时,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)
顺便说一句,在 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 上突然更改系统时间)。你不应该在普通的应用程序代码中这样做。
我写这段代码是为了在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);
}
请注意 stime
需要 root
权限。
使用 clock_settime
而不是 stime
的示例,因为 pointed out, stime
is now deprecated. I like the reference code from Converting between timespec & std::chrono 为此:
#include <time.h>
#include <chrono>
using std::chrono; // for example brevity
constexpr timespec timepointToTimespec(
time_point<system_clock, nanoseconds> tp)
{
auto secs = time_point_cast<seconds>(tp);
auto ns = time_point_cast<nanoseconds>(tp) -
time_point_cast<nanoseconds>(secs);
return timespec{secs.time_since_epoch().count(), ns.count()};
}
const char* timePointToChar(
const time_point<system_clock, nanoseconds>& tp) {
time_t ttp = system_clock::to_time_t(tp);
return ctime(&ttp);
}
const time_point system_time = system_clock::now();
cout << "System time = " << timePointToChar(system_time) << endl;
const timespec ts = timepointToTimespec(system_time);
clock_settime(CLOCK_REALTIME, &ts);
我正在开发一个将系统日期和时间更改为指定值的跨平台应用程序。我已经完成 Windows
的部分。
如何从 Linux
中的 C++
程序设置系统日期和时间?我正在寻找类似于 SetSystemTime(SYSTEMTIME &x)
.
据我了解 settimeofday()
对日期没有任何作用,我不确定函数 stime()
的用法。我希望mktime()
与我的需求无关。
谁能帮帮我。
你理解错了。 settimeofday(2) is setting the Epoch time. which is both date and time. Read time(7)
因此,如果您从表示日期的字符串开始,请使用 strptime(3) to a struct tm
then convert that to a Unix time with mktime(3) 转换该字符串,然后将其提供给 settimeofday
(即 tv_sec
字段)。
然而,settimeofday
需要 root 权限,我相信你通常应该避免调用它(至少在平时,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)
顺便说一句,在 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 上突然更改系统时间)。你不应该在普通的应用程序代码中这样做。
我写这段代码是为了在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);
}
请注意 stime
需要 root
权限。
使用 clock_settime
而不是 stime
的示例,因为 stime
is now deprecated. I like the reference code from Converting between timespec & std::chrono 为此:
#include <time.h>
#include <chrono>
using std::chrono; // for example brevity
constexpr timespec timepointToTimespec(
time_point<system_clock, nanoseconds> tp)
{
auto secs = time_point_cast<seconds>(tp);
auto ns = time_point_cast<nanoseconds>(tp) -
time_point_cast<nanoseconds>(secs);
return timespec{secs.time_since_epoch().count(), ns.count()};
}
const char* timePointToChar(
const time_point<system_clock, nanoseconds>& tp) {
time_t ttp = system_clock::to_time_t(tp);
return ctime(&ttp);
}
const time_point system_time = system_clock::now();
cout << "System time = " << timePointToChar(system_time) << endl;
const timespec ts = timepointToTimespec(system_time);
clock_settime(CLOCK_REALTIME, &ts);