Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong date calculated with lastModified() #1857

Open
lucasRolff opened this issue Feb 19, 2025 · 1 comment
Open

Wrong date calculated with lastModified() #1857

lucasRolff opened this issue Feb 19, 2025 · 1 comment

Comments

@lucasRolff
Copy link

Bug Report

Q A
Flysystem Version 3.29.1
Adapter Name ftp
Adapter version 3.29.0

Summary

If you have a file which is modified in e.g. September 30, 2024, lastModified() within the FTP adapter, will show September 30, 2025.
This is due to how Linux seemingly shows dates, if a file modified is within 6 months, it will not list the year.

If you do a stat on the file, it will obviously show the correct modified time:

[root@nlsh04 example.com]# stat .htaccess
  File: .htaccess
  Size: 525       	Blocks: 8          IO Block: 4096   regular file
Device: 97fh/2431d	Inode: 2150978085  Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1127/example)   Gid: ( 1130/example)
Access: 2025-02-19 02:01:31.360548141 +0000
Modify: 2024-09-30 12:58:00.331455453 +0000
Change: 2024-09-30 12:58:00.331455453 +0000
 Birth: 2024-09-30 12:58:00.331455453 +0000

However, through normal ls -al, you'll simply see Sep 30:

[root@nlsh04 example.com]# ls -al .htaccess
-rw-r--r-- 1 example example 525 Sep 30 12:58 .htaccess

In FTP, such as lftp, you'll see it similarly:

lftp [email protected]:/domains/example.com> ls -l .htaccess
-rw-r--r--    1 1127       example          525 Sep 30 12:58 .htaccess

If you dump the FileAttributes for a given file:

League\Flysystem\FileAttributes {#2520 ▼ // app/Livewire/Panel/Tools/FileManager.php:121
  -type: "file"
  -path: "domains/example.com/.htaccess"
  -fileSize: 525
  -visibility: "public"
  -lastModified: 1759237080
  -mimeType: null
  -extraMetadata: []
}

As we can see here, the lastModified according to flysystem, also converts to Tuesday, 30 September 2025 12:58:00.

It would be nice if it would list the actual modified time.

How to reproduce

  1. Create a file such as text.txt that has a modified date in the past 6 months, ideally something within 2024 on a Linux environment (I use RHEL9 flavor (AlmaLinux in this case).
  2. Do a stat on the file from the filesystem, you should see modified time being what you set, with 2024.
  3. Do a ls/stat within FTP, you should likely see the date but as in 2025, despite this date haven't yet passed
@lucasRolff
Copy link
Author

Something like this seems to produce the correct output:

    private function normalizeUnixTimestamp(string $month, string $day, string $timeOrYear): int
    {
        if (is_numeric($timeOrYear)) {
            $year = $timeOrYear;
            $hour = '00';
            $minute = '00';
        } else {
            $currentYear = (int) date('Y');
            [$hour, $minute] = explode(':', $timeOrYear);

            $tentativeDate = DateTime::createFromFormat('!Y-M-j G:i', "$currentYear-$month-$day $hour:$minute");
            
            if ($tentativeDate) {
                $now = new DateTime();
                $sixMonthsAgo = (clone $now)->modify('-6 months');
                
                if ($tentativeDate > $now || $tentativeDate < $sixMonthsAgo) {
                    $year = $currentYear - 1;
                } else {
                    $year = $currentYear;
                }
            } else {
                $year = $currentYear;
            }
        }

        $dateTime = DateTime::createFromFormat('Y-M-j-G:i:s', "$year-$month-$day-$hour:$minute:00");

        return $dateTime->getTimestamp();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant