Solved

perl date time calculation

Posted on 2009-04-03
7
1,295 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Regular expression 9 190
binary to char / hexadecimal 5 98
perl split by | 2 81
perl to convert excel to csv 3 150
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now