I’ve been Googling for a while now trying to find some sort of solution to this but have had little luck.
I’m trying to get a set of records that corresponds to a single day’s worth of data. A single day’s worth of data is 4:00:00EST-3:59:59EST. When I display my set of results to the user I would like the results to be displayed in the user’s local time (in my case CST). This wouldn’t normally be all that hard except:
- The timestamps in my database are all in GMT.
- The local time of the database is in EST.
- My local time is CST.
Since the database’s time is in EST but the records are saved in GMT I can’t simply call GETDATE() and adjust it.
It’s also worth noting that I’m using LINQ/Entity Framework 4.1 Code-First in C# and database I’m hitting is Sybase.
So, what would be a solid way for me to convert the data from GMT to the user’s local time given the information above?
It turns out that I was overthinking this quite a bit and the solution is actually quite simple. The following links were extremely helpful:
http://iamacamera.org/default.aspx?section=develop/code%20snippets&id=76
http://msdn.microsoft.com/en-us/library/ms973825.aspx
Before querying the database I set the following variables:
Which gives me UTC time of 5/10/2012 8:00:00 AM to 5/11/2012 8:00:00 AM. Converting this to EST is rather simple:
This will give me EST time of 5/10/2012 4:00:00 AM.
Using this in my LINQ query:
It’s worth nothing that TimeZoneInfo.ConvertTimeFromUtc() will probably not work unless you run the query on the client using .AsEnumerable(). I know it definitely doesn’t work with our version of Sybase. Thus, I’m forced to filter the results as much as I can, call .AsEnumerable(), and then do the time zone conversion/other logic.