Gossamer Forum
Home : Products : DBMan : Customization :

Auto Incrementing Dates

Quote Reply
Auto Incrementing Dates
Help!

I am trying to implement "Due Date" fields in my database that are automatically set to be 15 days after a user entered date "Start" date. The "Start" dates may not be entered at the time the record is created but may be entered later on during record modification.

I assume I need to write a function that turns the "Start" date to UNIX format then adds 1296000s (15 days), then reconverts the UNIX date to a real date, and finally enters the result in the proper field. This function needs to be called by both &add_record and &modify_record. I have tried the following, but they don't seem to work:

ADDITION TO &add_record
# Check to see if a Date_Sent Field is filled in and then Calculate Corresponding Due Date
if ($db_Date_Sent_R1) {$db_Date_Due_R1 = &due_date_calc($db_Date_Sent_R1);}
if ($db_Date_Sent_R2) {$db_Date_Due_R2 = &due_date_calc($db_Date_Sent_R2);}

OR

if ($in{7}) {$in{9} = &due_date_calc($in{7});}
if ($in{8}) {$in{10} = &due_date_calc($in{8});}

THE SUBROUTINE
sub due_date_calc {
# --------------------------------------------------------
# This routine adds 15 days a date.
my ($date) = $_[0];
my (%months) = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10,"Dec" => 11);
my ($time);
my ($day, $mon, $year) = split(/-/, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }

use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
$time = $time + 1296000;};
if ($@) { return undef; } # Could return 0 if you want.
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
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";
}


Please help this PERL neophyte!

Sincerely,
Lauren Stegman
Quote Reply
Re: Auto Incrementing Dates In reply to
Hi Lauren,

You could try:

if ($in{'StartDate'}) {
$in{'EndDate'} = &get_date ( &date_to_unix ($in{'StartDate'}) + 1296000);
}

which would set StartDate to 15 days later.

Depending on where you insert the code, you might need to alter that slightly. It should work as is for modifying by adding it in just before:

$output .= &join_encode(%in);

Cheers,

Alex

[This message has been edited by Alex (edited January 19, 1999).]
Quote Reply
Re: Auto Incrementing Dates In reply to
Alex,

Thanks for your prompt reply. The elegant if statement you suggested does add a date to the End_Date field, however it enters the CURRENT date. In other words is appears that the addition function is not executed and that $get_date current clock time not the value of the Start_Date field.

I placed this code:
# Add Date_Due fields if Date_Sent fields are specified.
if ($in{'Date_Sent_R1'}) {$in{'Date_Due_R1'} = &get_date(&date_to_unix($in{'Date_Sent_R1'})+1296000);}
if ($in{'Date_Sent_R2'}) {$in{'Date_Due_R2'} = &get_date(&date_to_unix($in{'Date_Sent_R2'})+1296000);}

just before the:
$output .= &join_encode(%in);
statement in the modify routine and just before: $status = &validate_record;
in the add record routine.

Any ideas?

Lauren
Quote Reply
Re: Auto Incrementing Dates In reply to
Alex,

I think the problem is that &get_date sets
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());.

I made sub get_date2 which simply declares:
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight);

Now however, the script always returns 0-Jan-1900.

What gives?

Lauren
Quote Reply
Re: Auto Incrementing Dates In reply to
Hmm, I thought I had updated that, sorry! My &get_date looks like:

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 $time = $_[0] &#0124; &#0124; time();
my ($day, $mon, $year) = (localtime($time))[3,4,5];
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";
}

Let me know how that works out!

Cheers,

Alex
Quote Reply
Re: Auto Incrementing Dates In reply to
Alex,

I made the fix last night, but didn't have a chance to let you know that it works like a charm!

As long as we are on the subject of auto-incrementing, I want the counter to keep track of my $db_key which has the format
"YY-###" eg "99-127" for the 127th record made in the year 1999. I still kinda stink at pattern matching, so I was wondering if you could help me alter the counter increment statement to only match the last 3 digits and increment them by 3 lets say. In other words, if the default.count file starts with 99-127 then it would add record 99-130.

Thanks alot,
Lauren

P.S. Any chance that future versions will support relations between databases.
Quote Reply
Re: Auto Incrementing Dates In reply to
For anyone who might be interested, I figured out how to auto-increment my strange hyphenated $db_key.

My db_keys are in the format 99-XXX and they must increment by 3.

To autoincrement the key in this fashion I changed sub get_defaults as follows:
Code:
sub get_defaults {
# --------------------------------------------------------
# Returns a hash of the defaults used for a new record.

my (%default);

foreach $field (keys %db_defaults) {
$default{$field} = $db_defaults{$field};
}

if ($db_key_track) {
open (ID, "<$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) { flock(ID, 1); }
$count = substr(<ID>,3)+3;
$default{$db_key} = "99-$count"; # Get next ID number
close ID;
}
return %default;
}

Sincerely,
Lauren


Quote Reply
Re: Auto Incrementing Dates In reply to
Getting even more sophisticated, the "YY-###" db_key format can be updated so that the correct 2-digit year is automatically entered and so that the ### resets to 100 when the year changes so that the first record of each new year starts with YY-100.

Code:
sub get_defaults {
# --------------------------------------------------------
# Returns a hash of the defaults used for a new record.

my (%default, $count, $prefix, $test);

foreach $field (keys %db_defaults) {
$default{$field} = $db_defaults{$field};
}

if ($db_key_track) {
open (ID, "<$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) { flock(ID, 1); }
($prefix, $count) = unpack("A2 x1 A*", <ID> );
$count=$count+3;
$test = substr(&get_date,-2);
if ($test ne $prefix) {$count="100" and $prefix=$test};
$default{$db_key} = "$prefix-$count"; # Get next ID number
close ID;
}
return %default;
}


Note that I use this for my userid's. Thus all of the statements that pattern match for acceptable userids in auth.pl and db.cgi must be changed to the following:

Code:
$userid =~ /^([A-Za-z0-9\/\-]+)\.\d+$/

Lauren
Quote Reply
Re: Auto Incrementing Dates In reply to
I just wanted to thank Lauren for asking this question and Alex for answering it. It really pays to read things that don't apply at the moment, because you never know when they will!

I was working a really convoluted way of figuring out how old files were. But with this, it's a breeze! Smile

Thanks again, to both of you.



------------------
JPD