13

Why C++ standard support function strftime() but not strptime()? strftime() is available to change a time to string, but there isn't a function which can change a string back to time.

On Posix strptime() is available as a C-like function, using it need to take care of the potential problem of mixed C and C++ code.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • I almost +1'd this because it seems like a fair question (this functionality _doesn't_ exist in C, and it _does_ seem odd), but then I reconsidered because I don't think the question is actually answerable. – Lightness Races in Orbit Feb 14 '14 at 10:24
  • Good question. I think there are in general only few and bulky functions to serialize time and date in C++. If expected more from C++11 chrono – TNA Feb 14 '14 at 10:29
  • @LightnessRacesinOrbit Well, people involved in the C++ standardisation process *do* frequent SO, so they might have an (f)actual answer. – Angew is no longer proud of SO Feb 14 '14 at 10:57
  • 2
    I think the main reason is that not only it depends on the locale, but also you have tons of different methods to write a date in a given locale. That would have made either a big module, or an incomplete solution. (at least that's my 2 cents) – Synxis Feb 14 '14 at 11:06
  • @Angew: Yes, true (you mean the C standardisation process), but the likelihood that even _they_ can back this up with any sort of meaningful rationale is fairly low. – Lightness Races in Orbit Feb 14 '14 at 11:18
  • 2
    It is a Posix function. Standards don't try to cater to specific operating system apis, even if they *look* like C library functions. – Hans Passant Feb 14 '14 at 14:33

1 Answers1

1

I'm not a part of the standards committee, but here goes:

strptime is defined by the ISO/IEC 9945 documents (essentially the Single Unix Specification / POSIX). It is not part of the 9899:1990 or 9899:1999 C standards documents (and I don't think it is part of 9899:2011, though I can't be sure as I don't have that one in my possession yet).

Since the C++ standards only mandate levels of compatibility with their C cousin, you won't find strptime mandated by the 14882:1998 or 14882:2011 C++ standards documents.

CasaDeRobison
  • 377
  • 2
  • 12
  • 1
    It seems like that `strptime()` is not supported by the C standards, and the C++ standards just inherit it. But I'm still puzzled that why the C standars don't support `strptime()`... And then I found that function `std::time_get::get()` is added in c++11, and does similar thing like `strptime()`, and can be used with `std::time_put::put()` in pairs. Thanks for your answer any way. – songyuanyao Feb 19 '14 at 02:50
  • The reality is that if strptime is available in the C environment, you should be able to use it from C++, you just can't count on it in a portable application (unless your only portability concern is to POSIX compatible platforms). – CasaDeRobison Feb 19 '14 at 03:35
  • Yes, the compatibility may be a problem. Anyway I've tried and it works well on my Linux. But have to pay attentoin to that the C global locale must fit with the C++ global locale especially when it has no name. – songyuanyao Feb 19 '14 at 04:08
  • Just curious, why the "unaccept" of this answer? In what way is it now not the best answer of the one answer listed? :) – CasaDeRobison Feb 24 '14 at 09:15
  • It isn't a complete answer for me. The main reason is just that `strptime()` is not a part of C standards, hmmm, so why the C standards don't support it? Is there a more appropriate function for using with `strftime()`? Maybe there is not a reasonable answer for the question from the beginning... :) – songyuanyao Feb 24 '14 at 10:21
  • Fair enough, maybe you should rephrase the question. The question "why does C++ not support the strptime function" is clearly answered here. "Why does C not mandate strptime" is a different question which you apparently now want answered. As to why C doesn't mandate it, I can only hazard a guess, and it is alluded to in the comments to the question: date parsing is very locale & culture specific, and it is much easier to format a date with differing component orders than it is to parse a date unambiguously. – CasaDeRobison Feb 24 '14 at 17:42
  • OK, maybe I will ask a new question for C, and thanks for your answer(guess?) for it. :) – songyuanyao Feb 25 '14 at 02:26
  • Thanks! My answer was definitely not a guess (as least as far at the C90, C99, C++98, C++03, & C++11 standards go). I looked at them specifically, and they do not reference the ISO/IEC 9945 standard. But yes, a good question would be "Why does C mandate strftime but not strptime?" – CasaDeRobison Feb 26 '14 at 23:32