LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2011, 11:31 PM   #1
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Rep: Reputation: 1
Formatting a datetime string(in g++)


Dear Advanced c/g++ programers:

I copied and tested a piece of simple formatting a datetime string code from
book, c++ cookbook, page 201, Chapter5 section2, Example5-4.
but it wont compile in my g++ 4.5.2(on ubuntu 10.04, kernel2.6.35-25)
----------------------------------------------------------------------
// Example 5-4 formatting a datetime string
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <stdexcept>
#include <iterator>
#include <sstream>

using namespace std;

ostream& formatDatetime(ostream& out, const tm& t, const char* fmt) {
const time_put<char>& dateWriter = use_facet<time_put<char> >(out.getloc());
int n = strlen(fmt);
if (dateWriter.put(out, out, ' ', &t, fmt, fmt + n).failed()) {
throw runtime_error("failure to format date time");
}
return out;
}

string dateTimeToString(const tm& t, const char* format) {
stringstream s;
formatDateTime(s, t, format);
return s.str();
}

tm now() {
time_t now = time(0);
return *localtime(&now);
}

int main()
{
try {
string s=dateTimeToString(now(), "%A %B, %d %Y %I:%M%p");
cout << s << endl;
s=dateTimeToString(now(), "%Y-%m-%d %H:%M:%S");
cout << s << endl;
}
catch(...) {
cerr << "failed to format date time" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
----------------------------------------------------------------------------
eric@eric-laptop:~/cppcookbook/download$ g++ 5-4.cpp
5-4.cpp: In function ‘std:stream& formatDateTime(std:stream&, const tm&, const char*)’:
5-4.cpp:15:17: error: invalid use of incomplete type ‘const struct std::time_put<char>’
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/localefwd.h:163:11: error: declaration of ‘const struct std::time_put<char>’
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.h:815:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/iostream:40,
from 5-4.cpp:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.tcc: In function ‘const _Facet& std::use_facet(const std::locale&) [with _Facet = std::time_put<char>]’:
5-4.cpp:13:78: instantiated from here
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.tcc:107:43: error: incomplete type ‘std::time_put<char>’ used in nested name specifier
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.tcc:112:56: error: cannot dynamic_cast ‘* *(__facets + ((unsigned int)(((unsigned int)__i) * 4u)))’ (of type ‘const class std::locale::facet’) to type ‘const struct std::time_put<char>&’ (target is not pointer or reference to complete type)
eric@eric-laptop:~/cppcookbook/download$
-----------------------------------------------------------------------
source code
http://examples.oreilly.com/9780596007614/
you can download and check and run/test by yourself.
Need your help to fix it and thanks a lot in advance, Eric
 
Old 07-11-2011, 06:38 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
In the future, please post your code using [CODE][/CODE ] tags!

Your code works with the following corrections, shown in bold text below:
Code:
// Example 5-4 formatting a datetime string
#include <locale>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <stdexcept>
#include <iterator>
#include <sstream>

using namespace std;

ostream& formatDateTime(ostream& out, const tm& t, const char* fmt)
{
   const time_put<char>& dateWriter = use_facet<time_put<char> >(out.getloc());
   int n = strlen(fmt);
   if (dateWriter.put(out, out, ' ', &t, fmt, fmt + n).failed())
   {
      throw runtime_error("failure to format date time");
   }
   return out;
}

string dateTimeToString(const tm& t, const char* format)
{
   stringstream s;
   formatDateTime(s, t, format);
   return s.str();
}

tm now()
{
   time_t now = time(0);
   return *localtime(&now);
}

int main()
{
   try
   {
      string s = dateTimeToString(now(), "%A %B, %d %Y %I:%M%p");
      cout << s << endl;
      s = dateTimeToString(now(), "%Y-%m-%d %H:%M:%S");
      cout << s << endl;
   }
   catch (...)
   {
      cerr << "failed to format date time" << endl;
      return EXIT_FAILURE;
   }
   return EXIT_SUCCESS;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
squid log string formatting tr3s Linux - Server 4 03-21-2008 04:15 AM
c++ - How to handle user specified string formatting? BrianK Programming 5 02-27-2008 08:14 AM
Convert Time String to DATETIME field Centinul Programming 2 09-25-2006 07:20 PM
php :: datetime gmarais Programming 3 03-06-2004 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration