Skip to content

Instantly share code, notes, and snippets.

@sansumbrella
Created April 24, 2011 05:35
Show Gist options
  • Save sansumbrella/939345 to your computer and use it in GitHub Desktop.
Save sansumbrella/939345 to your computer and use it in GitHub Desktop.
A sane way to format dates from boost. Simple interface, no noodling with stringstreams and locales.
//
// DateUtils.cpp
// Created by David Wicks on 4/23/11.
#include "DateUtils.h"
#include <sstream>
using namespace boost::gregorian;
using namespace std;
string DateUtils::formatDate( const date& d, const string& fmt )
{
date_facet* facet( new date_facet() );
facet->format( fmt.c_str() );
stringstream ss;
ss.imbue( locale( locale::classic(), facet ) );
ss << d;
return ss.str();
}
//
// DateUtils.h
// Created by David Wicks on 4/23/11.
//
#pragma once
#include <boost/date_time/gregorian/gregorian.hpp>
struct DateUtils
{
static std::string formatDate( const boost::gregorian::date& d, const std::string& fmt );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment