Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current UTC date in boost?

Tags:

c++

date

boost

Given this is a basic question, I imagine there might be duplicates, but I could not find any. I simply want to get the current iso_date ( like 20110503 ) from boost.

Any pointers ?

like image 730
Humble Debugger Avatar asked May 03 '11 00:05

Humble Debugger


1 Answers

I assume you're looking for a Boost.Date_Time-based solution?

#include <locale>
#include <string>
#include <iostream>
#include <sstream>
#include <boost/date_time/gregorian/gregorian.hpp>

std::string utc_date()
{
    namespace bg = boost::gregorian;

    static char const* const fmt = "%Y%m%d";
    std::ostringstream ss;
    // assumes std::cout's locale has been set appropriately for the entire app
    ss.imbue(std::locale(std::cout.getloc(), new bg::date_facet(fmt)));
    ss << bg::day_clock::universal_day();
    return ss.str();
}

See Date Time Input/Output for more information on the available format flags.

like image 104
ildjarn Avatar answered Sep 21 '22 18:09

ildjarn