Recent

Author Topic: TryStrToDate and {$INCLUDE %DATE}  (Read 10480 times)

Eclipticon

  • Jr. Member
  • **
  • Posts: 51
TryStrToDate and {$INCLUDE %DATE}
« on: May 01, 2011, 08:18:08 pm »
Hi,

I would like to convert the date I can obtain from {$INCLUDE %DATE} to a TDateTime ... if I output it directly, I get '2011/05/01'.

TryStrToDate({$INCLUDE %DATE}, BuildDateTime)
TryStrToDate({$INCLUDE %DATE}, BuildDateTime, '\')
TryStrToDate({$INCLUDE %DATE}, BuildDateTime, 'YYYY/MM/DD', '\')
TryStrToDate({$INCLUDE %DATE}, BuildDateTime, 'YYYY/MM/DD')

all fail, however. What is going wrong here and is there any documentation on the useformat parameter?

Thanks!
Lazarus 1.2.4 on Win XP/7 Virtual Machine

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #1 on: May 01, 2011, 08:57:31 pm »
var
  CompilationDate: TDateTime;
const
  CompilationDateStr = {$i %date%};

CompilationDate := ScanDateTime('yyyy/mm/dd', CompilationDateStr);

Eclipticon

  • Jr. Member
  • **
  • Posts: 51
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #2 on: May 01, 2011, 09:50:02 pm »
The value of the constant is still '2011/05/01', however, an exception with the message

Mismatch char "/" <> "." at pattern position 5, string position 5.

is raised. NOW I am confused ...
« Last Edit: May 01, 2011, 10:05:58 pm by Eclipticon »
Lazarus 1.2.4 on Win XP/7 Virtual Machine

Takeda

  • Full Member
  • ***
  • Posts: 157
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #3 on: May 02, 2011, 12:39:42 am »
Hi,

I would like to convert the date I can obtain from {$INCLUDE %DATE} to a TDateTime ... if I output it directly, I get '2011/05/01'.

TryStrToDate({$INCLUDE %DATE}, BuildDateTime)
TryStrToDate({$INCLUDE %DATE}, BuildDateTime, '\')
TryStrToDate({$INCLUDE %DATE}, BuildDateTime, 'YYYY/MM/DD', '\')
TryStrToDate({$INCLUDE %DATE}, BuildDateTime, 'YYYY/MM/DD')

all fail, however. What is going wrong here and is there any documentation on the useformat parameter?

Thanks!

Did you have take a look into : http://www.freepascal.org/docs-html/rtl/sysutils/trystrtodate.html ?
And for formatting output style take a look into : http://www.freepascal.org/docs-html/rtl/sysutils/tformatsettings.html .


Oh ya, I forget, I think it will suitable for you to use {$I %DATE%}.
Refers the default : ( http://www.freepascal.org/docs-html/prog/progsu38.html )

Best regards,

matsuki, takeda.
« Last Edit: May 02, 2011, 12:46:15 am by Takeda »
Call me Takeda coz that's my true name.
Pascal coding using Lazarus => "Be native in any where.."

ƪ(˘⌣˘)┐ ƪ(˘⌣˘)ʃ ┌(˘⌣˘)ʃ

Eclipticon

  • Jr. Member
  • **
  • Posts: 51
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #4 on: May 02, 2011, 09:19:44 am »
Errrrrr, yes, there was obviously a typo spread via copy and paste in my original post ... I meant to write %DATE% anyway ...
Lazarus 1.2.4 on Win XP/7 Virtual Machine

Eclipticon

  • Jr. Member
  • **
  • Posts: 51
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #5 on: May 05, 2011, 10:10:33 pm »
If one uses the right slash in the third version I tested, one even gets the right result:

TryStrToDate({$INCLUDE %DATE%}, BuildDateTime, 'YYYY/MM/DD', '/')

What fool I am ...
Lazarus 1.2.4 on Win XP/7 Virtual Machine

Takeda

  • Full Member
  • ***
  • Posts: 157
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #6 on: May 05, 2011, 11:38:11 pm »
If one uses the right slash in the third version I tested, one even gets the right result:

TryStrToDate({$INCLUDE %DATE%}, BuildDateTime, 'YYYY/MM/DD', '/')

What fool I am ...


Well, everything look fine now? And everything works according to your expectations, Sir?
So sorry, I'm late to read this thread.. coz  I just read the reply notification in my account.

BTW, Do you know "how trick to store our compilation executable-"self-checksum"?
It's the feature in the other compilers, such as Delphi and Visual Studio..

Self-Checksum is feature to prevent our executable from modification, so if stored-chekcsum value is not same with checksum test result at runtime, then our program will not running..
Call me Takeda coz that's my true name.
Pascal coding using Lazarus => "Be native in any where.."

ƪ(˘⌣˘)┐ ƪ(˘⌣˘)ʃ ┌(˘⌣˘)ʃ

PeterX

  • Sr. Member
  • ****
  • Posts: 404
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #7 on: March 17, 2019, 11:35:38 am »
Sorry to respond to such an old thread, but ..

I also needed a different string format for {$I %DATE%}
to be able to append it to a file name ..

Now this is a ready-to-use solution :

Code: Pascal  [Select][+][-]
  1. function MyDateString:string;
  2. var
  3.   OldShortDateFormat : string;
  4.   MyDateTime         : TDateTime;
  5. begin
  6.   OldShortDateFormat:= FormatSettings.ShortDateFormat;
  7.   FormatSettings.ShortDateFormat:= 'yyyy-mm-dd';
  8.   TryStrToDate( {$I %DATE%}, MyDateTime, 'YYYY/MM/DD', '/');
  9.  
  10.   Result:= DateTimeToStr( MyDateTime);
  11.  
  12.   {$ifdef DEBUG_MY_UNIT}
  13.   ShowMessage( 'original ShortDateFormat =' +OldShortDateFormat +LineEnding +LineEnding
  14.                +'new ShortDateFormat      =' +FormatSettings.ShortDateFormat +LineEnding +LineEnding
  15.                +'%date% = ' +{$I %date%} +LineEnding +LineEnding
  16.                +'formatted = ' +Result);
  17.   {$endif}
  18.  
  19.   // finally restore original format :
  20.   FormatSettings.ShortDateFormat:= OldShortDateFormat;
  21. end;
  22.  
usually using latest Lazarus release version with Windows 10

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #8 on: March 17, 2019, 01:26:12 pm »
From my AboutForm:
Code: Pascal  [Select][+][-]
  1. procedure TExtAboutDlgForm.SetVersionInfo(ProductName,Version, BuildDate, LazVersion,
  2.   FpcVersion: String);
  3. var
  4.   Y,M,D: String;
  5.   p: Integer;
  6. begin
  7.   if ProductName = '' then ProductName := Application.Title;
  8.   if BuildDate = '' then
  9.   begin
  10.     BuildDate := {$I %DATE%};  // y/m/d format
  11.     p := Pos('/',BuildDate);
  12.     Y := Copy(BuildDate,1,p-1);
  13.     System.Delete(BuildDate,1,p);
  14.     p := Pos('/',BuildDate);
  15.     M := Copy(BuildDate,1,p-1);
  16.     System.Delete(BuildDate,1,p);
  17.     D := BuildDate;
  18.     BuildDate := 'Build date: ' + D + '-'+ M + '-'+ Y;
  19.   end;
  20.   if LazVersion = '' then LazVersion := 'Lazarus '+ LCLVersion;
  21.   if FpcVersion = '' then FpcVersion := 'FPC ' + {$I %FPCVERSION%};
  22.   ProductLabel.Caption := ProductName;
  23.   VersionLabel.Caption := Version;
  24.   BuildDateLabel.Caption := BuildDate;
  25.   LazLabel.Caption := LazVersion;
  26.   FpcLabel.Caption := FpcVersion;
  27. end;

And I have this lying around:
Code: Pascal  [Select][+][-]
  1. function LocalizeBuildDateString(BuildDate: String; ADateFormat: String): String;
  2. //use {$I %DATE% as input!} (it MUST be in y/m/d/ format!)
  3. function StrToWord(const S: String): Word;
  4. var
  5.   L: LongInt;
  6. begin
  7.   if TryStrToInt(S, L) then
  8.   begin
  9.     if (L >=0) and (L < 65536) then Result := Word(L)
  10.     else Result := $FFFF;
  11.   end
  12.   else Result := $FFFF;
  13. end;
  14.  
  15. var
  16.   SlashPos1, SlashPos2: integer;
  17.   Date: TDateTime;
  18. begin
  19.   Result := 'Unknow build date';
  20.   //Input is in y/m/d format
  21.   SlashPos1 := Pos('/',BuildDate);
  22.   SlashPos2 := SlashPos1 +
  23.     Pos('/', Copy(BuildDate, SlashPos1+1, Length(BuildDate)-SlashPos1));
  24.   if (SlashPos1 = 0) or (SlashPos2 = 0) then exit;
  25.   if not TryEncodeDate(StrToWord(Copy(BuildDate,1,SlashPos1-1)),
  26.     StrToWord(Copy(BuildDate,SlashPos1+1,SlashPos2-SlashPos1-1)),
  27.     StrToWord(Copy(BuildDate,SlashPos2+1,Length(BuildDate)-SlashPos2)),Date) then exit;
  28.   if (ADateFormat = '') then ADateFormat := 'dd-mm-yyyy';
  29.   Result := FormatDateTime(ADateFormat, Date);
  30. end;

Bart

PeterX

  • Sr. Member
  • ****
  • Posts: 404
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #9 on: March 18, 2019, 04:26:56 pm »
Under which circumstances will  {$I %DATE%}  not be in y/m/d/ format ?
usually using latest Lazarus release version with Windows 10

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #10 on: March 18, 2019, 06:29:04 pm »
Under which circumstances will  {$I %DATE%}  not be in y/m/d/ format ?
According to the documentation, the format is even more strict: YYYY/MM/DD

PeterX

  • Sr. Member
  • ****
  • Posts: 404
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #11 on: March 26, 2019, 12:12:59 am »
https://www.freepascal.org/docs-html/prog/progsu41.html#x48-470001.2.41

Cool - there's much more inside. To be compiled into my EXE file ! Wow !
usually using latest Lazarus release version with Windows 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #12 on: March 26, 2019, 07:56:53 am »
TryStrToDate is a function that returns a Boolean. Why does nobody here check the result?
Code: Pascal  [Select][+][-]
  1.   if not TryStrToDate( {$I %DATE%}, MyDateTime, 'YYYY/MM/DD', '/') then writeln('format not correct);// etc
Note Bart uses the function result for TryStrToInt to handle that specific part, but you should already do that on TryStrToDate...
Specialize a type, not a var.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #13 on: March 26, 2019, 03:24:58 pm »
https://www.freepascal.org/docs-html/prog/progsu41.html#x48-470001.2.41

Cool - there's much more inside. To be compiled into my EXE file ! Wow !

There's more from Lazarus as well.  Here's the stuff I found on my early forays :-)

https://forum.lazarus.freepascal.org/index.php/topic,13957.msg73542.html#msg73542
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

PeterX

  • Sr. Member
  • ****
  • Posts: 404
Re: TryStrToDate and {$INCLUDE %DATE}
« Reply #14 on: March 26, 2019, 05:23:24 pm »
TryStrToDate is a function that returns a Boolean. Why does nobody here check the result?
Code: Pascal  [Select][+][-]
  1.   if not TryStrToDate( {$I %DATE%}, MyDateTime, 'YYYY/MM/DD', '/') then writeln('format not correct);// etc
Note Bart uses the function result for TryStrToInt to handle that specific part, but you should already do that on TryStrToDate...
Didn't see Your post until now - yes, I didn't know that.

Will update my code with this - thanks !  :)
usually using latest Lazarus release version with Windows 10

 

TinyPortal © 2005-2018