Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates Manipulation 1

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
Hi experts,

I'm working on something that will only display data in the "Last xx days" (Selected by user)

Here's what I have:

FYI:
$fields[14] is the string for the field in flat file that contains the date stamp in the following format: dd/mm/yyyy

$year,$mon,$mday is the current date

Code:
if ($input{'NoDays'}) {
my ($fld_day, $fld_mon, $fld_year) = split(/\//,$fields[14]);

if (($year-$fld_year==0) && ($mon-$fld_mon==0) && ($mday-$fld_day<=$input{'NoDays'})) {
push (@found_set,[@fields]) ;
  
 }
}

So if $input{NoDays} equals 5 (which is the Last 5 days) and the current date is 1/3/2004, well it's won't work because my statement is basically limited to the current month only.

Any input would be appreciated.
 
Use Data::Calc

It will let you give it a date and then add/subtract another date or a timespan. A nice flexible tool.

 
I'm not really familiar with Date::calc. How simple is it to verify the difference between two dates ?

Any help would be really really appreciated...

Thank you in advance
 
siberian
I've installed the Date::Calc (v 5.3).

I came up with the following:
String Definition
$fields[14] = Field containing date in following format yyyy/mm/dd
$year = current year
$mon = current month
$mday = current day


Code:
if ($input{'days'}) {
	use Date::Calc qw(Delta_Days);
	my ($stmp_year, $stmp_mon, $stmp_day) = split(/\//,$fields[14]);
	@current = ($year, $mon, $mday);
	@stmp = ($stmp_year, $stmp_mon, $stmp_day);
	if (Delta_Days(@current, @stmp) <= $input{'days'}) {
	print "results here";
	}
	}

Let me know if it makes sense.
Thank you
 
The code is valid... but it doesn't work.
It ignores this line:

Code:
my ($stmp_year, $stmp_mon, $stmp_day) = split(/\//,$fields
[14]);

So the @stmp contains nothing then...

But when I remove these lines the split statement is valid meaning it detects the date in the field...

Code:
if (Delta_Days(@current, @stmp) <= $input{'days'}) {
    print "results here";
    }

So there's a conflict between my "split $fields[14]" and the "Delta_Days".

Really strange...
 
Anyone with some experience using Date::Calc could help me ????

Thank you
 
Are you positive that $fields[14] is being populated?

 
Since I added this :
Code:
if (Delta_Days(@current, @stmp) <= $input{'days'}) {
    print "results here";
    }

The $fields[14] is not getting populated anymore. This is really strange.
 
This sounds fishy. Run this code and give us the output of STDERR:

if ($input{'days'}) {
print STDERR "Fields 14 at start is $fields[14]\n" ;
use Date::Calc qw(Delta_Days);
my ($stmp_year, $stmp_mon, $stmp_day) = split(/\//,$fields[14]);
print STDERR "Fields 14 postsplit is $fields[14] - stmp is $stmp_year - $stmp_mon - $stmp_day\n" ;
print STDERR "Current date is $year - $mon - $mday\n" ;
@current = ($year, $mon, $mday);
@stmp = ($stmp_year, $stmp_mon, $stmp_day);
print STDERR "Fields 14 pre-Delta Days is $fields[14]\n" ;
print STDERR "Current is " , join(":",@current);
print STDERR "\nstmp us ", join(":",@stmp);
if (Delta_Days(@current, @stmp) <= $input{'days'}) {
print "results here";
}
}



Thanks, you are not giving us any debug so its impossible to see what the data flow is.
 
siberian
Getting an error because the $fields[14] is ignore for unkown reason.

Code:
Date::Calc::Delta_Days(): not a valid date

I've isolated the coding and created a sample of the flat file and everything works perfectly. So there must be a conflict somewhere. This is really bizarre and I'm starting to get unpatient...
 
Siberian... Thank you for the support by the way. This truly appreciated
 
Can you give me the STDERR output of that code I pasted? It will help me see teh data flow better.
 
siberian

Here you go:

Code:
Fields 14 at start is Fields 14 postsplit is - stmp is - - Current date is 2004 - 3 - 8 Fields 14 pre-Delta Days is Current is 2004:3:8 stmp us ::Date::Calc::Delta_Days(): not a valid date

Thank you for your help.
 
Why is $fields[14] set to 'Fields 14' at the beginning of the code run? That of course will fail the split and thus mess everything else up.

Before date calc ever gets its hands on it your data seems munged.

Code:
print STDERR "Fields 14 at start is $fields[14]\n" ;
yields
Code:
Fields 14 at start is Fields 14

but you are depending on $fields[14] to be split like this :
Code:
my ($stmp_year, $stmp_mon, $stmp_day) = split(/\//,$fields[14]);
Of course 'Fields 14' will not pass that split.

John
 
No... Here's the results but organized

Code:
Fields 14 at start is 
Fields 14 postsplit is - stmp is - - 
Current date is 2004 - 3 - 8 
Fields 14 pre-Delta Days is 
Current is 2004:3:8 
stmp us 
::Date::Calc::Delta_Days(): not a valid date

So $fields[14] simply equals nothing.. it's a blank...


Thank you again

 
Great, so that tells me its something outside of that code snippet you have given sinec its nothing on entry and before you even call in Date::Calc with your use.

I think its a bug farther up. $fields[14] is undefined when you get here.

 
Yeah...But it's really strange but when I remove the Delta_Days(@stmp,@current) $fields[4] becomes defined...

Oh well, time to test, test and test again....

Thank you for your time by the way
 
Sorry.. Made an error in my last post... I mean $fields[14] not $fields[4].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top