home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_5.ZIP / WINSRC.ZIP / TIME.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  5.6 KB  |  254 lines

  1. //    Zinc Interface Library - TIME.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_gen.hpp"
  6. #include <dos.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12. static void NormalizeAmPmString(const char *src, char *dest)
  13. {
  14.     while (*src)
  15.     {
  16.         if (isalpha(*src))
  17.             *dest++ = toupper(*src);
  18.         else if (isdigit(*src))
  19.             break;
  20.         src++;
  21.     }
  22.     *dest = '\0';
  23. }
  24.  
  25. void UI_TIME::Export()
  26. {
  27.     struct time info;
  28.     info.ti_hour = tHour;
  29.     info.ti_min = tMinute;
  30.     info.ti_sec = tSecond;
  31.     info.ti_hund = tHundredth;
  32.     settime(&info);
  33. }
  34.  
  35. void UI_TIME::Export(int *packedTime)
  36. {
  37.     *packedTime = (tHour << 11) | (tMinute << 5) | tSecond / 2;
  38. }
  39.  
  40. void UI_TIME::Export(int *hour, int *minute, int *second, int *hundredth)
  41. {
  42.     if (hour) *hour = tHour;
  43.     if (minute) *minute = tMinute;
  44.     if (second) *second = tSecond;
  45.     if (hundredth) *hundredth = tHundredth;
  46. }
  47.  
  48. void UI_TIME::Export(char *string, USHORT tmFlags)
  49. {
  50.     // Get the initial separators.
  51.     ui_getcountryinfo();
  52.     char timeFormat = _countryInfo.co_time;
  53.     if (FlagSet(tmFlags, TMF_TWELVE_HOUR))
  54.         timeFormat = 0;
  55.     else if (FlagSet(tmFlags, TMF_TWENTY_FOUR_HOUR))
  56.         timeFormat = 1;
  57.     char *separator = _countryInfo.co_tmsep;
  58.     USHORT zeroFill = FlagSet(tmFlags, TMF_ZERO_FILL);
  59.     if (FlagSet(tmFlags, TMF_COLON_SEPARATOR))
  60.         separator = ":";
  61.     else if (FlagSet(tmFlags, TMF_NO_SEPARATOR))
  62.     {
  63.         separator = "";
  64.         zeroFill = TRUE;
  65.     }
  66.     char *ptr = string;
  67.     int values[4] = { tHour, tMinute, tSecond, tHundredth };
  68.     int pmFlag = 0;
  69.     if (timeFormat == 0)
  70.     {
  71.         values[0] = tHour % 12;
  72.         if (values[0] == 0)
  73.             values[0] = 12;
  74.         if (tHour >= 12)
  75.             pmFlag = 1;
  76.     }
  77.     int firstValue = 0;
  78.     if (FlagSet(tmFlags, TMF_NO_MINUTES))
  79.         firstValue = 2;
  80.     else if (FlagSet(tmFlags, TMF_NO_HOURS))
  81.         firstValue = 1;
  82.     int lastValue = 1;
  83.     if (FlagSet(tmFlags, TMF_HUNDREDTHS))
  84.         lastValue = 3;
  85.     else if (FlagSet(tmFlags, TMF_SECONDS))
  86.         lastValue = 2;
  87.     for (int i = firstValue; i <= lastValue; i++)
  88.     {
  89.         if (ptr != string)
  90.         {
  91.             if (i == 3)
  92.                 *ptr++ = '.';
  93.             else
  94.             {
  95.                 strcpy(ptr, separator);
  96.                 ptr += strlen(separator);
  97.             }
  98.         }
  99.         ptr += sprintf(ptr, (ptr != string || zeroFill) ? "%02d" : "%d", values[i]);
  100.     }
  101.     if (timeFormat == 0 && firstValue == 0)
  102.     {
  103.         sprintf(ptr, " %s", pmFlag ? pmPtr : amPtr);
  104.         if (FlagSet(tmFlags, TMF_UPPER_CASE))
  105.             strupr(ptr);
  106.         else if (FlagSet(tmFlags, TMF_LOWER_CASE))
  107.             strlwr(ptr);
  108.     }
  109. }
  110.  
  111. TMI_RESULT UI_TIME::Import()
  112. {
  113.     struct time info;
  114.     gettime(&info);
  115.     return(Import(info.ti_hour, info.ti_min, info.ti_sec, info.ti_hund));
  116. }
  117.  
  118. TMI_RESULT UI_TIME::Import(int time)
  119. {
  120.     return(Import((time & 0xF800) >> 11, (time & 0x07E0) >> 5, 2 * (time & 0x001F)));
  121. }
  122.  
  123. TMI_RESULT UI_TIME::Import(int hour, int minute, int second, int hundredth)
  124. {
  125.     if (hour < 0 || hour > 23 || minute < 0 || minute > 59 ||
  126.         second < 0 || second > 59 || hundredth < 0 || hundredth > 99)
  127.         return (TMI_INVALID);
  128.     tHour = hour;
  129.     tMinute = minute;
  130.     tSecond = second;
  131.     tHundredth = hundredth;
  132.     return (TMI_OK);
  133. }
  134.  
  135. TMI_RESULT UI_TIME::Import(const char *string, USHORT tmFlags)
  136. {
  137.     int values[4] = { 0, 0, 0, 0 };
  138.     int hour = 0;
  139.     int minute = 0;
  140.     int second = 0;
  141.     int hundredth = 0;
  142.     int numValues = 0;
  143.     int maxValues = 2;
  144.     int AmPm = 0;
  145.     int isBlank = TRUE;
  146.  
  147.     if (FlagSet(tmFlags, TMF_HUNDREDTHS))
  148.         maxValues += 2;
  149.     else if (FlagSet(tmFlags, TMF_SECONDS))
  150.         maxValues++;
  151.     if (FlagSet(tmFlags, TMF_NO_MINUTES))
  152.         maxValues -= 2;
  153.     else if (FlagSet(tmFlags, TMF_NO_HOURS))
  154.         maxValues--;
  155.     ui_getcountryinfo();
  156.     while (*string)
  157.     {
  158.         if (isalpha(*string))
  159.         {
  160.             int which = AmPmLookup(string);
  161.             if (which < 0 || AmPm)
  162.                 return (TMI_INVALID);
  163.             AmPm = which;
  164.             while (*string && !isdigit(*string))
  165.                 string++;
  166.             isBlank = FALSE;
  167.         }
  168.         else if (isdigit(*string))
  169.         {
  170.             char digits[3];
  171.             int  numDigits = 0;
  172.             while (numDigits < 2 && isdigit(*string))
  173.                 digits[numDigits++] = *string++;
  174.             digits[numDigits] = '\0';
  175.             int value = atoi(digits);
  176.             if (numValues >= maxValues) return TMI_INVALID;
  177.             values[numValues++] = value;
  178.             isBlank = FALSE;
  179.         }
  180.         else
  181.             string++;            // Ignore spaces and punctuation.
  182.     }
  183.     if (FlagSet(tmFlags, TMF_NO_MINUTES))
  184.     {
  185.         second = values[0];
  186.         hundredth = values[1];
  187.     }
  188.     else if (FlagSet(tmFlags, TMF_NO_HOURS))
  189.     {
  190.         minute = values[0];
  191.         second = values[1];
  192.         hundredth = values[2];
  193.     }
  194.     else
  195.     {
  196.         hour = values[0];
  197.         minute = values[1];
  198.         second = values[2];
  199.         hundredth = values[3];
  200.     }
  201.     if (AmPm == 2)
  202.     {
  203.         if (hour >= 1 && hour <= 11)
  204.             hour = hour + 12;
  205.     }
  206.     else if (AmPm == 1)
  207.     {
  208.         if (hour == 12)
  209.             hour = 0;
  210.     }
  211.     if (isBlank)
  212.     {
  213.         if (FlagSet(tmFlags, TMF_SYSTEM))
  214.         {
  215.             Import();            // Import from system time.
  216.             return (TMI_OK);
  217.         }
  218.         else
  219.         {
  220.             Import(0, 0, 0, 0);
  221.             return (TMI_INVALID);
  222.         }
  223.     }
  224.     return (Import(hour, minute, second, hundredth));
  225. }
  226.  
  227. int UI_TIME::AmPmLookup(const char *token)
  228. {
  229.     char normalizedAm[20];
  230.     char normalizedPm[20];
  231.     char normalizedToken[20];
  232.  
  233.     // Allow single character (if unambiguous).
  234.     if (*(token + 1) == '\0' && toupper(*amPtr) != toupper(*pmPtr))
  235.     {
  236.         if (toupper(*token) == toupper(*amPtr))
  237.             return (1);
  238.         else if (toupper(*token) == toupper(*pmPtr))
  239.             return (2);
  240.         else
  241.             return (-1);
  242.     }
  243.     // Or require full string match (ignoring punctuation).
  244.     NormalizeAmPmString(amPtr, normalizedAm);
  245.     NormalizeAmPmString(pmPtr, normalizedPm);
  246.     NormalizeAmPmString(token, normalizedToken);
  247.     if (strcmp(normalizedToken, normalizedAm) == 0)
  248.         return (1);
  249.     if (strcmp(normalizedToken, normalizedPm) == 0)
  250.         return (2);
  251.     return (-1);
  252. }
  253.  
  254.