4

I want to implement time zone conversion within SQL Server 2012. However, TimeZoneInfo is marked with the MayLeakOnAbort attribute. This causes a runtime error when the SQL function I defined (which uses TimeZoneInfo) is invoked.

The error is reported as follows

System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host.

The protected resources (only available with full trust) were: All

The demanded resources were: MayLeakOnAbort

Documentation hints that I can use "SafeHandle" to get around this leakage issue but I do not see how.

So, how does one use the class TimeZoneInfo in a SQLCLR context?

Follow Up:

I have found another question here on the site which, though it deals with SQL 2005, prescribes an action which works with 2012. There are aspects of this solution, though, which are unsatisfying.

3
  • What are you doing that SWITCHOFFSET() will not handle? msdn.microsoft.com/en-us/library/bb677244.aspx
    – Anon
    Jul 10, 2013 at 22:18
  • 4
    SWITCHOFFSET() requires that the author know all the time zone rules for DST transitions for all possible zones. TimeZoneInfo handles that automatically.
    – Tevya
    Jul 10, 2013 at 22:43
  • Are you trying to convert a datetimeoffset from one timezone to another timezone, or are you trying to determine the local datetime given a specific datetimeoffset and a location? These are two very different tasks.
    – Anon
    Jul 10, 2013 at 22:54

2 Answers 2

6

Updated Answer

I've written the utility I spoke about in the original answer, which you can find here.

Also, as of SQL Server 2016 (and Azure SQL Database), you can now use the AT TIME ZONE keyword to convert between time zones.


Original Answer

Unfortunately, there is no great solution for working with time zones in SQL Server.

I investigated heavily the issue you linked, along with this one also. There are no built-in time zone functions, and any use of TimeZoneInfo in SQLCLR requires the assembly to be registered as "unsafe". This is usually not desired.

I also investigated using Noda Time from SQLCLR. You can read about it in this issue. It also had to be registered as "unsafe" because of the way certain items are cached internally.

Ultimately, with either item, the problem is that there is no way to cache anything in SQLCLR. You can't use static variables in a thread-safe manner, and you cant do any thread synchronization or use classes like ConcurrentDictionary. SQL wants to have full control of the threading model of the assembly. Only single-threaded use-once-and-throw-away style code works in "safe" assemblies. I dug deep into this in this question: Multithreaded caching in SQL CLR

Hopefully, there will eventually be a build of Noda Time that will work in SQLCLR, but it will be a special build that doesn't do any caching. So it won't perform as quickly, but it will get the job done while still being safe.

TimeZoneInfo isn't likely to change. So unless the SQL Server team ever brings time zone functions directly into SQL Server properly (like Oracle and Postgres do), then you've only got a few options:

  • Don't attempt time zone conversions in the data layer. Work with datetime or datetime2 values in UTC, or use datetimeoffset values with any offset. But do all conversions between time zones in the application layer. This is my best recommendation for now.

  • Copy all of the data for the time zones into actual SQL tables, and write functions that work with that data. This isn't the best idea, because the data changes often so table maintenance can be a challenge. Also getting the functions accurate, including all of the rules of daylight saving changes, can be challenging. I am not aware of any project that has this bundled up nice and neat, but if anyone is - then please let me know in comments.

  • Enable xp_regread and work with the timezone data directly from the windows registry keys. Updates would be done for you, but you still have the same challenges in writing these functions. And enabling registry reads might be just as much of a security risk as enabling unsafe CLR assemblies anyway.

Another idea I am considering is to write an IANA/Olson TZDB parser and functions specifically for SQL Server. This would be similar to option 2 above, but done in a maintainable way, and with IANA standard data instead of Windows time zones. Maybe I'll get to this someday, or maybe someone will beat me to it. Again, I am not aware of any current project that does this, but if someone knows of one, please let me know in comments. (done - see update at top)

Regarding SWITCHOFFSET - that only works when you already know the target offset. That's half the battle, and probably why Microsoft still marks datetimeoffset as not "daylight savings aware" in the docs.

5
  • If your question can boil down to: "What does the clock on the wall next to the database server say", then you have a design problem. SQL Server has no problem converting from UTC to EDT or EST or any other known, static offset. Where you run into problems is asking SQL to convert from UTC to Local Prevailing Time. Do you mean where the server is? Do you mean where the client is?
    – Anon
    Jul 11, 2013 at 16:15
  • 1
    @Anon - Most of the time, when working in named time zones, the requirement isn't specified at a fixed offset like EST or EDT, but rather just "Eastern Time", or America/New_York. It's then expected that the system knows when the daylight saving transition points are and will use the correct offset as necessary. The only design issue I see here is that this is most often done in application logic or in the UI layer, rather than in the database. Jul 11, 2013 at 16:21
  • Thanks for the input. I did write a SQLCLR class to do time zone conversions which was able to cache a critical TimeZoneInfo object just fine. As for using xp_regread, My observation of the registry contents for time zone information showed that data there was being held in some binary format. Is there a published spec for reading that directly? If so, I haven't come across it.
    – Tevya
    Jul 27, 2013 at 0:48
  • It's not really meant for direct reading, no. I'm sure the spec is somewhere though. FYI - I have resumed the work on getting Noda Time into SQLCLR. I have it working in a "safe" assembly. I'm just finishing up some stuff with getting UDTs for Noda Time types now. It will be published at some point. Jul 27, 2013 at 1:03
  • 1
    If you were able to get TimeZoneInfo working at all in SQLCLR with it running as a "safe" assembly, I'd love to see what you did. Can you post it to github or similar? Jul 27, 2013 at 1:05
1

I know the question was how to use TimeZoneInfo in a SQLCLR context, but thought I'd just add that while TimeZoneInfo is not supported (as @MattJohnson wrote elegantly), TimeZone is still available for use without the need to make the assembly unsafe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.