On 27 April 2010 17:43, Frank Church <vfcli...@googlemail.com> wrote: > I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a > TDatetime in FPC but can't find but haven't found a way yet.
I have written the following for the tiOPF project and us it in our company projects for all date/time values (including storage in database fields). See attached file for the relevant functions. The code has been some unit tests as well, and can be found in the tiOPF testing framework. tiOPF is hosted on SourceForge. NOTE: I do not manage weeks or timezone information in the ISO 8601 standard. Some constants I forgot to add to the txt file. { Summary of ISO 8601 http://www.cl.cam.ac.uk/~mgk25/iso-time.html } cIntlDateTimeStor = 'yyyymmdd"T"hhmmss'; // for storage cIntlDateTimeDisp = 'yyyy-mm-dd hh:mm:ss'; // for display CIntlDateDisp = 'yyyy-mm-dd'; // for display -- Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/
// Seaches <sStr> and replaces <sDel> with <sIns> // Case sensitive. function tiStrTran(AValue, ADel, AIns : string): string; var i : integer; sToChange : string; begin result := ''; sToChange := AValue; i := pos(ADel, sToChange); while i <> 0 do begin result := result + copy(sToChange, 1, i-1) + AIns; delete(sToChange, 1, i+length(ADel)-1); i := pos(ADel, sToChange); end; result := result + sToChange; end; function tiDateTimeAsIntlDateStor(const ADateTime: TDateTime): string; begin Result := FormatDateTime(cIntlDateTimeStor, ADateTime); if Pos('18991230', Result) = 1 then Result := tiStrTran(Result, '18991230', '00000000'); end; function tiDateTimeAsIntlDateDisp(const ADateTime: TDateTime): string; begin Result := FormatDateTime(cIntlDateTimeDisp, ADateTime); if Pos('1899-12-30', Result) = 1 then Result := tiStrTran(Result, '1899-12-30', '0000-00-00'); end; function tiDateAsIntlDateDisp(const ADateTime: TDateTime): string; begin Result := FormatDateTime(CIntlDateDisp, ADateTime); if Pos('1899-12-30', Result) = 1 then Result := tiStrTran(Result, '1899-12-30', '0000-00-00'); end; function tiIntlDateStorAsDateTime(const AValue: string): TDateTime; var lY, lM, lD, lH, lMi, lS: Word; begin if Trim(AValue) = '' then begin Result := 0; Exit; //==> end; // 1 2 // 12345678901234567890123 // yyyymmddThhmmss lY := StrToInt(Copy(AValue, 1, 4)); lM := StrToInt(Copy(AValue, 5, 2)); lD := StrToInt(Copy(AValue, 7, 2)); lH := StrToInt(Copy(AValue, 10, 2)); lMi := StrToInt(Copy(AValue, 12, 2)); lS := StrToInt(Copy(AValue, 14, 2)); { Cannot EncodeDate if any part equals 0. EncodeTime is okay. } if (lY = 0) or (lM = 0) or (lD = 0) then Result := EncodeTime(lH, lMi, lS, 0) else Result := EncodeDate(lY, lM, lD) + EncodeTime(lH, lMi, lS, 0); end; function tiIntlDateDispAsDateTime(const AValue: string): TDateTime; var lY, lM, lD, lH, lMi, lS: Word; begin if Trim(AValue) = '' then begin Result := 0; Exit; //==> end; // 1 2 // 12345678901234567890123 // yyyy-mm-dd hh:mm:ss lY := StrToInt(Copy(AValue, 1, 4)); lM := StrToInt(Copy(AValue, 6, 2)); lD := StrToInt(Copy(AValue, 9, 2)); lH := StrToInt(Copy(AValue, 12, 2)); lMi := StrToInt(Copy(AValue, 15, 2)); lS := StrToInt(Copy(AValue, 18, 2)); { Cannot EncodeDate if any part equals 0. EncodeTime is okay. } if (lY = 0) or (lM = 0) or (lD = 0) then Result := EncodeTime(lH, lMi, lS, 0) else Result := EncodeDate(lY, lM, lD) + EncodeTime(lH, lMi, lS, 0); end;
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal