Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how many days ago is 2003-07-20 ?

Reply
Thread Tools

how many days ago is 2003-07-20 ?

 
 
Marcus
Guest
Posts: n/a
 
      08-27-2004
Can someone pls show the most solid way in perl to retrieve the number
of days ago a specific date string was?

Input: 2003-07-20
Output: 395

Thanks
M
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-27-2004
Marcus wrote:
> Can someone pls show the most solid way in perl to retrieve the
> number of days ago a specific date string was?
>
> Input: 2003-07-20
> Output: 395


What have you tried? Personally I'd probably use Time::Local, which
would require a couple of extra lines of code, while others would use
e.g. Date::Calc.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Zebee Johnstone
Guest
Posts: n/a
 
      08-28-2004
In comp.lang.perl.misc on 27 Aug 2004 16:35:04 -0700
Marcus <(E-Mail Removed)> wrote:
> Can someone pls show the most solid way in perl to retrieve the number
> of days ago a specific date string was?
>
> Input: 2003-07-20
> Output: 395
>


Date::Manip is one way.

4. The amount of time between two dates.

$date1=&ParseDate($string1);
$date2=&ParseDate($string2);
$delta=&DateCalc($date1,$date2,\$err);
=> 0:0:WKD:HH:MM:SS the weeks, days, hours, minutes,
and seconds between the two
$delta=&DateCalc($date1,$date2,\$err,1);
=> YY:MM:WKD:HH:MM:SS the years, months, etc. between
the two

There are other Date modules, that's just the one I've been using
because I handle dates in all sorts of weird formats and do all sorts of
weird thigns with them, it handles everything so far.

Zebee
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-28-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Marcus wrote:
> > Can someone pls show the most solid way in perl to retrieve the
> > number of days ago a specific date string was?
> >
> > Input: 2003-07-20
> > Output: 395

>
> What have you tried? Personally I'd probably use Time::Local, which
> would require a couple of extra lines of code, while others would use
> e.g. Date::Calc.


Do you have a simple, water-tight solution using only Time::Local?
Note that in the presence of DST a day may have more or less than 24
hours.

Anno
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-28-2004
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>> Marcus wrote:
>>> Can someone pls show the most solid way in perl to retrieve the
>>> number of days ago a specific date string was?
>>>
>>> Input: 2003-07-20 Output: 395

>>
>> What have you tried? Personally I'd probably use Time::Local,
>> which would require a couple of extra lines of code, while others
>> would use e.g. Date::Calc.

>
> Do you have a simple, water-tight solution using only Time::Local?


Think so.

sub daysago {
shift =~ /^(\d{4})-(\d{2})-(\d{2})$/
or die "Invalid date format";
require Time::Local;
import Time::Local 'timelocal';
my $diff = timelocal(0,0,0,(localtime $^T)[3..5])
- timelocal(0,0,0,$3,$2-1,$1-1900);
$diff >= 0 or die "Future date not allowed";
sprintf '%.0f', $diff / 86400
}

print daysago('2003-07-20'), "\n";

> Note that in the presence of DST a day may have more or less than
> 24 hours.


Doesn't the above sub take care of that?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Marcus
Guest
Posts: n/a
 
      08-29-2004
Brilliant. Both solutions works really well. Thanks

Zebee Johnstone <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> In comp.lang.perl.misc on 27 Aug 2004 16:35:04 -0700
> Marcus <(E-Mail Removed)> wrote:
> > Can someone pls show the most solid way in perl to retrieve the number
> > of days ago a specific date string was?
> >
> > Input: 2003-07-20
> > Output: 395
> >

>
> Date::Manip is one way.
>
> 4. The amount of time between two dates.
>
> $date1=&ParseDate($string1);
> $date2=&ParseDate($string2);
> $delta=&DateCalc($date1,$date2,\$err);
> => 0:0:WKD:HH:MM:SS the weeks, days, hours, minutes,
> and seconds between the two
> $delta=&DateCalc($date1,$date2,\$err,1);
> => YY:MM:WKD:HH:MM:SS the years, months, etc. between
> the two
>
> There are other Date modules, that's just the one I've been using
> because I handle dates in all sorts of weird formats and do all sorts of
> weird thigns with them, it handles everything so far.
>
> Zebee

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-29-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Gunnar Hjalmarsson wrote:
> >> Marcus wrote:
> >>> Can someone pls show the most solid way in perl to retrieve the
> >>> number of days ago a specific date string was?
> >>>
> >>> Input: 2003-07-20 Output: 395
> >>
> >> What have you tried? Personally I'd probably use Time::Local,
> >> which would require a couple of extra lines of code, while others
> >> would use e.g. Date::Calc.

> >
> > Do you have a simple, water-tight solution using only Time::Local?

>
> Think so.
>
> sub daysago {
> shift =~ /^(\d{4})-(\d{2})-(\d{2})$/
> or die "Invalid date format";
> require Time::Local;
> import Time::Local 'timelocal';
> my $diff = timelocal(0,0,0,(localtime $^T)[3..5])
> - timelocal(0,0,0,$3,$2-1,$1-1900);
> $diff >= 0 or die "Future date not allowed";
> sprintf '%.0f', $diff / 86400
> }
>
> print daysago('2003-07-20'), "\n";
>
> > Note that in the presence of DST a day may have more or less than
> > 24 hours.

