hujunsimon has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
i have a question here regarding to calculate the minutes difference between two dates. i have written a code here, but i know it's not very accurate, because i assume every month is 30 days and 365 days in a year (it goes very wrong when calculate the dates for example: 2007-12-31 16:50:00;2008-1-1 04:24:00;), anyone know there is a perl module can calculate the accurate minuets between two dates ?
thanks,#!/usr/bin/perl use strict; use warnings; #Using perl package use Date::Calc qw(Delta_YMDHMS); use Date::Calc qw(Add_Delta_DHMS); #subroutine: rounding no sub round($); while (<TEMP>) { my @line=split(";"); my @start_time=($line[3]=~/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/); + #parsing the start time my @stop_time=($line[4]=~/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/); + #parsing the stop time my @diff=Delta_YMDHMS(@start_time,@stop_time); + #calculate diff in YMDHMS my($yyyy,$m,$d,$hh,$mm,$ss)=@diff; $min_diff=($yyyy*365*24*60+$m*30*24*60+$d*24*60+$hh*60+$mm*1+$ss*0); + #convert to mins $interval=round($min_diff/15); + #get the interval print "minutes difference between two dates are: $min_diff"; } sub round($) { my($number) = shift; return int($number + .5); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Calculate time difference between two dates
by ikegami (Patriarch) on Oct 06, 2009 at 19:46 UTC | |
|
Re: Find minutes difference between two dates
by Anonymous Monk on Oct 06, 2009 at 20:36 UTC | |
by ikegami (Patriarch) on Oct 06, 2009 at 20:49 UTC | |
by Anonymous Monk on Oct 07, 2009 at 10:11 UTC | |
|
Re: Find minutes difference between two dates
by Anonymous Monk on Oct 06, 2009 at 19:50 UTC | |
by ikegami (Patriarch) on Oct 06, 2009 at 20:25 UTC |