home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / UT_DATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-10  |  4.8 KB  |  278 lines

  1. /*********************
  2.  *
  3.  *  ut_date.c - utility date functions.
  4.  *
  5.  *  Purpose: This file contains functions to operate on the date and time.
  6.  *
  7.  *  Blackstar C Function Library
  8.  *  (c) Copyright 1985,1989 Sterling Castle Software
  9.  *
  10.  *******/
  11.  
  12. #include <string.h>
  13. #include "blackstr.h"
  14. #include "ut_head.h"
  15.  
  16.  
  17. /********
  18.  *
  19.  *   ut_gtod() - gets time and date
  20.  *
  21.  **/
  22.  
  23. void ut_gtod(void)
  24. {
  25.     ut_gdate();
  26.     sy_gtime();
  27. }
  28.  
  29.  
  30. /********
  31.  *
  32.  *   ut_gdate() - get date
  33.  *
  34.  **/
  35.  
  36. void ut_gdate(void)
  37. {
  38.     sy_gdate();             /* use library function */
  39.     leapf_ = (year_%4)? 0 : 1;
  40. }
  41.  
  42.  
  43. /********
  44.  *
  45.  *   ut_gtime() - get time
  46.  *
  47.  **/
  48.  
  49. void ut_gtime(void)
  50. {
  51.     sy_gtime();             /* use pclib function */
  52. }
  53.  
  54.  
  55. /********
  56.  *
  57.  *   ut_datel() - get long date
  58.  *
  59.  **/
  60.  
  61. long ut_datel(void)
  62. {
  63.     ut_gdate();             /* get date */
  64.     return(((year_-1900)*10000L)+(month_*100L)+(long)day_);
  65. }
  66.  
  67.  
  68. /********
  69.  *
  70.  *   ut_timel() - get long time
  71.  *
  72.  **/
  73.  
  74. long ut_timel(void)
  75. {
  76.     sy_gtime();
  77.     return((hour_*10000L) + (minute_*100L) + (long)sec_);
  78. }
  79.  
  80.  
  81. /********
  82.  *
  83.  *   ut_dates(sdate) - get date to structure
  84.  *
  85.  **/
  86.  
  87. void ut_dates(struct DATE *sdate)
  88. {
  89.     ut_gdate();
  90.     sdate->yr = year_;
  91.     sdate->mo = month_;
  92.     sdate->dy = day_;
  93. }
  94.  
  95.  
  96. /********
  97.  *
  98.  *   ut_times(stime) - get time to structure
  99.  *
  100.  **/
  101.  
  102. void ut_times(struct TIME *stime)
  103. {
  104.     sy_gtime();
  105.     stime->hr = hour_;
  106.     stime->mn = minute_;
  107.     stime->sec = sec_;
  108.     stime->hsec = hsec_;
  109. }
  110.  
  111.  
  112. /********
  113.  *
  114.  *   ut_sdatestr(str,sdate) - get structured date to string
  115.  *
  116.  **/
  117.  
  118. void ut_sdatestr(char *str, struct DATE *sdate)
  119. {
  120.     char buff[30];
  121.     int year;
  122.  
  123.     if(dateoptf_&DTNDF) {
  124.     sprintf(str,"%s, ",ut_daystr(ut_sday(sdate)));/*day */
  125.     }else
  126.     strcpy(str,"");
  127.  
  128.     if(dateoptf_&DTNMF) {
  129.     sprintf(buff,"%s %d, ",ut_mostr(sdate->mo),sdate->dy);
  130.     strcat(str,buff);
  131.     }else {
  132.     sprintf(buff,"%2d/%d/",sdate->mo,sdate->dy);
  133.     strcat(str,buff);
  134.     }
  135.  
  136.     year = sdate->yr;
  137.     if(dateoptf_&DYR2F) {
  138.     year %= 100;                    /* 2 digit year */
  139.     sprintf(buff,"%2d",year);       /* add year     */
  140.     strcat(str,buff);
  141.     }else {                             /* 4 digit year */
  142.     sprintf(buff,"%4d",year);       /* add year     */
  143.     strcat(str,buff);
  144.     }
  145. }
  146.  
  147.  
  148. /********
  149.  *
  150.  *   ut_stimestr(str,stime) - get struct TIME to string in hh:mm am format
  151.  *
  152.  **/
  153.  
  154. void ut_stimestr(char *str, struct TIME *stime)
  155. {
  156.     char *ampm;
  157.     int hour;
  158.  
  159.     hour = stime->hr;
  160.     ampm = "";
  161.     if(!(timeoptf_&T24HF)) {                 /* 24 hr format */
  162.     if(hour>11)
  163.         ampm = "pm";
  164.     else
  165.         ampm = "am";
  166.     if(!hour)
  167.         hour= 12;
  168.     else
  169.         if(hour>12)
  170.     hour %= 12;
  171.     }
  172.     if(!(timeoptf_&TSECF))
  173.     sprintf(str,"%2d:%02d",hour,stime->mn);
  174.     else
  175.     sprintf(str,"%2d:%02d:%02d.%02d",hour,stime->mn,
  176.                             stime->sec,stime->hsec);
  177.     strcat(str,ampm);
  178. }
  179.  
  180.  
  181. /********
  182.  *
  183.  *   ut_ldates(sdate,ldate) - convert long date to structure
  184.  *
  185.  **/
  186.  
  187. void ut_ldates(struct DATE *sdate, long int ldate)
  188. {
  189.     sdate->yr = ldate/10000L;                       /* year */
  190.     sdate->mo = (ldate -(sdate->yr*10000L))/100;    /* month */
  191.     sdate->dy = (ldate-(sdate->yr * 10000L))%100;   /* day */
  192.     if(!(dateoptf_&DYR2F))
  193.     sdate->yr += 1900;
  194. }
  195.  
  196.  
  197. /********
  198.  *
  199.  *   ut_ldatestr(str,ldate) - convert long date to string
  200.  *
  201.  **/
  202.  
  203. void ut_ldatestr(char *str, long ldate)
  204. {
  205.     struct DATE sdate;
  206.  
  207.     ut_ldates(&sdate,ldate);                /* convert to structure */
  208.     ut_sdatestr(str,&sdate);
  209. }
  210.  
  211.  
  212. /********
  213.  *
  214.  *   ut_ltimestr(str,ltime) - convert long time to string
  215.  *
  216.  **/
  217.  
  218. void ut_ltimestr(char *str, long ltime)
  219. {
  220.     struct TIME stime;
  221.  
  222.     ut_ltimes(&stime,ltime);
  223.     ut_stimestr(str,&stime);
  224. }
  225.  
  226.  
  227. /********
  228.  *
  229.  *   ut_ltimes(stime,ltime) - convert long time to structure
  230.  *
  231.  **/
  232.  
  233. void ut_ltimes(struct TIME *stime, long ltime)
  234. {
  235.     stime->hr = ltime/10000L;
  236.     stime->mn = (ltime-stime->hr*10000L)/100L;
  237.     stime->sec = ltime%100;
  238.     stime->hsec = 0;        /* no room for hsecs */
  239. }
  240.  
  241.  
  242. /********
  243.  *
  244.  *   ut_hmtimestr(str,hr,mn) - convert hours,minutes to string
  245.  *
  246.  **/
  247.  
  248. void ut_hmtimestr(char *str, int hr, int mn)           /* must be 8 characters */
  249. {
  250.     struct TIME stime;
  251.  
  252.     stime.hr = hr;
  253.     stime.mn = mn;
  254.     stime.sec=stime.hsec=0;
  255.     ut_stimestr(str,&stime);
  256. }
  257.  
  258.  
  259. /********
  260.  *
  261.  *   ut_pause(time) - pause until time 1/100's secs passed
  262.  *
  263.  **/
  264.  
  265. void ut_pause(int time)
  266. {
  267.     struct TIME stime;
  268.     int t1,t2;
  269.  
  270.     ut_times(&stime);               /* get current time*/
  271.     t1 = t2 = 100*stime.sec+stime.hsec;
  272.     while(t2-t1<time) {
  273.     ut_times(&stime);
  274.     t2 = 100*stime.sec+stime.hsec;
  275.     if(t2<t1) t2 += 6000;     /* add a minute */
  276.     }
  277. }
  278.