home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name stpftime -- Format time of day in national style
- *
- * Synopsis presult = stpftime(ptarget,hour,minute,second,hund,fmt,
- * pinfo);
- *
- * char *presult The resulting string, or NIL if the
- * time is out of range.
- * char *ptarget Buffer in which to put the resulting
- * string. It must be long enough to
- * accommodate the result, whose size
- * depends on fmt.
- * int hour Hour (0-23)
- * int minute Minute (0-59)
- * int second Second (0-59); this may be ignored,
- * depending on the value of fmt.
- * int hund Hundredths of a second (0-99); this may be
- * ignored, depending on the value of fmt.
- * int fmt Format code (see below).
- * COUNTRY_INFO *pinfo
- * Pointer to structure with country info
- *
- * Description This function formats a time of day according to DOS's
- * information about national style. pinfo must point to a
- * COUNTRY_INFO structure in the format returned by
- * QYGCOUN: in particular, the timsep and tfmt members
- * (and possibly the decsep member) of the structure must
- * be valid. (This may not be true if the structure was
- * filled by DOS 2.x, or if QYGCOUN reported an error.)
- *
- * The possible codes for fmt are
- *
- * YES_AMPM 1 Include AM or PM for 12-hour clock.
- *
- * YES_HUNDS 2 Include hundredths of seconds.
- * YES_SECS 4 Include seconds. (Ignore hundredths of
- * seconds if this is absent.)
- *
- * LEAD_ZERO 0 If hour < 10, include leading zero.
- * LEAD_BLANK 8 If hour < 10, include leading blank.
- * LEAD_NOTHING 16 Include no leading character.
- *
- * The format codes can be combined by adding them
- * together or by the "|" (bitwise OR) operator.
- *
- * If the time is illegal then the function returns the
- * null string in *ptarget and NIL as the value of the
- * function. However, the values of second and hund are
- * not checked if fmt does not use them.
- *
- * Examples First example: leading blank, all fields present.
- *
- * hour = 14
- * minute = 30
- * second = 12
- * hund = 23
- * fmt = LEAD_BLANK | YES_SECS | YES_HUNDS | YES_AMPM
- * country = USA --> result = " 2:30:12.23 PM"
- *
- * Second example: default format
- *
- * hour = 9
- * minute = 30
- * second = 12
- * hund = 23
- * fmt = 0
- * country = USA --> result = "09:30"
- *
- * Returns presult Pointer to the resulting string.
- * *ptarget The resulting string.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1986
- *
- **/
-
- #include <string.h>
- #include <stdio.h>
-
- #include <bstring.h>
-
- char *stpftime(ptarget,hour,minute,second,hund,fmt,pinfo)
- register char *ptarget;
- int hour,minute,second,hund,fmt;
- register COUNTRY_INFO *pinfo;
- {
- int am;
- char time_sep,*save_target;
-
- if (utrange(hour,0,23) || utrange(minute,0,59))
- { /* Error: illegal time */
- *ptarget = '\0';
- return NIL;
- }
-
- save_target = ptarget;
- time_sep = (pinfo->timsep)[0];
-
- if (pinfo->tfmt == 0)
- { /* 12-hour clock */
- if (hour < 12) /* Set the AM or PM flag */
- {
- am = 1;
- if (hour == 0)
- hour = 12; /* Midnight */
- }
- else
- {
- am = 0;
- if (hour > 12)
- hour -= 12;
- }
- }
- if (hour < 10)
- switch (fmt & (LEAD_BLANK + LEAD_NOTHING))
- {
- case LEAD_BLANK + LEAD_NOTHING:
- case LEAD_BLANK:
- *ptarget++ = ' ';
- break;
- case LEAD_ZERO:
- *ptarget++ = '0';
- break;
- case LEAD_NOTHING:
- break;
- }
- ptarget += sprintf(ptarget,"%d%c%02d",hour,time_sep,
- minute);
- if (fmt & YES_SECS)
- {
- if (utrange(second,0,59))
- { /* Error: illegal time */
- *save_target = '\0';
- return NIL;
- }
- ptarget += sprintf(ptarget,"%c%02d",time_sep,
- second);
- if (fmt & YES_HUNDS)
- {
- if (utrange(hund,0,99))
- { /* Error: illegal time */
- *save_target = '\0';
- return NIL;
- }
- ptarget += sprintf(ptarget,"%c%02d",(pinfo->decsep)[0],
- hund);
- }
- }
-
- if ((fmt & YES_AMPM) && (pinfo->tfmt == 0))
- strcpy(ptarget,am ? " AM"
- : " PM");
-
- return save_target;
- }