home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - TIME.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_gen.hpp"
- #include <dos.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- static void NormalizeAmPmString(const char *src, char *dest)
- {
- while (*src)
- {
- if (isalpha(*src))
- *dest++ = toupper(*src);
- else if (isdigit(*src))
- break;
- src++;
- }
- *dest = '\0';
- }
-
- void UI_TIME::Export()
- {
- struct time info;
- info.ti_hour = tHour;
- info.ti_min = tMinute;
- info.ti_sec = tSecond;
- info.ti_hund = tHundredth;
- settime(&info);
- }
-
- void UI_TIME::Export(int *packedTime)
- {
- *packedTime = (tHour << 11) | (tMinute << 5) | tSecond / 2;
- }
-
- void UI_TIME::Export(int *hour, int *minute, int *second, int *hundredth)
- {
- if (hour) *hour = tHour;
- if (minute) *minute = tMinute;
- if (second) *second = tSecond;
- if (hundredth) *hundredth = tHundredth;
- }
-
- void UI_TIME::Export(char *string, USHORT tmFlags)
- {
- // Get the initial separators.
- ui_getcountryinfo();
- char timeFormat = _countryInfo.co_time;
- if (FlagSet(tmFlags, TMF_TWELVE_HOUR))
- timeFormat = 0;
- else if (FlagSet(tmFlags, TMF_TWENTY_FOUR_HOUR))
- timeFormat = 1;
- char *separator = _countryInfo.co_tmsep;
- USHORT zeroFill = FlagSet(tmFlags, TMF_ZERO_FILL);
- if (FlagSet(tmFlags, TMF_COLON_SEPARATOR))
- separator = ":";
- else if (FlagSet(tmFlags, TMF_NO_SEPARATOR))
- {
- separator = "";
- zeroFill = TRUE;
- }
- char *ptr = string;
- int values[4] = { tHour, tMinute, tSecond, tHundredth };
- int pmFlag = 0;
- if (timeFormat == 0)
- {
- values[0] = tHour % 12;
- if (values[0] == 0)
- values[0] = 12;
- if (tHour >= 12)
- pmFlag = 1;
- }
- int firstValue = 0;
- if (FlagSet(tmFlags, TMF_NO_MINUTES))
- firstValue = 2;
- else if (FlagSet(tmFlags, TMF_NO_HOURS))
- firstValue = 1;
- int lastValue = 1;
- if (FlagSet(tmFlags, TMF_HUNDREDTHS))
- lastValue = 3;
- else if (FlagSet(tmFlags, TMF_SECONDS))
- lastValue = 2;
- for (int i = firstValue; i <= lastValue; i++)
- {
- if (ptr != string)
- {
- if (i == 3)
- *ptr++ = '.';
- else
- {
- strcpy(ptr, separator);
- ptr += strlen(separator);
- }
- }
- ptr += sprintf(ptr, (ptr != string || zeroFill) ? "%02d" : "%d", values[i]);
- }
- if (timeFormat == 0 && firstValue == 0)
- {
- sprintf(ptr, " %s", pmFlag ? pmPtr : amPtr);
- if (FlagSet(tmFlags, TMF_UPPER_CASE))
- strupr(ptr);
- else if (FlagSet(tmFlags, TMF_LOWER_CASE))
- strlwr(ptr);
- }
- }
-
- TMI_RESULT UI_TIME::Import()
- {
- struct time info;
- gettime(&info);
- return(Import(info.ti_hour, info.ti_min, info.ti_sec, info.ti_hund));
- }
-
- TMI_RESULT UI_TIME::Import(int time)
- {
- return(Import((time & 0xF800) >> 11, (time & 0x07E0) >> 5, 2 * (time & 0x001F)));
- }
-
- TMI_RESULT UI_TIME::Import(int hour, int minute, int second, int hundredth)
- {
- if (hour < 0 || hour > 23 || minute < 0 || minute > 59 ||
- second < 0 || second > 59 || hundredth < 0 || hundredth > 99)
- return (TMI_INVALID);
- tHour = hour;
- tMinute = minute;
- tSecond = second;
- tHundredth = hundredth;
- return (TMI_OK);
- }
-
- TMI_RESULT UI_TIME::Import(const char *string, USHORT tmFlags)
- {
- int values[4] = { 0, 0, 0, 0 };
- int hour = 0;
- int minute = 0;
- int second = 0;
- int hundredth = 0;
- int numValues = 0;
- int maxValues = 2;
- int AmPm = 0;
- int isBlank = TRUE;
-
- if (FlagSet(tmFlags, TMF_HUNDREDTHS))
- maxValues += 2;
- else if (FlagSet(tmFlags, TMF_SECONDS))
- maxValues++;
- if (FlagSet(tmFlags, TMF_NO_MINUTES))
- maxValues -= 2;
- else if (FlagSet(tmFlags, TMF_NO_HOURS))
- maxValues--;
- ui_getcountryinfo();
- while (*string)
- {
- if (isalpha(*string))
- {
- int which = AmPmLookup(string);
- if (which < 0 || AmPm)
- return (TMI_INVALID);
- AmPm = which;
- while (*string && !isdigit(*string))
- string++;
- isBlank = FALSE;
- }
- else if (isdigit(*string))
- {
- char digits[3];
- int numDigits = 0;
- while (numDigits < 2 && isdigit(*string))
- digits[numDigits++] = *string++;
- digits[numDigits] = '\0';
- int value = atoi(digits);
- if (numValues >= maxValues) return TMI_INVALID;
- values[numValues++] = value;
- isBlank = FALSE;
- }
- else
- string++; // Ignore spaces and punctuation.
- }
- if (FlagSet(tmFlags, TMF_NO_MINUTES))
- {
- second = values[0];
- hundredth = values[1];
- }
- else if (FlagSet(tmFlags, TMF_NO_HOURS))
- {
- minute = values[0];
- second = values[1];
- hundredth = values[2];
- }
- else
- {
- hour = values[0];
- minute = values[1];
- second = values[2];
- hundredth = values[3];
- }
- if (AmPm == 2)
- {
- if (hour >= 1 && hour <= 11)
- hour = hour + 12;
- }
- else if (AmPm == 1)
- {
- if (hour == 12)
- hour = 0;
- }
- if (isBlank)
- {
- if (FlagSet(tmFlags, TMF_SYSTEM))
- {
- Import(); // Import from system time.
- return (TMI_OK);
- }
- else
- {
- Import(0, 0, 0, 0);
- return (TMI_INVALID);
- }
- }
- return (Import(hour, minute, second, hundredth));
- }
-
- int UI_TIME::AmPmLookup(const char *token)
- {
- char normalizedAm[20];
- char normalizedPm[20];
- char normalizedToken[20];
-
- // Allow single character (if unambiguous).
- if (*(token + 1) == '\0' && toupper(*amPtr) != toupper(*pmPtr))
- {
- if (toupper(*token) == toupper(*amPtr))
- return (1);
- else if (toupper(*token) == toupper(*pmPtr))
- return (2);
- else
- return (-1);
- }
- // Or require full string match (ignoring punctuation).
- NormalizeAmPmString(amPtr, normalizedAm);
- NormalizeAmPmString(pmPtr, normalizedPm);
- NormalizeAmPmString(token, normalizedToken);
- if (strcmp(normalizedToken, normalizedAm) == 0)
- return (1);
- if (strcmp(normalizedToken, normalizedPm) == 0)
- return (2);
- return (-1);
- }
-
-