3

In this answer I am initializing a tm like this:

tm a{0, 0, 0, 15, 5, 2006 - 1900};

Using the bare numbers is a bit unclear, but I found it less unclear than:

tm a{0, 0, 0, 15, 5, 106};

What I'd like is if there was a symbol defined as 1900 in the standard that I could use, because the most understandable would be something like this:

tm a{0, 0, 0, 15, 5, 2006 - TM_YEAR_ORIGIN};

Do I have to define that locally or is such a thing defined in the standard?

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    As far as I can see, no, even in the boost code this value is hardcoded to 1900. So you'll have to use your own constant if you want that... – Nim Mar 30 '15 at 11:56
  • @Nim Sounds like a reasonable answer. If no one else knows of something we don't looking at the Boost code seems pretty reasonable, since there doesn't seem to be anything defined in time.h either. – Jonathan Mee Mar 30 '15 at 11:59
  • Consult the documentation for the CRT you use. Like [this one](http://linux.die.net/man/3/asctime). They rarely ever fail to point out that tm_year is "The number of years since 1900". – Hans Passant Mar 30 '15 at 11:59
  • @HansPassant I haven't actually seen *any* documentation that fails to point out that `tm_year` is, "The number of years since 1900," but expecting anyone reading my code to go look up the `tm`documentation to figure out why I'm subtracting by a bare number seems far fetched. – Jonathan Mee Mar 30 '15 at 12:04
  • I've edited your question to clarify that you're asking about a symbol defined as `1900`. (The value `1900` itself is of course defined in the C standard.) – Keith Thompson Mar 30 '15 at 20:12

2 Answers2

0

There's no such constant defined by the C99 standard, which specifies tm for both C and C++.

I can't say whether or not it's defined anywhere.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

The answer is no there is not a value defined in the standard... at least not one that is publicly available.

In this implementation of mktime TM_YEAR_BASE is defined as 1900, that is the only occurrence of 1900 in the file, though TM_YEAR_BASE goes on to be used 6 times.

On Visual Studio I have searched time.h and it's includes (crtdefs.h and time.inl) 1900 is only in comments.

I take this research to mean one of the following:

  1. 1900 is not publicly defined in the standard
  2. 1900 is publicly defined but not used by code that modifies tm
  3. 1900 is publicly defined on some implementations of the standard but not all

Any of these options mean that even if a public define for 1900 could be found it could not be used across platforms.

As the OP, I will say that my decision has been to define TM_YEAR_BASE internally and use that.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288