A Complete Server Scriptlet

At this point let's build an example of Server Scriptlet and discover how to make use of it in various development environments like Visual Basic and Delphi 3. Server Scriptlets cannot have a user interface. We need to create one that can provide services to other modules. We have, therefore, chosen to create a Server Scriptlet that works as a Date Calculator. It exposes four methods and a property.

Function Description
DateAsDDMMYYYY() Returns the current date formatted as dd/mm/yyyy. The separator is the character stored in the Separator property.
Add( dateString, days ) Adds the specified day to a given date. The date is specified through a valid string. The return value is always a string of the form Month day, year.
Diff( dateString1, dateString2 ) Calculates the difference in days between the two dates expressed in strings. If the second date is greater than the first returns –1.
IsGreaterThan( dateString1, dateString2 ) Returns a boolean value denoting whether the first date string is greater than the second one.
Separator It's a property that denotes the separator character to be used for the date returned by DateAsDDMMYYYY().

The scriptlet exposes methods to calculate the difference in days between two dates and also the specified number of days to a given date. The dates are expressed through strings. All the formats recognized by the JScript's Date object are valid. For example, September 23, 1995 or the number of milliseconds since the start date of 1/1/1970. Check out Instant Javascript by Nigel McFarlane ISBN 1-861001-27-4 for more details.

In addition, we have added a method to compare two date strings and another one to return the current date formatted as dd/mm/yyyy where we can set the separator through a property.

The source code for this server is in the file datecalc.sct. I created the CLSID using guidgen.exe (we talked about it earlier).

<SCRIPTLET>

<REGISTRATION 
  ProgID = "DateCalculator.Scriptlet"
  Version = "1.00"
  Description = "Date Calculator"
  ClassID = "{70AE1C41-99A4-11d1-83D1-F46705C10000}"
>
</REGISTRATION>

<IMPLEMENTS ID=Automation TYPE=Automation>
    <METHOD name="Diff" internalname="DoDiff" />
    <METHOD name="Add" />
    <METHOD name="IsGreaterThan" />
    <METHOD name="DateAsDDMMYYYY" />
    <PROPERTY name="Separator" internalname="mSep" />
</IMPLEMENTS>

<SCRIPT language=JavaScript>

var mSep = "/";
var g_msPerDay = 1000*60*60*24;
var g_aMonths = new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );

// Add method (Month day, year)
function Add( sDate, nDays )  {

  d1 = new Date( sDate );   
  ms1 = d1.getTime();
  ms2 = nDays * g_msPerDay;
    
  d2 = new Date( ms1 + ms2 );
  s = g_aMonths[d2.getMonth()] + " " + d2.getDate() + ", ";

  nYear = d2.getYear();
  if( nYear < 2000 )
    nYear += 1900;

  return  s + nYear;
}

// Diff method
function DoDiff( sDate1, sDate2 )  {
  
  d1 = new Date( sDate1 );   
  d2 = new Date( sDate2 );   
  ms1 = d1.getTime();
  ms2 = d2.getTime();  
  
  if( ms2 > ms1 )
    return -1;

  ms = (ms1 - ms2);
  nDays = ms/g_msPerDay;
  x = parseInt( nDays, 10 );
  if( nDays > x ) 
    nDays = x+1;

  return nDays;
}

// IsGreaterThan method
function IsGreaterThan( sDate1, sDate2 ) {

  d1 = new Date( sDate1 );   
  d2 = new Date( sDate2 );   

  ms1 = d1.getTime();
  ms2 = d2.getTime();  
  
  return (ms1 > ms2);
}

// DateAsDDMMYYYY method
function DateAsDDMMYYYY() {

  d = new Date(); 
  nMonth = 1 + d.getMonth();
  nYear = d.getYear();
  if( nYear < 2000 )
    nYear += 1900;

  return d.getDate() + mSep + nMonth + mSep + nYear;
}
</SCRIPT>

</SCRIPTLET>

What this code does has been explained in the table above, so we aren't going to detail it here.

Once we finish writing a Server Scriptlet we are still far from testing. There is one significant step to take before we can check our work—registering the scriptlet. This registration allows any COM-aware development tool to retrieve and use our server. We can't test this scriptlet until we register it.

The built-in JScript date object already supports the year 2000. In fact if you add a number of days so that it exceeds Dec 31, 99 then the year returned is 2000 and not 99 or 98 as can happen.

© 1997 by Wrox Press. All rights reserved.