NAME

Filename::Timestamp - Extract date/timestamp from filename, if any

VERSION

This document describes version 0.003 of Filename::Timestamp (from Perl distribution Filename-Timestamp), released on 2025-10-20.

SYNOPSIS

use Filename::Timestamp qw(extract_timestamp_from_filename);
my $res = extract_timestamp_from_filename(filename => "foo.tar.gz");
if ($res) {
    printf "Filename contains timestamp: %s\n", $res->{epoch};
} else {
    print "Filename does not contain timestamp\n";
}

DESCRIPTION

FUNCTIONS

extract_timestamp_from_filename

Usage:

extract_timestamp_from_filename(%args) -> bool|hash

Extract date/timestamp from filename, if any.

Examples:

  • Example #1:

    extract_timestamp_from_filename(filename => "2024-09-08T12_35_48+07_00.JPEG");

    Result:

    {
      day       => 8,
      epoch     => 1725824148,
      hour      => 12,
      minute    => 35,
      month     => 9,
      second    => 48,
      tz_offset => 25200,
      year      => 2024,
    }
  • Example #2:

    extract_timestamp_from_filename(filename => "IMG_20240908_095444.jpg");

    Result:

    {
      day         => 8,
      epoch       => 1725764084,
      epoch_local => 1725764084,
      epoch_utc   => 1725789284,
      hour        => 9,
      minute      => 54,
      month       => 9,
      second      => 44,
      tz          => "floating",
      year        => 2024,
    }
  • Example #3:

    extract_timestamp_from_filename(filename => "VID_20240908_092426.mp4");

    Result:

    {
      day         => 8,
      epoch       => 1725762266,
      epoch_local => 1725762266,
      epoch_utc   => 1725787466,
      hour        => 9,
      minute      => 24,
      month       => 9,
      second      => 26,
      tz          => "floating",
      year        => 2024,
    }
  • Example #4:

    extract_timestamp_from_filename(filename => "Screenshot_2024-09-01-11-40-44-612_org.mozilla.firefox.jpg");

    Result:

    {
      day         => 1,
      epoch       => 1725165644,
      epoch_local => 1725165644,
      epoch_utc   => 1725190844,
      hour        => 11,
      minute      => 40,
      month       => 9,
      second      => 44,
      tz          => "floating",
      year        => 2024,
    }
  • Example #5:

    extract_timestamp_from_filename(filename => "IMG-20241204-WA0001.jpg");

    Result:

    {
      day         => 4,
      epoch       => 1733245200,
      epoch_local => 1733245200,
      epoch_utc   => 1733270400,
      hour        => 0,
      minute      => 0,
      month       => 12,
      second      => 0,
      tz          => "floating",
      year        => 2024,
    }
  • Example #6:

    extract_timestamp_from_filename(filename => "foo.txt"); # -> 0

Handling of timezone. If timestmap contains timezone indication, e.g. +0700, will return tz_offset key in the result hash as well as epoch. Otherwise, will return tz key in the result hash with the value of floating and epoch_local calculated using Time::Local's timelocal_posix as well as epoch_utc calculated using timegm_posix.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • all => bool

    Find all timestamps instead of the first found only.

    Not yet implemented.

  • filename* => str

    (No description)

Return value: (bool|hash)

Return false if no timestamp is detected. Otherwise return a hash of information, which contains these keys: epoch, year, month, day, hour, minute, second.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Filename-Timestamp.

SOURCE

Source repository is at https://github.com/perlancar/perl-Filename-Timestamp.

SEE ALSO

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

% prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Filename-Timestamp

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.