2011-09-14 08:29:32 +00:00
|
|
|
unit jcontrolutils;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils;
|
|
|
|
|
|
|
|
function ReplaceChar(const s: string; ch1: char; ch2: char): string;
|
|
|
|
function CountChar(const s: string; ch: char): integer;
|
|
|
|
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
|
2011-09-15 08:57:15 +00:00
|
|
|
function NormalizeDate(const Value: string; theValue: TDateTime;
|
|
|
|
const theFormat: string = ''): string;
|
2011-09-14 08:29:32 +00:00
|
|
|
function NormalizeDateSeparator(const s: string): string;
|
2011-09-15 08:57:15 +00:00
|
|
|
function IsValidDateString(const Value: string): boolean;
|
2011-09-14 08:29:32 +00:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
function ReplaceChar(const s: string; ch1: char; ch2: char): string;
|
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
Result := s;
|
|
|
|
for i := 1 to length(Result) do
|
|
|
|
if Result[i] = ch1 then
|
|
|
|
Result[i] := ch2;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function CountChar(const s: string; ch: char): integer;
|
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
for i := 1 to length(s) do
|
|
|
|
if s[i] = ch then
|
|
|
|
Inc(Result);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
|
|
|
|
begin
|
|
|
|
Assert(Assigned(Strings));
|
|
|
|
Strings.Clear;
|
|
|
|
Strings.Delimiter := Delimiter;
|
|
|
|
Strings.DelimitedText := Input;
|
|
|
|
end;
|
|
|
|
|
2011-09-15 08:57:15 +00:00
|
|
|
function NormalizeDate(const Value: string; theValue: TDateTime;
|
|
|
|
const theFormat: string): string;
|
2011-09-14 08:29:32 +00:00
|
|
|
var
|
|
|
|
texto: string;
|
|
|
|
i: integer;
|
|
|
|
d, m, y: word;
|
2011-09-15 08:57:15 +00:00
|
|
|
ds, ms, ys: string;
|
|
|
|
aDate: TDateTime;
|
|
|
|
tokens: TStringList;
|
2011-09-15 10:17:01 +00:00
|
|
|
aDateFormat: string;
|
2011-09-14 08:29:32 +00:00
|
|
|
begin
|
2011-09-15 10:17:01 +00:00
|
|
|
if theFormat <> '' then
|
|
|
|
aDateFormat := ShortDateFormat
|
|
|
|
else
|
|
|
|
aDateFormat := theFormat;
|
2011-09-14 08:29:32 +00:00
|
|
|
if theValue = 0 then
|
|
|
|
DecodeDate(Now, y, m, d)
|
|
|
|
else
|
|
|
|
decodedate(theValue, y, m, d);
|
2011-09-15 08:57:15 +00:00
|
|
|
ds := IntToStr(d);
|
|
|
|
ms := IntToStr(m);
|
|
|
|
ys := IntToStr(y);
|
2011-09-14 08:29:32 +00:00
|
|
|
texto := Value;
|
2011-09-15 08:57:15 +00:00
|
|
|
texto := NormalizeDateSeparator(texto);
|
|
|
|
Result := texto; // default value
|
2011-09-14 08:29:32 +00:00
|
|
|
i := countchar(texto, DateSeparator);
|
2011-09-15 08:57:15 +00:00
|
|
|
tokens := TStringList.Create;
|
|
|
|
Split(DateSeparator, texto, tokens);
|
|
|
|
if tokens.Count > 0 then
|
|
|
|
begin
|
|
|
|
// for now only working date forma d/m/y
|
|
|
|
// TODO add logic to make it working with m/d/y and ISO format
|
|
|
|
// update jdbgridutils to work with this code
|
|
|
|
if tokens[0] <> '' then
|
|
|
|
ds := tokens[0];
|
|
|
|
if (tokens.Count > 1) and (tokens[1] <> '') then
|
|
|
|
ms := tokens[1];
|
|
|
|
if (tokens.Count > 2) and (tokens[2] <> '') then
|
|
|
|
ys := tokens[2];
|
|
|
|
texto := ds + DateSeparator + ms + DateSeparator + ys;
|
|
|
|
if IsValidDateString(texto) then
|
|
|
|
begin
|
2011-09-15 10:17:01 +00:00
|
|
|
aDate := StrToDate(texto);
|
|
|
|
Result := FormatDateTime(aDateFormat, aDate);
|
2011-09-15 08:57:15 +00:00
|
|
|
end;
|
2011-09-14 08:29:32 +00:00
|
|
|
end;
|
2011-09-15 08:57:15 +00:00
|
|
|
tokens.Free;
|
2011-09-14 08:29:32 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function NormalizeDateSeparator(const s: string): string;
|
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
Result := s;
|
|
|
|
for i := 1 to length(Result) do
|
|
|
|
if Result[i] in ['.', ',', '/', '-'] then // valid date separators
|
|
|
|
Result[i] := DateSeparator;
|
|
|
|
end;
|
|
|
|
|
2011-09-15 08:57:15 +00:00
|
|
|
function IsValidDateString(const Value: string): boolean;
|
|
|
|
begin
|
|
|
|
if StrToDateDef(Value, MaxDateTime) = MaxDateTime then
|
|
|
|
Result := False
|
|
|
|
else
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2011-09-14 08:29:32 +00:00
|
|
|
end.
|
|
|
|
|