DS3231 icon indicating copy to clipboard operation
DS3231 copied to clipboard

DateTime comparison operators

Open namanvs opened this issue 5 years ago • 9 comments

Can we get '<', '>', '-', '+' operator overloading for the DateTime class?

I can submit a pull request to implement this if you like.

namanvs avatar Jun 15 '20 13:06 namanvs

Please do submit a PR! I am no longer very actively maintaining this code, so community support is much appreciated.

awickert avatar Jun 23 '20 03:06 awickert

Overloading might be desirable. If @namanvs would submit a PR I would be happy to review it.

It may be worth noting, however, that such comparisons and arithmetic can be done presently without overloading. The approach would be to calculate a difference in seconds, taking advantage of the DateTime::unixtime() method.

Code segment:

DateTime A(); // assume initiated properly
DateTime B(); // assume initiated properly
uint32_t a = A.unixtime();
uint32_t b = B.unixtime

if (a > b) { // A is after B }

uint32_t diff = a - b;

IowaDave avatar Aug 16 '22 21:08 IowaDave

Good point @IowaDave.

I don't have need for this overloading so won't be dev'ing it. How about this as a plan?

  • If we hear from @namanvs soon-ish about creating a PR, great
  • Otherwise, we close this as wontfix with reason "not enough current motivation"

awickert avatar Aug 19 '22 20:08 awickert

Hi @awickert and @IowaDave, sorry, I've been travelling but I would be happy to write these operators. I will need about 10 days to get back up to speed and have something ready, but I plan on implementing the following operators: '<', '>', '+', and '-'. I'll probably use @awickert 's suggested strategy but wrap it up into the operators for convenience. I am toying with a modulo operator to return the remainder between 2 operands in the next-smallest unit:

DateTime a % DateTime::DAYS returns number of hours in the remainder.

Would this seem useful?

namanvs avatar Aug 20 '22 00:08 namanvs

I will wait to complete and PR my documentation on DateTime until after the proposed enhancements to DateTime are in place.

Thanks!

On Fri, Aug 19, 2022 at 7:03 PM Naman Shah @.***> wrote:

Hi @awickert https://github.com/awickert and @IowaDave https://github.com/IowaDave, sorry, I've been travelling but I would be happy to write these operators. I will need about 10 days to get back up to speed and have something ready, but I plan on implementing the following operators: '<', '>', '+', and '-'. I'll probably use @awickert https://github.com/awickert 's suggested strategy but wrap it up into the operators for convenience. I am toying with a modulo operator to return the remainder between 2 operands in the next-smallest unit:

DateTime a % DateTime::DAYS returns number of hours in the remainder.

Would this seem useful?

— Reply to this email directly, view it on GitHub https://github.com/NorthernWidget/DS3231/issues/24#issuecomment-1221179878, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIOVVUBUWKW4I4XMDP6ACMDV2AOGBANCNFSM4N6FTSMA . You are receiving this because you were mentioned.Message ID: @.***>

IowaDave avatar Aug 20 '22 19:08 IowaDave

@namanvs , @awickert , I have added the DateTime documentation to the master branch of this repo. In the near future, PR #70 may add an example program that uses a DateTime calculation to advance the time values of an alarm.

I'll look forward to viewing that PR you mentioned, if you choose to offer it.

Thank you sincerely,

David

IowaDave avatar Sep 15 '22 03:09 IowaDave

I have submitted a PR. This is actually the first time I'm contributing to code on Github. Open to feedback and willing to make improvements where identified.

namanvs avatar Sep 16 '22 17:09 namanvs

@namanvs Welcome! I look forward to reviewing your code.

Please see my comments in the PR (#71). Thanks!

IowaDave avatar Sep 16 '22 23:09 IowaDave

Changed the label to Enhancement because that is really what @namanvs is proposing. Leaving this Issue open pending resolution of his PR #71.

cc: @awickert

IowaDave avatar Sep 18 '22 14:09 IowaDave