tags:

views:

840

answers:

4

I have a web application that I'm writing (C#, MSSQL) and I need to store the timestamp when a record is stored in the system. Normally, I would just do this with SQL and the DATETIME function. However, the server is located in a different time zone than our company is located... AND we may change to another server in completely different time zone. The hosting company will not change the server time to match our local time zone. (Not that I blame them, but it was one thing I tried.)

So, my question is, what is the best way to store the date/time of the record update and what is the best way to present that date/time back to the user in our local time zone?

I want the least messy way of doing this, so a combo of C# and SQL would be fine as long as the solution is easy to implement. (My normal style is to do more work in stored procedures and less in C#, if that matters.)

Can you show me some sample code? Thanks!

+6  A: 

Save UTC times in your database and localize those times every time you wanna show them to a user

sebastian
In your stored procedures use GETUTCDATE() instead of GETDATE() if not providing from the application.
tvanfosson
+4  A: 

Store it as UTC time and then preform the time zone calculation on the client end in C# use .ToUniversalTime() on the DateTime object to get the UTC time, store that on the server and then use .ToLocalTime() to convert it back.

This is similar to what I do except I have my own ToLocalTime extension method in C#. That is because the web server is located in the USA but I want the times to appear as Australian times.
Craig
+13  A: 

always store DATETIME data in Universal Time Coordinated (UTC aka GMT)

  • this avoids all timezone issues
  • this avoids daylight-savings-time issues
  • this allows simple date math to always work
  • this allows transactions in different time zones to stay in synch
  • this allows you to move the data to a different server in a different time zone and not screw everything up
  • etc. "Universal" time, make sense?

C# DateTime provides DateTime.UtcNow and ToLocalTime(), SQL provides GETUTCDATE(). Here's a SQL function to convert UTC to local time -

-- convert UTC to local time
create FUNCTION [dbo].[udfUtcToLocalTime]
(
    @gmt datetime
)
RETURNS datetime
AS
BEGIN
    DECLARE @dt datetime
    SELECT 
        @dt = dateadd(millisecond,datediff(millisecond,getutcdate(), getdate()),@gmt)
    RETURN @dt
END

example:

SELECT dbo.udfUtcToLocalTime(someDateTimeField)
FROM someTable
Steven A. Lowe
This function will fail when the timzeone offset changes because of daylight saving time.
Dave Markle
@[Dave Markle]: it is supposed to change when daylight saving time changes!
Steven A. Lowe
What he means is that if you look at information from last week, prior to the date change, you will see it with today's offset not the offset from last week. Your function only works if you want to see local times based on today's offset. The .Net function DateTime.ToLocalTime does not work this way. It will show you what the local time was based on the offset on that date.
Darrel Miller
A: 

my data is not been saved in the database with exception that iti is sqltime overflow

You are not making any sense to me.
hopla