Skip to content
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

[.NET Timexlib] Implement missing features of TimexProperty.ToString() #2903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shana
Copy link
Contributor

@shana shana commented Mar 15, 2022

Following up on PR #2894 , this implements the missing features of TimexProperty.ToString(), adding support for date ranges, date + duration, etc.

This also fixes a bunch of off-by-one bugs when adding days or months to a Timex, where it would not take into account potential month/year overflows.

It also adds caching of the Types hashset of the Timex, so that it doesn't recalculate it every time the property is accessed. Instead, Types get cleared whenever a meaningful piece of data in the TimexProperty is changed.

Some tests have been converted to using DataRow instead of inline asserts, so that the test log will show the data being tested. It should also make it easier to see what the tests are doing. I also moved ToString tests to a separate class from the Roundtrip tests so it's easier to follow.

There's a lot of static helper methods that have been made into extension methods, because they're already in static classes so there's no overhead in doing it, and it makes caller code shorter and hopefully simpler to read. This has the side effect of making these DateTime extensions methods visible to consumers that include the TimexExpression namespace in the code - I don't know if that's undesirable or not, let me know if it's not something you want.

Fixes #2904

This implements the missing features of TimexProperty.ToString(), adding support for date ranges, date + duration, etc.

This also fixes a bunch of off-by-one bugs when adding days or months to a Timex where it would not take into account potential month/year overflows.

It also adds caching of the `Types` hashset of the Timex, so that it doesn't recalculate it every time the property is accessed. Instead, `Types` get cleared whenever a meaningful piece of data in the `TimexProperty` is changed.

Some tests have been converted to using `DataRow` instead of inline asserts, so that the test log will show the data being tested. It should also make it easier to see what the tests are doing.

There's a lot of static helper methods that have been made into extension methods, because they're already in static classes so there's no overhead in doing it, and it makes caller code shorter and hopefully simpler to read.
@@ -217,7 +226,7 @@ public static TimexProperty TimexDateAdd(TimexProperty start, TimexProperty dura
return new TimexProperty
{
Year = start.Year,
Month = (int)(start.Month + duration.Months),
Month = ((int)(start.Month + duration.Months) % 12) + 1, // months are from 1 to 12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is still buggy, will add a test for it to tomorrow.

Copy link
Collaborator

@tellarin tellarin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review

}

[TestMethod]
public void DataTypes_Timex_ToString()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to the cases from this one onwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved them to TestTimexToString because all the tests before this are pure roundtrip tests and it was getting hard to track what tests tested what codepaths.

}

[TestMethod]
public void DataTypes_Convert_FridayEvening()
[DataRow("(XXXX-XX-XX,XXXX-XX-XX,P1D)", "1 day")]
public void DataTypes_Convert_Period(string timex, string expected)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to rename to DataTypes_Convert_Undefined_Period or DataTypes_Convert_Undefined_Range for clarity.

}

[TestMethod]
public void DataTypes_Convert_WednesdayToSaturday()
[DataRow("(2017-09-08T21:19:29,2017-09-08T21:24:29,PT5M)", "8th September 2017 9:19:29PM to 9:24:29PM")]
public void DataTypes_Convert_Last5Minutes(string timex, string expected)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the dates are different? Do we have a case for that somewhere for it?

Also, this is technically not "last 5 min". It's just a period of 5 min with those parameters.
And the order in the timex is invalid. Start should be smaller than end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't actually do anything with this, I just converted the test to DataRow and added the output that it was missing - the github inline diff is misleading. This is the original code of the test:

[TestMethod]
        public void DataTypes_Convert_Last5Minutes()
        {
            // date + time + duration
            var timex = new TimexProperty("(2017-09-08T21:19:29,2017-09-08T21:24:29,PT5M)");

            // TODO
        }

I didn't change anything in the minute duration to string conversion, so the output here is the same output that it has always had. I agree that the output doesn't seem to match the output of the test, I just added the output of the original test here so I could see what it was doing.

@@ -93,7 +93,7 @@ public void DataTypes_Helpers_TimeRangeFromTimex()
public void DataTypes_Helpers_DateFromTimex()
{
var timex = new TimexProperty("2017-09-27");
var date = TimexHelpers.DateFromTimex(timex);
var date = TimexHelpers.DateTimeFromTimex(timex);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both should return the same results, but the first one is "more correct" as the timex represents a date, not a datetime.

@shana
Copy link
Contributor Author

shana commented Apr 19, 2022

Fyi, I've been away for Easter and life and couldn't follow up on this before, I should have more time now to get this thing done, it's not forgotten.

@tellarin
Copy link
Collaborator

Fyi, I've been away for Easter and life and couldn't follow up on this before, I should have more time now to get this thing done, it's not forgotten.

Thanks for the update, @shana!

@tellarin
Copy link
Collaborator

@shana Will you still update this PR? Looking forward to merging it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[.NET Timexlib] TimexProperty.ToString() does not support daterange/datetimerange and other complex variants
2 participants