Comments (6)
This library is meant to be a foundational library upon which you can efficiently build higher-level date libraries (like tz.h). A core component of this library is that it makes expensive computations explicit, so that you can see where they are in your code. Higher-level code can hide these expensive/explicit operations as desired.
Cutting your example down a bit, your meeting computation (the day after the 3rd Tuesday of the month) looks like:
date::year_month_day
meeting1()
{
namespace d = date;
auto today = d::year_month_day{ d::floor< d::days >( std::chrono::system_clock::now() ) };
auto hackDay = d::tue[ 3 ]/today.month()/today.year();
return d::year_month_day{ d::day_point( hackDay ) + d::days{ 1 } };
}
This looks pretty good to me. I wouldn't personally use hackDay
as a variable name, but I'm not here to lecture in that department. Here is another way to compute the same day. It is about as much verbosity, and also about the same expense:
date::year_month_day
meeting2()
{
namespace d = date;
auto today = d::year_month_day{ d::floor< d::days >( std::chrono::system_clock::now() ) };
auto hackDay = d::year_month_day{d::tue[ 3 ]/today.month()/today.year()};
return hackDay.year()/hackDay.month()/(hackDay.day()+d::days{1});
}
The computation on the day field in this latter example is known to be safe as the 3rd Tuesday of the month never falls on the last day of the month. But it hasn't gained you anything (unless you like the readability better, which is a subjective point).
If we were to define day arithmetic on the year_month_weekday
type, then to be generally correct, the operation would have to internally convert to day_point
, do the arithmetic, and then convert back to year_month_weekday
. We would save a line of code:
date::year_month_day
meeting3()
{
namespace d = date;
auto today = d::year_month_day{ d::floor< d::days >( std::chrono::system_clock::now() ) };
return d::year_month_day{d::tue[ 3 ]/today.month()/today.year() + d::days{1}};
}
But we would not make the code any more efficient. Indeed, it might actually make the code less efficient. This would lend itself to inefficient programming. For example, following this logic we should put push_front
on vector
and operator[]
on list
. Both are easily done. But both make expensive operations look cheap to the programmer.
Feature request rejected. However this is a good question that others will ask, so leaving this open to help others find this rationale. Thank you for using this library, and for your feedback.
from date.
Very well
I was trying to use your library after seeing your CPPCON talk, and it just seemed to be more awkward to use than what you showed, so I thought it might be me that had overlooked something, or it really was missing.
The argument about exposing costly functions is very appreciated.
BTW. The day before our meeting is when the hacker space most regularly convene, so the variable name hackDay is actually meant quite literally :)
Fine by me to keep it open, thanks for supplying the library and for the feedback on using it.
from date.
Very nice example.
I wonder if it would make sense to add a section "Non-features and their rationale" or something like that to the documentation rather than keeping issues open?
from date.
This point is mentioned under "Efficiency" here: http://howardhinnant.github.io/date_v2.html . However clearly this is somewhat hard to find. More organized documentation is always a plus. Perhaps the wiki would be a good place to put something like this: https://github.com/HowardHinnant/date/wiki.
from date.
Finally got around to writing this up: https://github.com/HowardHinnant/date/wiki/FAQ#day_arithmetic
from date.
Closing with the issue documented in the wiki per my previous comment.
from date.
Related Issues (20)
- How to use date/tz.h properly HOT 4
- When I compile on x64 uwp I get the error HOT 1
- Waybar fails to compile with clang/libcxx because of date/tz.h HOT 8
- Change the type of the data member of class year to int from short HOT 3
- Undefined symbol HOT 1
- date_test_pass_parse_test fails with gcc (Debian 10.2.1-6) 10.2.1 20210110 HOT 1
- Date Parse: one pattern for ISO8601 DATE or DATE TIME HOT 6
- How can I convert between std::chrono::time_point and string (2023-12-06 00:46:45.801479+08)? HOT 4
- OSSFuzz Integration HOT 9
- Incorrect value of date::sys_info::save HOT 1
- Cannot override CMake options when fetching the library via FetchContent_Declare()
- .zip files have "text" git attribute HOT 1
- Possibly big performance improvement for `time_zone::to_sys()` HOT 10
- Failed to install tzdb HOT 2
- Transform date::local_seconds and timezone offset to struct tm HOT 1
- Missing tz.h header file when selecting compiler for ARM architecture HOT 1
- Opposite inner 'if' condition leads to a dead code block HOT 1
- Use timezone name like "-08:00" for `locate_zone()` HOT 2
- date::parse() doesn't have a formatting character for ISO 8601 strings without 'T' separator HOT 2
- How to parse datetime with milliseconds and timezone? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from date.