boost를 이용하여 문자열로 이루어진 시간을 파싱하여 초로 변환해본다.
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
int main(void) {
boost::posix_time::ptime set_time = boost::posix_time::time_from_string("2021-10-26 10:00:00");
struct timespec stime = boost::interprocess::ipcdetail::ptime_to_timespec(set_time);
printf("seconds : %ld\n", stime.tv_sec);
}
컴파일 시
libboost_date_time.so
를 연결한다.
clock_settime
을 이용하여 OS의 시간을 변경해본다.
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
int main(void) {
boost::posix_time::ptime set_time = boost::posix_time::time_from_string("2021-10-26 10:00:00");
struct timespec stime = boost::interprocess::ipcdetail::ptime_to_timespec(set_time);
stime.tv_sec -= 60*60*9;
if(clock_settime(CLOCK_REALTIME, &stime) == -1)
{
perror("clock set time error ");
}
}
컴파일 시
libboost_date_time.so
를 연결한다.