DSN8EUMN

Returns the calendar name of the month name in which a given date in ISO format (YYYY-MM-DD) falls.

 /*********************************************************************
 * Module name = DSN8EUMN (DB2 sample program)                        *
 *                                                                    *
 * DESCRIPTIVE NAME = Query calendar month name (UDF)                 *
 *                                                                    *
 *                                                                    *
 * LICENSED MATERIALS - PROPERTY OF IBM                               *
 * 5675-DB2                                                           *
 * (C) COPYRIGHT 1998, 2000 IBM CORP.  ALL RIGHTS RESERVED.           *
 *                                                                    *
 * STATUS = VERSION 7                                                 *
 *                                                                    *
 *                                                                    *
 * Function: Returns the calendar name of the month name in           *
 *           which a given date in ISO format (YYYY-MM-DD) falls.     *
 *                                                                    *
 *           Example invocation:                                      *
 *            EXEC SQL SET :monthname = MONTHNAME( "2000-01-29" );    *
 *            ==> monthname = January                                 *
 * Notes:                                                             *
 *   Dependencies: Requires IBM C/C++ for OS/390 V1R3 or higher       *
 *                                                                    *
 *   Restrictions:                                                    *
 *                                                                    *
 * Module type: C++ program                                           *
 *   Processor: IBM C/C++ for OS/390 V1R3 or higher                   *
 * Module size: See linkedit output                                   *
 *  Attributes: Re-entrant and re-usable                              *
 *                                                                    *
 * Entry Point: DSN8EUMN                                              *
 *     Purpose: See Function                                          *
 *     Linkage: DB2SQL                                                *
 *              Invoked via SQL UDF call                              *
 *                                                                    *
 *       Input: Parameters explicitly passed to this function:        *
 *              - *ISOdateIn   : pointer to a char[11], null-termi-   *
 *                               nated string having a date in ISO    *
 *                               format.                              *
 *              - *niISOdateIn : pointer to a short integer having    *
 *                               the null indicator variable for      *
 *                               *ISOdateIn.                          *
 *              - *fnName      : pointer to a char[138], null-termi-  *
 *                               nated string having the UDF family   *
 *                               name of this function.               *
 *              - *specificName: pointer to a char[129], null-termi-  *
 *                               nated string having the UDF specific *
 *                               name of this function.               *
 *                                                                    *
 *                                                                    *
 *      Output: Parameters explicitly passed by this function:        *
 *              - *monthNameOut: pointer to a char[10], null-termi-   *
 *                               nated string to receive the month-   *
 *                               name for ISOdateIn.                  *
 *              - *niMonthNameOut: pointer to a short integer to re-  *
 *                               ceive the null indicator variable    *
 *                               for *monthNameOut.                   *
 *              - *sqlstate    : pointer to a char[06], null-termi-   *
 *                               nated string to receive the SQLSTATE.*
 *              - *message     : pointer to a char[70], null-termi-   *
 *                               nated string to receive a diagnostic *
 *                               message if one is generated by this  *
 *                               function.                            *
 *                                                                    *
 * Normal Exit: Return Code: SQLSTATE = 00000                         *
 *              - Message: none                                       *
 *                                                                    *
 *  Error Exit: Return Code: SQLSTATE = 38601                         *
 *              - Message: DSN8EUMN Error: No date entered            *
 *              Return Code: SQLSTATE = 38602                         *
 *              - Message: DSN8EUMN Error: Input date not valid       *
 *                                         or not in ISO format"      *
 *                                                                    *
 *    External References:                                            *
 *             - Routines/Services:                                   *
 *               - strftime: Formatted time conversion routine        *
 *                 - from IBM C/C++ for z/OS run-time library         *
 *               - strptime: Date and time conversion routine         *
 *                 - from IBM C/C++ for z/OS run-time library         *
 *             - Data areas       : None                              *
 *             - Control blocks   : None                              *
 *                                                                    *
 *                                                                    *
 *  Pseudocode:                                                       *
 *   DSN8EUMN:                                                        *
 *   - Verify that a date was passed in:                              *
 *     - if *ISOdateIn blank or niISOdateIn is not 0, no date passed: *
 *       - issue SQLSTATE 38601 and a diagnostic message.             *
 *   - Use strptime to validate the entry and convert the date to tm  *
 *     - if *ISOdateIn is not a valid ISO date:                       *
 *       - issue SQLSTATE 38602 and a diagnostic message              *
 *   - Use strftime to get the full monthname for the locale          *
 *   End DSN8EUMN                                                     *
 *                                                                    *
 *  Change log:                                                       *
 *  2004-02-25: Rewritten due to demise of IBM Open Class library     *
 *                                                                    *
 *********************************************************************/
 extern "C" void DSN8EUMN              /* Establish linkage          */
 ( char        *ISOdateIn,             /* in: date to look up        */
   char        *monthNameOut,          /* out: ISOdateIn's month name*/
   short int   *niISOdateIn,           /* in: indic var, ISOdateIn   */
   short int   *niMonthNameOut,        /* out: indic var,monthNameOut*/
   char        *sqlstate,              /* out: SQLSTATE              */
   char        *fnName,                /* in: family name of function*/
   char        *specificName,          /* in: specific name of func  */
   char        *message                /* out: diagnostic message    */
 );

 #pragma linkage(DSN8EUMN,fetchable)   /* Establish linkage          */

 /********************** C library definitions ***********************/
 #include <stdio.h>
 #include <time.h>

 /***************************** Equates ******************************/
 #define     NULLCHAR     '\0'         /* Null character             */

 #define     MATCH          0          /* Comparison status: Equal   */
 #define     NOT_OK         0          /* Run status indicator: Error*/
 #define     OK             1          /* Run status indicator: Good */

 /*********************** DSN8EUMN functions *************************/

 /********************************************************************/
 /*************************** main routine ***************************/
 /********************************************************************/
 void DSN8EUMN                         /* main routine               */
 ( char        *ISOdateIn,             /* in: date to look up        */
   char        *monthNameOut,          /* out: ISOdateIn's month name*/
   short int   *niISOdateIn,           /* in: indic var, ISOdateIn   */
   short int   *niMonthNameOut,        /* out: indic var,monthNameOut*/
   char        *sqlstate,              /* out: SQLSTATE              */
   char        *fnName,                /* in: family name of function*/
   char        *specificName,          /* in: specific name of func  */
   char        *message                /* out: diagnostic message    */
 )
 /*********************************************************************
 * Returns the name of the month for the date in isoDate.             *
 *                                                                    *
 * Assumptions:                                                       *
 * - *ISOdateIn        points to a char[11], null-terminated string   *
 * - *monthNameOut     points to a char[10], null-terminated string   *
 * - *niISOdateIn      points to a short integer                      *
 * - *niMonthNameOut   points to a short integer                      *
 * - *sqlstate         points to a char[06], null-terminated string   *
 * - *fnName           points to a char[138], null-terminated string  *
 * - *specificName     points to a char[129], null-terminated string  *
 * - *message          points to a char[70], null-terminated string   *
 *********************************************************************/
 {

   /************************ local variables *************************/
   short int   status = OK;            /* DSN8EUMN run status        */
   struct tm   tmbuff;                 /* buffer for time.h tm struct*/
   char        *rc;                    /* gets strf/ptime return code*/
   char        *isoFormat              /* format of isoDate:         */
               = "%Y-%m-%d";           /* %Y = YYYY, %m = MM, %d = DD*/
   char        *fullMonthName          /* format of fullMonthName    */
               = "%B";                 /* %B = full month name       */


   /*******************************************************************
   * Verify that something has been passed in                         *
   *******************************************************************/
   if( *niISOdateIn != 0 || ( strlen( ISOdateIn ) == 0 ) )
     {
       status = NOT_OK;
       strcpy( message,
               "DSN8EUMN Error: No date entered" );
       strcpy( sqlstate, "38601" );
     }

   /*******************************************************************
   * Convert ISOdateIn to C tm format                                 *
   *******************************************************************/
   if( status == OK )
     { rc = strptime( ISOdateIn,isoFormat,&tmbuff );
       if( rc == NULL )                /* Unable to convert ISOdateIn*/
         {
           status = NOT_OK;
           strcpy( message,
                   "DSN8EUMN Error: Input date not valid "
                                   "or not in ISO format" );
           strcpy( sqlstate, "38602" );
         }
     }

   /*******************************************************************
   * Convert the date from C tm format to the locale's full monthname *
   *******************************************************************/
   if( status == OK )
     { *rc = strftime( monthNameOut,10,fullMonthName,&tmbuff );
     }

   /*******************************************************************
   * If month name was obtained, clear the message buffer and sql-    *
   * state, and unset the SQL null indicator for monthNameOut.        *
   *******************************************************************/
   if( status == OK )
     {
       *niMonthNameOut = 0;
       message[0] = NULLCHAR;
       strcpy( sqlstate,"00000" );
     }
   /*******************************************************************
   * If errors occurred, clear the monthNameOut buffer and set the SQL*
   * NULL indicator.  A diagnostic message and the SQLSTATE have been *
   * set where the error was detected.                                *
   *******************************************************************/
   else
     {
       monthNameOut[0] = NULLCHAR;
       *niMonthNameOut = -1;
     }

   return;
 }  /* end DSN8EUMN */