HP3000-L Archives

March 1996, Week 2

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Doug Myers <[log in to unmask]>
Reply To:
Doug Myers <[log in to unmask]>
Date:
Tue, 12 Mar 1996 20:10:09 GMT
Content-Type:
text/plain
Parts/Attachments:
text/plain (1135 lines)
Attached is an article that I wrote on how to use the new Date/Time API that
was added in ALLBASE/SQL A.G1.15 which is now available as a beta patch.  This
of course also applied to IMAGE/SQL.  The article is rather long because it
includes a sample program written in COBOL, C, Pascal and Fortran.
The following article is also on the web under
'http://jazz.external.hp.com/papers/Communicator/dbG1/dtapi/dtapi.html'.
 
Doug
 
----------------------------------------------------------------------------
Doug Myers
Hewlett-Packard Company (MS 47UX)                  e-mail:  [log in to unmask]
19111 Pruneridge Avenue                            phone:   (408) 447-6756
Cupertino, CA  USA  95014-9974                     FAX:     (408) 447-7902
----------------------------------------------------------------------------
 
Date/Time API
BY: Doug Myers, HP Database Lab
===============================
 
One of the requests that came out of INTEREX '95 was to allow conversion
between ALLBASE/SQL date/time values programmatically.  This request was
mainly being pushed by IMAGE/SQL customers who are taking advantage of data
type mapping allowed in the IMAGESQL utility to map X16 fields to one of the
ALLBASE/SQL date/time data types.  By doing this, dates, times, and intervals
can be entered through the SQL interface and stored directly in an IMAGE
database in the internal format of the ALLBASE/SQL date/time data types.  This
is particularly useful in a PC client/server environment where many of the PC
tools have date and time data types which map directly to the ALLBASE/SQL
date/time data types through the PC API layers supported with IMAGE/SQL and
ALLBASE/SQL.  With this new functionality, customers now have the desire to be
able to read and write data from these fields in their IMAGE database using
the Turbo IMAGE intrinsic interface.  Since these fields store data in the
internal ALLBASE/SQL date/time format, a way of converting this data into a
readable character string was needed.  Also character data needs to be
converted to the ALLBASE/SQL date/time format before it can be inserted into
the IMAGE database using the Turbo IMAGE intrinsics.
 
In response to this request, two approaches were considered.
 
1.  Fully describe the internal ALLBASE/SQL date/time data type format, and
    have the customers write there own routines to convert data to and from
    this internal format.
 
2.  Provide callable routines which will handle the conversion.
 
The second approach seemed more desirable from both a customer perspective
(less work for the customer) and from an HP perspective (easier to support).
Therefore, we have reworked the conversion routines which we use internally to
perform the ALLBASE/SQL date/time functions (TO_DATE, TO_TIME, TO_DATETIME,
TO_INTERVAL, TO_CHAR, TO_INTEGER) and made them externally callable.  The
collection of these routines will be referred to as the Date/Time API
(Application Programming Interface).  This article describes the Date/Time API
and how to use it.  The Date/Time API is available in the G1.15 release of
ALLBASE/SQL.
 
As a side note, one of the other requests from Interex was to allow K8 to be
mapped to the ALLBASE date/time data types, in addition to X16.  K8 is more
desirable than X16 since Query will not try to print out K8 as it does X16.
Trying to print out the date/time data as characters, as does Query with X16,
can cause terminals to hang.  This change was made in IMAGE/SQL version
B.G1.10.
 
There is a one-to-one mapping between the Date/Time API routines and the
ALLBASE/SQL date/time functions.
 
Date/Time API routine           ALLBASE/SQL date/time function
---------------------           ------------------------------
 
DBTODATE                        TO_DATE
DBTOTIME                        TO_TIME
DBTODTTM                        TO_DATETIME
DBTOITVL                        TO_INTERVAL
DBTOCHAR                        TO_CHAR
DBTOINT                         TO_INTEGER
 
The functionality is equivalent.  In fact, these are the routines used
internally in the ALLBASE/SQL and IMAGE/SQL products to perform these
date/time functions.  You should refer to the 'Date/Time Functions' discussion
in the 'Expressions' chapter of the 'ALLBASE/SQL Reference Manual' for a
complete description of the ALLBASE/SQL date/time functions.
 
The following is a description of all the parameters for each of the
Date/Time API routines.
 
 
DBTODATE (CHARVAL, STRINGLEN, FORMAT, FMTLEN, DATEVAL, ERROR)
 
Field Name      Date Type               Description
----------      --------------          -------------------------------------
 
CHARVAL         4 byte address          Address of an array of characters
                                        holding the character representation
                                        of a date.
 
STRINGLEN       4 byte integer          Length of CHARVAL in bytes.
 
FORMAT          4 byte address          Address of an array of characters
                                        holding the format specification of
                                        the input character string, CHARVAL.
                                        Only valid format specifications for
                                        the TO_DATE function are allowed.  See
                                        the ALLBASE/SQL Reference Manual for
                                        details on allowed format
                                        specifications.
 
FMTLEN          4 byte integer          Length of FORMAT in bytes
 
DATEVAL         4 byte integer          Address of a 16 byte buffer in which
                                        the resulting date in ALLBASE/SQL
                                        internal format is stored.
 
