1

Different answers to this question about formatting date + time strings using Boost use different facets.

One answer uses boost::posix_time::time_facet, and the other answer uses boost::gregorian::date_facet (note that the latter also includes the time, despite the name of the facet class).

I.e., we have:

boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S");

vs.

boost::gregorian::date_facet("%Y-%m-%d %H:%M:%S");

(with irrelevant characters slightly adjusted by me from the linked answers).

Notice, again, that the boost::gregorian::date_facet includes the time, so in a sense calling it a date_facet is a bit deceptive, especially when contrasting it with boost::posix_time::time_facet, whose developers clearly felt should include "time" in the name of the facet class.

Also notice that boost::posix_time::time_facet includes the date, which is perhaps more intuitive (though it is possible to think of purely formatting times, but not dates, so maybe it shouldn't be more intuitive that a "time" facet can include the "date" than that a "date" facet can include the "time").

But back specifically to my question: I feel that somehow I must be missing something important involving the gregorian aspect of things if I use the boost::posix_time version - people wouldn't bother making a namespace to represent the Gregorian calendar if that doesn't matter, right?

And conversely - is boost::gregorian's version missing something important from the boost::posix_time version?

Is one of them a superset of the other? Alternatively, do their features overlap, but differ?

What is the difference between boost::posix_time::time_facet and boost::gregorian::date_facet?

Community
  • 1
  • 1
Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
  • Posix time starts in 1970. Gregorian calendar starts in 1572 (and the facet supports dates earlier than that). – n. m. could be an AI Mar 01 '17 at 16:49
  • Thanks. Does Posix also support dates prior to 1970 (via negative values)? I assume it does. – Dan Nissenbaum Mar 01 '17 at 16:55
  • 1
    I looked in the documentation after typing the comment. Apparently posix_time::time_facet extends gregorian::date_facet. – n. m. could be an AI Mar 01 '17 at 17:01
  • That's very helpful, thanks. – Dan Nissenbaum Mar 01 '17 at 17:02
  • 2
    Those two are typedefs. ([1](https://github.com/boostorg/date_time/blob/6015e3039114c0f2ba456e00dd50404e3fb88275/include/boost/date_time/gregorian/gregorian_io.hpp#L32) [2](https://github.com/boostorg/date_time/blob/6015e3039114c0f2ba456e00dd50404e3fb88275/include/boost/date_time/posix_time/posix_time_io.hpp#L38) ). So it's really `boost::date_time::date_facet` and `boost::date_time::time_facet`. [Documentation](http://www.boost.org/doc/libs/1_63_0/doc/html/date_time/date_time_io.html) says: "The boost::date_time::time_facet is an extension of the boost::date_time::date_facet." – Dan Mašek Mar 01 '17 at 17:02

0 Answers0