0

I got pretty much confused today when talking to a colleague, so please excuse me. I have this:

  1. date: 2020-04-01 (yyyy-MM-dd)
  2. time: 10:00:00
  3. 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.

mslot
  • 4,959
  • 9
  • 44
  • 76
  • See [Wiki ISO 8601 - Time offsets from UTC](https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC) – Igor Apr 01 '20 at 15:16
  • The `DateTime` value is the local value. You can use the `Offset` to calculate UTC from that `DateTime` value. – Heretic Monkey Apr 01 '20 at 15:18
  • Does this answer your question? [DateTime vs DateTimeOffset](https://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset) – Heretic Monkey Apr 01 '20 at 15:21
  • @HereticMonkey somewhat it does. I actually understand the answer you refer to, but, i dont understand what the date and time represent in a DateTimeOffset. I can see now that the DateTime in a DateTimeOffset has a DateTime. That DateTime is the local value. – mslot Apr 01 '20 at 15:27
  • 1
    You could solve this problem forever by changing the JSON structure you have agreed upon to use to have two fields, one called "userLocalTime" and one called "userTimeZone" and now there is no ambiguity to have an argument about. Or "userUTCTime" and one called "userTimeZone" and again, no ambiguity to have an argument about. The problem is that you've having an argument, so fix that problem. – Eric Lippert Apr 01 '20 at 15:34
  • @EricLippert and that is what I want, but I also want to assure that the problem gets solved and for that I need to understand why his Angular frontend is adding the two hours, so the 12:00:00 is becoming the result, instead of 10:00:00. So I have been confused if the time in the offset was local time or utc time. I can now see it is local time, as I expected. So now I can use that to 1) fix his code 2) make a more clean api – mslot Apr 01 '20 at 15:45
  • @HereticMonkey could you put it in this was: if you remove the +02:00 from 2020-04-01T10:00:00+02:00 you will have the local time? – mslot Apr 01 '20 at 16:21
  • Well, now you're adding a serialization step. I was just talking about how the .NET `DateTimeOffset` type works. According to [this dotnetfiddle](https://dotnetfiddle.net/G3nLQR) it looks like indeed, removing "+02:00" does indeed leave the local time. – Heretic Monkey Apr 01 '20 at 16:32
  • @HereticMonkey that is correct. I am. DateTime is the local part of the DateTimeOffset and it looks like the DateTime part is indeed serialized and shown. That must be the correct way of looking at it. There must be some sort of bug in the javascript since it adds the offset to the local part. That is what I find the most strange. – mslot Apr 01 '20 at 17:10

0 Answers0