Skip to content

DateTimeStyles.AdjustToUniversal documentation misleading #9521

Open
@Arithmomaniac

Description

@Arithmomaniac

<summary>Date and time are returned as a Coordinated Universal Time (UTC). If the input string denotes a local time, through a time zone specifier or <see cref="F:System.Globalization.DateTimeStyles.AssumeLocal" />, the date and time are converted from the local time to UTC. If the input string denotes a UTC time, through a time zone specifier or <see cref="F:System.Globalization.DateTimeStyles.AssumeUniversal" />, no conversion occurs. If the input string does not denote a local or UTC time, no conversion occurs and the resulting <see cref="P:System.DateTime.Kind" /> property is <see cref="F:System.DateTimeKind.Unspecified" />. This value cannot be used with <see cref="F:System.Globalization.DateTimeStyles.RoundtripKind" />.</summary>

The documentation for AdjustToUniversal says

If the input string denotes a UTC time, through a time zone specifier or AssumeUniversal, no conversion occurs.

This is not strictly true. When AssumeUniversal is used with DateTimeOffset:

If format does not require that input contain an offset value, the returned DateTimeOffset object is given the UTC offset (+00:00).

So AdjustToUniversal indeed has no effect.

But when AssumeUniversal is used for DateTime:

Specifies that if s lacks any time zone information, it is assumed to represent UTC. Unless the DateTimeStyles.AdjustToUniversal flag is present, the method converts the returned DateTime value from UTC to local time and sets its Kind property to DateTimeKind.Local.

So AdjustToUniversal will do the following conversation:

calls ToUniversalTime to convert the returned DateTime value to UTC, and sets the Kind property to DateTimeKind.Utc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.DateTimeuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions