Formatting date and time


In Windows "Control Panel" / "Regional Settings" you can choose a Locale and change several formats for that Locale. However Progress displays dates and times using its own display formats.
You can use GetDateFormatA or GetTimeFormatA to format a date or time according to Windows settings. The resulting character string can be useful for reports or display-only fields. When you call GetDateFormatA or GetTimeFormatA you will probably want it to use the current locale, although it is also possible to use any non-current locale as demonstrated in the above example window.

/* these prototypes are declared in windows.p.
   constants are declared in {i18n.i}.
   i18n.i is available in everything.zip */
 
PROCEDURE GetDateFormatA EXTERNAL "KERNEL32" :
   DEFINE INPUT PARAMETER        Locale      AS LONG.
   DEFINE INPUT PARAMETER        dwFlags     AS LONG.
   DEFINE INPUT PARAMETER        lpTime      AS LONG.
   DEFINE INPUT PARAMETER        lpFormat    AS LONG.
   DEFINE INPUT-OUTPUT PARAMETER lpDateStr   AS CHARACTER.
   DEFINE INPUT PARAMETER        cchDate     AS LONG.
   DEFINE RETURN PARAMETER       cchReturned AS LONG.
END PROCEDURE.
 
PROCEDURE GetTimeFormatA EXTERNAL "KERNEL32" :
   DEFINE INPUT PARAMETER        Locale    AS LONG.
   DEFINE INPUT PARAMETER        dwFlags   AS LONG.
   DEFINE INPUT PARAMETER        lpTime    AS LONG.
   DEFINE INPUT PARAMETER        lpFormat  AS LONG.
   DEFINE INPUT-OUTPUT PARAMETER lpTimeStr AS CHARACTER.
   DEFINE INPUT PARAMETER        cchTime   AS LONG.
   DEFINE RETURN PARAMETER       cchReturned AS LONG.
END PROCEDURE.

Parameter lpTime is a MEMPTR to a SYSTEMTIME structure. lpTime=0 will use the current system date/time.
Parameter lpFormat is a pointer to a format string. lpFormat=0 will use the format as specified in Control Panel. There are numerous possibilities, you best check the help file in Control Panel for examples.
Parameter lpTimeString is the returned character string. The memory for this string must be allocated in your P4GL program as usual. The size of this allocated string must be supplied in cchTime.
The demo window was based on a radio set with a couple of Language ID's:

{i18n.i}
 
DEFINE VARIABLE RD-LANGID AS INTEGER 
     VIEW-AS RADIO-SET VERTICAL
     RADIO-BUTTONS 
          "LOCALE_USER_DEFAULT", {&LOCALE_USER_DEFAULT},
          "LOCALE_SYSTEM_DEFAULT", {&LOCALE_SYSTEM_DEFAULT},
          "Dutch", {&LANGID_DUTCH},
          "French", {&LANGID_FRENCH},
          "German", {&LANGID_GERMAN}, 
          "Spanish", {&LANGID_SPANISH},
          "English", {&LANGID_ENGLISH},
          "Italian", {&LANGID_ITALIAN}
     SIZE 34 BY 5.24 NO-UNDO.
--------------------------------------------------------------------------------
ON VALUE-CHANGED OF RD-LANGID IN FRAME DEFAULT-FRAME
DO:
  ASSIGN rd-langid.
 
  DEFINE VARIABLE chDate AS CHARACTER NO-UNDO.
  DEFINE VARIABLE cchRet AS INTEGER NO-UNDO.
 
  chDate = FILL("x",50).
  RUN GetDateFormatA IN hpApi( RD-LANGID ,
                               2,
                               0,
                               0,
                               INPUT-OUTPUT chDate,
                               LENGTH(chDate),
                               OUTPUT cchRet
                             ).  
  fill-in-longdate:SCREEN-VALUE = chDate.
 
 
  chDate = FILL("x",50).
  RUN GetDateFormatA IN hpApi( RD-LANGID ,
                               1,
                               0,
                               0,
                               INPUT-OUTPUT chDate,
                               LENGTH(chDate),
                               OUTPUT cchRet
                             ).  
  fill-in-shortdate:SCREEN-VALUE = chDate.
 
 
  chDate = FILL("x",50).
  RUN GetTimeFormatA IN hpApi( RD-LANGID ,
                               0,
                               0,
                               0,
                               INPUT-OUTPUT chDate,
                               LENGTH(chDate),
                               OUTPUT cchRet
                             ).  
  fill-in-time:SCREEN-VALUE = chDate.
END.

This example passes 0 for lpTime so it will always format the current system time. To format any other date or time you will have to pass a MEMPTR to an initialized SYSTEMTIME structure, for example:

{i18n.i}
 
  DEFINE VARIABLE cchRet AS INTEGER NO-UNDO.
  DEFINE VARIABLE lpTime AS MEMPTR  NO-UNDO.
  SET-SIZE (lpTime)    =   16.
  PUT-SHORT(lpTime, 1) = 1998.  /* = year                  */
  PUT-SHORT(lpTime, 3) =    6.  /* = month                 */
  PUT-SHORT(lpTime, 5) =    5.  /* = day of week, ignored  */
  PUT-SHORT(lpTime, 7) =   26.  /* = day                   */
  PUT-SHORT(lpTime, 9) =   21.  /* = hour                  */
  PUT-SHORT(lpTime,11) =   42.  /* = minute                */
  PUT-SHORT(lpTime,13) =   21.  /* = seconds               */
  PUT-SHORT(lpTime,15) =    4.  /* = milliseconds          */
 
  chDate = FILL("x",50).
  RUN GetDateFormatA IN hpApi( RD-LANGID ,
                               0,
                               GET-POINTER-VALUE(lpTime),
                               0,
                               INPUT-OUTPUT chDate,
                               LENGTH(chDate),
                               OUTPUT cchRet
                             ).  
  SET-SIZE(lpTime) = 0.