Piotr Szczepanik
Piotr Szczepanik

Reputation: 381

Comparing date and time and the same time in perl

I want to compare both date and time check if the timestamp from the file I'm going to open will have equal or greater date and time as if the my timestamp which looks like this:

$Date = "20170608";
$Time = "105006";

My main problem is how to do it efficiently possibly without adding perl libraries and how to check it when there's going to be situation of date switching and the hour will be for example 23:59:44

Upvotes: 0

Views: 124

Answers (1)

Sobrique
Sobrique

Reputation: 53478

Time::Piece is core in perl, and supports 'strptime'.

#!/usr/bin/env perl

use strict;
use warnings;
use Time::Piece;

my $Date = "20170608";
my $Time = "10506";

my $ts = Time::Piece->strptime( "$Date $Time", "%Y%m%d %H%M%S" );
print $ts, "\n";

print "Delta:", $ts->epoch - time(), "\n";

Was unclear on what time that $Time represented - strptime converts it to 10:50:06, but I'm guessing it might be intended to be 01:05:06?

If so, then zero pad.

$Time = sprintf ( "%06d", $Time );

To read the timestamp from the file metadata, then you need stat:

my $mtime = (stat $filename)[9];

Upvotes: 2

Related Questions