How to correctly initialize DateTimeTimeZone from DateTimeOffset instance?
Extensions class DateTimeTimeZoneExtensions has two variants of ToDateTimeTimeZone for DateTimeOffset.
public static DateTimeTimeZone ToDateTimeTimeZone(this DateTimeOffset dateTimeOffset)
{
return new DateTimeTimeZone
{
DateTime = dateTimeOffset.ToString(DateTimeFormat, CultureInfo.InvariantCulture),
TimeZone = string.Empty
};
}
public static DateTimeTimeZone ToDateTimeTimeZone(this DateTimeOffset dateTimeOffset, TimeZoneInfo timeZoneInfo)
{
return new DateTimeTimeZone
{
DateTime = dateTimeOffset.ToString(DateTimeFormat, CultureInfo.InvariantCulture),
TimeZone = timeZoneInfo.Id
};
}
Which one should i use?
Why the first variant keep TimeZone field empty and doesn't calculate it from offset parameter of DateTimeOffset value?
What's point of TimeZone field in the second variant if dateTimeOffset.ToString() with DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffK" will already have correct time offset in result string because of K symbol in formatting string?
The second variant is easiest way to have incorrectly initialized DateTimeTimeZone by putting wrong timeZoneInfo parameter. Because in this case you will have DateTime with offset of some time zone and TimeZone field with name for other time zone.