Saturday, July 23, 2011

GETUTCDATE vs. GETDATE with timezones and daylight savings


In a recent project, we had to deal with SQL Server transactions that involved multiple timezones and required us to calculate the time employee’s worked across a 24×7 retail chain.  Every time someone clocked-in or clocked-out we recorded the current time so we could later calculate the hours worked.  The problem was that the original developers had used the “DATETIME” field but had placed the default to be “GETDATE()” which uses the current time of the server.  Therefore,  in order to accurately calculate the time difference two times requires complicated daylight savings time lookup tables and more effort in the User Interface.  Here is how we solved it.
We were able to find an easy way to solve this problem.  We converted all entries to use “GETUTCDATE()” which is the current UTC time which does not vary by time of year or timezone.  Therefore, all the time was based off the same timezone and it never changed for daylight savings.  When computing people’s paycheck and summing the hours worked we were able to perform simple calculations and could guarantee the correct number of hours no matter where they clocked-in or clocked-out or whether they worked during a daylight savings time change.
Using GETUTCDATE instead of GETDATE was a simple solution to a complicated problem to solve timezones and daylight savings changes.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Deep's | Bloggerized by Deep - Deep's Templates | ElearSQL-Server