jujiboutils: TJLabeledDateEdit: added support for abbreviated month names, Trigger OnEditingDone on selecting a date from calendar.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5830 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
jesusr 2017-04-04 06:14:30 +00:00
parent b13c6aff73
commit 9678243a40
3 changed files with 40 additions and 11 deletions

View File

@ -33,7 +33,8 @@ function NormalizeTime(const Value: string; theValue: TTime;
function NormalizeDateTime(const Value: string; theValue: TDateTime;
const theFormat: string = ''): string;
function NormalizeDateSeparator(const s: string): string;
function IsValidDateString(const Value: string): boolean;
function IsValidDateString(Value: string): boolean;
function ValidateDateString(Value:string; out aDate:TDateTime): boolean;
function IsValidTimeString(const Value: string): boolean;
function IsValidDateTimeString(const Value: string): boolean;
@ -131,9 +132,8 @@ begin
'y', 'Y': BigEndianForm;
end;
if IsValidDateString(texto) then
if ValidateDateString(texto, aDate) then
begin
aDate := StrToDate(texto);
Result := FormatDateTime(aDateFormat, aDate);
end;
end;
@ -242,13 +242,42 @@ begin
Result[i] := DefaultFormatSettings.DateSeparator;
end;
function IsValidDateString(const Value: string): boolean;
procedure HackAbbreviatedDates(var Value: string);
var
i: Integer;
begin
with DefaultFormatSettings do
if pos('mmm', Lowercase(ShortDateFormat))>0 then begin
// Value is probably formatted using month names (mmmm) or
// abbreviated month names (mmm), and that is not supported
// by free pascal. Lets hack it ...
for i:=1 to 12 do begin
if pos(LongMonthNames[i], value)>0 then begin
Value := StringReplace(Value, LongMonthNames[i], format('%.2d',[i]), [rfIgnoreCase]);
break;
end else
if pos(ShortMonthNames[i], value)>0 then begin
Value := StringReplace(Value, ShortMonthNames[i], format('%.2d',[i]), [rfIgnoreCase]);
break;
end;
end;
end;
end;
function IsValidDateString(Value: string): boolean;
var
bTime: TDateTime;
begin
HackAbbreviatedDates(Value);
Result := TryStrToDate(Value, bTime);
end;
function ValidateDateString(Value: string; out aDate: TDateTime): boolean;
begin
HackAbbreviatedDates(Value);
Result := TryStrToDate(Value, aDate);
end;
function IsValidTimeString(const Value: string): boolean;
var
bTime: TDateTime;

View File

@ -762,6 +762,8 @@ begin
end;
procedure TJDbGridDateCtrl.myEditOnEditingDone(Sender: TObject);
var
aDate: TDateTime;
begin
if not Assigned(Field) or not Assigned(Field.Dataset) or not Field.DataSet.Active then
exit;
@ -782,11 +784,11 @@ begin
else
begin
CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue);
if IsValidDateString(CellEditor.Caption) then
if ValidateDateString(CellEditor.Caption, aDate) then
begin
if (not updated) then
begin
theValue := StrToDate(CellEditor.Caption);
theValue := aDate;
if FormatDateTime(DisplayFormat, theValue) <>
FormatDateTime(DisplayFormat, Field.AsDateTime) then
begin
@ -880,9 +882,8 @@ begin
if Length(CellEditor.Caption) = 0 then
theValue := 0
else
if IsValidDateString(CellEditor.Caption) then
if ValidateDateString(CellEditor.Caption, theValue) then
begin
theValue := StrToDate(CellEditor.Caption);
Field.DataSet.Edit;
Field.AsDateTime := theValue;
CellEditor.SelectAll;

View File

@ -209,9 +209,7 @@ begin
if Length(Text) = 0 then
theValue := 0
else
if IsValidDateString(Text) then
theValue := StrToDate(Text)
else
if not ValidateDateString(Text, theValue) then
begin
ShowMessage(Format(SInvalidDate, [Text]));
SetFocus;
@ -312,6 +310,7 @@ procedure TJLabeledDateEdit.CalendarPopupReturnDate(Sender: TObject;
const ADate: TDateTime);
begin
Value := ADate;
EditingDone;
end;
constructor TJLabeledDateEdit.Create(TheOwner: TComponent);