Reputation: 183
I would like to calculate the the current time and add 2 minutes to it and print the output in the following format. HH:MM . I searched online and came to know that there are lot of CPAN modules that can be used to implement this. But I'd like to do it without cpan modules.
$current_time = time();
$new_time = $current_time + (2*60); // adding two minutes
print( ' the time is ' . $ new_time ) ;
Output : the time is 1424906904
I searched online and came to know that we need to use POSIX perl interface to print the time in the appropriate format. However i'd like to know if there is a way to do this without using any cpan modules.
Upvotes: 1
Views: 61
Reputation: 69244
Time::Piece and Time::Seconds have been included with all Perl installations since 2007.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
use Time::Seconds;
my $time = localtime;
$time += 2 * ONE_MINUTE;
say $time->strftime('%H:%M');
Upvotes: 0
Reputation: 890
That's easy to do with localtime. Hours, minutes, and seconds are the 2nd, 1st, and 0th values returned. For example:
my ($sec, $min, $hours) = localtime(time()+120); # add 120 seconds
printf "%02d:%02d:%02d\n", $hours, $min, $sec;
Upvotes: 0
Reputation: 5290
You can use localtime
:
print scalar localtime($current_time);
Or you can run localtime
's return values through POSIX::strftime
(which is distributed with Perl as a core module):
use POSIX qw(strftime);
print strftime('%Y-%m-%d %H:%M:%S', localtime $current_time);
Upvotes: 1