home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / SGETTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  4.2 KB  |  194 lines

  1. /*
  2.  * sgettime.c
  3.  * contains: sgettime(),fmttime(),ggettime()
  4.  *
  5.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "gfuncts.h"
  11.  
  12.  
  13. void GF_CONV_xstrncpy();
  14.  
  15. static char stam[]="am";
  16. static char stpm[]="pm";
  17.  
  18. /*
  19.  *  struct TIMEDATE *
  20.  * sgettime(f)
  21.  *
  22.  * ARGUMENT
  23.  *  (int)    f    -    format specifier (see manual)
  24.  *
  25.  * DESCRIPTION
  26.  *  This function obtains the time from the system and places integer and
  27.  *  ASCII string values in the respective buffer positions.
  28.  *
  29.  * RETURNS
  30.  * Pointer to structure containing the time and date. This is set to 0 if 
  31.  * the time has not been set.. i.e. if the system indicates Jan 1, 1980.
  32.  */
  33. struct TIMEDATE * GF_CONV sgettime(f)
  34. int f;
  35. {
  36.     static struct TIMEDATE buf,*ptd;
  37.  
  38.     fetchtime(&buf);
  39.     ptd=fmttime(&buf,f);
  40.     return ptd;
  41. }
  42.  
  43. /*
  44.  *  struct TIMEDATE *
  45.  * fmttime(ptd,f)
  46.  *
  47.  * ARGUMENT
  48.  *  (struct TIMEDATE *)    ptd    -    structure containing integer variables
  49.  *  (int)        f    -    as above in sgettime
  50.  *
  51.  * DESCRIPTION
  52.  *  Examines the integer values in a TIMEDATE structure and produces a string
  53.  *  result suitable for printing.  Formatting is according to the format
  54.  *  specification f.
  55.  *
  56.  * RETURNS
  57.  *  Pointer to structure containing string.
  58.  */
  59. struct TIMEDATE * GF_CONV fmttime(ptd,f)
  60. struct TIMEDATE *ptd;
  61. int f;
  62. {
  63.     char *pm, *pwkday,*am_pm,adate[40],atime[25],temp[45],aday[12];
  64.     int wday,hr;
  65.  
  66.     pm=monthname(ptd->month);
  67.     wday=dayweek(ptd->month,ptd->day,ptd->year);
  68.     pwkday=wkday(wday);
  69.     strcpy(aday,pwkday);
  70.     switch (f)  {
  71.     case 2:
  72.     case 3:
  73.     case 12:
  74.     case 13:
  75.     case 22:
  76.     case 23:
  77.         sprintf(adate,"%9s %2d, %4d",pm,ptd->day,ptd->year);
  78.         break;
  79.     case 4:
  80.     case 5:
  81.     case 14:
  82.     case 15:
  83.     case 24:
  84.     case 25:
  85.         sprintf(adate,"%2d/%2d/%2d",ptd->month,ptd->day,
  86.             (ptd->year%100));
  87.         break;
  88.     case 6:
  89.     case 7:
  90.     case 16:
  91.     case 17:
  92.     case 26:
  93.     case 27:
  94.         sprintf(adate,"%2d %.3s %2d",ptd->day,pm,
  95.             (ptd->year%100));
  96.         break;
  97.     case 8:
  98.     case 9:
  99.     case 18:
  100.     case 19:
  101.     case 28:
  102.     case 29:
  103.         sprintf(adate,"%2d %.3s %4d",ptd->day,pm,ptd->year);
  104.         break;
  105.     case 0:
  106.     case 10:
  107.     case 20:
  108.     case 30:
  109.         sprintf(adate,"%02d-%.3s-%02d",ptd->day,pm,
  110.             (ptd->year%100));
  111.         break;
  112.     default:
  113.     case 1:
  114.     case 11:
  115.     case 21:
  116.         sprintf(adate,"%.3s %2d, %4d",pm,ptd->day,ptd->year);
  117.         break;
  118.     }
  119.     if(ptd->hours<=12)
  120.         hr=(ptd->hours==0)?12:ptd->hours;
  121.     if(f==3||f==5||f==7||f==13||f==15||f==17||f==23||f==25||f==27||
  122.         f==29||f==9||f==19||f==19)
  123.         sprintf(atime,"%02d:%02d:%02d.%02d",ptd->hours,ptd->minutes,
  124.             ptd->seconds,ptd->hsecs);
  125.     else if(f==0||f==10||f==20||f==30) {
  126.         am_pm=(ptd->hours>=12)?stpm:stam;
  127.  
  128.         if(ptd->hours>12)
  129.             sprintf(atime,"%2d:%02d:%02d%s ",(ptd->hours-12),
  130.                 ptd->minutes,ptd->seconds,am_pm );
  131.         else 
  132.             sprintf(atime,"%2d:%02d:%02d%s ",hr,ptd->minutes,
  133.                 ptd->seconds,am_pm);
  134.     } else 
  135.         sprintf(atime,"%02d:%02d:%02d ",ptd->hours,ptd->minutes,
  136.             ptd->seconds);
  137.     if((f>=10&&f<=30)||f==0) {
  138.         strcpy(temp,adate);
  139.         if(f==0||(f>=20&&f<=30))
  140.             _xstrncpy(adate,aday,3);
  141.         else 
  142.             strcpy(adate,aday);
  143.         if(f>=11&&f<=19)
  144.             strcat( adate, ", " );
  145.         else 
  146.             strcat( adate, " " );
  147.         strcat(adate,temp);
  148.     }
  149.     strcpy(ptd->dateline,adate);
  150.     strcat(ptd->dateline," - ");
  151.     strcat(ptd->dateline, atime);
  152.     return ptd;
  153. }
  154.  
  155. /*  special no-return string copy n characters for fmttime().
  156.  */
  157. void GF_CONV _xstrncpy(to,fro,n)
  158. char *to,*fro;
  159. unsigned n;
  160. {
  161.     char *p;
  162.  
  163.     for(p=to;n--&&(*p++ = *fro++);)
  164.         ;
  165.     if(n==0xFFFF)
  166.         *p=0;
  167. }
  168.  
  169. /*
  170.  *  struct TIMEDATE *
  171.  * ggettime(void)
  172.  *
  173.  * ARGUMENT
  174.  *  none
  175.  *
  176.  * DESCRIPTION
  177.  *  Obtain the time from the system and places integer and ASCII string
  178.  *  values in the respective buffer positions.
  179.  *
  180.  * RETURNS
  181.  *     Pointer to structure containing the time and date.
  182.  *    This is set to 0 if the time has not been set.. i.e.
  183.  *    if the system indicates Jan 1, 1980.
  184.  * MODIFICATIONS
  185.  *  David Nienhiser Wed 11-Jan-1989 10:50:46
  186.  *   Renamed to ggettime() from gettime() to avoid name collisions with
  187.  *   compiler library.
  188.  */
  189. struct TIMEDATE * GF_CONV ggettime()
  190. {
  191.     struct TIMEDATE * GF_CONV sgettime();
  192.     return sgettime(0);
  193. }
  194.