Solved

perl date time calculation

Posted on 2009-04-03
7
1,311 Views
Last Modified: 2012-05-06
I have a time stored as a string in a db.  I want to find the difference in minutes between that value and the current time.
$row_ref->{ordertime} = 04/03/09 07:08:13 pm
localtime(time) = Fri Apr 3 19:17:59 2009
my result shows as 19:-8:13

What am I doing wrong?
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},localtime(time),\$err,1);
my $weekDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%wv");
my $dayDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%dv");
my $hourDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%hv");
my $minuteDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%mv");
my $secondDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%sv");
$deltaStr = substr('00' . $hourDeltaStr,-2,2) . ':' .
substr('00' . $minuteDeltaStr,-2,2) . ':' .
substr('00' . $secondDeltaStr,-2,2) ;

$body .=$row_ref->{ordertime} . "=" . localtime(time) . "=" . $deltaStr;
0
Comment
Question by:jackjohnson44
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24066080
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},scalar localtime(time),\$err,1);
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24086077
you could change the way you get the date, and use perl time() instead of unix date.

example:

$prev_date=time();
.
.
.
$current_date=time();
# time reports seconds since the epoch (00:00:00 UTC, January 1, 1970) so
$minutes=($current_date-$prev_date)/60;
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24086101
Also this is how you can convert a time into epoch seconds:
# Convert to seconds
my $epoch = timelocal($sec, $min, $hour, $mday, $mon, $year);
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 84

Expert Comment

by:ozo
ID: 24086185
#you could also say
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},"now",\$err,1);
0
 

Author Comment

by:jackjohnson44
ID: 24124044
Thanks, how do I find out how many minutes in the dateDelta

I have this
                        $dateDelta = Date::Manip::DateCalc(DateCalc($row_ref->{ordertime},"- 0 hours"), scalar localtime(time),\$err,1);


I know how to find out the minutes?
I know how to get minutes, but not minutes since, for instance 1 hour 5 min, will print out 5, if I do this, not 65
                        my $minuteDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%mv");
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24124056
Date::Manip::Delta_Format( Date::Manip::DateCalc('04/03/09 07:08:13 pm',"now",\$err,1), 9,"%mh")
0
 
LVL 84

Expert Comment

by:ozo
ID: 24124803
sorry, I meant to type
0,"%mh"
although the number there would only make a difference for %md or %mt
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question