4

I just noticed that is going to have chrono::year. It's constructor takes in an int in the range: [-32767, 32767], however I am unclear what this argument represents.

  • Would this be consistent with tm_year's 1900 origin?
  • Or perhaps time_t's 1970 origin?
  • Or perhaps it's in Anno Domini with a 0 origin?

EDIT:
This is key to the understanding of what is meant by the is_leap function chrono::year offers. Without an origin it's unclear what year is represented here.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 2
    The standard is maddeningly unhelpful: *year represents a year in the civil calendar* https://timsong-cpp.github.io/cppwp/time.cal.year#overview-1 – NathanOliver Sep 18 '18 at 19:14
  • Since it's Gregorian time, I'd assume 0 is 0, but that's just an assumption. Fortunately there's still another couple years to get the wording sorted out in the Standard. – user4581301 Sep 18 '18 at 19:14
  • 1
    What's unclear about cppreference's description? "The class year represents a year in the [proleptic Gregorian calendar](https://en.m.wikipedia.org/wiki/Proleptic_Gregorian_calendar). Its range is [-32767, 32767]."? – Jesper Juhl Sep 18 '18 at 19:15
  • @JesperJuhl does the Proleptic Gregorian calendar have an origin? – Jonathan Mee Sep 18 '18 at 19:17
  • @user4581301 I was actually going to try to find a compiler which had already implemented C++2A and see if I couldn't use this. – Jonathan Mee Sep 18 '18 at 19:18
  • @Jonathan Mee - see the link. – Jesper Juhl Sep 18 '18 at 19:19
  • The calendar's origin is "year zero" which technically didn't exist. The first year of the Gregorian calendar (then [Julian](https://en.wikipedia.org/wiki/Julian_calendar)) was year 1. – tadman Sep 18 '18 at 19:21
  • 1
    Standard link says nothing about Gregorian, just Civil Calendar, of which Gregorian is merely the elephant in the room. This is interesting wording. – user4581301 Sep 18 '18 at 19:22
  • @JesperJuhl I... don't see anything about an origin in that article... though 1582 is thrown around a good deal. Is that supposed to be the origin? – Jonathan Mee Sep 18 '18 at 19:24

1 Answers1

6

In 25.8.1 [time.cal.general]:

The types in 25.8 describe the civil (Gregorian) calendar and its relationship to sys_days and local_days.

The wording on this was (is) challenging as the intent is to model the Gregorian calendar (as does C++ currently via the C API) without offending those who follow other calendars.

I also am just now noting that the word "proleptic" is missing from the spec, and should probably be added in a strategic spot.

To directly answer the question, the integral associated with std::chrono::year is the Anno Domini reference, as defined by Pope Gregory in 1582, but running both backwards and forwards in time. As I write this, the year is 2018y.

And (answering Jonathan Mee's comment below), this program:

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    const auto foo = 2018y;
    cout << int{foo} << '\n';
}

Outputs:

2018

Live demo that you can experiment with with the proviso that the "date.h" example implementation puts things in namespace date instead of namespace std::chrono.

I should also note that this software allows for user-written calendars to interoperate with the std::chrono system. Here is an example of the Julian calendar. There are a couple more examples here.


Finally, a brief note on the rationale as to why the current year is represented as year{2018} (Anno Domini), as opposed to year{48} (time_t's 1970 origin), or year{118} (tm_year's 1900 origin):

This philosophy is hysterical when used in movies. But gets tiresome when used in software design. This library tries to do the expected.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • OK, so if I do `const auto foo = 2018y` Then what will this output? `cout << static_cast(foo)` Is it 2018, indicating the origin is at 0? Or is it 436, indicating the origin is at 1582? – Jonathan Mee Sep 18 '18 at 19:33
  • I missed the `#include "date.h"` on my first reading of the example and couldn't figure out how this could possibly work in C++17. Anyway, on second pass, thank you so much for pasting all that in! – Jonathan Mee Sep 18 '18 at 19:49
  • You're welcome. But the date.h was already pasted in as a "live demo" from the project website. I just modified a copy for this question. The example implementation will work (with varying degrees of success) back to C++11 on clang, gcc, and VS. – Howard Hinnant Sep 18 '18 at 19:51
  • I had a follow up question, but I realized I was cheating on the scope of this question. If you have some insight could you help me here: https://stackoverflow.com/q/52394623/2642059 – Jonathan Mee Sep 18 '18 at 21:10