ERROR           4 byte address          Address of a 4 byte integer where
                                        error code is returned.  ERROR is set
                                        to 0 if no error occurred.  Otherwise,
                                        it is set to the DBERR code for the
                                        error returned.  Refer to the
                                        ALLBASE/SQL Message Manual for details
                                        on a particular DBERR code.
 
 
DBTOTIME (CHARVAL, STRINGLEN, FORMAT, FMTLEN, TIMEVAL, ERROR)
 
Field Name      Data Type               Description
----------      --------------          -------------------------------------
 
CHARVAL         4 byte address          Address of an array of characters
                                        holding the character representation
                                        of a time.
 
STRINGLEN       4 byte integer          Length of CHARVAL in bytes.
 
FORMAT          4 byte address          Address of an array of characters
                                        holding the format specification of
                                        the input character string, CHARVAL.
                                        Only valid format specifications for
                                        the TO_TIME function are allowed.  See
                                        the ALLBASE/SQL Reference Manual for
                                        details on allowed format
                                        specifications.
 
FMTLEN          4 byte integer          Length of FORMAT in bytes
 
TIMEVAL         4 byte integer          Address of a 16 byte buffer in which
                                        the resulting time in ALLBASE/SQL
                                        internal format is stored.
 
ERROR           4 byte address          Address of a 4 byte integer where
                                        error code is returned.  ERROR is set
                                        to 0 if no error occurred.  Otherwise,
                                        it is set to the DBERR code for the
                                        error returned.  Refer to the
                                        ALLBASE/SQL Message Manual for details
                                        on a particular DBERR code.
 
 
DBTODTTM (CHARVAL, STRINGLEN, FORMAT, FMTLEN, DTTMAL, ERROR)
 
Field Name      Data Type               Description
----------      --------------          -------------------------------------
 
CHARVAL         4 byte address          Address of an array of characters
                                        holding the character representation
                                        of a date-time value.
 
STRINGLEN       4 byte integer          Length of CHARVAL in bytes.
 
FORMAT          4 byte address          Address of an array of characters
                                        holding the format specification of
                                        the input character string, CHARVAL.
                                        Only valid format specifications for
                                        the TO_DATETIME function are allowed.
                                        See the ALLBASE/SQL Reference Manual
                                        for details on allowed format
                                        specifications.
 
FMTLEN          4 byte integer          Length of FORMAT in bytes
 
DTTMVAL         4 byte integer          Address of a 16 byte buffer in which
                                        the resulting date-time in ALLBASE/SQL
                                        internal format is stored.
 
ERROR           4 byte address          Address of a 4 byte integer where
                                        error code is returned.  ERROR is set
                                        to 0 if no error occurred.  Otherwise,
                                        it is set to the DBERR code for the
                                        error returned.  Refer to the
                                        ALLBASE/SQL Message Manual for details
                                        on a particular DBERR code.
 
 
DBTOITVL (CHARVAL, STRINGLEN, FORMAT, FMTLEN, ITVLVAL, ERROR)
 
Field Name      Data Type               Description
----------      --------------          -------------------------------------
 
CHARVAL         4 byte address          Address of an array of characters
                                        holding the character representation
                                        of a interval value.
 
STRINGLEN       4 byte integer          Length of CHARVAL in bytes.
 
FORMAT          4 byte address          Address of an array of characters
                                        holding the format specification of
                                        the input character string, CHARVAL.
                                        Only valid format specifications for
                                        the TO_INTERVAL function are allowed.
                                        See the ALLBASE/SQL Reference Manual
                                        for details on allowed format
                                        specifications.
 
FMTLEN          4 byte integer          Length of FORMAT in bytes
 
ITVLVAL         4 byte integer          Address of a 16 byte buffer in which
                                        the resulting internal in ALLBASE/SQL
                                        internal format is stored.
 
ERROR           4 byte address          Address of a 4 byte integer where
                                        error code is returned.  ERROR is set
                                        to 0 if no error occurred.  Otherwise,
                                        it is set to the DBERR code for the
                                        error returned.  Refer to the
                                        ALLBASE/SQL Message Manual for details
                                        on a particular DBERR code.
 
 
DBTOCHAR(DATEVAL, DATATYPE, FORMAT, FMTLEN, CHARVAL, BUFFLEN, ERROR)
 
Field Name      Date Type               Description
----------      --------------          -------------------------------------
 
DATEVAL         4 byte address          Address of the 16 byte date, time,
                                        datetime, or interval value stored in
                                        the ALLBASE/SQL date/time format to be
                                        converted.
 
DATATYPE        4 byte integer          Data type of the input, DATEVAL.
                                        It must be set one of the following
                                        values:
                                           10 - date
                                           11 - time
                                           12 - datetime
                                           13 - interval
 
FORMAT          4 byte address          Address of an array of characters
                                        holding the format specification of
                                        the desired format for the character
                                        string result.  Only valid format
                                        specifications for the TO_CHAR
                                        function are allowed.  See the
                                        ALLBASE/SQL Reference Manual for
                                        details on allowed format
                                        specifications.
 
FMTLEN          4 byte integer          Length of FORMAT in bytes
 
CHARVAL         4 byte address          Address of a character buffer to put
                                        result.  This routine will fill this
                                        buffer with the character string
                                        representation of the date/time value,
                                        blank-filling to the end of the buffer
                                        as indicated by the length.  If the
                                        character string representation for
                                        the date/time value is longer than the
                                        specified length of the buffer, the
                                        character string will be truncated to
                                        specified length.
 
BUFFLEN         4 byte integer          Length of CHARVAL buffer in bytes
 
