Skip to content

Conversation

bleaphar
Copy link
Contributor

@bleaphar bleaphar commented Nov 6, 2024

Introducing a change for datetime iso8601 so that it reflects the same format as visual studio code and reduces round tripping difficulties.

if (currentDateTimeOffset.Offset.TotalMinutes == 0)
{
// 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?

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

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.

@@ -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?

Copy link
Contributor

@shyamnamboodiripad shyamnamboodiripad left a comment

Choose a reason for hiding this comment

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

lgtm modulo comments

@jonsequitur jonsequitur merged commit 8480c35 into dotnet:main Nov 15, 2024
4 checks passed
@jonsequitur jonsequitur added the Area-HTTP HttpKernel, Visual Studio HTTP Editor label Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-HTTP HttpKernel, Visual Studio HTTP Editor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants