Home > Back-end >  C Boost libraries how timer on each month 3, no. 13, no. 23 timing to perform a task
C Boost libraries how timer on each month 3, no. 13, no. 23 timing to perform a task

Time:09-23

Wrote a c + + console application, before a month 3, 13, 23, 8 timing from essentially a database, respectively for late last month, earlier this month, in the middle of this month the site data, and then according to the corresponding time interval to generate XML data, I use is the Boost c + + library as a timer, the code is as follows:
//the header file
 
# pragma once

#include
#include

# include "my_log. H"

The class XmlFileGenerateTimer: public my_log
{
Public:
XmlFileGenerateTimer (sig_log & amp; Lg);
~ XmlFileGenerateTimer ();
Public:
Void the start ();

Void the stop ();

//start the XML data to generate timer
Void start_timer_xml ();

//stop XML data generation timer
Void stop_timer_xml ();

Private:
//IO_CONTEXT
Boost: : an asio: : io_context m_io_context;

//XML data generated by the timer regularly
//hubei in month 3, 13, 23, 8 push data, hunan in 10 push data
Boost: : an asio: : deadline_timer m_timer_xml;

Boost: : thread m_thread;

//run for the first time
Bool m_is_first_generate=true;

//upload time regularly (the default is 1 minute)
Int m_xmlRate=1;

Volatile bool m_bRun=false;
};


//the source file
 
# include "XmlFileGenerateTimer. H"

# include "DateFormat. H"
# include "SetUpConf. H"
# include "MSSQLProc. H"
# include "xmlDataConf. H"

#include

#include

XmlFileGenerateTimer: : XmlFileGenerateTimer (sig_log & amp; Lg)
: my_log (lg)
, m_io_context ()
, m_timer_xml (m_io_context)
{

}

XmlFileGenerateTimer: : ~ XmlFileGenerateTimer ()
{
Stop ();
}

Void XmlFileGenerateTimer: : start ()
{
M_bRun=true;

Start_timer_xml ();

//create XML files are generated thread
M_thread=boost: : thread (boost: : bind (& amp; Boost: : an asio: : io_context: : run, & amp; M_io_context));

While (m_bRun)
{
Msleep (1000);//sleep
1 SEC}
}

Void XmlFileGenerateTimer: : stop ()
{
M_bRun=false;

M_io_context. Stop ();
M_io_context. Reset ();

Stop_timer_xml ();

If (m_thread joinable ())
{
M_thread. The join ();
}
}

//start the XML data to generate timer
Void XmlFileGenerateTimer: : start_timer_xml ()
{
Time_t next_point=DateFormat: : now_s ();

If (m_is_first_generate)//if it is the first time generate XML documents, will delay 5 s
{
M_is_first_generate=false;
Next_point +=5;//delay 5 s upload
}
The else
{
//regularly check once every 45 seconds, to determine whether the current time of the current month 3, 13, 23 of the eight
Next_point=(next_point/45 + 1) * 45 + 2;
}

M_timer_xml. Expires_at (boost: : posix_time: : from_time_t (next_point));
M_timer_xml. Async_wait ([this next_point] (boost: : system: : error_code ec) {
if (! Ec)
{
//get the underlined (date) (month) (year) time string yyyy_MM_dd
Tm * t=STD: : localtime (& amp; Next_point);

Int year=t - & gt; Tm_year + 1900;
Int the month=t - & gt; Tm_mon + 1;
Int day=t - & gt; Tm_mday;
Int hour=t - & gt; Tm_hour;
Int minute=t - & gt; Tm_min;
Int second=t - & gt; Tm_sec;


Char strBeginTime [40]={0};
Char strEndTime [40]={0};
If ((day==3 | | day 13==| | day==23)
& & Hour==8 & amp; & Minute==0)
{
If (day==3)//no. 3 generating late last month of data
{
If (the month==1)
{
//if it is the beginning of the New Year in January, the start time need special handling
//(2020-01-03 08:00, such as 2020-01-03 01:00:00 ~ 2020-01-01 00:00:00)
Sprintf (strBeginTime, "% 4 d - 12-21 01:00:00", year 1);
Sprintf (strEndTime, "% % 4 d - 02 d - 01 00:00:00", year, month);
}
The else
{
//the 2019-11-03 08:00
//the 01:00:00 2019-10-21 ~ 2019-11-01 00:00:00
Sprintf (strBeginTime, "% % 4 d - 02 d - 21 01:00:00", year, month - 1);
Sprintf (strEndTime, "% % 4 d - 02 d - 01 00:00:00", year, month);
}
}
Else if (day==13)//generated data earlier this month 13
{
Sprintf (strBeginTime, "% % 4 d - 02 d - 01 01:00:00", year, month);
Sprintf (strEndTime, "% % 4 d - 02 d - 11 00:00:00", year, month);
}
The else//23
the middle of this month{
Sprintf (strBeginTime, "% % 4 d - 02 d - 11 01:00:00", year, month);
Sprintf (strEndTime, "% % 4 d - 02 d - 21 00:00:00", year, month);
}

String currentTime=DateFormat: : to_std_string ();
STD: : cout & lt; <"[" & lt; & lt; currentTime & lt; & lt;"] "& lt; <"Start the XML generated task regularly & lt;"
Unsigned long tStart=GetTickCount ();

XmlDataConf xmlConf (signal_log_);
//xmlConf generate_xmlFile (strBeginTime strEndTime);//every spot site an XML file, about 10 days 109 sites, that is, 109
XmlConf. Generate_oneXmlFile (strBeginTime strEndTime);//109 sites use the same file, a file, 10 days a month, that is, three XML files

Unsigned long tEnd=GetTickCount ();

CurrentTime=DateFormat: : to_std_string ();
STD: : cout & lt; <"[" & lt; & lt; currentTime & lt; & lt;"] "& lt; <"XML timing generation task to complete,"
<"Time:" & lt; <(tEnd to tStart) & lt; <"Ms" & lt; }

//start the timer again
Start_timer_xml ();
}
});
}

//stop XML data generation timer
Void XmlFileGenerateTimer: : stop_timer_xml ()
{
M_timer_xml. Cancel ();
}


nullnull
  • Related