>
> Doesn't the above sub take care of that?


I don't know, but it is only obviously correct if the interval between
the two times is a multiple of 24 hours. Since that isn't always so,
I'd prefer a solution that has been checked for these cases instead of
checking myself.

Anno
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-29-2004
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>> Anno Siegel wrote:
>>> Do you have a simple, water-tight solution using only
>>> Time::Local?

>>
>> Think so.
>>
>> sub daysago {
>> shift =~ /^(\d{4})-(\d{2})-(\d{2})$/
>> or die "Invalid date format";
>> require Time::Local;
>> import Time::Local 'timelocal';
>> my $diff = timelocal(0,0,0,(localtime $^T)[3..5])
>> - timelocal(0,0,0,$3,$2-1,$1-1900);
>> $diff >= 0 or die "Future date not allowed";
>> sprintf '%.0f', $diff / 86400
>> }
>>
>> print daysago('2003-07-20'), "\n";
>>
>>> Note that in the presence of DST a day may have more or less
>>> than 24 hours.

>>
>> Doesn't the above sub take care of that?

>
> I don't know, but it is only obviously correct if the interval
> between the two times is a multiple of 24 hours.


Hey, how is your math? We know it may differ 1/24 day, and since
that it far less than 1/2, to me it's pretty obvious that sprintf()
makes it return the correct number of days.

> Since that isn't always so, I'd prefer a solution that has been
> checked for these cases instead of checking myself.


Sometimes I feel that there is something religious about the faith in
using oversized modules.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-29-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Gunnar Hjalmarsson wrote:
> >> Anno Siegel wrote:
> >>> Do you have a simple, water-tight solution using only
> >>> Time::Local?
> >>
> >> Think so.
> >>
> >> sub daysago {
> >> shift =~ /^(\d{4})-(\d{2})-(\d{2})$/
> >> or die "Invalid date format";
> >> require Time::Local;
> >> import Time::Local 'timelocal';
> >> my $diff = timelocal(0,0,0,(localtime $^T)[3..5])
> >> - timelocal(0,0,0,$3,$2-1,$1-1900);
> >> $diff >= 0 or die "Future date not allowed";
> >> sprintf '%.0f', $diff / 86400
> >> }
> >>
> >> print daysago('2003-07-20'), "\n";
> >>
> >>> Note that in the presence of DST a day may have more or less
> >>> than 24 hours.
> >>
> >> Doesn't the above sub take care of that?

> >
> > I don't know, but it is only obviously correct if the interval
> > between the two times is a multiple of 24 hours.

>
> Hey, how is your math? We know it may differ 1/24 day, and since
> that it far less than 1/2, to me it's pretty obvious that sprintf()
> makes it return the correct number of days.


Then there's the problem of non-existent and ambiguous times that
comes with DST.

> > Since that isn't always so, I'd prefer a solution that has been
> > checked for these cases instead of checking myself.

>
> Sometimes I feel that there is something religious about the faith in
> using oversized modules.


There is a well-founded preference for peer-reviewed solutions over
ad-hoc ones.

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-29-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Gunnar Hjalmarsson wrote:
> >> Anno Siegel wrote:
> >>> Do you have a simple, water-tight solution using only
> >>> Time::Local?
> >>
> >> Think so.
> >>
> >> sub daysago {
> >> shift =~ /^(\d{4})-(\d{2})-(\d{2})$/
> >> or die "Invalid date format";
> >> require Time::Local;
> >> import Time::Local 'timelocal';
> >> my $diff = timelocal(0,0,0,(localtime $^T)[3..5])
> >> - timelocal(0,0,0,$3,$2-1,$1-1900);
> >> $diff >= 0 or die "Future date not allowed";
> >> sprintf '%.0f', $diff / 86400
> >> }
> >>
> >> print daysago('2003-07-20'), "\n";
> >>
> >>> Note that in the presence of DST a day may have more or less
> >>> than 24 hours.
> >>
> >> Doesn't the above sub take care of that?

> >
> > I don't know, but it is only obviously correct if the interval
> > between the two times is a multiple of 24 hours.

>
> Hey, how is your math? We know it may differ 1/24 day, and since
> that it far less than 1/2, to me it's pretty obvious that sprintf()
> makes it return the correct number of days.


....supposing that the rounded number is indeed what is wanted in
this case. It may differ from the number of complete calendar days
between the dates.

Then there's the problem of non-existent and ambiguous times that
comes with DST.

> > Since that isn't always so, I'd prefer a solution that has been
> > checked for these cases instead of checking myself.

>
> Sometimes I feel that there is something religious about the faith in
> using oversized modules.


There is a well-founded preference for peer-reviewed solutions over
ad-hoc ones.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how do I find out how many days between 2 days? could.net@gmail.com C++ 5 11-02-2006 01:03 PM
What was that Java code analyzer link someone posted a few days ago? Alex Hunsley Java 6 03-28-2006 03:20 PM
Calculating days using Business Days for the Calendar David Stockwell Python 2 09-20-2004 09:58 PM
calculate date 4 days ago joe shaboo Perl Misc 10 05-05-2004 07:52 PM



Advertisments