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:
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
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