SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 UTC datetime conversion
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jeffnc
Starting Member

14 Posts

Posted - 11/03/2008 :  11:43:45  Show Profile  Reply with Quote
I'm writing script to convert datetimes in a database from local time to UTC time. I'm using the following formula to convert the time.

DATEADD(minute, DATEDIFF(minute, GETDATE(), GETUTCDATE()), {datetimevalue})

where {datetimevalue} is the datetime I'm converting. DATEDIFF gets the difference between local time and UTC time in minutes. It then adds those minutes (could be a negative number) to the datetime value.

This is almost correct, however it doesn't account for Daylight Savings Time. For example, let's say we are not currently in DST. I happen to be in EST, so the DATEDIFF function above returns 300, which is correct.

The problem is that a datetime such as 11/15/2008 12:00 gets converted correctly to 5:00. However a datetime such as 10/15/2008 12:00 gets converted to 5:00 as well, which is incorrect. 10/15 was in DST, so it should be converted to 4:00.

Anyone know how to handle this?

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/03/2008 :  11:50:15  Show Profile  Reply with Quote
use CASE... WHEN and decrease one more hour when MONTH(datetimevalue) BETWEEN DSTstarttime AND DSTEndTime. so it will be

DATEADD(minute, CASE WHEN MONTH(datetimevalue) BETWEEN DSTStartmonth AND DSTEndmonth THEN DATEDIFF(minute, GETDATE(), GETUTCDATE())-1 ELSE DATEDIFF(minute, GETDATE(), GETUTCDATE()) END, {datetimevalue})
Go to Top of Page

jeffnc
Starting Member

14 Posts

Posted - 11/03/2008 :  12:01:59  Show Profile  Reply with Quote
You lost me. Where did DSTstarttime, etc come from?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/03/2008 :  12:06:06  Show Profile  Reply with Quote
you need to replace it with your start month and end month value of DST
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 11/03/2008 :  12:11:32  Show Profile  Reply with Quote
The best way to handle it is to have a table that has the DST start datetime and end datetime for each year., and the offset to UTC for DST and non-DST. Then you just do a lookup for that year, and get the correct UTC offset, depending on if it is DST or not.
select
	UTC_OFFSET =
		case
		when 	a.MyDate >= b.DST_Start and
			a.MyDate <  b.DST_End
		then 	b.DST_Offset
		else 	b.Non_DST_Offset
		end
from
	MyTable a
	join
	DST b
	on	a.MyDate >= b.DST_Year_Start and
		b.MyDate <  b.DST_NextYear_Start







CODO ERGO SUM
Go to Top of Page

jeffnc
Starting Member

14 Posts

Posted - 11/03/2008 :  12:22:21  Show Profile  Reply with Quote
That might work if you're in a single time zone, but it doesn't work in general.

The impression I'm getting is there's nothing built in to SQL (as there is in .NET) that will convert this for you. That's really a shame. Creating tables for DSTs for time zones all over the world would be a real hassle. It's not even clear to me how I'd know which table to use at runtime.
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 11/03/2008 :  12:33:54  Show Profile  Reply with Quote
This is a display issue that belongs in the presentation layer of your application, not in the database, that's why there's no built-in function to handle this.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 11/03/2008 :  12:35:14  Show Profile  Reply with Quote
You didn't mention that as a requirement in your first post, and the way you were trying to handle it would certainly not work for multiple time zones.

There is no reason why a time zone table couldn’t contain multiple time zones.

If what you want is available in .NET, there is no reason that you couldn't write a CLR function to handle this.






CODO ERGO SUM
Go to Top of Page

jeffnc
Starting Member

14 Posts

Posted - 11/03/2008 :  12:37:35  Show Profile  Reply with Quote
quote:
Originally posted by hanbingl

This is a display issue that belongs in the presentation layer of your application, not in the database, that's why there's no built-in function to handle this.



It's not a display issue, it's a database issue. Dates in the database need to be converted to UTC time regardless of any UI concerns, so that certainly can't be the reason there's no built-in function.
Go to Top of Page

jeffnc
Starting Member

14 Posts

Posted - 11/03/2008 :  12:42:28  Show Profile  Reply with Quote
quote:
If what you want is available in .NET, there is no reason that you couldn't write a CLR function to handle this.


That will have to be the way to go then. First I've tried a CLR function. Any pointers to sites with helpful information on this appreciated. I'm trying to figure out the MSoft documentation on CLR functions and I'm not getting far yet :-)
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 11/03/2008 :  13:09:04  Show Profile  Reply with Quote
quote:
Originally posted by jeffnc

quote:
Originally posted by hanbingl

This is a display issue that belongs in the presentation layer of your application, not in the database, that's why there's no built-in function to handle this.



It's not a display issue, it's a database issue. Dates in the database need to be converted to UTC time regardless of any UI concerns, so that certainly can't be the reason there's no built-in function.



If you do not pass the timezone offset info how can SQL server know which offset to use?? Basically SQL server can not tell 15:00:00 NY time and 15:00:00 LA time. So it is the presentation lay's job to pass the correct UTC time info.
Go to Top of Page

sandeepkonda
Starting Member

1 Posts

Posted - 07/26/2013 :  07:17:05  Show Profile  Reply with Quote
quote:
Originally posted by Michael Valentine Jones

The best way to handle it is to have a table that has the DST start datetime and end datetime for each year., and the offset to UTC for DST and non-DST. Then you just do a lookup for that year, and get the correct UTC offset, depending on if it is DST or not.
select
	UTC_OFFSET =
		case
		when 	a.MyDate >= b.DST_Start and
			a.MyDate <  b.DST_End
		then 	b.DST_Offset
		else 	b.Non_DST_Offset
		end
from
	MyTable a
	join
	DST b
	on	a.MyDate >= b.DST_Year_Start and
		b.MyDate <  b.DST_NextYear_Start







CODO ERGO SUM




Can you please help me with the structure of this DST table u mentioned. I have same requirement but I am not getting correct results while implementing.
Thanks in advance.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000