3

I am writing a .NET application with the Stack Exchange API (using the Stacky library) and I am using it to ask the server for some questions that occured in the last hour. However, I and the server are not in the same time zone, so if I ask it for questions from DateTime.Now.AddHours(-1) to DateTime.Now, it gives me questions from 9 hours ago to 8 hours ago.

This is obviously a problem. Right now I am getting over it by asking for questions from DateTime.Now.AddHours(7) to DateTime.Now.AddHours(8), but will obviously not work for people in other time zones. How can I compensate for the time difference in such a way that it will work for all users?

4
  • 1
    The API uses UTC for all methods / results. You need to find a way to get the current UTC time in C#. This would make an excellent question for Stack Overflow. May 23, 2011 at 6:36
  • @George thanks, I thought questions about the API were supposed to go here. May 23, 2011 at 6:37
  • 1
    They are and this is certainly an on-topic question. I just thought your C# question might get a quicker answer on Stack Overflow. May 23, 2011 at 6:40
  • @George as it happens, a 2 second Google yielded DateTime.ToUniversalTime, so problem solved. If you copypaste that into an answer, I'll mark it as such. May 23, 2011 at 6:41

1 Answer 1

4

As I learned from George Edison in his comment, the server handles time in UTC format. Just replace your calls to DateTime.Now with DateTime.UtcNow or you can convert any DateTime to UTC format with the ToUniversalTime method. Just make sure not to call ToUniversalTime on the server's time, or it'll get messed up.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .