-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement iso8601 TimeSpan formatting #190
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few comments:
@@ -231,6 +234,9 @@ class TimeSpan { | |||
|
|||
protected: | |||
int32_t _seconds; ///< Actual TimeSpan value is stored as seconds | |||
static constexpr int32_t SECS_PER_MIN = 60; ///< Number of seconds in a minute | |||
static constexpr int32_t MINS_PER_HOUR = 60; ///< Number of minutes in an hour | |||
static constexpr int32_t HOURS_PER_DAY = 24; ///< Number of hours in a day |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you feel about making these either global or public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no strong feelings either way: they certainly look like generally useful, but they are kind of trivial, and they would risk collisions in the global namespace. Maybe public would be OK. I guess you should get the opinion of a maintainer: I am just a random contributor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a static (constexpr) global then? That way they won't cause global namespace collisions. This library as-is has the magic number antipattern. Fixing it is out of scope for this PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, static constexpr
s won't work in this case because they are in the header. Statics in a header are textually #include
d, and static
means limited to a "translation uint", so the constants would still be exposed in the public interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Some more comments:
long num = strtol(cursor, &rest, 10); | ||
cursor = rest; | ||
if (*cursor == 'D') { | ||
_seconds += sign * SECONDS_PER_DAY * num; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can overflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it can. What should I do about it? Detect overflow, return 0 on failure, and add this failure mode to the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. It seems to me that the overflow cannot be detected without significant extra complexity. :-(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you feel about using __builtin_smull_overflow
? It is supported in both gcc and clang, but it's non-standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__builtin_smull_overflow()
looks nice, but I do not know whether it is supported by all the environments where RTClib can potentially be used.
Maybe a simple fix would be to build the absolute value of the duration as a uint32_t
. That won't prevent the overflow, but at least it would avoid undefined behavior. Then, at the end of the constructor,
_seconds = sign * abs_seconds;
is guaranteed to wrap correctly to the correct signed value, as long as the result is in the range of an int32_t
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the environments where RTClib can potentially be used
Is RTClib used outside the Arduino ecosystem? And if so, can it be used with any compiler other than Clang or GCC? (clang includes these intrinsics too)
What's the status of this PR? Should I do something to move it along? |
I guess you could solve the two issues raised by GitHub:
The conflict is pretty easy to fix: diff --cc RTClib.cpp
index e282548,2ecffb4..3a0ff37
--- a/RTClib.cpp
+++ b/RTClib.cpp
@@@ -694,8 -713,8 +713,8 @@@ bool DateTime::operator==(const DateTim
@return Timestamp string, e.g. "2020-04-16T18:34:56".
*/
/**************************************************************************/
-String DateTime::timestamp(timestampOpt opt) {
+String DateTime::timestamp(timestampOpt opt) const {
- char buffer[20];
+ char buffer[25]; // large enough for any DateTime, including invalid ones
// Generate timestamp according to opt
switch (opt) { I would consider rebasing on top of master, and maybe squashing the last 9 commits together. As for the “work in progress” status, you should be able to change it to “Ready for review”, when you are ready. |
ac9e45a
to
a123854
Compare
This PR is ready for (final) review and I have time to fix any problems identified in said review. |
9f002c2
to
1dca08a
Compare
// At the time of writing, this is only used in logic in TimeSpan::toCharArray | ||
// This function's inclusion is very unfortunate, and if it is used anywhere | ||
// else, should probably use a template | ||
static size_t sat_sub(size_t left, size_t right) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels dirty to add this function in here, but it is the cleanest way I could think to handle the repeated
if (written >= len) {
buf[len - 1] = '\0';
return 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I hope it gets merged, with or without my suggestions.
Uses iso 8601 period format. Includes a constructor from a string. Makes all toString-like methods const
fixes #181
I ran this test. It basically tests that serializing and constructing returns the same TimeSpan that was serialized.
I was looking for somewhere to put tests like this, but it seems like this repository doesn't really have tests.