ERROR           4 byte address          Address of a 4 byte integer where
                                        error code is returned.  ERROR is set
                                        to 0 if no error occurred.  Otherwise,
                                        it is set to the DBERR code for the
                                        error returned.  Refer to the
                                        ALLBASE/SQL Message Manual for details
                                        on a particular DBERR code.
 
 
DBTOINT(DATEVAL, DATATYPE, FORMAT, FMTLEN, INTVAL, ERROR)
 
Field Name      Date Type               Description
----------      --------------          -------------------------------------
 
DATEVAL         4 byte address          Address of the 16 byte date, time,
                                        datetime, or interval value stored in
                                        the ALLBASE/SQL date/time format to be
                                        converted.
 
DATATYPE        4 byte integer          Data type of the input, DATEVAL.
                                        It must be set one of the following
                                        values:
                                           10 - date
                                           11 - time
                                           12 - datetime
                                           13 - interval
 
FORMAT          4 byte address          Address of an array of characters
                                        holding the format specification
                                        specifying which component (month,
                                        day, hour, minute, etc.)  of the
                                        input, DATEVAL, should be converted to
                                        an integer.  Only valid format
                                        specifications for the TO_INTEGER
                                        function are allowed.  See the
                                        ALLBASE/SQL Reference Manual for
                                        details on TO_INTEGER and the allowed
                                        format specifications.
 
FMTLEN          4 byte integer          Length of FORMAT in bytes
 
INTVAL          4 byte address          Address of the 4 byte buffer where
                                        the integer result gets stored.
 
ERROR           4 byte address          Address of a 4 byte integer where
                                        error code is returned.  ERROR is set
                                        to 0 if no error occurred.  Otherwise,
                                        it is set to the DBERR code for the
                                        error returned.  Refer to the
                                        ALLBASE/SQL Message Manual for details
                                        on a particular DBERR code.
 
 
Sample programs
---------------
 
Attached is a sample program written in four different languages:  C, Pascal,
FORTRAN, and COBOL.  This program call each of the Date/Time API routines at
least once.  For C and Pascal it is fairly straight forward.  However, there
is currently a linking problem which exists when trying to link directly to
the Date/Time API routines from FORTRAN, and COBOL.  In order to work around
this problem, the FORTRAN and COBOL programs make calls through a very thin
layer of C code or Pascal code.  I have also included the code for this thin
layer for both C and Pascal.  This problem should be fixed in the next
available patch G1.16.
 
 
C
-----
 
#include <stdio.h>
 
/* Program cex14 */
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* This program illustrates the use the Date/Time API used to      */
/* programmatically perform the data/time functions outside SQL.   */
/* Since this program contains no SQL, it does not need to be      */
/* preprocessed.                                                   */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
#define DATE 10
#define TIME 11
#define DATETIME 12
#define INTERVAL 13
#define MAX_FORMAT_LEN 72  /* The maximum length of a character    */
                           /* string representation of date/time   */
                           /* value is 72 bytes                    */
 
main()
 
{
long int intval;
int error;
char charval[40];
char format[40];
char dtval[16];    /* This variable is being declared to hold a date/time */
                   /* value in ALLBASE internal format.  It could have    */
                   /* been declared as anything as long as it is 16 bytes.*/
 
/* convert date string to date */
strcpy(charval,"08/18/62");
strcpy(format, "MM/DD/YY");
dbtodate(charval, strlen(charval), format, strlen(format), dtval, &error);
if (error == 0)
  {
  /* convert date back to date string */
  strcpy(format, "Month DD, YYYY");
  dbtochar(dtval, DATE, format, strlen(format),
           charval, sizeof(charval)-1, &error);
  if (error == 0)
    {
    charval[sizeof(charval)-1] = '\0';  /* have to put ascii 0 at the end */
    printf("%s\n", charval);
    }
  else printf("Error %d on dbtochar\n", error);
  }
else printf("Error %d on dbtodate\n", error);
 
/* convert time string to time */
strcpy(charval, "16:10:00");
strcpy(format, "HH:MI:SS");
dbtotime(charval, strlen(charval), format, strlen(format), dtval, &error);
if (error == 0)
  {
  /* convert time back to time string */
  strcpy(format, "ZHH12:MI AM");
  dbtochar(dtval, TIME, format, strlen(format),
           charval, sizeof(charval)-1, &error);
  if (error == 0)
    {
    charval[sizeof(charval)-1] = '\0';  /* have to put ascii 0 at the end */
    printf("%s\n", charval);
    }
  else printf("Error %d on dbtochar\n", error);
  }
else printf("Error %d on dbtotime\n", error);
 
/* convert date-time string to date-time */
strcpy(charval, "02/06/95 01:28:48.500");
strcpy(format, "MM/DD/YY HH:MI:SS.FFF");
dbtodttm(charval, strlen(charval), format, strlen(format), dtval, &error);
if (error == 0)
  {
  /* convert date-time back to date-time string */
  strcpy(format, "DAY  MON ZDD, YYYY  HH:MI:SS.FFF");
  dbtochar(dtval, DATETIME, format, strlen(format),
           charval, sizeof(charval)-1, &error);
  if (error == 0)
    {
    charval[sizeof(charval)-1] = '\0';  /* have to put ascii 0 at the end */
    printf("%s\n", charval);
    }
  else printf("Error %d on dbtochar\n", error);
  }
else printf("Error %d on dbtodttm\n", error);
 
/* convert interval string to interval */
strcpy(charval, "0000080 05:20:30");
strcpy(format, "DAYS HH:MI:SS");
dbtoitvl(charval, strlen(charval), format, strlen(format), dtval, &error);
if (error == 0)
  {
  /* convert interval to integer (number of days ) */
  strcpy(format, "DAYS");
  dbtoint(dtval, INTERVAL, format, strlen(format),
           &intval, &error);
  if (error == 0)
    {
    printf("%d days\n", intval);
    }
  else printf("Error %d on dbtoint\n", error);
  }
else printf("Error %d on dbtoitvl\n", error);
}
 
 
Pascal
------
 
 
(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)
(* This program illustrates the use the Date/Time API used to      *)
(* programmatically perform the data/time functions outside SQL.   *)
(* Since this program contains no SQL, it does not need to be      *)
(* preprocessed.                                                   *)
(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)
 
