Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.DotNet.Interactive.Parsing;

namespace Microsoft.DotNet.Interactive.Http.Parsing
Expand Down Expand Up @@ -183,6 +184,13 @@ private static DateTimeOffset DefaultGetDateTimeOffset(bool isLocal)
else if (string.Equals(type.Value, "iso8601", StringComparison.OrdinalIgnoreCase))
{
format = "o";
if (currentDateTimeOffset.Offset.TotalMinutes == 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have unit tests that cover each of the formatting cases covered in this function?

{
// for $datetime, format the DateTime in order to eliminate the +00:00 offset and use Z
string text = currentDateTimeOffset.UtcDateTime.ToString(format, formatProvider);
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a try catch around the ToString below, should there be one here too?


return node.CreateBindingSuccess(text);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a huge deal but could we do something to consolidate the return statements for the success cases? There are many returns in this block and it makes the code a little difficult to read...

For example, consider changing it to something like below where each block computes a value for text and there is a single return statement at the end of the try.

try
{
    string text;
    if (...)
    {
         text = type.Value.ToString("r", CultureInfo.InvariantCulture;
    }
    else if (...)
    {
        text = type.Value.ToString("o", Thread.CurrentThread.CurrentUICulture);
    }
...
    else
    {
        var format = type.Value.Substring(1, type.Value.Length - 2);
        text = type.Value.ToString(format, Thread.CurrentThread.CurrentUICulture);
    }

    return node.CreateBindingSuccess(text);
}
catch (FormatException)
{
   return node.CreateBindingFailure(HttpDiagnostics.IncorrectDateTimeCustomFormat(format));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

It may also make sense to extract a separate function for the try block above if you refactor the code to look like above...

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's set up a little time for a group coding session.

}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void can_bind_datetime_with_iso8601_format()
var code = $@"@var = {{{{{expression}}}}}""";

var result = HttpRequestParser.Parse(code);
var currentTime = DateTimeOffset.UtcNow;
var currentTime = DateTimeOffset.UtcNow.UtcDateTime;
var node = result.SyntaxTree.RootNode.DescendantNodesAndTokens().OfType<HttpExpressionNode>().Single();

var binding = DynamicExpressionUtilities.ResolveExpressionBinding(node, () => currentTime, expression);
Expand All @@ -66,7 +66,7 @@ public void can_bind_datetime_with_iso8601_format_with_offset()
var code = $@"@var = {{{{{expression}}}}}""";

var result = HttpRequestParser.Parse(code);
var currentTime = DateTimeOffset.UtcNow;
var currentTime = DateTimeOffset.UtcNow.UtcDateTime;
var node = result.SyntaxTree.RootNode.DescendantNodesAndTokens().OfType<HttpExpressionNode>().Single();

var binding = DynamicExpressionUtilities.ResolveExpressionBinding(node, () => currentTime, expression);
Expand Down
Loading