Recent

Author Topic: Incorrect result of function FileAge, when date 12-30-1899  (Read 410 times)

7bit

  • New Member
  • *
  • Posts: 15
Incorrect result of function FileAge, when date 12-30-1899
« on: February 23, 2025, 09:14:25 am »
I found that if the file modification date in the system is 12/30/1899 (TDateTime value = 0), the FileAge returns an incorrect result. Here is the code that demonstrates the problem:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   BaseUnix, DateUtils;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. var
  33.   FlNm: String;
  34.   Utime: UtimBuf;
  35.   St: stat;
  36. begin
  37.   FlNm := GetTempDir + 'temp.tmp';
  38.   with TFileStream.Create(FlNm, fmCreate) do
  39.   begin
  40.     Free;
  41.   end;
  42.  
  43.   //UTime.modtime := DateTimeToUnix(StrToDateTime('30-12-1901'));
  44.   UTime.modtime := DateTimeToUnix(0);
  45.   UTime.actime := UTime.modtime;
  46.   FpUtime(FlNm, @Utime);
  47.  
  48.   // invalid time
  49.   ShowMessage(FormatDateTime('dd-mm-yyyy', UnixToDateTime(FileAge(FlNm))));
  50.  
  51.   // valid time
  52.   FpStat(FlNm, St);
  53.   ShowMessage(FormatDateTime('dd-mm-yyyy', UnixToDateTime(St.st_mtime)));
  54. end;
  55.  
  56. initialization
  57.   {$I unit1.lrs}
  58.  
  59. end.
  60.                                                
  61.  

Lazarus/FPC version: Lazarus 4.0RC2 (rev lazarus_4_0RC2-104-gb0513d9031) FPC 3.2.2 x86_64-linux-gtk2

MarkMLl

  • Hero Member
  • *****
  • Posts: 8364
Re: Incorrect result of function FileAge, when date 12-30-1899
« Reply #1 on: February 23, 2025, 10:01:35 am »
What happens if that's a simple non-GUI program? What date does the OS show for the file? Is it different depending on what utility you use? What filesystem are you using? Does the date survive an fsck?

I'm troubled by the fact that unix- in general- has no support for files older than its own epoch, so trying to force things might lead to inconsistent results.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

7bit

  • New Member
  • *
  • Posts: 15
Re: Incorrect result of function FileAge, when date 12-30-1899
« Reply #2 on: February 23, 2025, 12:13:38 pm »
Here is a simple program:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   SysUtils, Classes, BaseUnix, DateUtils;
  5.  
  6. var
  7.   FlNm: String;
  8.   Utime: UtimBuf;
  9.   St: stat;
  10. begin
  11.   FlNm := GetTempDir + 'temp.tmp';
  12.   with TFileStream.Create(FlNm, fmCreate) do
  13.   begin
  14.     Free;
  15.   end;
  16.  
  17.   UTime.modtime := DateTimeToUnix(0);
  18.   UTime.actime := UTime.modtime;
  19.   FpUtime(FlNm, @Utime);
  20.  
  21.   // invalid time
  22.   WriteLn(FormatDateTime('dd-mm-yyyy', UnixToDateTime(FileAge(FlNm))));
  23.  
  24.   // valid time
  25.   FpStat(FlNm, St);
  26.   WriteLn(FormatDateTime('dd-mm-yyyy', UnixToDateTime(St.st_mtime)));
  27. end.  
  28.  
The result is the same.

File system is Ext4.

Ouput of command ls -l:

Code: Bash  [Select][+][-]
  1. #ls -l
  2. -rw-r--r-- 1 user user  0 дек 30  1899 temp.tmp
  3.  

Result fsck i not check.

Incorrect result of function FileAge, because it does not fit into the LongInt type. The Int64 type is required for the result.


MarkMLl

  • Hero Member
  • *****
  • Posts: 8364
Re: Incorrect result of function FileAge, when date 12-30-1899
« Reply #3 on: February 23, 2025, 12:44:05 pm »
Incorrect result of function FileAge, because it does not fit into the LongInt type. The Int64 type is required for the result.

Well deduced, in that case raise a bug report.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1500
    • Lebeau Software
Re: Incorrect result of function FileAge, when date 12-30-1899
« Reply #4 on: February 23, 2025, 08:10:44 pm »
What about using the FileAge() overload that outputs a TDateTime  instead of an integer?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018