AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

StringToDateTime macht murx

Ein Thema von Sergej_Molotov · begonnen am 4. Sep 2012 · letzter Beitrag vom 4. Sep 2012
Antwort Antwort
Sergej_Molotov

Registriert seit: 18. Jul 2006
61 Beiträge
 
Delphi 11 Alexandria
 
#1

AW: StringToDateTime macht murx

  Alt 4. Sep 2012, 14:49
Delphi-Quellcode:
function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
var
  Pos: Integer;
  NumberPos: Integer;
  BlankPos, LastBlankPos, OrigBlankPos: Integer;
  LDate, LTime: TDateTime;
  Stop: Boolean;
begin
  Result := True;
  Pos := 1;
  LTime := 0;

  // jump over all the non-numeric characters before the date data
  ScanToNumber(S, Pos);

  // date data scanned; searched for the time data
  if ScanDate(S, Pos, LDate) then
  begin
    // search for time data; search for the first number in the time data
    NumberPos := Pos;
    ScanToNumber(S, NumberPos);

    // the first number of the time data was found
    if NumberPos < Length(S) then
    begin
      // search between the end of date and the start of time for AM and PM
      // strings; if found, then ScanTime from this position where it is found
      BlankPos := Pos - 1;
      LastBlankPos := BlankPos;
      Stop := False;
      while (not Stop) and (BlankPos < NumberPos) do
      begin
        // blank was found; scan for AM/PM strings that may follow the blank
        if (BlankPos > 0) and (BlankPos < NumberPos) then
        begin
          Inc(BlankPos); // start after the blank
          OrigBlankPos := BlankPos; // keep BlankPos because ScanString modifies it
          Stop := ScanString(S, BlankPos, TimeAMString) or
                  ScanString(S, BlankPos, 'AM') or
                  ScanString(S, BlankPos, TimePMString) or
                  ScanString(S, BlankPos, 'PM');

          // ScanString jumps over the AM/PM string; if found, then it is needed
          // by ScanTime to correctly scan the time
          BlankPos := OrigBlankPos;
        end
        // no more blanks found; end the loop
        else
          Stop := True;

        // search of the next blank if no AM/PM string has been found
        if not Stop then
        begin
          LastBlankPos := BlankPos;
          BlankPos := PosEx(' ', S, LastBlankPos);
        end;
      end;

      // loop was forcely stopped; check if AM/PM has been found
      if Stop then
        // AM/PM has been found; check if it is before or after the time data
        if BlankPos > 0 then
          if BlankPos < NumberPos then // AM/PM is before the time number
            Pos := BlankPos
          else
            Pos := NumberPos // AM/PM is after the time number
        else
          Pos := NumberPos
      // the blank found is after the the first number in time data
      else
        Pos := NumberPos;

      // get the time data
      Result := ScanTime(S, Pos, LTime);

      // time data scanned with no errors
      if Result then
        if LDate >= 0 then
          Value := LDate + LTime // Hier wird 21.12.1993 + 30.12.1899 00:10:00 = 21.12.1993 00:09:59
        else
          Value := LDate - LTime
    end
    // no time data; return only date data
    else
      Value := LDate;
  end
  // could not scan date data; try to scan time data
  else
    Result := TryStrToTime(S, Value)
end;
Sieht der Source bei euch auch so aus???
Thomas
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:47 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz