Pergunta

I'm using yaml-cpp amazing library to parse YAML files and I need to parse a scalar of !!timestamp type. For example:

- timeSpec:
    startTime: 2013-05-15T02:59:43.138Z
    endTime: 2013-05-23T02:59:43.138Z

1 - How could I do that? Should I parse it as a std::string and handle the parsing of the date time myself? Do I need to import some boost library so the data type conversion is straightforward?

2 - And in general, what YAML basic data types are supported by the library?

Foi útil?

Solução

You'll have to parse the datetime yourself. If you have some structure DateTime, as a skeleton, you could write:

namespace YAML {
   template<>
   struct convert<DateTime> {
      static Node encode(const DateTime& rhs) {
         std::string str = YourCodeToConvertToAString(rhs);
         return Node(str);
      }

      static bool decode(const Node& node, DateTime& rhs) {
         if(!node.IsScalar())
            return false;

         std::string str = node.as<std::string>();
         // Fill in the DateTime struct.
         return true;
      }
   };
}

If you can find a library (maybe boost) to do that, that'd be easier, but it's possible the YAML format for datetime isn't exactly what some other library expects.

In general, yaml-cpp doesn't support any automatic type detection.

Outras dicas

I know this is a bit late, but I came across the same thing. The quickest, easiest solution for me was to make the dates in the YAML documents strings and use boost to convert from string into the posix time type:

boost::posix_time::from_iso_string(node[0]["timeSpec"]["startTime"].as<std::string>())
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top