1

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();
}
6
  • Good point mjwills - not sure why I hadn't noticed that before. I'll edit the code above because it will make it easier to read too. Commented Jul 28, 2017 at 13:36
  • I've not got an example of their raw data unfortunately, so probably not much help, but they're claiming that they're sending the following datetime; 18/07/2017 16:28:09 - and that this appears in my system (having converted the incoming time stamp using the method above) as; 18/07/2017 15:28:09 Commented Jul 28, 2017 at 13:39
  • 1
    Could you ask them to provide some sample data for you to use in testing? ie they send you some numbers and what datetime they should represent and then you can use those for testing properly. I can't imagine they'd be reluctant to do this... Commented Jul 28, 2017 at 13:40
  • 1
    Unless you can tell us the matching t value, it is hard to be super helpful alas. Commented Jul 28, 2017 at 13:41
  • What do you get before ToLocal? Basically you need to determine if they are sending the date in UTC to begin with or not. If they will not provide the numbers maybe you can add some logging? Commented Jul 28, 2017 at 13:42

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.