Link to home
Create AccountLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

Working with formatDateTime - doubt

Hello guys

I am creating a xml that has a field that requires a format this way: '20111107T15:01:04-03:00'

The documentation tells:

A combination Date and Time. Note the presence of a “T” character between the date and time portions, and the use of colons to separate hours, minutes, and seconds.
  //  These are as per ISO 8601.Datetime Format: YYYYMMDDTHH:MM:SS[[+-]HH:MM]? (the first MM is Datetime Format: YYYYMMDDTHH:MM:SS[[+-]HH:MM]

for date and time it is ok for me. But I didn't understand this part [[+-]HH:MM]

what is it?

thanks
Avatar of Armand G
Armand G
Flag of New Zealand image

That is known to be the Time offset from the UTC(Coordinated Universal Time).

For example:

The offset from UTC changes with daylight saving time, e.g. a time offset in Chicago, would be "-06:00" for the winter (Central Standard Time) and "-05:00" for the summer (Central Daylight Time).

Avatar of hidrau

ASKER

ok, I got it.

how can I assemble that formating?
You can assemble formatting like the example you already have on your code:
'20111107T15:01:04-03:00' where -03:00 is the UTC (Greenland, Uruguay, Suriname countries)

Then if you need to change to another country, you can take a look on this chart:

http://en.wikipedia.org/wiki/List_of_time_zones_by_country
Avatar of hidrau

ASKER

hummmm

this way:

  EDIT1.Text := FormatDateTime('YYYYMMDD', NOW) + 'T' + FormatDateTime('HH:MM:SS', NOW) + FormatDateTime('[[+-]HH:MM]',now)

the result is not what I expect

take a look at: 20111108T17:20:21[[+-]17:20]

if you could give me an example
The FormatDateTime can't get the timezone instead use the GetTimeZoneInformation():

procedure TForm1.Button1Click(Sender: TObject);
var
  MyTime : TDateTime;
  UTC: TSystemTime;
  TimeZoneInfo:TIME_ZONE_INFORMATION;
  bias : Double;
begin
  GetTimeZoneInformation(TimeZoneInfo);
  bias := TimeZoneInfo.bias/60;
  with Memo1.Lines do
  begin
    Add(FloatToStr(bias));
  end;
end;
Avatar of hidrau

ASKER

armchang:

ok, I had a value 3 as result.

But how can I reach this? '20111107T15:01:04-03:00'
Tried and tested, working but I need to post the unit file as there are needed functions:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    function rightString(ctext : string; nlen: integer) : string ;
    function RTrim(ctext: string) : string;
    function LTrim(ctext: string) : string;
    function PadL(ctext : string; nlen:integer ; cchar : char) : string ;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.PadL(ctext : string; nlen:integer ; cchar : char) : string ;
  var
    creturn : string ;
    ntemp : integer ;
    nmax  : integer ;
  begin
    creturn := LTrim(RTrim(ctext));
    nmax    := nlen ;
    dec(nmax,length(creturn)) ;
    if nmax > 0 then
      begin
        for ntemp := 1 to nmax do
          creturn := concat(cchar,creturn) ;
        result := creturn ;
      end
    else
      result := rightString(creturn,nlen) ;
  end;

  function TForm1.rightString(ctext : string; nlen: integer) : string ;
  var
    ntemp : word ;
  begin
    result := ctext ;
    if (length(ctext) > nlen)  then
      begin
        ntemp := (length(ctext)-nlen)+1 ;
        result := copy(ctext,ntemp,nlen) ;
      end;
  end;

  { Right Trim }
function TForm1.RTrim(ctext: string) : string;
  var
    nloop : integer ;
  begin
    if (length(ctext) > 0) then
      begin
        for nloop := length(ctext) downto 1 do begin
          if (not (ord(ctext[nloop]) in [0,1,2,32])) then
            begin
              ctext := copy(ctext,1,nloop) ;
              break ;
            end;
        end;
      end;
    result := ctext ;
  end;

  function TForm1.LTrim(ctext: string) : string;
  var
    nloop : integer ;
    ctemp : string ;
  begin
    ctemp  := '' ;
    if (length(ctext) > 0) then
      begin
        for nloop := 1 to length(ctext) do begin
          if (ord(ctext[nloop]) <> 32) then
            begin
              ctemp := ctemp + copy(ctext,nloop,length(ctext)) ;
              break ;
            end;
        end;
      end;
    result := ctemp ;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyTime : TDateTime;
  UTC: TSystemTime;
  TimeZoneInfo:TIME_ZONE_INFORMATION;
  bias : Double;
  sFinal : string;
begin
  Edit1.Text := FormatDateTime('YYYYMMDD', NOW) + 'T' + FormatDateTime('HH:MM:SS', NOW);
  GetTimeZoneInformation(TimeZoneInfo);
  if (TimeZoneInfo.Bias > 0) then
        sFinal := '+' + PadL(IntToStr(TimeZoneInfo.bias div 60), 2, '0') + ':' + PadL(IntToStr(Abs(TimeZoneInfo.bias mod 60)), 2,'0')
  else
        sFinal := '-' + PadL(IntToStr(Abs(TimeZoneInfo.bias div 60)), 2, '0') + ':' + PadL(IntToStr(Abs(TimeZoneInfo.bias mod 60)), 2, '0');

  Edit1.Text := Edit1.Text + sFinal;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Armand G
Armand G
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of hidrau

ASKER

thanks very much friend