#14704: New function to wxDateTime to get wxDateSpan

46 views
Skip to first unread message

wxTrac

unread,
Sep 27, 2012, 6:26:46 PM9/27/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704>

#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)
}}}


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704>

wxTrac

unread,
Sep 27, 2012, 6:39:40 PM9/27/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:1>

#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].

TIA!


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:1>

wxTrac

unread,
Sep 27, 2012, 6:56:11 PM9/27/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:2>

#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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:2>

wxTrac

unread,
Sep 29, 2012, 1:25:23 PM9/29/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:3>

#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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:3>

wxTrac

unread,
Sep 29, 2012, 1:49:35 PM9/29/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:4>

#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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:4>

wxTrac

unread,
Sep 30, 2012, 9:31:19 PM9/30/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:5>

#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"


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:5>

wxTrac

unread,
Oct 1, 2012, 5:44:23 AM10/1/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:6>

#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.

In the meanwhile I'll apply this patch, thanks!


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:6>

wxTrac

unread,
Oct 1, 2012, 5:55:07 AM10/1/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:7>

#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):

* status: confirmed => closed
* resolution: => fixed


Comment:

(In [72600]) Add wxDateTime::DiffAsDateSpan().

This method returns the difference between the dates as wxDateSpan, unlike
the
existing Subtract() and overloaded operator-() that return wxTimeSpan.

Closes #14704.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:7>

wxTrac

unread,
Oct 4, 2012, 5:39:22 PM10/4/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:8>

#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):

* status: closed => reopened
* resolution: fixed =>


Comment:

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.

Regards
//Jonas


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:8>

wxTrac

unread,
Oct 4, 2012, 6:46:17 PM10/4/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:9>

#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.

Will apply the fixes soon, thanks again!


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:9>

wxTrac

unread,
Oct 4, 2012, 6:48:09 PM10/4/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:10>

#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.

See #14704.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:10>

wxTrac

unread,
Oct 4, 2012, 6:48:32 PM10/4/12
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:11>

#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):

* status: reopened => closed
* resolution: => fixed


Comment:

(In [72616]) Fix bugs in the recently added wxDateTime::DiffAsDateSpan().

Correct the test for negative spans less than a month and use the correct
month for computing the number of days in it.

Also add unit tests for problematic cases.

Closes #14704.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/14704#comment:11>
Reply all
Reply to author
Forward
0 new messages