0

I have a use case that I'm not sure how to solve in a nice way. I'm currently developing a .Net Core WebApi that is receiving data from various current systems, from a cross the whole world. Which I then process and lastly I commit it to SAP through oData endpoint.

The problem I'm having is on of parameters I'm receiving in the body payload, is a DateTime. Previous I have not have any issues. But not long ago I started getting data from a other system which deliverers it in a slightly differently way.

Previously this was the format I got: 2020-09-16T16:30:00 not problem with it. But the new system looks like this: 2020-09-16T16:00:00 -05:00 Could also end in +08:00.

The problem I'm facing is that SAP needs to get in local time. But in the my code it converts this: 2020-09-16T16:00:00 -05:00 to 2020-09-16T23:00:00 when I see the incoming payload in the controller.

  • I have searched quite a bit to find a solution. But 99% only suggest using UTC time, which is not a option for me.

  • Another option is to use DateTimeOffset, which I have tried but can't the time conversion to use localTime.

My question is. Are it not possible to custom convert to strip the timezone before it hits the controller?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
mortenstarck
  • 2,713
  • 8
  • 43
  • 76
  • You want something strange... it usually means custom binding with string manipulation or switching to strings for data type and doing string manipulation in controller. I'd strongly recommend to stop and think what you are doing - 01:23 -01:00, 01:23 -02:00, 01:23 -03:00 are 3 different moments in time an hour apart... stripping them of timezone and saying all happened at 1:23 sounds incorrect (which may be what your goal is, just a strange goal) – Alexei Levenkov Sep 16 '20 at 21:52
  • @AlexeiLevenkov Yearh i know its a strange goal. My issue is that im bound on what i recieve and what in need to deliver. If you have another suggestion for a solution i welcome that. – mortenstarck Sep 16 '20 at 22:19
  • No, I don't see any other solutions except I suggested already... You may get better code with https://stackoverflow.com/questions/6676856/datetime-parseexact-ignore-the-timezone to strip timezone... but you have to ether switch to strings or implement custom model binding / custom JSON parsing depending on where this parameter is. – Alexei Levenkov Sep 16 '20 at 22:40

2 Answers2

1

Generarally when you're working with datetime data that includes offsets for time zone the DateTimeOffset type is a good place to start. The sample string 2020-09-16T16:00:00 -05:00 can be passed to DateTimeOffset.Parse() to get a correct DTO value with timezone information attached. From there you can get the local time, UTC time or a DateTime value with the timezone stripped.

string source = "2020-09-16T16:00:00 -05:00";
string fmt = @"yyyy-MM-dd\THH:mm:ss zzz"

// Same as input
Console.WriteLine(DateTimeOffset.Parse(source).ToString(fmt));

// Adjusted to your local timezone
Console.WriteLine(DateTimeOffset.Parse(source).ToLocalTime().ToString(fmt));

// DateTime portion of the source, timezone offset ignored
Console.WriteLine(DateTimeOffset.Parse(source).DateTime.ToString());

Getting the UTC time is simple too via the UtcDateTime property.

It sounds like what you want is the last one - just the date and time from the inputt string with the timezone offset stripped. If you just want the corresponding local time then DateTime.Parse should give that to you directly.

The JsonSerializer class doesn't support this format for DateTimeOffset so you might have some trouble getting it converted before hitting your controller. In that case you'd need to accept a string and do the conversion by hand in your code. You also might need to investigate the TryParseExact method.

Corey
  • 15,524
  • 2
  • 35
  • 68
0

Use DateTime.Parse() , for example

string timeInString1 = "2020-09-16T16:00:00 -05:00";
DateTime moment1 = DateTime.Parse(timeInString1);

string timeInString2 = "2020-09-16T16:00:00 +08:00";
DateTime moment2 = DateTime.Parse(timeInString2);

string timeInString3 = "2020-09-16T16:30:00";
DateTime moment3 = DateTime.Parse(timeInString3);

but momen1, momen2, or moment3 is non-timezone awareness value.

Vy Do
  • 46,709
  • 59
  • 215
  • 313