Reputation: 81
I appreciate that this question has been covered many times over in various forms, but none seem to help with the particular problem that I'm experiencing. I have the following method that I use to convert incoming data to a C# DateTime
variable. The trouble is, the company sending the data in are claiming that I'm converting it incorrectly because the times are appearing one hour behind in my system. I would assume that this is something to do with British Summertime, but is the problem at my end, or at their end, because I thought the method below would be taking account of British Summertime by the fact I'm using ToLocalTime
? Any help would be gratefully accepted.
private DateTime ConvertTimeStamp(double t)
{
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
.AddMilliseconds(t)
.ToLocalTime();
}
Upvotes: 1
Views: 799
Reputation: 5398
Well it depends what they are giving you. But assuming they are giving you UTC linux time you could be using this method of .Net 4.6 and convert it to your local time via .ToLocalTime()
. As the other answer suggests, it would be better to make sure to specify a "TimeZoneInfo" instead of using the .ToLocalTime()
.
Upvotes: 0
Reputation: 21147
ToLocalTime
will adjust the timezone to be whatever the local time is of the computer actually running this method. Depending on where it's run from/deployed in the cloud this can give you wildly different results. Timezones can be a huge pain in the butt for this kind of stuff. It would be ideal if they would send you the UTC unix representation and you could unwrap your DateTime with ToUniversal and from there you can safely convert it back to a timezone of your choosing.
If they're not sending UTC you need to find out exactly what Timezone that they're expecting and use that specifically instead of ToLocal.
Upvotes: 4