home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / easrc.zip / DateTime.c < prev    next >
Text File  |  1994-09-28  |  4KB  |  122 lines

  1. /***************************************************************************
  2.  *
  3.  * PROGRAM NAME: DATETIME.C
  4.  * -------------
  5.  *
  6.  * REVISION LEVEL: 1.2
  7.  * ---------------
  8.  *
  9.  * WHAT THIS PROGRAM DOES:
  10.  * -----------------------
  11.  *  Get date and time in country specific format
  12.  *
  13.  * ROUTINES:
  14.  * ---------
  15.  *  GetDateTime
  16.  *
  17.  * COMPILE REQUIREMENTS:
  18.  * ---------------------
  19.  *  IBM C++ Set/2 Compiler Version 2.0
  20.  *  IBM OS/2 2.1 Programmers Toolkit
  21.  *
  22.  * REQUIRED FILES:
  23.  * ---------------
  24.  *  CUTIL.H
  25.  *
  26.  * REQUIRED LIBRARIES:
  27.  * -------------------
  28.  *  OS2386.LIB    -   OS/2 32-Bit import library
  29.  *
  30.  * CHANGE LOG:
  31.  * -----------
  32.  * 
  33.  *  Ver.    Date      Comment
  34.  *  ----    --------  -------
  35.  *  1.20    02-19-94  First release
  36.  *
  37.  *  Copyright (C) 1994 Noller & Breining Software
  38.  *
  39.  ******************************************************************************/
  40. #define INCL_DOS
  41.  
  42. #include <os2.h>
  43. #include <string.h>
  44.  
  45. /* --- internal function only --- */
  46. PCHAR itoNc (PCHAR, USHORT, USHORT);
  47.  
  48. /*******************************************************************
  49.    Get date and time in country specific format
  50.    Entry:  pDateTime: Pointer to DATETIME-structure
  51.    Exit:   szTime: string with time (minimal buffer length 10 chars)
  52.            szDate: string with date (minimal buffer length 11 chars)
  53.    return: Return value of DosQueryCtryInfo
  54.  *******************************************************************/
  55. APIRET GetDateTime (PDATETIME pDateTime, PCHAR szTime, PCHAR szDate)
  56.     {
  57.     ULONG ulLen;
  58.     PCHAR pszString;
  59.     COUNTRYCODE CtryCode;
  60.     COUNTRYINFO CtryInfo;
  61.     APIRET rc;
  62.  
  63.     *szTime = *szDate = '\0';
  64.     ulLen = sizeof (COUNTRYINFO);
  65.     CtryCode.country  = 0;
  66.     CtryCode.codepage = 0;
  67.  
  68.     if ((rc = DosQueryCtryInfo (ulLen, &CtryCode, &CtryInfo, &ulLen)) == 0)
  69.         {
  70.         /* Assembly of date */
  71.         switch (CtryInfo.fsDateFmt)
  72.             {
  73.             case 1:
  74.                 pszString = itoNc (szDate, pDateTime->day, 10) + 1;
  75.                 pszString = itoNc (pszString, pDateTime->month, 10) + 1;
  76.                 pszString = itoNc (pszString, pDateTime->year, 1000);
  77.                 break;
  78.  
  79.             case 2:
  80.                 pszString = itoNc (szDate, pDateTime->year, 1000) + 1;
  81.                 pszString = itoNc (pszString, pDateTime->month, 10) + 1;
  82.                 pszString = itoNc (pszString, pDateTime->day, 10);
  83.                 break;
  84.  
  85.             default:
  86.                 pszString = itoNc (szDate, pDateTime->month, 10) + 1;
  87.                 pszString = itoNc (pszString, pDateTime->day, 10) + 1;
  88.                 pszString = itoNc (pszString, pDateTime->year, 1000);
  89.             }
  90.         szDate[2] = szDate[5] = CtryInfo.szDateSeparator[0];
  91.         *pszString = '\0';
  92.  
  93.         /* Assembly of time */
  94.         if (CtryInfo.fsTimeFmt)
  95.             pszString = itoNc (szTime, pDateTime->hours, 10);
  96.         else
  97.             pszString = itoNc (szTime, ((pDateTime->hours+11) % 12) + 1, 10);
  98.         *pszString++ = CtryInfo.szTimeSeparator[0];
  99.         pszString = itoNc (pszString, pDateTime->minutes, 10);
  100.         *pszString++ = CtryInfo.szTimeSeparator[0];
  101.         pszString = itoNc (pszString, pDateTime->seconds, 10);
  102.         *pszString = '\0';
  103.         if (CtryInfo.fsTimeFmt == 0)
  104.             strcpy (pszString, (pDateTime->hours-1 < 13) ? "AM" : "PM");
  105.         }
  106.  
  107.     return rc;
  108.     }
  109.  
  110. PCHAR itoNc (PCHAR psz, USHORT usVal, USHORT N)
  111.     {
  112.     USHORT i;
  113.  
  114.     for (i=N; i>0; i/=10)
  115.         {
  116.         *psz++ = (CHAR)(usVal / i) + '0';
  117.         usVal %= i;
  118.         }
  119.  
  120.     return psz;
  121.     }
  122.