Add Keep origional date with XML UTC Format


Problem - I have an application that items needs to keep the original time of where it was entered.  This information is saved in XML and when you call XMLTimeToDateTime it auto adjusts the time.  For example 11:00am entered on eastern time is 9:00am mountain time.  Daylight savings also has the same issue when the time changes.

I didn't find any simple clean solutions online, so I am posting this for any others that have this problem.

Solution to adjust the time.  UTC format adds a Bias time on to the end of the time format.  So all you need to do is get the Bias for the time coming in and the Bias for the current computer and then adjust the time after calling XMLTimeToDateTime.

Code - Hopefully I didn't forget anything in stripping this out.

Uses
  XSBuiltIns, IdGlobalProtocols, DateUtils, Windows;

procedure TDateInput.SetDateXml(const Value: String);
var
  curAdj     : Integer;
  adjDiff    : Integer;
  XSDateTime : TXSDateTime;
  tz         : TTimeZoneInformation;

begin

  adjDiff := 0
  if value <> '' then
  begin
    XSDateTime := TXSDateTime.Create;
    try
      XSDateTime.XSToNative(Value);
      adjDiff := XSDateTime.HourOffset;

      case getTimeZoneInformation(tz) of
        TIME_ZONE_ID_STANDARD: curAdj := -(tz.StandardBias + tz.Bias) div 60;
        TIME_ZONE_ID_DAYLIGHT: curAdj := -(tz.DaylightBias + tz.Bias) div 60;
      else
        curAdj := -(tz.Bias div 60);
      end;
         
      adjDiff := adjDiff - curAdj;

    finally
      XSDateTime.Free;
    end;
  end;

 

  Self.fDate := XMLTimeToDateTime(value);
  fDate := IncHour(fDate, adjDiff);
end;

Related Discussions
  • RUNNING A DATABASE APPLICATION ON ANOTHER COMPUTER (2001-01-07 07:36:33)
    In the BDE on the other PC (the one without the database), you must add an alias to point to the location of the database in the format of:...
  • .BMP ISN'T A VALID PICTURE FILE FORMAT!? UHUM!? (2001-01-05 16:59:28)
    I think you have to Specify what line the text is in, like this. Image1.Picture.LoadFromFile(Memo1.Lines.Strings); If the text in the...
  • PRINTING A PDF FORM (2001-01-07 05:57:11)
    Hello Tom, If you find a delphi solution to bypass the acrobat Printer i am very interrested. Actually i am printing a lot of invoice to the...
  • ICONS (2001-01-10 09:09:37)
    This component is not working correctly. Colors look different when saved with this component.
  • ABOUT THE TIME...? (2001-01-10 20:05:50)
    Yes. See the thread about opening control panel applets. -- The Smurf
  • SENDING TEXT TO ANOTHER APPLICATION USING SENDMESSAGE (2001-01-10 07:34:10)
    Hi : Try this, it may be a better aproach, With the help of the API function GlobalAddAtom its possible to send a string via the Windows API....
  • REGISTRY BINARY DATA (2001-01-10 01:42:13)
    I took this from a newsgroup and was originally posted by Ralph from Team B. uses Registry; procedure...
  • SAVING WITHOUT USING SAVETOFILE() ? (2001-01-11 08:50:58)
    I know that one.. It's not working correctly unforutnatly. When I save a 256 color as 8bit it looks *very* different.. if I save it in 32bit...
  • INVALID VARIANT TYPE CONVERSION ON DBGRID WITH LOOKUP FIELDS (2001-01-11 06:07:45)
    there are two reason for this error first and less likely is you are converting something of one type to another type which is not compatible...
  • ANYONE KNOW HOW TO BRING UP CONTROL PANEL APPLETS??? (2001-01-11 08:05:57)
    Or ... ShellExecute(Application.Handle,'OPEN','CONTROL','NCPA.CPL',NiL,1); Where the network dialog executes. Regards /Filip
Latest News
Submit News Form Past News
Latest Forum Entries