#14704: New function to wxDateTime to get wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: new
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Hi
A new function to wxDateTime similar to
{{{
wxTimeSpan Subtract(const wxDateTime &dt)
}}}
which returns a wxDateSpan with all fields filled:
{{{
wxDateSpan SubtractDate(const wxDateTime &dt)
}}}
Discussion subject changed to "#14704: New function to get difference of two wxDateTime objects as a wxDateSpan (was: New function to wxDateTime to get wxDateSpan)" by wxTrac
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: confirmed
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Changes (by vadz):
* status: new => confirmed
Comment:
Thanks for your patch, I agree that this could be useful.
But I'm not sure if `SubtractDate()` is really a good name for it, I
really would be hard pressed to explain how is it different from just
`Subtract()`. I can propose `SubtractLogical()` but this is not ideal
neither so what about a longer but perfectly clear `DiffAsDateSpan()`? It
is inconsistent with `Subtract()`, of course, but OTOH it's really a
different operation so I think clarity is more important than consistency
here. But any other suggestions for the name would be welcome.
Also, and this is really important, could you please add a few tests for
the new function to
[source:wxWidgets/trunk/tests/datetime/datetimetest.cpp
tests/datetime/datetimetest.cpp]? If you have trouble with this, please
see [source:wxWidgets/trunk/docs/tech/tn0017.txt the instructions].
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: confirmed
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Comment(by jonasr):
I'll think about the name. I wanted it to be as close to Subtract as
possible, but perhaps i'ts too close.
As for tests, I didn't find any tests for Subtract/Add in
datetimetest.cpp, and the size of it, and its embedded python scripts,
scared me.
But now that i revisit it, I realize that operator- is an alias for
subtract, so now i feel dumb. I'll try to cook up some tests.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: confirmed
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Comment(by jonasr):
Writing the test I noted that arithmetics with the resulting wxDateSpan
gives wrong results.
Consider this:
{{{
wxDateTime dt1(11, wxDateTime::Jan, 1996);
wxDateTime dt2(5, wxDateTime::Jun, 1998);
wxDateSpan diff = dt2.DiffAsDateSpan(dt1);
// now diff has years=1, months=28, weeks=125, days=0
wxDateTime dt3 = dt1 + diff;
// now dt3 = 2001-10-02, so dt3 != dt2
}}}
This is because DiffAsDateSpan fills all fields that it can, so calls to
GetYears()/GetMonths()/GetDays() etc. returns correctly for the diff.
But wxDateSpan::Add(const wxDateSpan&) uses all fields for adding, so it
effectivly adds the span four times.
This maybe is somewhat unexpected, so i make a not of it in the
documentation.
This means that writing a test that checks arithmetics is not possible, so
the only thing I can think of is testing that the calculation of
year/month/week/day is correct, hope that's ok.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: confirmed
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Comment(by vadz):
Err, I think the implementation is wrong then (hence the usefulness of
writing tests!). I'd expect the operation above to give "2 years, 4
months, 25 days", i.e. the number of months should never be greater than
12 in `wxDateSpan`. And if it worked like this, then addition would work,
as expected.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: confirmed
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Comment(by jonasr):
For my purpose, I needed to get a span that returns the actual diff
between two dates for each part of
GetYears()/GetMonths()/GetWeeks()/GetDays(), so I wanted GetMonths() to
return 28 in example above. (btw, i see it's a typo in the example, year
in diff is ofcourse 2, not 1)
But I guess that this is a misunderstanding/misuse of wxDateSpan, and I
agree that arithmetics should work as expected.
Now this function is of no use to me, but if someone else finds it useful
I made a new patch.
Note that for the tests to pass, and to keep with how I understand
wxDateSpan, weeks are set, so it's not exactly like your example, but
rather "2 years, 4 months, 3 weeks, 4 days"
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
-------------------------+------------------------------------------------- -
Reporter: jonasr | Owner: Type: enhancement | Status: confirmed
Priority: normal | Milestone: Component: base | Version: Keywords: wxDateTime | Blockedby: Patch: 1 | Blocking: -------------------------+------------------------------------------------- -
Comment(by vadz):
I think you may want to add `wxDateSpan::GetTotalMonths()` method, just as
already have `GetTotalDays()`. Then you should be able to get the values
that you need. Of course, it's not exactly difficult to write
`12*GetYears()+GetMonths()` directly neither but I wouldn't be against
adding such method if people think it can be useful.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
--------------------------+------------------------------------------------ -
Reporter: jonasr | Owner:
Type: enhancement | Status: closed
Priority: normal | Milestone:
Component: base | Version:
Resolution: fixed | Keywords: wxDateTime
Blockedby: | Patch: 1 Blocking: |
--------------------------+------------------------------------------------ -
Changes (by VZ):
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
--------------------------+------------------------------------------------ -
Reporter: jonasr | Owner:
Type: enhancement | Status: reopened
Priority: normal | Milestone:
Component: base | Version:
Resolution: | Keywords: wxDateTime
Blockedby: | Patch: 1 Blocking: |
--------------------------+------------------------------------------------ -
Changes (by jonasr):
Here's a patch for {{{wxDateSpan::GetTotalMonths()}}}.
But starting to use this function in my app, I'm embarrased to say that my
previous patch had a couple of bugs in it. Turns out that the test I added
were to trivial. Dates are tricky.
Attached is a patch that fixes {{{wxDateTime::DiffAsDateSpan}}}, plus more
extensive tests.
Hope it's ok to add 2 patches to an old ticket like this.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
--------------------------+------------------------------------------------ -
Reporter: jonasr | Owner:
Type: enhancement | Status: reopened
Priority: normal | Milestone:
Component: base | Version:
Resolution: | Keywords: wxDateTime
Blockedby: | Patch: 1 Blocking: |
--------------------------+------------------------------------------------ -
Comment(by vadz):
If it helps, it's rare for me to write any non trivial code working with
dates without bugs too. The best is to generate a lot of random data
(there are small Python scripts embedded into the test, e.g. see
source:wxWidgets/trunk/tests/datetime/datetimetest.cpp#L383) and check
that everything works correctly with it.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
--------------------------+------------------------------------------------ -
Reporter: jonasr | Owner:
Type: enhancement | Status: reopened
Priority: normal | Milestone:
Component: base | Version:
Resolution: | Keywords: wxDateTime
Blockedby: | Patch: 1 Blocking: |
--------------------------+------------------------------------------------ -
Comment(by VZ):
(In [72615]) Add wxDateSpan::GetTotalMonths() method.
This is similar to the existing GetTotalDays() and counts both months and
years.
#14704: New function to get difference of two wxDateTime objects as a wxDateSpan
--------------------------+------------------------------------------------ -
Reporter: jonasr | Owner:
Type: enhancement | Status: closed
Priority: normal | Milestone:
Component: base | Version:
Resolution: fixed | Keywords: wxDateTime
Blockedby: | Patch: 1 Blocking: |
--------------------------+------------------------------------------------ -
Changes (by VZ):