Obtenga la hora actual en milisegundos usando C++ y Boost
En mi hilo (usando boost::thread) necesito recuperar la hora actual en ms o menos y convertir a ms:
En realidad, leyendo aquí he encontrado esto:
tick = boost::posix_time::second_clock::local_time();
now = boost::posix_time::second_clock::local_time();
Y parece funcionar, pero después necesito tener un valor largo de los milisegundos del ahora...
¿Cómo puedo hacerlo?
4 answers
Puede usar boost::posix_time::time_duration
para obtener el rango de tiempo. Por ejemplo, así
boost::posix_time::time_duration diff = tick - now;
diff.total_milliseconds();
Y para obtener una resolución más alta puede cambiar el reloj que está utilizando. Por ejemplo al boost::posix_time::microsec_clock
, aunque esto puede ser dependiente del sistema operativo. En Windows, por ejemplo, boost::posix_time::microsecond_clock
tiene una resolución de milisegundos, no de microsegundos.
Un ejemplo que depende un poco del hardware.
int main(int argc, char* argv[])
{
boost::posix_time::ptime t1 = boost::posix_time::second_clock::local_time();
boost::this_thread::sleep(boost::posix_time::millisec(500));
boost::posix_time::ptime t2 = boost::posix_time::second_clock::local_time();
boost::posix_time::time_duration diff = t2 - t1;
std::cout << diff.total_milliseconds() << std::endl;
boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
boost::this_thread::sleep(boost::posix_time::millisec(500));
boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration msdiff = mst2 - mst1;
std::cout << msdiff.total_milliseconds() << std::endl;
return 0;
}
En mi máquina win7. La primera salida es 0 o 1000. Segunda resolución. El segundo es casi siempre 500, debido a la mayor resolución del reloj. Espero que eso ayude un poco.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-07 16:38:53
Si quieres decir milisegundos desde época {[3] } podrías hacer
ptime time_t_epoch(date(1970,1,1));
ptime now = microsec_clock::local_time();
time_duration diff = now - time_t_epoch;
x = diff.total_milliseconds();
Sin embargo, no está particularmente claro lo que buscas.
Eche un vistazo al ejemplo en la documentación de DateTime at Boost Date Time
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-18 14:39:33
// Get current date/time in milliseconds.
#include "boost/date_time/posix_time/posix_time.hpp"
namespace pt = boost::posix_time;
int main()
{
pt::ptime current_date_microseconds = pt::microsec_clock::local_time();
long milliseconds = current_date_microseconds.time_of_day().total_milliseconds();
pt::time_duration current_time_milliseconds = pt::milliseconds(milliseconds);
pt::ptime current_date_milliseconds(current_date_microseconds.date(),
current_time_milliseconds);
std::cout << "Microseconds: " << current_date_microseconds
<< " Milliseconds: " << current_date_milliseconds << std::endl;
// Microseconds: 2013-Jul-12 13:37:51.699548 Milliseconds: 2013-Jul-12 13:37:51.699000
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-12 20:46:09
Intente esto: importar encabezados como se mencionó.. solo da segundos y milisegundos. Si necesitas explicar el código lee este enlace.
#include <windows.h>
#include <stdio.h>
void main()
{
SYSTEMTIME st;
SYSTEMTIME lt;
GetSystemTime(&st);
// GetLocalTime(<);
printf("The system time is: %02d:%03d\n", st.wSecond, st.wMilliseconds);
// printf("The local time is: %02d:%03d\n", lt.wSecond, lt.wMilliseconds);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-29 23:19:04