Find the time difference (including the date in the time)
To find the difference when the time contains a date, use the timelocal function of the Time::Local module to convert the time containing the date to seconds and find the difference.
Since the formatted time is described in logs etc., I created an example to find the time difference from that state.
use strict; use warnings; # Difference is 2 seconds Calculate this print "(1) Find the time difference (time includes date)\n"; my $time_str1 = '30/Sep/2006:23:59:59'; my $time_str2 = '01/Oct/2006:00:00:01'; # Converts a string representation of time into a hash of time information, including dates. my $time1 = time_from_str ($time_str1); my $time2 = time_from_str ($time_str2); # Convert time information hash to seconds to calculate elapsed seconds my $sec_interval = time_to_sec ($time2) - time_to_sec ($time1); print "Time difference is $sec_interval seconds.\n"; # A function that converts a string representation of time into a hash that represents time information sub time_from_str { my $time_str = shift; return unless $time_str; my $time = {}; # If it matches the format of the time including the date # if ($time_str =~ m ^ (\d{2})/(\w{3})/(\d{4}): (\d{2}): (\d{2}) :(\d{2}) $# ) { # Create hash @{$time}{qw/mday mon year hour min sec /} = ($1, $2, $3, $4, $5, $6); # Hash corresponding to the English notation of the month and the number of the month my %num_of_mon = ( jan => 0, feb => 1, mar => 2, apr => 3, may => 4, jun => 5, jul => 6, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11, ); # Return if the month notation is incorrect $time->{mon} = $num_of_mon{lc($time->{mon})}; return unless defined $time->{mon}; $time->{year}-= 1900; return wantarray?%$time: $time; } return; } # Function to convert time to seconds sub time_to_sec { my $time; if (ref $_[0] eq 'HASH') { $time = shift; } else { %$time = @_; } return unless defined $time->{sec}; return unless defined $time->{min}; return unless defined $time->{hour}; return unless defined $time->{mday}; return unless defined $time->{mon}; return unless defined $time->{year}; # Convert to seconds using the Time::Local module require Time::Local; my $sec = Time::Local::timelocal ( $time->{sec}, $time->{min}, $time->{hour}, $time->{mday}, $time->{mon}, $time->{year}, ); return $sec; }
Output
(1) Find the time difference (time includes date) The time difference is 2 seconds.
Explanation
Convert time string representation to hash representing time information
Converting a time containing a date such as '30/Sep/2006:23:59:59'to a hash representing time information.
The hash that represents the time information has six keys: sec, min, hour, mday, mon, and year.
After cutting out the date and time with a regular expression, the English name of the month such as Sep is converted to the month number.
sub time_from_str { my $time_str = shift; return unless $time_str; my $time = {}; # If it matches the format of the time including the date # if ($time_str =~ m ^ (\d{2})/(\w{3})/(\d{4}): (\d{2}): (\d{2}) :(\d{2}) $# ) { # Create hash @{$time}{qw/mday mon year hour min sec /} = ($1, $2, $3, $4, $5, $6); # Hash corresponding to the English notation of the month and the number of the month my %num_of_mon = ( jan => 0, feb => 1, mar => 2, apr => 3, may => 4, jun => 5, jul => 6, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11, ); # Return if the month notation is incorrect $time->{mon} = $num_of_mon{lc($time->{mon})}; return unless defined $time->{mon}; $time->{year}-= 1900; return wantarray?%$time: $time; } return; }
Others are the same as "Find the time difference (if it does not exceed 24 hours)".
Convert time to seconds
To convert a time containing a date to seconds (epoch seconds), use the timelocal function of the Time::Local module.
Others are the same as "Find the time difference (if it does not exceed 24 hours)".
# Convert time information hash to seconds to calculate elapsed seconds my $sec_interval = time_to_sec ($time2) - time_to_sec ($time1); # Function to convert time to seconds sub time_to_sec { my $time; if (ref $_[0] eq 'HASH') { $time = shift; } else { %$time = @_; } return unless defined $time->{sec}; return unless defined $time->{min}; return unless defined $time->{hour}; return unless defined $time->{mday}; return unless defined $time->{mon}; return unless defined $time->{year}; # Convert to seconds using the Time::Local module require Time::Local; my $sec = Time::Local::timelocal ( $time->{sec}, $time->{min}, $time->{hour}, $time->{mday}, $time->{mon}, $time->{year}, ); return $sec; }