Subtracting two dates in PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtracting two dates in PERL
# 1  
Old 05-19-2012
Subtracting two dates in PERL

Hi guys,

First of all, I would like to say this is my first post in the unix.com forums. I am a beginner in PERL and have only started writing my first scripts.

With that out of the way, I have a question regarding the calculation of time dates in PERL.

I have two scalar variables with the following dates, notice the format of YYYY:MM:dd:hh:mm

$date1 = 2012:05:10:08:55
$date2 = 2012:05:15:09:55

I would like my script to take $date2 and subtract it by $date1 in hours and minutes as follows:

$total = 121 hours

Any idea how I could do this?

Thanks in advance
# 2  
Old 05-19-2012
Okay. You want to learn; I'll show you.
time/date in unix and in perl (which was written on UNIX originally) use a so-called broken down time/date. Everything in dates is based on the number of seconds (epoch time) since Jan 1 1970. VERY important. So most of what you do is turn dates into epoch seconds, subtract or add, then convert the seconds back into something humans like to see.

this returns time in seconds, epoch time:
Code:
use Time::Local;
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);

The array is sort of self documenting == Broken down time is the array.
Read this to learn how to use timelocal and timegm:
Time::Local - perldoc.perl.org

pseudocode:
Code:
sec1 =  epoch seconds for date 1  (from timelocal)
sec2 = epoch seconds for date 2
subtract sec1 from sec2  to get diff
There are 86400 seconds in a day, so
days = diff /86400;
There are   3600 seconds per hour so,  (diff % 3600)/60 is minutes
60 seconds per minute    so seconds are diff % 60;

% is modulo operator - the remainder from division. You get to work out "hours".
hint: start with diff % 86400.

For your hours total just divide diff/3600 to get total=121, but you'll need to do this other stuff as well. Remember you want whole number answers, so perl's int is your friend.

have fun.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : difference between two dates in years

hello folks, I have a requirement in which I have to calculate the difference of localdate(today's date) and the given(earlier) date and to check whether the difference is exactly a year or more than that(can be 1 year or 2 years or 3 years.. ) . Could anyone please let me know the logic... (2 Replies)
Discussion started by: scriptscript
2 Replies

2. Shell Programming and Scripting

Perl ::duration of time in between dates

Hello All, I have two strings with date and time as follows.. $starttime= "06/11/2013 "; $starttime= "05:15"; $enddate="06/12/2013"; $endtime="04:45"; dates are in mm/dd/yyyy format and time in military format. and I am looking the duration of time(in minutes) in between dates. ... (3 Replies)
Discussion started by: scriptscript
3 Replies

3. Programming

Perl read file with dates in

Hi All I have a text file that has a list of dates in it ( see below example) is there i can just pull out the lines that are from this week ( week starting on monday) and then work out the how many occurances there are on each name in collum 2 2013-05-13 08:20:02 bacha ... (8 Replies)
Discussion started by: ab52
8 Replies

4. Shell Programming and Scripting

Comparing the dates with the current date in perl scripting

Hi i have a file containg dates likebelow 4/30/2013 3/31/2013 4/30/2013 4/16/2013 4/30/2013 4/30/2013 5/30/2013 5/30/2013 4/30/2013 5/30/2013 5/30/2013 3/31/2013 now i want to compare the above dates with current date and i want to display the difference . (10 Replies)
Discussion started by: siva kumar
10 Replies

5. Shell Programming and Scripting

Calculate time difference between pst and pdt dates in perl

Hi, how to calculate the time difference between PST date and PDT date in perl scripting. date1: Mon Dec 31 16:00:01 PST 2015 date2: Tue Mar 19 06:09:30 PDT 2013 and also difference between PST-PST and PDT-PDT need difference in months or days (months prefereble). (3 Replies)
Discussion started by: praveen265
3 Replies

6. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

7. Shell Programming and Scripting

Perl script to toggle through dates by week

Hi, I need help to toggle through dates on a weekly basis to be fed into a script as inputs. The format should be: yyyy/mm/dd (start) yyyy/mm/dd (end), where end date is 7 days increments. The date (start) would be input as an ARGV and would continue until current date. I can check... (2 Replies)
Discussion started by: subhap
2 Replies

8. Shell Programming and Scripting

Perl difference between dates

Hi, Is there any way I can get the difference between two dates in terms of days? I have used this method so far, but I cant format it in terms of days. @a=&DateCalc($date1,$date2,0); The o/p that I am getting is sort of like this: +0:0:0:4:0:0:0 I just want to get 4 days as an o/p.... (1 Reply)
Discussion started by: King Nothing
1 Replies

9. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

Help w/ Perl dates

I need to create 12 variables, the first of which is the date of the first day of the current month (01/01/2006), and the remaining 11 are to equal each month after the current. var1 = 01/01/2006 var2 = 02/01/2006 var3 = 03/01/2006 var4 = 04/01/2006 etc. How can I easily do this is in... (7 Replies)
Discussion started by: ssmiths001
7 Replies
Login or Register to Ask a Question