[Boost] 시간

Alexandria·2024년 3월 4일
0

Boost

목록 보기
4/5
post-thumbnail

1. 파싱

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를 연결한다.

2. 시간 설정

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를 연결한다.

profile
IT 도서관

0개의 댓글

관련 채용 정보