Recent

Author Topic: Passing TDate parameter issue  (Read 1527 times)

Arskin_Fertrubble

  • New Member
  • *
  • Posts: 15
Passing TDate parameter issue
« on: September 27, 2023, 11:16:56 pm »
Hi
Old Delphi hack, newbie to Free Pascal

I have a procedure copied/ pasted from Delphi:

Quote
procedure TfrmMain.FileSearch(const dirName: string;const fromdate : TDate = 0 )

I call the function with
Quote
adate := Date() - 7;
FileSearch(DirName, adate);
The procedure receives the DirName  correctly, but the fromdate is always the first system date, i.e. 30 Dec 1999. I've changed things around numerous ways but no matter what, I don't seem to be able to pass an adjusted date value to the procedure.

Is this by design, is it me, am I missing something, or are the Pascal gods  simply testing me?

Thanks for your patience....




« Last Edit: September 27, 2023, 11:31:33 pm by Arskin_Fertrubble »

Wallaby

  • Full Member
  • ***
  • Posts: 104
Re: Passing TDate parameter issue
« Reply #1 on: September 27, 2023, 11:28:44 pm »
Your code works correctly:

Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     procedure FormCreate(Sender: TObject);
  3.   private
  4.     procedure FileSearch(const dirName: string;const fromdate : TDate = 0);
  5.   public
  6.  
  7.   end;
  8.  
  9. var
  10.   Form1: TForm1;
  11.  
  12. implementation
  13.  
  14. {$R *.lfm}
  15.  
  16. { TForm1 }
  17.  
  18. procedure TForm1.FormCreate(Sender: TObject);
  19. var
  20.   dirname: string;
  21.   adate: Tdate;
  22. begin
  23.   dirname := 'lalala';
  24.   adate := Date() - 7;
  25.   FileSearch(DirName, adate);
  26. end;
  27.  
  28. procedure TForm1.FileSearch(const dirName: string; const fromdate: TDate);
  29. begin
  30.   ShowMessage(dirName + ' ' + DateToStr(FromDate));
  31. end;    

Result:

Code: Text  [Select][+][-]
  1. [Window Title]
  2. project1
  3.  
  4. [Content]
  5. lalala 21/09/2023
  6.  
  7. [OK]

There's likely another issue or typo in your code, e.g. it can be a different variable that you are reading, or (less likely) you have a memory corruption/overwrite somewhere.

Arskin_Fertrubble

  • New Member
  • *
  • Posts: 15
Re: Passing TDate parameter issue
« Reply #2 on: September 27, 2023, 11:35:20 pm »
Hi
Thanks for the feedback.

