Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Time Values and Time Zones


Date Time Values and Time Zones

Author
Message
Dinesh Asanka
Dinesh Asanka
SSC Eights!
SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)

Group: General Forum Members
Points: 914 Visits: 223
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/dasanka/datetimevaluesandtimezones.asp






My Blog: http://dineshasanka.spaces.live.com/
Chris Hedgate
Chris Hedgate
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1499 Visits: 7
Dinesh, please note that GMT and UTC is not exactly the same thing. As I discussed in my article SQL and Java go on a Date (http://www.sqlservercentral.com/columnists/chedgate/sqlandjavagoonadate.asp), UTC adds a "leap second" every year or two. Therefore it is not possible to retrieve the utc-time for a specific datetime using code like this:


DATEADD ( hh , (DATEDIFF ( hh , GetDate(), GetUTCDate() )) , @dt_Date_Time )


The reason is that the difference between getdate and getutcdate will be different depending on when you run it. Of course, if you want the difference in hours (as in your example) you might not notice it because it is only a second every year or two, but there will be a difference. And if you want the difference in milliseconds you would definately note this.

--
Chris Hedgate @ Extralives (http://www.extralives.com/)
Contributor to Best of SQL Server Central 2002 (http://www.sqlservercentral.com/bestof/)
Articles: http://www.sqlservercentral.com/columnists/chedgate/

--
Chris Hedgate http://www.hedgate.net/
Contributor to the Best of SQL Server Central volumes
Articles: http://www.sqlservercentral.com/columnists/chedgate/
Chris Hedgate
Chris Hedgate
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1499 Visits: 7
I forgot to mention one thing. The Oracle function SYS_EXTRACT_UTC that you mention is exactly what we would need in SQL Server to solve this problem easily. And from what I understand, this and other time functions will be included in next version of SQL Server.

--
Chris Hedgate @ Extralives (http://www.extralives.com/)
Contributor to Best of SQL Server Central 2002 (http://www.sqlservercentral.com/bestof/)
Articles: http://www.sqlservercentral.com/columnists/chedgate/

--
Chris Hedgate http://www.hedgate.net/
Contributor to the Best of SQL Server Central volumes
Articles: http://www.sqlservercentral.com/columnists/chedgate/
Dinesh Asanka
Dinesh Asanka
SSC Eights!
SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)SSC Eights! (914 reputation)

Group: General Forum Members
Points: 914 Visits: 223
Thankx for the information.I didn't notice this
quote:

Dinesh, please note that GMT and UTC is not exactly the same thing. As I discussed in my article SQL and Java go on a Date (http://www.sqlservercentral.com/columnists/chedgate/sqlandjavagoonadate.asp), UTC adds a "leap second" every year or two. Therefore it is not possible to retrieve the utc-time for a specific datetime using code like this:


DATEADD ( hh , (DATEDIFF ( hh , GetDate(), GetUTCDate() )) , @dt_Date_Time )


The reason is that the difference between getdate and getutcdate will be different depending on when you run it. Of course, if you want the difference in hours (as in your example) you might not notice it because it is only a second every year or two, but there will be a difference. And if you want the difference in milliseconds you would definately note this.

--
Chris Hedgate @ Extralives (http://www.extralives.com/)
Contributor to Best of SQL Server Central 2002 (http://www.sqlservercentral.com/bestof/)
Articles: http://www.sqlservercentral.com/columnists/chedgate/








My Blog: http://dineshasanka.spaces.live.com/
Antares686
Antares686
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: Moderators
Points: 8424 Visits: 780
Besides

DATEADD ( hh , (DATEDIFF ( hh , GetDate(), GetUTCDate() )) , @dt_Date_Time )

will only deal with the current time difference between the values. Consider that Time Zones in many places shift by an 1 hour back and forth for daylight savings and standard if say you run this on Jann 1 for a Jul 1 date you values will be off by an hour (espeically in the US where the change is 1st Sunday in April and Last Sunday in October). I have previously posted code to deal with transfer from GMT to local time for Eastern Time Zone in US to show the ease and complexity of the situation.



jwiner
jwiner
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 18
Has anyone found a way to deal with daylight savings time changes?

In the fall, there would be the possilbility of having duplicate times for events that happened an hour apart...and in the spring, there would be a missing hour.

Just wondering...



Chris Hedgate
Chris Hedgate
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1499 Visits: 7
I think the best solution would be to save all datetimes in utc-time. Then you could convert to whichever time zone / format you want for presenting it. Unfortunately though SQL Server does not have all the functions that is needed for this, but I am sure Antares has something up his sleeve.

--
Chris Hedgate @ Extralives (http://www.extralives.com/)
Contributor to Best of SQL Server Central 2002 (http://www.sqlservercentral.com/bestof/)
Articles: http://www.sqlservercentral.com/columnists/chedgate/

--
Chris Hedgate http://www.hedgate.net/
Contributor to the Best of SQL Server Central volumes
Articles: http://www.sqlservercentral.com/columnists/chedgate/
Ɖiamond ǤeezeƦ
Ɖiamond ǤeezeƦ
Old Hand
Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)

Group: General Forum Members
Points: 389 Visits: 57

I agree that the best solution is to store all datetimes in UTC time and convert to whichever time zone you want for presenting it. As SQL Server 2000 does not have all the necessary functions needed to do this, your developers will have to write additional code for formatting dates and you will also have to know each user's time zone. If developing in Microsoft.NET then the SimpleTimeZone solution developed by Michael Brumm (http://www.michaelbrumm.com/simpletimezone.html) may be of use.

In your example the two countries are in time zones whose offset is hours different to UTC. However, not all time zones are hours different. For example, India is UTC +4.5 hours. So it would be better to compare datetimes in minutes and not hours.

Keith



Keith
jcb
jcb
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2742 Visits: 969

4 tips:

1) Note datetime is a float and a hour is a diference by 4.1666666671517305E-2 u can use these values to make the quick calculations from -4.5 hour or +1 hour o 15 minutes! Less processing at functions is good for tunning.

2) Read a good article about diference in datetimes formats around the world http://www.sqlservercentral.com/columnists/dasanka/globilizationinsqlserver.asp

3) Store all data at the UTC (or UMC) is good. You can also store the one local datetime at another colummn or just the diference with a LOCAL_datetime-UTC_datetime.

4) U can make all at UTC and let the developers concern about show the correct local datetime and datetime format at the user's interface layer. Argue its cheaper to DB maintenace and good for globalization (yeah it´s right!).

Jean


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search