Skip to content

Instantly share code, notes, and snippets.

@kwk
Last active June 9, 2018 17:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwk/9410910 to your computer and use it in GitHub Desktop.
Save kwk/9410910 to your computer and use it in GitHub Desktop.
Print the current date + time in UTC using boost::date_time
cmake_minimum_required(VERSION 2.8.8)
project(datetime)
find_package(Boost COMPONENTS date_time REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(datetime datetime.cpp)
target_link_libraries(datetime ${Boost_LIBRARIES})
#include <iostream>
// Not sure if this function is really needed.
namespace boost { void throw_exception(std::exception const & e) {} }
// Includes
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time_adjustor.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>
int main(int argc, char *argv[])
{
// Code
using boost::posix_time::ptime;
using boost::posix_time::second_clock;
using boost::posix_time::to_simple_string;
using boost::gregorian::day_clock;
ptime todayUtc(day_clock::universal_day(), second_clock::universal_time().time_of_day());
std::cout << to_simple_string(todayUtc) << std::endl;
// This outputs something like: 2014-Mar-07 12:56:55
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment