perlvroom has asked for the wisdom of the Perl Monks concerning the following question:

I know how to do date/time calculations with epoch time for localtime(time), but suppose i just have a string that's the following format: "12:00" I want to subtract 30 minutes from it so the string is now "11:30"

Replies are listed 'Best First'.
Re: Subtracting minutes from a string ( Time::Piece->strptime )
by Anonymous Monk on Aug 15, 2013 at 13:03 UTC
      Thank you! A new tool in my toolkit.
Re: Subtracting minutes from a string
by mtmcc (Hermit) on Aug 15, 2013 at 16:20 UTC
    If it's just hours and minutes, you could write a subroutine:

    #!/usr/bin/perl use strict; use warnings; my $time = "12:30"; my $secondTime = minutes($time, 120); print STDERR "TIME: $time\tNEW: $secondTime\n"; sub minutes { my $extraHour = 0; my $time = $_[0]; my $minutes = $_[1]; my @time = split(/:/, $time); my $modMinutes = $minutes%60; $extraHour = 1 if $minutes > $time[1]; my $hours = $extraHour + (int($minutes/60)); my $newHour = sprintf("%02d", ($time[0] - $hours)%24); my $newMinutes = sprintf("%02d", ($time[1] - $modMinutes)%60); my $newTime = "$newHour:$newMinutes"; return $newTime; }

    EDIT: changed extra hour.