0
votes

I am using Google TimeZone API to give me the UTC offset based on an address. For instance for the city Lima it returns -18000 seconds.

How can I use this info in the DateTimeOffset and get it to only change it's Offset property accounting the difference in seconds, so that it shows current time, but where the Offset changes like this?

DateTime = {12-01-2020 21:15:33} => DateTimeOffset = {12-01-2020 21:15:33 -05:00}

And for Paris it should +01:00 in the Offset property.

1
What language are you using? What have you tried so far? If you're asking about a .Net language (C#, VB.Net, etc), this is one of the constructors of DateTimeOffset. Have you tried it yet? Did something not meet your expectations? - Matt Johnson-Pint
It is C#. I have tried a couple of things but all of them change the time. I just want to change the Offset based on the answer from the Google TimeZone API. - Oliver Nilsen

1 Answers

1
votes

I think I have figured it out. This seems to work:

DateTimeOffset now = DateTimeOffset.UtcNow; //{13-01-2020 07:39:04 +00:00}
TimeSpan timeSpanInterval = TimeSpan.FromSeconds(googleTimeZone.dstOffset + googleTimeZone.rawOffset); // {-05:00:00}
dateTimeOffset = now.ToOffset(timeSpanInterval); // {13-01-2020 02:39:04 -05:00}

Maybe it can be done in another way, but this seems to set the Offset property the way I want it.