Jump to content
  • Advertisement
Sign in to follow this  

Perl Weirdness

This topic is 3622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im having a very weird problem. I made some changes to some code, and now it seems to have broken parts that I have not really changed (I can compare the old and new code). 2 SQL statements return nothing when using the new datetime im fetching from another tbl, that is the only difference between the code. The datetime used in the query. When I hardcode the datetime, everything works fine. So I compare the datetime I fetch against the hardcoded datetime, it says its equal, however one works one doesn't. I compare them with eq and == catering for both strings and numerics, and perl says they are equal. I have checked for spaces in the string etc, they look exactly the same. So why does my code break when the values are supposedly the same? I also pull out the hyphens and use the hyphenless date string to pull records from other tables. The hyphenless string works fine for the other tables whether I hardcode it or not, which makes even less sense. I have tripple checked the datetime format of the tables of the queries which are returning nothing. They are hyphened dates in the correct format. In all the sql queries I am using lt and gt operators to compare dates >< and thats basically how I always compare dates, so that can't be the problem. any ideas? Im out of them.. =P

Share this post

Link to post
Share on other sites
I managed to get it working. Seems like its not treating it as a string for some reason..?? The function returns a string, so I dunno...

Here is the relevant snippet. The reason it is working is because I concat an empty string. Kinda messy and not very intuitive. Im looking for a more explicit solution.

As you can see MinDT and Purge_DateTime_Arg are being compared. Purge_DateTime_Arg will take on the MinDT value if the MinDT val is a smaller date. Both dates get their values from the JumpDate sub which returns a string.
Times are ignored, only dates matter here btw
I'm also open to suggestions about the date string validation im using in the jumpdate function? I will actually be seperating the validation and 2nd arg existance check into seperate parts. Bad date strings will return an error.

my $MinDT = JumpDate((60*60*24*20), $HBMinDT[0]);
#print "Min = |$MinDT|\n";
if ($Purge_DateTime_Arg > $MinDT)
$Purge_DateTime_Arg = $MinDT;

$Purge_DateTime_Arg = $Purge_DateTime_Arg."";
#If I don't concat an empty string, AND $MinDT is the smaller date
#Then the sql queries return nothing.

# JumpDate
# Adds or subtracts time to a datetime. Defaults to adding or subtracting to the current DT if 2nd arg not specified.
sub JumpDate
#Fetch the Variable change in time

my $DateString;
my $TimeChange = $_[0];

my $TimeInSeconds;
#Check if a date was passed as a 2nd arg
#if not, default to the current date_time
if ($_[1]=~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!)
#note that we minus one from the month string. timelocal #starts at element 0 for a month.
#It balances further down in this function by incrementing #after localtime, which
#is also 0 based
$TimeInSeconds = timelocal(0,0,0,substr($_[1],8,2),(substr($_[1],5,2)-1),substr($_[1],0,4));

$TimeInSeconds = time();

my $Timer = $TimeInSeconds + ($TimeChange);

#get the local time and read it into the variables
(my $sec,my $min,my $hr,my $day,my $month,my $year) = localtime($Timer);

#convert year to 4 digits
$year += 1900;

#increment month cause from localtime Jan=0, convert the month to 2 digits
$month ++;
if($month<10){ $month = '0'.$month; }

#convert the day, hour, minutes and seconds to 2 digits
if($day<10){ $day = '0'.$day; }
if($hr<10){ $hr = '0'.$hr; }
if($min<10){ $min = '0'.$min; }
if($sec<10){ $sec = '0'.$sec; }

$DateString = "$year-$month-$day"; #$hr:$min:$sec";

return $DateString;

Share this post

Link to post
Share on other sites
The date validation only checks that the date is in the correct format, not that the date is actually valid (February 31, for instance, would "validate"). Other than that, though, JumpDate is fine.

Are you certain that you're passing valid dates to your SQL statements? Can you post the SQL query, perhaps with connection and db structure details sanitized/removed?

Share this post

Link to post
Share on other sites

I found why the conversion to numeric was taking place

if ($Purge_DateTime_Arg > $MinDT)

The comparrison operator I was using is actually for numerics.
Strings have their own versions. gt & lt
I had forgotten about that ^_^

if ($Purge_DateTime_Arg gt $MinDT)

works fine, without concatting an empty string.

Thanks for the help

Im so rusty with perl =P

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!