Not working for me :-( - tried using your code snippets. Getting the same undesirable result.

I am very confused....

NB I'm on 'nix, BTW.

As a work around I am using a global variable now and grab that in the procedure. Not very elegant but I can live with it.

Fibonacci

  • Hero Member
  • *****
  • Posts: 612
  • Internal Error Hunter
Re: Passing TDate parameter issue
« Reply #3 on: September 27, 2023, 11:40:00 pm »
You may want to try Now instead of Date()

Code: Pascal  [Select][+][-]
  1.   showmessage(
  2.     DateTimeToStr(Date()-7)+#10+  //20.09.2023
  3.     DateTimeToStr(Now-7)          //20.09.2023 23:39:27
  4.   );  

Without seeing FileSearch I cant suggest anything more

Maybe I'd change TDate to TDateTime

wp

  • Hero Member
  • *****
  • Posts: 12470
Re: Passing TDate parameter issue
« Reply #4 on: September 27, 2023, 11:40:55 pm »
Not working for me :-( - tried using your code snippets. Getting the same undesirable result.
Then remove everything from the project what is not needed to demonstrate the issue, pack the .pas, .lfm, .lpi and .lpr files into a common zip and upload it to the forum (via "Attachments and other options") so that we can have a look. But make sure that the uploaded project does show the issue.

Arskin_Fertrubble

  • New Member
  • *
  • Posts: 15
Re: Passing TDate parameter issue
« Reply #5 on: September 27, 2023, 11:47:14 pm »
Then remove everything from the project what is not needed to demonstrate the issue, pack the .pas, .lfm, .lpi and .lpr files into a common zip and upload it to the forum (via "Attachments and other options") so that we can have a look. But make sure that the uploaded project does show the issue.

As mentioned the code provided by Wallaby also fails on my platform (linux mint)

Arskin_Fertrubble

  • New Member
  • *
  • Posts: 15
Re: Passing TDate parameter issue
« Reply #6 on: September 27, 2023, 11:49:14 pm »
Thanks for the responses, people. I'm going to leave it there for now and use a global variable to get the required date into the procedure.

Might pursue this later...


Cheers

wp

  • Hero Member
  • *****
  • Posts: 12470
Re: Passing TDate parameter issue
« Reply #7 on: September 28, 2023, 12:09:10 am »
Then remove everything from the project what is not needed to demonstrate the issue, pack the .pas, .lfm, .lpi and .lpr files into a common zip and upload it to the forum (via "Attachments and other options") so that we can have a look. But make sure that the uploaded project does show the issue.

As mentioned the code provided by Wallaby also fails on my platform (linux mint)

This can't be. The code contains nothing which is specific to the operating system. It is not a complete compilable project, though, you must complete it with .lpi, .lpr, .lfm files, and even the code itself not a complete unit. You also must assign the OnCreate event to the FormCreate method.

In the attachment you can find his complete project which I assembled in Linux Mint where it compiles and runs as expected, of course.
« Last Edit: September 28, 2023, 01:15:25 am by wp »

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Passing TDate parameter issue
« Reply #8 on: September 28, 2023, 01:13:54 am »
Is it possible he isn't using the {$mode objfpc}{$H+} at the top and the compiler may have some issue otherwise with the default parameter?
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 3647
Re: Passing TDate parameter issue
« Reply #9 on: September 28, 2023, 08:43:39 am »
Is it possible he isn't using the {$mode objfpc}{$H+} at the top and the compiler may have some issue otherwise with the default parameter?
No.

The following code based on the example from Wallaby compiles (and works as expected) in all modes except for mode iso and extendedpascal.
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses
  4.   sysutils;
  5.  
  6. procedure FileSearch(const aDirName: string; const aFromDate: TDate);
  7. begin
  8.   writeln('searching for file in ', aDirName, ' using date ', DateToStr(aFromDate));
  9. end;
  10.  
  11. procedure dodo;
  12. var
  13.   lDirname : string;
  14.   lDate    : TDate;
  15. begin
  16.   lDirName := 'tralala';
  17.   lDate := sysutils.Date - 7;
  18.   FileSearch(lDirName, lDate);
  19. end;
  20.  
  21. begin
  22.   dodo;
  23. end.
  24.  

Code: [Select]
searching for file in tralala using date 21-9-23

ofc using a default parameter value for the const only compiles (by default) in mode objfpc and mode delphi unless explicitly using modeswitch defaultparameters. Where it then compiles and works again as expected.
« Last Edit: September 28, 2023, 09:10:32 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: Passing TDate parameter issue
« Reply #10 on: September 28, 2023, 09:25:08 pm »
You may want to try Now instead of Date()

If Arskin_Fertrubble then using Date() instead of Now() is indeed correct and as they said the argument type is TDate so a TDateTime like returned by Now() is not required.

Arskin_Fertrubble

  • New Member
  • *
  • Posts: 15
SOLVED: Passing TDate parameter issue
« Reply #11 on: September 29, 2023, 03:54:53 am »
Sometimes one should just walk away from the screen and ponder the wonders of hardware...

The one thing I did not do yesterday when I was frustrated by this issue was to reboot the PC. After all, it's Linux and the pundits tell me the OS is too robust to warrant reboots, right?  :D

Anyhoo, after returning to the machine with a cold restart this morning the TDate parameter was received by the function as expected.

</sigh>


Cheers & thanks


 

TinyPortal © 2005-2018