$check_actual_parm  2$
$check_formal_parm  2$
 
program pasex14(input,output);
 
const
  DATE = 10;
  TIME = 11;
  DATETIME = 12;
  INTERVAL = 13;
  MAX_FORMAT_LEN = 72;     (* The maximum length of a character    *)
                           (* string representation of date/time   *)
                           (* value is 72 bytes                    *)
 
type
  charstr = packed array [1..MAX_FORMAT_LEN] of char;
  dttype  = packed array [1..16] of char;
                           (* This type is being declared to       *)
                           (* hold a date/time value in ALLBASE    *)
                           (* internal format.  It could have      *)
                           (* been declared as anything as long    *)
                           (* as it is 16 bytes.                   *)
 
var
  intval: integer;
  error: integer;
  charval: charstr;
  format:  charstr;
  dtval: dttype;
 
(**************************************************************************)
(* In the following procedure declarations, since the parameters of       *)
(* 'charstr' type are all being passed by reference, 'charstr' could have *)
(* been declared as any length <= 72 bytes as long as the length is       *)
(* passed to the routine properly.                                        *)
(**************************************************************************)
 
(* Perform TO_CHAR datetime function.                                     *)
PROCEDURE DBTOCHAR   (var dtval    : dttype;     (* datetime input        *)
                          datatype : integer;    (* data type of input:   *)
                                                 (* 10 - date             *)
                                                 (* 11 - time             *)
                                                 (* 12 - datetime         *)
                                                 (* 13 - interval         *)
                      var format   : charstr;    (* format spec           *)
                          fmtlen   : integer;    (* length of format spec *)
                      var charval  : charstr;    (* result buffer         *)
                          bufflen  : integer;    (* result buffer length  *)
                      var error    : integer);   (* error code            *)
external;
 
