Harry
Harry

Reputation: 863

C++ Formatted Date

This might be a VERY simple question, but coming from the PHP world, is there a SIMPLE (not around-the-world) way to output the current date in a specific format in C++?

I'm looking to express the current date as "Y-m-d H:i" (PHP "date" syntax), comes out like "2013-07-17 18:32". It'd always be expressed with 16 characters (incl. leading zeros).

I am fine including Boost libraries if that helps. This is vanilla/linux C++ though (no Microsoft headers).

Thanks so much!

Upvotes: 4

Views: 17866

Answers (5)

Mark Lakata
Mark Lakata

Reputation: 20818

C++11 supports std::put_time

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    std::time_t t = std::time(nullptr);
    std::tm tm = *std::localtime(&t);
    std::cout.imbue(std::locale("ru_RU.utf8"));
    std::cout << "ru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
    std::cout.imbue(std::locale("ja_JP.utf8"));
    std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
}

Upvotes: 2

Jaffa
Jaffa

Reputation: 12700

You can use boost date facets to print a date using the given format :

//example to customize output to be "LongWeekday LongMonthname day, year"
//                                  "%A %b %d, %Y"
date d(2005,Jun,25);
date_facet* facet(new date_facet("%A %B %d, %Y"));
std::cout.imbue(std::locale(std::cout.getloc(), facet));
std::cout << d << std::endl;
// "Saturday June 25, 2005"

Or again using boost date time library it is possible, though not exactly in the same way.

  //Output the parts of the date - Tuesday October 9, 2001
  date::ymd_type ymd = d1.year_month_day();
  greg_weekday wd = d1.day_of_week();
  std::cout << wd.as_long_string() << " "
            << ymd.month.as_long_string() << " "
            << ymd.day << ", " << ymd.year
            << std::endl;

As suggested in other answers, using the strftime function may be easier for simple case and to begin in C++, even if it's originally a C function :)

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129324

The traditional C method is to use strftime, which can be used to format a time_t (PHP allows you to use either current time or "a timestamp got from somewhere else"), so if you want "now", you need to call time first.

Upvotes: 1

rectummelancolique
rectummelancolique

Reputation: 2237

strftime is the simplest I can think of without boost. Ref and exemple: http://en.cppreference.com/w/cpp/chrono/c/strftime

Upvotes: 4

Varvarigos Emmanouil
Varvarigos Emmanouil

Reputation: 757

You mean something like this:

#include <iostream>
#include <ctime>

using namespace std;

int main( )
{
   // current date/time based on current system
   time_t now = time(0);

   // convert now to string form
   char* dt = ctime(&now);

   cout << "The local date and time is: " << dt << endl;

   // convert now to tm struct for UTC
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "The UTC date and time is:"<< dt << endl;
}

result:

The local date and time is: Sat Jan  8 20:07:41 2011

The UTC date and time is:Sun Jan  9 03:07:41 2011

Upvotes: 3

Related Questions