Extract datetime from Dynamic::Var using custom format

Please post support and help requests here.
Posts: 20
Joined: 01 Apr 2009, 18:32

Extract datetime from Dynamic::Var using custom format

Postby Hitnrun » 21 Aug 2012, 16:44


I need to convert a DateTime value stored in a Dynamic::Var to a custom string format. ( std::string ColumnInfo::DATETIME_FORMAT("%Y-%m-%d %H:%M:%S"); )
(I am using trunk).

I need to use generic code, because my database is defined by metadata, so when using I don't really know the field names on extraction time.

VarHolderImpl<DateTime> always converts to ISO8601:

Code: Select all

   void convert(std::string& val) const
      val = DateTimeFormatter::format(_val, Poco::DateTimeFormat::ISO8601_FORMAT);

I am exporting to XML, so my code basically loops over all the fields, converting them to std::string (removed some xml code for brevity - _value is a map<Dynamic::Any>):

Code: Select all

   for (TableInfo::columns_t::const_iterator i=_tableInfo->columns().begin(); i!=_tableInfo->columns().end(); i++)
      if (_values.find(i->second->name()) != _values.end())
         ostr << "<![CDATA[" << _values[i->second->name()]->convert<string>() << "]]>";

Is there any strategy for this to work automatically, without needing to pass the value thru a custom formatter?

I tried creating a custom VarHolderImpl, but the data from RecordSet always come as DateTime, I can't change it.

Also I tried to create my own "fmtstring" derived from std::string, but I can't add a new "convert" method to VarHolderImpl<DateTime>, as this class can't be overriden (or can it?)

Posts: 1375
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Extract datetime from Dynamic::Var using custom format

Postby alex » 21 Aug 2012, 18:37

No generic way to do it since you will always get DateTime and it will always convert to fixed format. You will either have to engage in type detection in your code or use RowFormatter, which was introduced precisely to address the type of problem you have. You will pay some performance price, though.

NB: inheriting from STL containers is not a good idea - they do not have virtual destructors.

Return to “Support”

Who is online

Users browsing this forum: No registered users and 1 guest