I got pretty much confused today when talking to a colleague, so please excuse me. I have this:
- date: 2020-04-01 (yyyy-MM-dd)
- time: 10:00:00
- timezone: +2
When I return this to my colleague, I do it through an API that returns json, it returns something on the line
{
...
"dateTimeOffset": "2020-04-01T10:00:00+02:00"
...
}
I build my DateTimeOffset like this:
var utc = DateTime.UtcNow;
string time = "10:00:00";
string timezoneIdentifier = "Romance Standard Time";
var date = DateTime.Now.Date;
var zone = TimeZoneInfo.FindSystemTimeZoneById(timezoneIdentifier);
var timespanOffset = zone.GetUtcOffset(utc);
var baseDateTime = date + TimeSpan.Parse(time);
baseDateTime= DateTime.SpecifyKind(baseDateTime, DateTimeKind.Unspecified);
var dateTimeOffset = new DateTimeOffset(baseDateTime, timespanOffset);
When he pulls it in, his part converts the time to "12:00:00", that is: it adds the timezone, and it really got me, because I got so confused, because, he wanted me to send him "08:00:00 +2", so it was showing correctly to the end user, but i refused, because my understanding is: the time part is the local time and the +2, describes the offset from UTC, and it isn't the other way around: that the time part is UTC and you have to add the offset to get the local time. I can't find it anywhere in the docs: https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=netcore-2.2 other than
The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC). Because it exactly defines a date and time relative to UTC, the DateTimeOffset structure does not include a Kind member, as the DateTime structure does
But that is not good enough for me. My brain will not accept it. So: 1. is the time in a DateTimeOffset the local time of a given zone, or 2. is the time the UTC time, and you have to add/substract the offset to get the local time
I know this should be basic stuff, but we have discussed it so much that I dont know what to think anymore.
Hope someone can help.