๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“– C๋ž‘ C++

Boost๋กœ ์‹œ๊ฐ„ ๊ฐ’ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

Boost๋กœ ์‹œ๊ฐ„ ๊ฐ’ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด, ๋กœ๊ทธ๋ผ๋˜๊ฐ€ HTTP ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ๋ผ๋˜๊ฐ€ ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ •๋ณด๋ฅผ ๋‚จ๊ธธ ๋•Œ๊ฐ€ ์ข…์ข… ์žˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” C์–ธ์–ด์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋‚จ๊ธฐ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค

long get_current_msec(void)
{
    long            cur_msec = 0;
    struct timeval  tv_time;

    gettimeofday(&tv_time, 0);
    cur_msec = (long)(tv_time.tv_sec*1000) + (long)(tv_time.tv_usec/1000);

    return cur_msec;
}

Boost์—์„œ๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—ญ์‹œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค

(๋ฌผ๋ก  ์œ„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ C++ ์ฝ”๋“œ์— ์ ์šฉํ•ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Š )


Boost์˜ Posix Time ์†Œ๊ฐœ๊ธ€์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช… ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค

 

The class boost::posix_time::ptime is the primary interface for time point manipulation. In general, the ptime class is immutable once constructed although it does allow assignment.
Class ptime is dependent on gregorian::date for the interface to the date portion of a time point.
Other techniques for creating times include time iterators.


Boost์˜ ptime ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹œ์  ์กฐ์ž‘์„ ์œ„ํ•œ ๊ธฐ๋ณธ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ˆ๊นŒ ์ž˜ ์‚ฌ์šฉํ•˜๋ž€ ๋‚ด์šฉ ๐Ÿ˜ฃ
Header๋Š” ์ด๋ ‡๊ฒŒ ์ ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

#include <boost/date_time.hpp> 
// posix_time๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋ฉด
#include "boost/date_time/posix_time/posix_time.hpp"

 

์ƒ์„ฑ์ž๋Š” ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค

// 1) ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
ptime timeEx; 
// 2) date๋ž‘ offset ์„ค์ • ๊ฐ€๋Šฅ
ptime timeEx2(date(2022, Nov, 10),hours(1)); 
// 3) Copy constructor๋„ ์ง€์›
ptime timeEx(timeEx2); 
// 4) infinities, not-a-date-time, max_date_time, and min_date_time๋กœ ํŠน๋ณ„ํ•œ ์‹œ๊ฐ„๋„ ๋Œ€์ž… ๊ฐ€๋Šฅ
time infinTime(pos_infin); 

 

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด๋กœ๋œ string ๊ฐ’์„ time ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ฑฐ๋‚˜ ("2022-11-29" → 2022-11-29), iso ํ˜•์‹์˜ ๊ฐ’์„ time ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” api๋„ ์ œ๊ณต์„ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹น



Boost๋กœ ํ˜„์žฌ ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ

๊ทธ๋Ÿฌ๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ค๋Š” ์˜ˆ์ œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

struct timeInfo
{
    string YYYY;
    string MM;
    string DD;
    string HH;
    string mm;
    string ss;
};

timeInfo getBoostTimeLocal()
{
    timeInfo time;

    try
    {
        boost::posix_time::ptime timeLocal = 
            boost::posix_time::second_clock::local_time();
            // timeLocal์— ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์™€์„œ string์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์ฐจ๊ณก์ฐจ๊ณก ๋„ฃ์–ด์ค๋‹ˆ๋‹ค
        time.YYYY = to_string(timeLocal.date().year());
        time.MM = to_string(timeLocal.date().month().as_number());
        time.DD = to_string(timeLocal.date().day());
        time.HH = to_string(timeLocal.time_of_day().hours());
        time.mm = to_string(timeLocal.time_of_day().minutes());
        time.ss = to_string(timeLocal.time_of_day().seconds());

        return time;
    }
    catch(std::exception const& e)
    {
        return time;
    }
    return time;
}


timeInfo๋ผ๋Š” ๊ตฌ์กฐ์ฒด์— time ์ •๋ณด๋ฅผ YYYY/MM/DD/HH/mm/ss ๋ณ„๋กœ ๋‹ด์•„์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ํ•„์š”ํ•œ ์‹œ๊ฐ„ ํ˜•์‹์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿ™‚


๐ŸŒ ์ฐธ๊ณ  ์ž๋ฃŒ

Boost Posix Time