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?

有帮助吗?

解决方案

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.

其他提示

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>())
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top