Gossamer Forum
Home : Products : DBMan : Customization :

PUZZLED BY DATE, please help :)

Quote Reply
PUZZLED BY DATE, please help :)
Greetings, everyone.

I'm experiencing some puzzling problems with my dates when the record is created only.

I've set up my dbman so that the current date appears in a text field when the record is being added. (my .cfg file has the date field as 'date' and default as '&get_date').

So far it works fine. The viewer would get the current date (14-Apr-2002, for instance) but when the record would be added, the date was changed to 14-Dec-2000!!!!!!!

I've being looking up and down the code but I just cannot figure it out.

Any suggestions or comments would be more than appreciated :)

Here's my code from my .cgi file:


Code:

sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.

my ($time1) = $_[0];
($time1) or ($time1 = time());

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}




sub date_to_unix {
# --------------------------------------------------------
# This routine takes dates in almost any format and returns the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# Unlike the original subroutine, months are indexed from 1, not 0.

my ($date) = $_[0];


my (%months) = ("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5,
"Jun" => 6,"Ju" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11,
"Dec" => 12,"Janeiro" => 1, "Fevereiro" => 2, "Março" => 3, "Abril" => 4, "Maio" => 5,
"Junho" => 6,"Julho" => 7, "Agosto" => 8, "Setembro" => 9, "Outubro" => 10, "Novembro" => 11,
"Dezembro" => 12);


my ($time);
$date =~ s/,|\.//g; # delete any commas or periods that might be in there
$date =~ s/\W/-/g; # change any separators into hyphens

if ((defined($months{lc(substr($date,2,3))})) && (length($date) == 9)) { # date format ddMmmyyyy
$day = substr($date,0,2);
$month = $months{lc(substr($date,2,3))};
$year = substr($date,-4);
}
elsif ((defined($months{lc(substr($date,1,3))})) && (length($date) == 8)) { # date format dMmmyyyy
$day = substr($date,0,1);
$month = $months{lc(substr($date,1,3))};
$year = substr($date,-4);
}
elsif ((substr($date,4,2) > 12) && (length($date) == 8)) { # date format ddmmyyyy
$year = substr($date,4,4);
$month = substr($date,2,2);
$day = substr($date,0,2);
}
elsif (($date > 19000000) && (length($date) == 8)) { # date format yyyymmdd
$year = substr($date,0,4);
$month = substr($date,4,2);
$day = substr($date,6,2);
}
else {
$date = lc($date);
@date_part = split /-/,$date;
$year = $date_part[2];
$year = int($year);
if ($year<100) {
if ($year<20) {
$year += 2000;
}
else {
$year += 1900;
}
}
if (defined($months{$date_part[1]})) { #date format dd-Mmm-yyyy
$day = $date_part[0];
$month = $months{$date_part[1]};
}
elsif (defined($months{$date_part[0]})) { #date format Mmm-dd-yyyy
$day = $date_part[1];
$month = $months{$date_part[0]};
}
elsif ($date_part[0] > 12) { #date format dd-mm-yyyy
$day = $date_part[0];
$month=$date_part[1];
}
elsif ($date_part[1] > 12) { #date format mm-dd-yyyy
$day = $date_part[1];
$month=$date_part[0];
}
elsif ($american_dates) { #ambiguous date -- American format
$day = int($date_part[1]);
$month=int($date_part[0]);
}
else { #ambiguous date -- the rest of the world
$day = int($date_part[0]);
$month=int($date_part[1]);
}
}
unless ($day and $month and $year) { return undef; }
($day < 10) and ($day = "0" . int($day));
$year = int($year);
($month < 10) and ($month="0" . int($month));
$time= $year . $month . $day;
if ($time < 10000101) {
return undef;
}

return ($time);
}

# These are the sorting functions used in &query.
# --------------------------------------------------------
sub alpha_ascend { lc($sortby{$a}) cmp lc ($sortby{$b}) }
sub alpha_descend { lc($sortby{$b}) cmp lc ($sortby{$a}) }
sub numer_ascend { $sortby{$a} <=> $sortby{$b} }
sub numer_descend { $sortby{$b} <=> $sortby{$a} }
sub date_ascend { &date_to_unix($sortby{$a}) <=> &date_to_unix($sortby{$b}) }
sub date_descend { &date_to_unix($sortby{$b}) <=> &date_to_unix($sortby{$a}) }














sub get_computed_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yyyy".
# If you have changed your date format in sub get_date, you should also change it here.

my ($time) = $_[0];

$year = substr($time,0,4);
$mon = substr($time,4,2);
$day = substr($time,6,2);

unless ($day && $mon && $year) {
return undef;
}
--$mon;
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;


# return "$day"." - "."$months[$mon]"." - "."$year";
return "$day-$months[$mon]-$year";
}

Once again, many, many thanks ;)

L
Quote Reply
Re: [lubatico] PUZZLED BY DATE, please help :) In reply to
What do you mean "the viewer gets the current date"? Which "viewer"? Do you have the preview mod installed and do you mean that you see the current date in the preview, but that some other date is then written to the file?
kellner
Quote Reply
Re: [kellner] PUZZLED BY DATE, please help :) In reply to
I would think that the months should only be displayed in one language. You have double values specified??

my (%months) = ("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6,"Ju" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12,"Janeiro" => 1, "Fevereiro" => 2, "Março" => 3, "Abril" => 4, "Maio" => 5,
"Junho" => 6,"Julho" => 7, "Agosto" => 8, "Setembro" => 9, "Outubro" => 10, "Novembro" => 11, "Dezembro" => 12);

In your .cfg file are you putting &get_date in single quotes or like this:

'Date' => [ 2,'date',10,15,0,&get_date(),''],

The example above is how it should be without the single quotes.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/

Last edited by:

LoisC: Apr 14, 2002, 5:11 PM
Quote Reply
Re: [LoisC] PUZZLED BY DATE, please help :) In reply to
HI and thanks for your suggestions.

I changed the .cfg file to what you suggested and reverted the names all back to english (to avoid any compatability problems), but it still did not work.

I think I may have not explained the problem correctly. Here it goes again:

1) When I try to create a record, some info is appears already filled out in the text fields to make it easeier to use. The "date" field (reflecting the date submitted is one). So far, so good. The date appears fine (today, for instance, would show up correctly as 17-April-2002

2) When I actually submit the record into the database I get a confirmation message ("Thanks. This is what you submitted...") but the date it show is wrong! It shows (in this particular case) 17-Dec-2002.

If I chose to "modify" the record and submit again with the correct date, the record takes that and is modified successfully. What is going on with the first submission, though?

Once again, thank you for all your help :)

L
Quote Reply
Re: [lubatico] PUZZLED BY DATE, please help :) In reply to
That is very strange, I wish I knew what to suggest.

I searched the forum looking for threads related to the mod and couldn't find anything with having it return the wrong date. I'm wondering if something else could possibly be causing it to do this? Are you using the latest version from the Resource Center.

When i suggested removing the extra month definitions I was looking at an old copy on my computer and didn't realize that is what was displayed with the 5-Apr-2001 version.

I did notice in sub date_to_unix that the months are defined in all lower case, not sure if that would make a difference.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [lubatico] PUZZLED BY DATE, please help :) In reply to
I reckon you use the preview mod. Look at the subs preview_record and html_preview in db.cgi and html.pl and check whether any date calculations are done there before showing the record.

That you had two sets of month names in your sub get_date shouldn't really be a problem, I guess, as long as the names of the months ("Jan", "Janeiro", etc.) are unique and don't occur twice in the declaration.
kellner