For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2007 > Calculate date









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Calculate date
Shan

2007-01-31, 6:59 pm

I need help calculting a dta.

My data is in the following format

10
89
17
10


The above means 10 days ago, 89 days ago, 17 days ago, etc from
01.16/2007. I need to claculate the exact dates.

so 10 should be conversted to 01/06/2007 and so forth.

I appreciate your help. Thank you.

Paul Lalli

2007-01-31, 6:59 pm

On Jan 31, 10:18 am, "Shan" <Shani...@gmail.com> wrote:
> I need help calculting a dta.
>
> My data is in the following format
>
> 10
> 89
> 17
> 10
>
> The above means 10 days ago, 89 days ago, 17 days ago, etc from
> 01.16/2007. I need to claculate the exact dates.
>
> so 10 should be conversted to 01/06/2007 and so forth.


The easiest way is going to be with Date::Calc's Add_Delta_Days
function:
$ perl -MDate::Calc=Add_Delta_Days -wle'
for my $delta (10, 89, 17) {
my ($y, $m, $d) = Add_Delta_Days(2007, 1, 16, -$delta);
print "1/16/2007 - $delta days = ", join "/", $m, $d, $y;
}
'
1/16/2007 - 10 days = 1/6/2007
1/16/2007 - 89 days = 10/19/2006
1/16/2007 - 17 days = 12/30/2006

If you have an aversion to installing the module for some reason,
you'll have to do the calculations manually: use Time::Local's
timelocal() function to get a timestamp for 1/16/2007, then subtract
from that the number of seconds that are in your given number of days,
then use the builtin localtime() to go from that newtime stamp back to
a series of seconds, minutes, hours, days, months, years, making sure
you take into account the odd values (year is actually year-1900,
month is actually month-1), not to mention leap-year problems....

Date::Calc takes care of all of the above for you. It's far easier.

Paul Lalli

Shan

2007-01-31, 6:59 pm

Thank you taht was very helpful

Purl Gurl

2007-01-31, 6:59 pm

Shan wrote:

> I need help calculting a dta.


"calculating" "date"

> My data is in the following format


> 10
> 89
> 17
> 10



> The above means 10 days ago, 89 days ago, 17 days ago, etc from
> 01.16/2007. I need to claculate the exact dates.


"01/16/2007" "calculate"

You have failed to define "day" for usage. A presumption is
this is a time period measured from midnight to midnight over
a period of twenty-four hours.

Simple arithmetic. Convert your 01/16/2007 date to epoch seconds.
Subtract your "days back" in epoch seconds. Use your resultant
epoch seconds to print a date.

Base Date minus Difference equals Prior Date (all in epoch seconds)

#!perl

use Time::Local;

$base_day_epoch = timelocal (0, 0, 0, 16, 0, 2007);

@Previous_Days = qw (10 89 17 10);

for (@Previous_Days)
{
$difference_date_epoch = ($_ * 86400);
print "$_ days back from 1-16-2007 is: ", scalar localtime ($base_day_epoch - $difference_date_epoch), "\n";
}


PRINTED RESULTS:

10 days back from 1-16-2007 is: Sat Jan 6 00:00:00 2007
89 days back from 1-16-2007 is: Thu Oct 19 01:00:00 2006 (note hour oddity)
17 days back from 1-16-2007 is: Sat Dec 30 00:00:00 2006
10 days back from 1-16-2007 is: Sat Jan 6 00:00:00 2007


My method above will not work correctly in early 2038 year.
However, in thirty years, you will not care. Additionally,
this will fail for dates prior to late 1901 year. I tend
to doubt this will be a problem.

Research and reading, which you should have already performed,
will help you to understand components of my example and will
help you to learn how to modify my example to meet your needs.

Purl Gurl

nobull67@gmail.com

2007-01-31, 6:59 pm

On Jan 31, 4:27 pm, Purl Gurl <purlg...@purlgurl.net> wrote:
> Shan wrote:
>
> "calculating" "date"


http://www.google.com/search?q=%22spelling+flames

>
> You have failed to define "day" for usage. A presumption is
> this is a time period measured from midnight to midnight over
> a period of twenty-four hours.


This presumption is, of course, one of the classic newbie programmer
mistakes that us veterans have seen repeated over and over.

In fact, in most parts of the world, there's one day in the year
that's 23 hours and one that's 25 hours. In some places ISTR there are
(were?) more than one of each.

> Simple arithmetic. Convert your 01/16/2007 date to epoch seconds.
> Subtract your "days back" in epoch seconds. Use your resultant
> epoch seconds to print a date.


> use Time::Local;
>
> $base_day_epoch = timelocal (0, 0, 0, 16, 0, 2007);


To avoid nasty surprises make the 3rd 0 be 12 (i.e. midday).

Alternatively use timegm() and gmtime() rather than timelocal() and
localtime() since in UCT the days are all the same length. Leap
seconds can be ignored because they are blurred out in the normal
definition of epoch time.

Purl Gurl

2007-01-31, 6:59 pm



nobull67@gmail.com wrote:

> Purl Gurl wrote:
[color=darkred]
[color=darkred]
[color=darkred]
> http://www.google.com/search?q=%22spelling+flames


[color=darkred]
> This presumption is, of course, one of the classic newbie programmer
> mistakes that us veterans have seen repeated over and over.


"we veterans"

Ha! You are such an obvious troll!

Troll, troll, troll, this is all you do and this
is all you are good for!

Purl Gurl

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com