Should DateTime.UtcNow have a timezone offset?

This (LINQPad snippet):

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz").Dump();
DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz").Dump();

Return:

2016-01-08T09:05:04-07:00 // Expected
2016-01-08T16:05:04-07:00 // Not what I expected

Given that the second result is universal time, I expected it to return 2016-01-08T16:05:04-00:00(clockwise offset of zero).

Am I missing something or is this a mistake?

+4
source share
1 answer

I want to combine the comments of Guzman and Evan and create some arguments against them, if they allow me. Let's do it step by step.

Should it DateTime.UtcNowhave a time zone offset?

, Kind, (Local, Utc Unspecified), DateTime UTC . .

, "zzz";

DateTime "zzz" UTC, . DateTime.Kind. "zzz" DateTime.

, , LINQPad, . ( ) Visual Studio.

" " (Ctrl + F5)

Console.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
Console.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz"));

, " " (F5)?

, DateTimeInvalidLocalFormat " . Debug, Exceptions.

enter image description here

, , :

A UTC DateTime . DateTime.ToString 'z', . 'z' , UTC 'o'string, DateTime .

, , , LINQPad.

, - , , 2016-01-08T16:05:04-00:00 ( ).

, zzz , DateTime.

- DateTimeOffset, :

DateTimeOffset DateTimeOffset value Offset UTC .

DateTimeOffset.Now DateTimeOffset.UtcNow , LINQPad Visual Studio, :

DateTimeOffset, - - (UTC).

UtcNow

, (UTC) , TimeSpan.Zero.

Console.WriteLine(DateTimeOffset.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
Console.WriteLine(DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz"));

2016-01-08T09:05:04-07:00
2016-01-08T16:05:04-00:00

Tl; dr , . LINQPad , . Visual Studio , .

+5

Source: https://habr.com/ru/post/1623455/


All Articles