How to add an hour or a minute to a time?

Hi,

The timestamp is June 06 2011 11:05AM
i need 2 results.

first, an hour added to it, June 06 2011 12:05AM
second, a minute added to it, June 06 2011 11:06AM

How can i do this?

Also when it reaches 12:59, it needs to start from 1 again without giving the output as 13:00. it should be 1:00 again

Try a time calculator. There are some on the Web. Or use paper and a pencil.

I can suggest you need do it in a shell, but it's very hard to know what system you use. Please try to be more exact.

Am doing this in perl

Use DateTime from CPAN - very easy. Or you can use localtime function (or standard Time::localtime module).

i have used like this. But this gives 13:00 instead of 1:00 while adding.

my $hr_to_add = 1 * 60 * 60;
my $hr_dt = qx/
  date -d "$dt_str" +%s
  / + $hr_to_add;
my $hr_var = strftime( '%B %d %Y %H:%M%p', localtime $hr_dt );

Can you please be more specific

Try changing %H to %I .

Of course: strftime

I am curious as to where people found the strftime function in Perl?

perl -e 'print strftime("%B %d %Y %H:%M%p", localtime(time)), "\n"'
Undefined subroutine &main::strftime called at -e line 1.

A sample solution with DateTime

echo "June 06 2011 11:05AM" |perl -MDateTime -e '
my %months=(January=>1,Febuary=>2,March=>3,April=>4,May=>5,June=>6,July=>7,August=>8,September=>9,October=>10,November=>11,December=>12);
chomp(my $start_date=<STDIN>);
print "Start date is $start_date \n";
my @date=split/ /,$start_date;
my($hour,$min,$mod)=$date[3]=~/(\d+):(\d+)([AP]M)/;
$hour+=12 if($mod eq "PM");
my $dt=DateTime->new(year=>$date[2],month=>$months{$date[0]},day=>$date[1],hour=>$hour,minute=>$min);
my $plus_hour = DateTime->from_epoch( epoch => $dt->epoch() + (60 * 60) );
printf ("%s plus one hour is %s\n",$start_date, format_time($plus_hour));
$plus_minute=DateTime->from_epoch( epoch => $dt->epoch() + (60) );
printf ("%s plus one minute is %s\n",$start_date, format_time($plus_minute));
sub format_time{
   my $time=shift; 
   return sprintf("%s  %0.2d %d %0.2d:%0.2d%s", $time->month_name, $time->mday(),$time->year,$time->hour_12(),$time->minute,$time->am_or_pm());
}'
Start date is June 06 2011 11:05AM 
June 06 2011 11:05AM plus one hour is June  06 2011 12:05PM
June 06 2011 11:05AM plus one minute is June  06 2011 11:06AM

In the standard module POSIX.

1 Like

Thanks all of you.

Changing %H to %I works fine.
will it change 'AM' and 'PM' correctly or someother option should be used in addition to %p?
is %p compulsory in this case?

Ohh, interesting module, lots of reading on my horizon, thanks

It should handle AM/PM correctly. Try it and let us know.

1 Like

Thanks. Its working fine.
I have

my code:

my $date_var = "Jun  6 2011  8:02AM";
if ($date_var != '')
{
my $min_to_add = 1 * 1 * 60;
my $from_dt = qx/
  date -d "$date_var" +%s
  / + $min_to_add;
my $min_var = strftime( '%B %d %Y %I:%M%p', localtime $from_dt );
print("\n the mins added  is $min_var\n");

my $hr_to_add = 1 * 60 * 60;
my $to_dt = qx/
  date -d "$min_var" +%s
  / + $hr_to_add;
my $hr_var = strftime( '%B %d %Y %I:%M%p', localtime $to_dt );
print("\n the hours  added is $hr_var\n");
}
else
{
use POSIX qw(strftime);
my $to_dt = strftime "%b %d %Y %H:%M%p", localtime;
print("\n The original time is  $to_dt\n");
my $hr_to_sub = 1 * 1 * 86400;
my $from_dt
 = qx/
  date -d "$to_dt" +%s
  / - $hr_to_sub;
my $to_dt_now = strftime( '%B %d %Y %H:%M%p', localtime $from_dt );
print("\n the to date now  is $to_dt_now \n");
}

When am executing this as a whole, the if condition does the opposite.

when $date_var != '' then it goes to the else part,
when $date_var == '' then it does the if loop part which is not supposed to be.

why is this so?

You need:

$date_var ne ''

instead of:

$date_var != ''

Change all the %H in %I in order to provide valid dates to the date command.

1 Like

Thanks radoulov. It works.

Can you please explain what is the difference between the two, in what cases each has to be used and why doesnt $date_var != '' work?

Numerical and string comparisons require different operators.

This is from perldoc perlop :

1 Like

Hi,

This works perfectly fine.
But, $my $min_to_add = 1 * 1 * 60; and my $hr_to_sub = 1 * 1 * 86400; i may need to change the values in future. so am keeping them in a separate configuration file like

MIN = 1 * 1 * 60 
HR = 24 * 60 * 60 

in the script, i use a package use et_config and Et_Config_Init()
I call them inside the script like

my $min_to_add = Et_Config_Value("MIN");
my $hr_to_sub = Et_Config_Value("HR");

The issue here is, when i use those values directly in the script, it takes the multiplied value as 1 * 1 * 60 =60 but when i get them from the configuration file, it remains as 1 * 1 * 60 only as such , so i am not getting the result expected.

Is there any function that can do this?
The configuration file is mandatory. How can this be achieved?

Code:

my $date_var = "Jun  6 2011  8:02AM";
if ($date_var != '')
{
my $min_to_add = 1 * 1 * 60;
my $from_dt = qx/
  date -d "$date_var" +%s
  / + $min_to_add;
my $min_var = strftime( '%B %d %Y %I:%M%p', localtime $from_dt );
print("\n the mins added  is $min_var\n");

my $hr_to_add = 1 * 60 * 60;
my $to_dt = qx/
  date -d "$min_var" +%s
  / + $hr_to_add;
my $hr_var = strftime( '%B %d %Y %I:%M%p', localtime $to_dt );
print("\n the hours  added is $hr_var\n");
}
else
{
use POSIX qw(strftime);
my $to_dt = strftime "%b %d %Y %H:%M%p", localtime;
print("\n The original time is  $to_dt\n");
my $hr_to_sub = 1 * 1 * 86400;
my $from_dt
 = qx/
  date -d "$to_dt" +%s
  / - $hr_to_sub;
my $to_dt_now = strftime( '%B %d %Y %H:%M%p', localtime $from_dt );
print("\n the to date now  is $to_dt_now \n");
}

This is what I would store in the configuration file:

MIN = 1
HR = 24

And I'll calculate the values in run-time:

my $min_to_add =  Et_Config_Value("MIN") * 60;
...