GithubHelp home page GithubHelp logo

Comments (3)

HowardHinnant avatar HowardHinnant commented on June 5, 2024

I'm curious as to the context you are experiencing overflow. Can you show a brief code example? Thanks.

from date.

HowardHinnant avatar HowardHinnant commented on June 5, 2024

To be clearer, there are benefits to having year be 2 bytes:

  1. A year_month_day can be held in 4 bytes. Experience with boost::date_time over decades has shown that clients want a 4 byte date.
  2. A 2 byte year can handle the civil calendar years -32767 to 32767 which is a larger range than nearly everyone needs. The civil calendar models the solar system to an accuracy of about 1 day in 4000 years.
  3. The two-byte year is now standardized in C++20. And the limited range has enabled at least one implementation to make performance optimizations in the conversion to and from sys_days that would not be available had the year range been 4 bytes.

If all one needs is thousands or millions of years in the past or future, and not a date on the civil calendar, years (plural) is not a bad choice. It has a 4 byte representation. And it would be trivial to create a duration (with a period of years, kiloyears, or megayears) with other representations, including floating point.

If I knew more about your use case, I could almost certainly recommend a reasonable solution.

from date.

HowardHinnant avatar HowardHinnant commented on June 5, 2024

I've briefly scanned your code base linked above and see that you're already aware of the year::min() and year::max() functions being set to -32767 and 32767 respectively. My advice is to restrict your calendrical computations to that range. This range is what has been voted into the C++20 chrono specification: http://eel.is/c++draft/time.cal.year#members-19

If you have sufficient motivation to extend your range beyond these limits, here are the calendrical algorithms this library is based on and can be used with any number of bit-sized integers to achieve ranges far greater than +/- the age of the universe: http://howardhinnant.github.io/date_algorithms.html. The algorithms are in the public domain. Changing your copy of this library to use int (as you've done) is a safe and correct method of extending these public domain algorithms to a range of +/-5.8 million years.

The motivations for staying within the more restricted range are outlined in my previous comment. Closing as not-to-be-fixed.

from date.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.