(* convert char representation to date internal format                      *)
PROCEDURE DBTODATE   (var charval  : charstr;    (* input date string*)
                          valuelen : integer;    (* length of input value   *)
                      var format   : charstr;    (* format string           *)
                          fmtlen   : integer;    (* length of format string *)
                      var dateval  : dttype;     (* result date             *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(* convert char representation to datetime internal format                  *)
PROCEDURE DBTODTTM   (var charval  : charstr;    (* input date string       *)
                          valuelen : integer;    (* length of input value   *)
                      var format   : charstr;    (* format string           *)
                          fmtlen   : integer;    (* length of format string *)
                      var dttmval  : dttype;     (* result datetime         *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(* Perform TO_INTEGER datetime function.                                  *)
PROCEDURE DBTOINT    (var dateval  : dttype;     (* datetime input        *)
                          datatype : integer;    (* data type of input:   *)
                                                 (* 10 - date             *)
                                                 (* 11 - time             *)
                                                 (* 12 - datetime         *)
                                                 (* 13 - interval         *)
                      var format   : charstr;    (* format spec           *)
                          fmtlen   : integer;    (* length of format spec *)
                      var result   : integer;    (* result                *)
                      var error    : integer);
external;
 
(* convert char representation to interval internal format                  *)
PROCEDURE DBTOITVL   (var charval  : charstr;    (* input interval          *)
                                                 (* string                  *)
                          valuelen : integer;    (* length of input value   *)
                      var format   : charstr;    (* format string           *)
                          fmtlen   : integer;    (* length of format string *)
                      var itvlval  : dttype;     (* result interval         *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(* convert char representation to time internal format                      *)
PROCEDURE DBTOTIME   (var charval  : charstr;    (* input date string       *)
                          valuelen : integer;    (* length of input value   *)
                      var format   : charstr;    (* format string           *)
                          fmtlen   : integer;    (* length of format string *)
                      var timeval  : dttype;     (* result time             *)
                      var error    : integer);   (* Error code returned     *)
external;
 
 
begin
(* convert date string to date *)
charval := '08/18/62                                ';
format := 'MM/DD/YY';
dbtodate(charval, sizeof(charval), format, sizeof(format), dtval, error);
if (error = 0) then
  begin
  (* convert date back to date string *)
  format := 'Month DD, YYYY';
  dbtochar(dtval, DATE, format, sizeof(format),
           charval, sizeof(charval), error);
  if (error = 0) then
    begin
    writeln( charval);
    end
  else writeln('Error ',error:1,' on dbtochar');
  end
else writeln('Error ',error:1,' on dbtodate');
 
(* convert time string to time *)
charval := '16:10:00';
format := 'HH:MI:SS';
dbtotime(charval, sizeof(charval), format, sizeof(format), dtval, error);
if (error = 0) then
  begin
  (* convert time back to time string *)
  format := 'ZHH12:MI AM';
  dbtochar(dtval, TIME, format, sizeof(format),
           charval, sizeof(charval), error);
  if (error = 0) then
    begin
    writeln( charval);
    end
  else writeln('Error ',error:1,' on dbtochar');
  end
else writeln('Error ',error:1,' on dbtotime');
 
(* convert date-time string to date-time *)
charval := '02/06/95 01:28:48.500';
format := 'MM/DD/YY HH:MI:SS.FFF';
dbtodttm(charval, sizeof(charval), format, sizeof(format), dtval, error);
if (error = 0) then
  begin
  (* convert date-time back to date-time string *)
  format := 'DAY  MON ZDD, YYYY  HH:MI:SS.FFF';
  dbtochar(dtval, DATETIME, format, sizeof(format),
           charval, sizeof(charval), error);
  if (error = 0) then
    begin
    writeln( charval);
    end
  else writeln('Error ',error:1,' on dbtochar');
  end
else writeln('Error ',error:1,' on dbtodttm');
 
(* convert interval string to interval *)
charval := '0000080 05:20:30';
format := 'DAYS HH:MI:SS';
dbtoitvl(charval, sizeof(charval), format, sizeof(format), dtval, error);
if (error = 0) then
  begin
  (* convert interval to integer (number of days ) *)
  format := 'DAYS';
  dbtoint(dtval, INTERVAL, format, sizeof(format), intval, error);
  if (error = 0) then
    begin
    writeln(intval:1, ' days');
    end
  else writeln('Error ',error:1,' on dbtoint');
  end
else writeln('Error ',error:1,' on dbtoitvl');
end.
 
 
FORTRAN
-------
 
 
$ALIAS DBTOCHAR = 'cdbtochar' (%REF,%VAL,%REF,%VAL,%REF,%VAL,%REF)
$ALIAS DBTODATE = 'cdbtodate' (%REF,%VAL,%REF,%VAL,%REF,%REF)
$ALIAS DBTODTTM = 'cdbtodttm' (%REF,%VAL,%REF,%VAL,%REF,%REF)
$ALIAS DBTOINT  = 'cdbtoint'  (%REF,%VAL,%REF,%VAL,%REF,%REF)
$ALIAS DBTOITVL = 'cdbtoitvl' (%REF,%VAL,%REF,%VAL,%REF,%REF)
$ALIAS DBTOTIME = 'cdbtotime' (%REF,%VAL,%REF,%VAL,%REF,%REF)
C
      PROGRAM FOREX14
C
C     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
C     * This program illustrates the use the Date/Time API used to      *
C     * programmatically perform the data/time functions outside SQL.   *
C     * Since this program contains no SQL, it does not need to be      *
C     * preprocessed.                                                   *
C     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
C
      INTEGER DATE
      INTEGER TIME
      INTEGER DATETIME
      INTEGER INTERVAL
 
      INTEGER      INTVAL
      INTEGER      ERROR
      CHARACTER*72 CHARVAL
      CHARACTER*72 FMTSTR
C
C     ** This variable is being declared to **
C     ** hold a date/time value in ALLBASE  **
C     ** internal format.  It could have    **
C     ** been declared as anything as long  **
C     ** as it is 16 bytes.                 **
      CHARACTER*16 DTVAL
 
      DATE     = 10
      TIME     = 11
      DATETIME = 12
      INTERVAL = 13
 
C     ** Convert date string to date **
      CHARVAL = '08/18/62'
      FMTSTR  = 'MM/DD/YY'
      CALL DBTODATE(CHARVAL, 72, FMTSTR, 72, DTVAL, ERROR)
      IF (ERROR .EQ. 0) THEN
C       ** CONVERT DATE BACK TO DATE STRING **
        FMTSTR = 'Month DD, YYYY'
        CALL DBTOCHAR(DTVAL, DATE, FMTSTR, 72, CHARVAL, 72, ERROR)
        IF (ERROR .EQ. 0) THEN
          WRITE(6,100) CHARVAL
        ELSE
          WRITE(6,210) ERROR
        END IF
      ELSE
        WRITE(6,220) ERROR
      END IF
 
C     ** CONVERT TIME STRING TO TIME **
      CHARVAL = '16:10:00'
      FMTSTR = 'HH:MI:SS'
      CALL DBTOTIME(CHARVAL, 72, FMTSTR, 72, DTVAL, ERROR)
      IF (ERROR .EQ. 0) THEN
C       ** CONVERT TIME BACK TO TIME STRING **
        FMTSTR = 'ZHH12:MI AM'
        CALL DBTOCHAR(DTVAL, TIME, FMTSTR, 72, CHARVAL, 72, ERROR)
        IF (ERROR .EQ. 0) THEN
          WRITE(6,100) CHARVAL
        ELSE
          WRITE(6,210) ERROR
        END IF
      ELSE
        WRITE(6,230) ERROR
      END IF
 
C     ** CONVERT DATE-TIME STRING TO DATE-TIME **
      CHARVAL = '02/06/95 01:28:48.500'
      FMTSTR = 'MM/DD/YY HH:MI:SS.FFF'
      CALL DBTODTTM(CHARVAL, 72, FMTSTR, 72, DTVAL, ERROR)
      IF (ERROR .EQ. 0) THEN
C       ** CONVERT DATE-TIME BACK TO DATE-TIME STRING **
        FMTSTR = 'DAY  MON ZDD, YYYY  HH:MI:SS.FFF'
        CALL DBTOCHAR(DTVAL, DATETIME, FMTSTR, 72, CHARVAL, 72, ERROR)
        IF (ERROR .EQ. 0) THEN
          WRITE(6,100) CHARVAL
        ELSE
          WRITE(6,210) ERROR
        END IF
      ELSE
        WRITE(6,240) ERROR
      END IF
 
C     ** Convert interval string to interval **
      CHARVAL = '0000080 05:20:30'
      FMTSTR = 'DAYS HH:MI:SS'
      CALL DBTOITVL(CHARVAL, 72, FMTSTR, 72, DTVAL, ERROR)
      IF (ERROR .EQ. 0) THEN
C       ** CONVERT INTERVAL TO INTEGER (NUMBER OF DAYS ) **
        FMTSTR = 'DAYS'
        CALL DBTOINT(DTVAL, INTERVAL, FMTSTR, 72, INTVAL, ERROR)
        IF (ERROR .EQ. 0) THEN
          WRITE(6,110) INTVAL
        ELSE
          WRITE(6,250) ERROR
        END IF
      ELSE
        WRITE(6,260) ERROR
      END IF
 
100   FORMAT(' ',A72)
110   FORMAT(' ',I8,' Days')
210   FORMAT(' Error ',I6,' in DBTOCHAR')
220   FORMAT(' Error ',I6,' in DBTODATE')
230   FORMAT(' Error ',I6,' in DBTOTIME')
240   FORMAT(' Error ',I6,' in DBTODTTM')
250   FORMAT(' Error ',I6,' in DBTOINT')
260   FORMAT(' Error ',I6,' in DBTOITVL')
      END
 
 
 
 
COBOL
-----
 
 
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * This program illustrates the use the Date/Time API used to      *
      * programmatically perform the data/time functions outside SQL.   *
      * Since this program contains no SQL, it does not need to be      *
      * preprocessed.                                                   *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
 
       IDENTIFICATION DIVISION.
 
       PROGRAM-ID.             COBEX14.
 
       ENVIRONMENT DIVISION.
 
       DATA DIVISION.
 
       WORKING-STORAGE SECTION.
 
       77  DATE-CODE      PIC S9(9) COMP VALUE 10.
       77  TIME-CODE      PIC S9(9) COMP VALUE 11.
       77  DATETIME-CODE  PIC S9(9) COMP VALUE 12.
       77  INTERVAL-CODE  PIC S9(9) COMP VALUE 13.
 
       01  INTVAL         PIC S9(9) COMP.
       01  ERROR-CODE     PIC S9(9) COMP.
       01  CHARVAL        PIC X(72).
       01  FMTSTR         PIC X(72).
 
      ****************************************
      ** This variable is being declared to **
      ** hold a date/time value in ALLBASE  **
      ** internal format.  It could have    **
      ** been declared as anything as long  **
      ** as it is 16 bytes.                 **
      ****************************************
       01  DTVAL        PIC X(16).
 
       PROCEDURE DIVISION.
 
       A100-MAIN.
 
      *********************************
      ** Convert date string to date **
      *********************************
           MOVE "08/18/62" TO CHARVAL
           MOVE 'MM/DD/YY' TO FMTSTR
           CALL "cdbtodate"
             USING CHARVAL, \72\, FMTSTR, \72\, DTVAL, ERROR-CODE
           END-CALL
           IF ERROR-CODE = 0 THEN
      **************************************
      ** CONVERT DATE BACK TO DATE STRING **
      **************************************
             MOVE "Month DD, YYYY" TO FMTSTR
             CALL "cdbtochar"
               USING DTVAL, \DATE-CODE\, FMTSTR, \72\, CHARVAL, \72\,
                     ERROR-CODE
             END-CALL
             IF ERROR-CODE = 0 THEN
               DISPLAY CHARVAL
             ELSE
               DISPLAY "Error " ERROR-CODE " in DBTOCHAR"
           ELSE
             DISPLAY "Error " ERROR-CODE " in DBTODATE".
 
      *********************************
      ** CONVERT TIME STRING TO TIME **
      *********************************
           MOVE "16:10:00" TO CHARVAL
           MOVE "HH:MI:SS" TO FMTSTR
           CALL "cdbtotime"
             USING CHARVAL, \72\, FMTSTR, \72\, DTVAL, ERROR-CODE
           END-CALL
           IF ERROR-CODE = 0 THEN
      **************************************
      ** CONVERT TIME BACK TO TIME STRING **
      **************************************
             MOVE "ZHH12:MI AM" TO FMTSTR
             CALL "cdbtochar"
               USING DTVAL, \TIME-CODE\, FMTSTR, \72\, CHARVAL, \72\,
                     ERROR-CODE
             END-CALL
             IF ERROR-CODE = 0 THEN
               DISPLAY CHARVAL
             ELSE
               DISPLAY "Error " ERROR-CODE " in DBTOCHAR"
           ELSE
             DISPLAY "Error " ERROR-CODE " in DBTOTIME".
 
      *******************************************
      ** CONVERT DATE-TIME STRING TO DATE-TIME **
      *******************************************
           MOVE "02/06/95 01:28:48.500" TO CHARVAL
           MOVE "MM/DD/YY HH:MI:SS.FFF" TO FMTSTR
           CALL "cdbtodttm"
             USING CHARVAL, \72\, FMTSTR, \72\, DTVAL, ERROR-CODE
           END-CALL
           IF ERROR-CODE = 0 THEN
      ************************************************
      ** CONVERT DATE-TIME BACK TO DATE-TIME STRING **
      ************************************************
             MOVE "DAY  MON ZDD, YYYY  HH:MI:SS.FFF" TO FMTSTR
             CALL "cdbtochar"
               USING DTVAL, \DATETIME-CODE\, FMTSTR, \72\, CHARVAL,
                     \72\, ERROR-CODE
             END-CALL
             IF ERROR-CODE = 0 THEN
               DISPLAY CHARVAL
             ELSE
               DISPLAY "Error " ERROR-CODE " in DBTOCHAR"
           ELSE
             DISPLAY "Error " ERROR-CODE " in DBTODTTM".
 
      *****************************************
      ** Convert interval string to interval **
      *****************************************
           MOVE "0000080 05:20:30" TO CHARVAL
           MOVE "DAYS HH:MI:SS" TO FMTSTR
           CALL "cdbtoitvl"
             USING CHARVAL, \72\, FMTSTR, \72\, DTVAL, ERROR-CODE
           END-CALL
           IF ERROR-CODE = 0 THEN
      ***************************************************
      ** CONVERT INTERVAL TO INTEGER (NUMBER OF DAYS ) **
      ***************************************************
             MOVE "DAYS" TO FMTSTR
             CALL "cdbtoint"
               USING DTVAL, \INTERVAL-CODE\, FMTSTR, \72\, INTVAL,
                     ERROR-CODE
             END-CALL
             IF ERROR-CODE = 0 THEN
               DISPLAY INTVAL
             ELSE
               DISPLAY "Error " ERROR-CODE " in DBTOINT"
           ELSE
             DISPLAY "Error " ERROR-CODE " in DBTOITVL".
 
           STOP RUN.
 
       A100-EXIT.
           EXIT.
 
 
C-LAYER
-------
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* This is a thin layer of c code to work around a linking problem */
/* with COBOL and FORTRAN.  COBOL and FORTRAN programs should make */
/* calls to this routines which will in turn call the Date/Time    */
/* API routines.                                                   */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
cdbtochar(dtval, datatype, format, fmtlen, charval, bufflen, error)
char *dtval, *format, *charval;
int datatype, fmtlen, bufflen, *error;
{
dbtochar(dtval, datatype, format, fmtlen, charval, bufflen, error);
}
 
cdbtodate(charval, valuelen, format, fmtlen, dateval, error)
char *charval, *format, *dateval;
int valuelen, fmtlen, *error;
{
dbtodate(charval, valuelen, format, fmtlen, dateval, error);
}
 
cdbtotime(charval, valuelen, format, fmtlen, timeval, error)
char *charval, *format, *timeval;
int valuelen, fmtlen, *error;
{
dbtotime(charval, valuelen, format, fmtlen, timeval, error);
}
 
cdbtodttm(charval, valuelen, format, fmtlen, dttmval, error)
char *charval, *format, *dttmval;
int valuelen, fmtlen, *error;
{
dbtodttm(charval, valuelen, format, fmtlen, dttmval, error);
}
 
cdbtoitvl(charval, valuelen, format, fmtlen, itvlval, error)
char *charval, *format, *itvlval;
int valuelen, fmtlen, *error;
{
dbtoitvl(charval, valuelen, format, fmtlen, itvlval, error);
}
 
cdbtoint(dtval, datatype, format, fmtlen, result, error)
char *dtval, *format;
int datatype, fmtlen, *result, *error;
{
dbtoint(dtval, datatype, format, fmtlen, result, error);
}
 
 
P-layer
-------
 
(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)
(* This is a thin layer of Pascal code to work around a linking    *)
(* problem with COBOL and FORTRAN.  COBOL and FORTRAN programs     *)
(* should make calls to this routines which will in turn call the  *)
(* Date/Time API routines.                                         *)
(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)
 
$check_actual_parm  2$
$check_formal_parm  2$
$Subprogram$
 
program dummy;
 
type
  charstr = packed array [1..72] of char;
  dttype  = packed array [1..16] of char;
 
(*******************************************************************)
(* The following is the declaration of the Date/Time API routine   *)
(* as they are actually declared today in ALLBASE code.            *)
(*******************************************************************)
 
(* Perform TO_CHAR datetime function.                                     *)
PROCEDURE dbtochar   (    dateval  : integer;    (* ptr to datetime input *)
                          datatype : integer;    (* data type of input:   *)
                                                 (* 10 - date             *)
                                                 (* 11 - time             *)
                                                 (* 12 - datetime         *)
                                                 (* 13 - interval         *)
                          format   : integer;    (* ptr to format spec    *)
                          fmtlen   : integer;    (* length of format spec *)
                          result   : integer;    (* ptr to result buffer  *)
                          bufflen  : integer;    (* result buffer length  *)
                      var error    : integer);   (* error code            *)
external;
 
(* convert char representation to date internal format                      *)
PROCEDURE dbtodate   (    charval  : integer;    (* ptr to input date string*)
                          valuelen : integer;    (* length of input value   *)
                          format   : integer;    (* ptr to format string    *)
                          fmtlen   : integer;    (* length of format string *)
                          dateval  : integer;    (* ptr to result date      *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(* convert char representation to datetime internal format                  *)
PROCEDURE dbtodttm   (    charval  : integer;    (* ptr to input date string*)
                          valuelen : integer;    (* length of input value   *)
                          format   : integer;    (* ptr to format string    *)
                          fmtlen   : integer;    (* length of format string *)
                          dttmval  : integer;    (* ptr to result datetime  *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(* Perform TO_INTEGER datetime function.                                  *)
PROCEDURE dbtoint    (    dateval  : integer;    (* ptr to datetime input *)
                          datatype : integer;    (* data type of input:   *)
                                                 (* 10 - date             *)
                                                 (* 11 - time             *)
                                                 (* 12 - datetime         *)
                                                 (* 13 - interval         *)
                          format   : integer;    (* ptr to format spec    *)
                          fmtlen   : integer;    (* length of format spec *)
                      var result   : integer;    (* result                *)
                      var error    : integer);
external;
 
(* convert char representation to interval internal format                  *)
PROCEDURE dbtoitvl   (    charval  : integer;    (* ptr to input interval   *)
                                                 (* string                  *)
                          valuelen : integer;    (* length of input value   *)
                          format   : integer;    (* ptr to format string    *)
                          fmtlen   : integer;    (* length of format string *)
                          itvlval  : integer;    (* ptr to result interval  *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(* convert char representation to time internal format                      *)
PROCEDURE dbtotime   (    charval  : integer;    (* ptr to input date string*)
                          valuelen : integer;    (* length of input value   *)
                          format   : integer;    (* ptr to format string    *)
                          fmtlen   : integer;    (* length of format string *)
                          timeval  : integer;    (* ptr to result time      *)
                      var error    : integer);   (* Error code returned     *)
external;
 
(*******************************************************************)
(* The following is the declaration of the Date/Time API routine   *)
(* as probably should have been declared for compatibility         *)
(*******************************************************************)
 
(* Perform TO_CHAR datetime function.                                  *)
PROCEDURE pdbtochar (var dtval    : dttype;   (* datetime input        *)
                         datatype : integer;  (* data type of input:   *)
                                              (* 10 - date             *)
                                              (* 11 - time             *)
                                              (* 12 - datetime         *)
                                              (* 13 - interval         *)
                     var format   : charstr;  (* format spec           *)
                         fmtlen   : integer;  (* length of format spec *)
                     var charval  : charstr;  (* result buffer         *)
                         bufflen  : integer;  (* result buffer length  *)
                     var error    : integer); (* error code            *)
begin
dbtochar(baddress(dtval), datatype, baddress(format), fmtlen,
         baddress(charval), bufflen, error);
end;
 
(* convert char representation to date internal format                   *)
PROCEDURE pdbtodate (var charval  : charstr;  (* input date string       *)
                         valuelen : integer;  (* length of input value   *)
                     var format   : charstr;  (* format string           *)
                         fmtlen   : integer;  (* length of format string *)
                     var dateval  : dttype;   (* result date             *)
                     var error    : integer); (* Error code returned     *)
begin
dbtodate(baddress(charval), valuelen, baddress(format), fmtlen,
         baddress(dateval), error);
end;
 
(* convert char representation to datetime internal format               *)
PROCEDURE pdbtodttm (var charval  : charstr;  (* input datetime string   *)
                         valuelen : integer;  (* length of input value   *)
                     var format   : charstr;  (* format string           *)
                         fmtlen   : integer;  (* length of format string *)
                     var dttmval  : dttype;   (* result datetime         *)
                     var error    : integer); (* Error code returned     *)
begin
dbtodttm(baddress(charval), valuelen, baddress(format), fmtlen,
         baddress(dttmval), error);
end;
 
(* Perform TO_INTEGER datetime function.                               *)
PROCEDURE pdbtoint  (var dtval    : dttype;   (* datetime input        *)
                         datatype : integer;  (* data type of input:   *)
                                              (* 10 - date             *)
                                              (* 11 - time             *)
                                              (* 12 - datetime         *)
                                              (* 13 - interval         *)
                     var format   : charstr;  (* format spec           *)
                         fmtlen   : integer;  (* length of format spec *)
                     var result   : integer;  (* result                *)
                     var error    : integer);
begin
dbtoint(baddress(dtval), datatype, baddress(format), fmtlen,
        result, error);
end;
 
(* convert char representation to interval internal format               *)
PROCEDURE pdbtoitvl (var charval  : charstr;  (* input interval          *)
                         valuelen : integer;  (* length of input value   *)
                     var format   : charstr;  (* format string           *)
                         fmtlen   : integer;  (* length of format string *)
                     var itvlval  : dttype;   (* result interval         *)
                     var error    : integer); (* Error code returned     *)
begin
dbtoitvl(baddress(charval), valuelen, baddress(format), fmtlen,
         baddress(itvlval), error);
end;
 
(* convert char representation to time internal format                   *)
PROCEDURE pdbtotime (var charval  : charstr;  (* input time string       *)
                         valuelen : integer;  (* length of input value   *)
                     var format   : charstr;  (* format string           *)
                         fmtlen   : integer;  (* length of format string *)
                     var timeval  : dttype;   (* result time             *)
                     var error    : integer); (* Error code returned     *)
begin
dbtotime(baddress(charval), valuelen, baddress(format), fmtlen,
         baddress(timeval), error);
end;
 
begin
end.

ATOM RSS1 RSS2