LoginSignup
2
0

Linux系OSなら素直にstatコマンドを使ったほうが早い。

ファイルのマイクロ秒単位での最終修正タイムスタンプ

/**
 * ファイルのマイクロ秒単位での最終修正タイムスタンプを返します。
 *
 * @param   \SplFileInfo|string $file   ファイルパス
 * @return  string|false        ファイルのマイクロ秒単位での最終修正タイムスタンプの文字列表現 取得に失敗した場合はfalse
 */
function micro_mtime(\SplFileInfo|string $file): string|false
{
    $pipes      = [];

    $process    = \proc_open(['stat', '--format=%z', \is_string($file) ? $file : $file->getPathname()], [
        0 => ['pipe', 'r'],
        1 => ['pipe', 'w'],
        2 => ['pipe', 'w'],
    ], $pipes);

    if (!\is_resource($process)) {
        return false;
    }

    $dateTime   = \DateTimeImmutable::createFromFormat(
        'Y-m-d H:i:s.u P',
        \preg_replace('/\.(\d{6})\d+ /', '.$1 ', \trim(\stream_get_contents($pipes[1])))
    );

    \fclose($pipes[1]);

    \proc_close($process);

    return $dateTime === false ? false : $dateTime->format('U.u');
}

ファイルのマイクロ秒単位での最終状態変更タイムスタンプ

/**
 * ファイルのマイクロ秒単位での最終状態変更タイムスタンプを返します。
 *
 * @param   \SplFileInfo|string $file   ファイルパス
 * @return  string|false        ファイルのマイクロ秒単位での最終状態変更タイムスタンプの文字列表現 取得に失敗した場合はfalse
 */
function micro_ctime(\SplFileInfo|string $file): string|false
{
    $pipes      = [];

    $process    = \proc_open(['stat', '--format=%y', \is_string($file) ? $file : $file->getPathname()], [
        0 => ['pipe', 'r'],
        1 => ['pipe', 'w'],
        2 => ['pipe', 'w'],
    ], $pipes);

    if (!\is_resource($process)) {
        return false;
    }

    $dateTime   = \DateTimeImmutable::createFromFormat(
        'Y-m-d H:i:s.u P',
        \preg_replace('/\.(\d{6})\d+ /', '.$1 ', \trim(\stream_get_contents($pipes[1])))
    );

    \fclose($pipes[1]);

    \proc_close($process);

    return $dateTime === false ? false : $dateTime->format('U.u');
}

ファイルのマイクロ秒単位での最終アクセスタイムスタンプ

/**
 * ファイルのマイクロ秒単位での最終アクセスタイムスタンプを返します。
 *
 * @param   \SplFileInfo|string $file   ファイルパス
 * @return  string|false        ファイルのマイクロ秒単位での最終アクセスタイムスタンプの文字列表現 取得に失敗した場合はfalse
 */
function micro_atime(\SplFileInfo|string $file): string|false
{
    $pipes      = [];

    $process    = \proc_open(['stat', '--format=%x', \is_string($file) ? $file : $file->getPathname()], [
        0 => ['pipe', 'r'],
        1 => ['pipe', 'w'],
        2 => ['pipe', 'w'],
    ], $pipes);

    if (!\is_resource($process)) {
        return false;
    }

    $dateTime   = \DateTimeImmutable::createFromFormat(
        'Y-m-d H:i:s.u P',
        \preg_replace('/\.(\d{6})\d+ /', '.$1 ', \trim(\stream_get_contents($pipes[1])))
    );

    \fclose($pipes[1]);

    \proc_close($process);

    return $dateTime === false ? false : $dateTime->format('U.u');
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0