Delphi XE: TTimeZone is not threadsafe

This is only a very short post, but perhaps it can help some people out there :) When you want to use the abstract class TTimeZone respectively its derived class TLocalTimeZone then be aware of that it is NOT threadsafe – the two mentioned classes are defined in the unit DateUtils since Delphi XE and are really helpful.

Why I stumbled over this problem? At the moment I write a log component which feeds some modules that write each log message to a file, a database or some other target. Each log message has its own timestamp which tells the modules when it was created. One module just writes the messages to a text file – here an exmaple line:

2011-03-14T16:06:58.769+01:00 Notice This is just an example.

You can see that the time is in UTC, so thats the place where I’m using the TTimeZone class. First I have not used a threadsafe call of TTimeZone methods. So after writing a test for my component, where I used three such text-file-modules, there was sometimes a EDivByZero exception. I don’t know exactly where this exception occurs but I know that it comes from the TTimeZone class because I commented the corresponding code and then it worked correctly ;) With only one text-file-module it worked too, so it had to be problem with the two new threads. After making the code threadsafe this problem does not exist and more. So, be careful ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.