Use new NormalizeDate code in jinpututils, add missed info header

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1952 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
jujibo
2011-09-15 11:08:34 +00:00
parent 41bfecf680
commit 2ca3b8e514
6 changed files with 160 additions and 65 deletions

View File

@ -1,3 +1,20 @@
{ jcontrolutils
Copyright (C) 2011 Julio Jiménez Borreguero
Contact: jujibo at gmail dot com
This library is free software; you can redistribute it and/or modify it
under the same terms as the Lazarus Component Library (LCL)
See the file license-jujiboutils.txt and COPYING.LGPL, included in this distribution,
for details about the license.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit jcontrolutils;
{$mode objfpc}{$H+}

View File

@ -0,0 +1,133 @@
{ jcontrolutils
Copyright (C) 2011 Julio Jiménez Borreguero
Contact: jujibo at gmail dot com
This library is free software; you can redistribute it and/or modify it
under the same terms as the Lazarus Component Library (LCL)
See the file license-jujiboutils.txt and COPYING.LGPL, included in this distribution,
for details about the license.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
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);
function NormalizeDate(const Value: string; theValue: TDateTime;
const theFormat: string = ''): string;
function NormalizeDateSeparator(const s: string): string;
function IsValidDateString(const Value: string): boolean;
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;
function NormalizeDate(const Value: string; theValue: TDateTime;
const theFormat: string): string;
var
texto: string;
i: integer;
d, m, y: word;
ds, ms, ys: string;
aDate: TDateTime;
tokens: TStringList;
aDateFormat: string;
begin
if theFormat <> '' then
aDateFormat := ShortDateFormat
else
aDateFormat := theFormat;
if theValue = 0 then
DecodeDate(Now, y, m, d)
else
decodedate(theValue, y, m, d);
ds := IntToStr(d);
ms := IntToStr(m);
ys := IntToStr(y);
texto := Value;
texto := NormalizeDateSeparator(texto);
Result := texto; // default value
i := countchar(texto, DateSeparator);
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
aDate := StrToDate(texto);
Result := FormatDateTime(aDateFormat, aDate);
end;
end;
tokens.Free;
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;
function IsValidDateString(const Value: string): boolean;
begin
if StrToDateDef(Value, MaxDateTime) = MaxDateTime then
Result := False
else
Result := True;
end;
end.

View File

@ -22,7 +22,7 @@ unit jinpututils;
interface
uses
Classes, StdCtrls, SysUtils;
Classes, StdCtrls, SysUtils, jcontrolutils;
type
{ TJCurrencyCtrl }
@ -89,7 +89,6 @@ type
procedure formatInput;
procedure setFormat(const AValue: string);
procedure OnKeyPress(Sender: TObject; var key : char);
function IsValidDate(const Value: string): Boolean;
procedure setValue(const AValue: TDateTime);
public
function isNull : Boolean;
@ -98,10 +97,6 @@ type
constructor Create(widget : TCustomEdit);
end;
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);
implementation
uses
@ -305,10 +300,11 @@ end;
procedure TJDateCtrl.myEditExit(Sender: TObject);
begin
myEdit.Caption:= NormalizeDate(myEdit.Caption, theValue);
if Length(myEdit.Caption) = 0 then
theValue := 0
else
if IsValidDate(myEdit.Caption) then
if IsValidDateString(myEdit.Caption) then
theValue:= StrToDate(myEdit.Caption)
else
begin
@ -335,32 +331,6 @@ begin
if not (Key in ['0'..'9',#8,#9, '.', '-', '/']) then Key := #0;
end;
function TJDateCtrl.IsValidDate(const Value: string): Boolean;
var
texto : string;
i : integer;
d, m, y : word;
begin
Result := false;
// normalize date
texto:= myEdit.Caption;
texto := replacechar(texto, '.', DateSeparator);
texto := replacechar(texto, '-', DateSeparator);
texto := replacechar(texto, '/', DateSeparator);
i := countchar(texto, DateSeparator);
decodedate(theValue, y, m, d);
case i of
1 : texto := texto + DateSeparator + inttostr(y);
0 : texto := texto + DateSeparator + inttostr(m) + DateSeparator + inttostr(y);
end;
myEdit.Caption:= texto;
// comprobar que la fecha es valida
if StrToDateDef(texto, MaxDateTime) = MaxDateTime then
Result := False
else
Result := True;
end;
procedure TJDateCtrl.setValue(const AValue: TDateTime);
begin
theValue:= AValue;
@ -383,33 +353,6 @@ begin
format:= 'dd/mm/yyyy';
end;
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;
end.

View File

@ -1,5 +1,3 @@
{ This is an automatically generated lazarus resource file }
LazarusResources.Add('TForm1','FORMDATA',[
'TPF0'#6'TForm1'#5'Form1'#4'Left'#3'N'#1#6'Height'#3','#1#3'Top'#3#239#0#5'Wi'
+'dth'#3#144#1#13'ActiveControl'#7#5'Edit2'#7'Caption'#6#5'Form1'#12'ClientHe'

View File

@ -5,7 +5,6 @@
<General>
<MainUnit Value="0"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
</General>
<VersionInfo>
<Language Value=""/>
@ -33,7 +32,7 @@
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="3">
<Units Count="4">
<Unit0>
<Filename Value="testnuminput.lpr"/>
<IsPartOfProject Value="True"/>
@ -51,6 +50,11 @@
<IsPartOfProject Value="True"/>
<UnitName Value="jinpututils"/>
</Unit2>
<Unit3>
<Filename Value="jcontrolutils.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="jcontrolutils"/>
</Unit3>
</Units>
</ProjectOptions>
<CompilerOptions>

View File

@ -10,7 +10,7 @@ uses
{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms
{ you can add units after this }, main, LResources;
{ you can add units after this }, main, jcontrolutils, LResources;
{$IFDEF WINDOWS}{$R testnuminput.rc}{$ENDIF}