TTimeZone in Delphi XE

by Radek Červinka 11. October 2010 01:13

Delphi XE include in RTL support for working with local time zone (TTimeZone). Implementation is multi platform (Windows, Linux, Mac OS) and is simple for use.

    1TTimeZone = class abstract
    2  private
    3    class var FLocal: TTimeZone;
    4    5  public
    6    function GetUtcOffset(const ADateTime: TDateTime; 
    7          const ForceDaylight: Boolean = False): TTimeSpan; inline;
    8    function ToUniversalTime(const ADateTime: TDateTime; 
    9          const ForceDaylight: Boolean = False): TDateTime; inline;
   10    function ToLocalTime(const ADateTime: TDateTime): TDateTime;
   11    function GetDisplayName(const ADateTime: TDateTime; 
   12          const ForceDaylight: Boolean = False): string;
   13    function GetAbbreviation(const ADateTime: TDateTime; 
   14          const ForceDaylight: Boolean = False): string;
   15    function GetLocalTimeType(const ADateTime: TDateTime): TLocalTimeType; inline;
   16    function IsStandardTime(const ADateTime: TDateTime; 
   17          const ForceDaylight: Boolean = False): Boolean;
   18    function IsInvalidTime(const ADateTime: TDateTime): Boolean; inline;
   19    function IsAmbiguousTime(const ADateTime: TDateTime): Boolean; inline;
   20    function IsDaylightTime(const ADateTime: TDateTime; 
   21          const ForceDaylight: Boolean = False): Boolean;
   22    property ID: string read DoGetID;
   23    property DisplayName: string read GetDisplayNameForNow;
   24    property Abbreviation: string read GetAbbreviationForNow;
   25    property UtcOffset: TTimeSpan read GetCurrentUtcOffset;
   26    class property Local: TTimeZone read FLocal;
   27  end;

For obtain local time zone write TTimeZone.Local, see demo.

    1program Project1;
    6  SysUtils, TypInfo,
    7  DateUtils;
    9  with TTimeZone.Local do
   10  begin
   11    writeln('Now:', DatetimeToStr(Now));
   12    writeln('UtcOffset (h):', GetUtcOffset(Now).Hours);
   13    writeln('Now ToUniversalTime :', DatetimeToStr(ToUniversalTime(Now)));
   14    // zpatky ToLocalTime
   15    writeln('GetDisplayName:', GetDisplayName(Now));
   16    writeln(GetDisplayName(EncodeDate(2010, 12, 1)));
   17    writeln('GetAbbreviation:', GetAbbreviation(Now));
   18    writeln('Actual time is (summer / standard): ',
   19      GetEnumName(TypeInfo(TLocalTimeType), Ord(GetLocalTimeType(Now))));
   20    //lttStandard, lttDaylight, lttInvalid, lttAmbiguous
   21    writeln('IsStandardTime:', IsStandardTime(Now));
   22    writeln('IsDaylightTime:', IsDaylightTime(Now));
   23    writeln('TimeZoneID:', ID);
   24    writeln('DisplayName:', DisplayName);
   25  end;

And output is :

Now:11.10.2010 0:07:28
UtcOffset (h):2
Now ToUniversalTime :10.10.2010 22:07:28
GetDisplayName:Střední Evropa (letní čas)
Střední Evropa (běžný čas)
Actual time is (summer / standard: lttDaylight
TimeZoneID:Střední Evropa (běžný čas)
DisplayName:Střední Evropa (letní čas)

Maybe only code where can be problem is part with GetLocalTimeType, where use RTTI for display of name of enumerate item. This type determine time type - Daylight(lttDaylight) or Standard (lttStandard), and lttAmbiguous for time between and lttInvalid.

Tags: ,


Comments are closed