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
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?
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.
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");
}