Skip to content

Change default DateTime/TimeOnly/DateOnly parsing logic to use UTC #36725

@davidfowl

Description

@davidfowl

Describe the bug

Today we parse DateTime/TimeOnly/DateOnly types as local time (the default), instead, we should default to universal time. A similar change was made in MVC and we should align in minimal APIs.

To Reproduce

See the repro from here #11584 (comment)

using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/", (HttpRequest request, DateTime time) =>
{
    var result = $"Current culture: {CultureInfo.CurrentCulture}";

    var rawTime = request.Query["time"];
    result += $"\r\nRaw querystring value is: {rawTime}";

    // Format string "O" is ISO 8601 round-trippable https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip
    result += $"\r\nModel-bound value is: {time.ToString("O")}, Kind is: {time.Kind}";

    var utcTime = time.ToUniversalTime();
    result += $"\r\nModel-bound converted to UTC time value is: {utcTime.ToString("O")}, Kind is: {utcTime.Kind}";

    var parsedTime = DateTime.Parse(rawTime);
    result += $"\r\nParsed time value is: {parsedTime.ToString("O")}, Kind is: {parsedTime.Kind}";

    var parsedTimeUtc = DateTime.Parse(rawTime, CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal);
    result += $"\r\nParsed UTC time value is: {parsedTimeUtc.ToString("O")}, Kind is: {parsedTimeUtc.Kind}";

    return result;
});

app.Run();

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions