zoukankan      html  css  js  c++  java
  • ISO 8601: Delphi way to convert XML date and time to TDateTime and back (via: Stack Overflow)

    Recently I needed a way of concerting back and forth ISO 8601 DateTime values used in XML from Delphi.

    Thoug the Delphi DateUtils unit has some ISO 8601 features for calculating week numbers, you actually need to the XSBuiltIns unit for converting back and forth to ISO 8601 text representation of a DateTime.

    I remembered answering the related In Delphi is there a function to convert XML date and time to TDateTime question on StackOverflow a while ago (and forgot that this was back in 2009 <g>).

    ISO 8601 dates can either include a time-zone, or be in UTC, which is not part of that answer. So lets elaborate on that answer a bit now:

    UTC times in ISO 8601 format end in a Z time zone designator like this:

        <Created>2011-06-29T17:01:45.505Z</Created>

    The Z UTC indicator is basically a shortcut for a timezone offset of +00:00 or -00:00, effectively being a zero (or zulu) timezone.

    Nonzero timezones start with an optional + or -, followed by the hours and minutes offset from UTC, for instance +01:00 for the Central European Time zone.

        <Created>2011-06-29T17:01:45.505+01:00</Created>

    When you want historical time zones, then you need the Delphi TZDB interface to the historical TZ database.

    To do the timezone calculations, I used the TimeZoneBias function from Indy, which is either in the IdGlobal unit (Indy <= version 9) or the IdGlobalProtocols unit (Indy 10 and up).

    Conversion is done by using the TXSDateTime (that like all the XS conversion classes descends from TRemotableXS in the InvokeRegistry unit).

    Most of the classes descending from TRemotableXS contain two methods: NativeToXS and XSToNative doing the underlying conversions.

    Since I didn’t need the historical reference in the TZDB, this is the code that I came up with:

    unit Iso8601Unit;
    
    interface
    
    type
      TIso8601 = class(TObject)
      public
        class function DateTimeFromIso8601(const Value: string): TDateTime; static;
        class function UtcDateTimeToIso8601(const Value: TDateTime): string; static;
        class function DateTimeToIso8601(const Value: TDateTime): string; static;
        class function UtcNow: TDateTime; static;
        class function ToUtc(const Value: TDateTime): TDateTime; static;
        class function FromUtc(const Value: TDateTime): TDateTime; static;
      end;
    
    implementation
    
    uses
      IdGlobalProtocols, {IdGlobal for Index   SysUtils,
      XSBuiltIns;
    
    class function TIso8601.DateTimeFromIso8601(const Value: string): TDateTime;
    begin
      with TXSDateTime.Create() do
      try
        XSToNative(value); // convert from WideString
        Result := AsDateTime; // convert to TDateTime  finally
      finally
        Free();
      end;
    end;
    
    class function TIso8601.UtcDateTimeToIso8601(const Value: TDateTime): string;
    begin
      with TXSDateTime.Create() do
      try
        AsUTCDateTime := Value;
        Result := NativeToXS; // convert to WideString
      finally
        Free();
      end;
    end;
    
    class function TIso8601.DateTimeToIso8601(const Value: TDateTime): string;
    begin
      with TXSDateTime.Create() do
      try
        AsDateTime := Value; // convert from TDateTime
        Result := NativeToXS; // convert to WideString
      finally
        Free();
      end;
    end;
    
    class function TIso8601.UtcNow: TDateTime;
    begin
      Result := ToUtc(Now);
    end;
    
    class function TIso8601.ToUtc(const Value: TDateTime): TDateTime;
    var
      Bias: TDateTime;
    begin
      Bias := TimeZoneBias;
      Result := Value + TimeZoneBias;
    end;
    
    class function TIso8601.FromUtc(const Value: TDateTime): TDateTime;
    var
      Bias: TDateTime;
    begin
      Bias := TimeZoneBias;
      Result := Value - TimeZoneBias;
    end;
    
    end.

    –jeroen

    via In Delphi is there a function to convert XML date and time to TDateTime – Stack Overflow.

  • 相关阅读:
    快考试了
    16号了
    终于找到网吧了,写写今天
    又打了一天的篮球
    (转载)Andoid2.X各字段意义
    (转载)AndroidMatrixCursor
    (转载)非常好 必须要顶
    (转载)Android Cursor之MergeCursor
    七天开发安卓软件(二)
    “Visual Studio.net已检测到指定的Web服务器运行的不是Asp.net1.1版。您将无法运行Asp.net Web应用程序或服务”问题的解决方法
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/3352608.html
Copyright © 2011-2022 走看看