home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 108_01 / datedemo.doc < prev    next >
Text File  |  1985-11-14  |  12KB  |  398 lines

  1. .OP
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.                     SOME_USEFUL_C_TIME_FUNCTIONS
  12.  
  13.  
  14.                              By Bill Bolton
  15.  
  16.                              Software Tools,
  17.  
  18.                              P.O. Box 80,
  19.  
  20.                              Newport Beach,
  21.  
  22.                              NSW, 2106
  23.  
  24.                              AUSTRALIA
  25.  
  26.  
  27.  
  28.                              Phone (02) 997-1018
  29.  
  30.  
  31. .HESOME USEFUL C TIME FUNCTIONS, By Bill Bolton              PAGE # 
  32.  
  33. .PA      
  34.                     SOME_USEFUL_C_TIME_FUNCTIONS
  35.  
  36. ABSTRACT
  37.  
  38.      Rather  than show why you SHOULD use a programming  language 
  39.  
  40. (in this case C) this article endeavours to show why I HAVE  used 
  41.  
  42. it.  An example program demonstrates some aspects of the language 
  43.  
  44. and is directly useful to owners of the Mountain Hardware 100,000 
  45.  
  46. day  clock board as well as being generally useful to others with 
  47.  
  48. real time clock facilities in their systems. A basic knowledge of 
  49.  
  50. C  will  be required to understand the  example  program,  it  is 
  51.  
  52. structured   ,commented  and  brief  extra  descriptions  of  the 
  53.  
  54. functions are given. 
  55.   
  56. WHY_C?
  57.  
  58.      While  I  have admired the C language from afar for  a  long 
  59.  
  60. time  it is only relatively recently that I have  discovered  its 
  61.  
  62. true delights. Because PASCAL was supposed to be THE language for 
  63.  
  64. structured programming and there was a need for more structure in 
  65.  
  66. my  programming  efforts,  I  had given PASCAL a  good  try.  The  
  67.  
  68. system utilities which I was interested in writing just would not 
  69.  
  70. come together properly under PASCAL,  I had the distinct  feeling 
  71.  
  72. that  the  language  was  fighting  me  rather  than  helping  my 
  73.  
  74. programming efforts.  My problems with PASCAL were not limited to 
  75.  
  76. one  implementation,  PASCAL/M,  PASCAL  MT+,  PASCAL/Z and  UCSD 
  77.  
  78. PASCAL all presented similar generic problems.  Before the PASCAL 
  79.  
  80. phreaks  rise  up again,  let me stress that there are a  lot  of 
  81.  
  82. things  about PASCAL which seem very worthwhile,  but I  couldn't 
  83.  
  84. get  I to do what the things that I need done,  in a way I  could 
  85.  
  86. grasp.
  87.  
  88.      So  for a time I retreated to assembler and FORTRAN for  the 
  89.  
  90. serious  stuff  and BASIC for the quickies.  I found that it  was 
  91.  
  92. possible to write reasonably structured programs in BASIC-80  (1) 
  93.  
  94. and  FORTRAN  80  if  a moderate amount of care  was  taken   but 
  95.  
  96. assembler was still the mainstay.  A brief flirtation with  FORTH 
  97.  
  98. followed.  FORTH  showed  great  promise but  the  temptation  to 
  99.  
  100. produce  code  which even I couldn't understand a week later  was 
  101.  
  102. too  great.   Still,  the  idea  of  an  extensible  language  is 
  103.  
  104. fascinating !
  105.  
  106.       In  the  meantime  I had been reading  "The  C  Programming 
  107.  
  108. Language"(2)  and  it was clear that C was a  language  that  was 
  109.  
  110. structured  but  not to verbose or inflexible (my major  problems 
  111.  
  112. with  PASCAL).  It also appeared to have an extensible aspect  in 
  113.  
  114. that the overhead in writing,  maintaining and using a library of 
  115.  
  116. functions was quite small.
  117.   
  118.      At  about this time a major software project of the AWA Data 
  119.  
  120. Group was started in C.  I was able to look over the programmer's 
  121.  
  122. shoulder as the project progressed and I liked what I saw. What's 
  123.  
  124. more  when  the project finished the programmers  involved  still 
  125.  
  126. seemed  sane.  I  had  a  chance to  see  a  working  C  compiler 
  127.  
  128. (Whitesmith's  for  the  PDP-11)  in  action  and  to  study  the 
  129.  
  130. documentation.  While  the  Whitesmith's compiler was out  of  my 
  131.  
  132. financial range for starters,  Leor Zolman had his BDS C compiler 
  133.  
  134. available  at a very reasonable price (on reflection I'd say  its 
  135.  
  136. an absolute steal) so that was my choice.
  137.  
  138.      As   soon  as  the compiler arrived I was  thrown  into  the 
  139.  
  140. situation  that forced me to jump in at the deep end and  use  it 
  141.  
  142. for a commercial project.  I had quoted a client 4 hours to write 
  143.  
  144. a  file conversion program in assembler,  it soon became  obvious 
  145.  
  146. that  I  had  made  a major error with my time  prediction  so  I 
  147.  
  148. decided  to try and salvage the situation by writing the  program 
  149.  
  150. in  C.  This first programming effort in C went so smoothly  that 
  151.  
  152. the project was finished within the budgeted time and the  client 
  153.  
  154. got  a  product with error handling an order of magnitude  better 
  155.  
  156. than he would have received from an assembler program. Since then 
  157.  
  158. I  have  abandoned BASIC almost entirely,  only use  FORTRAN  for 
  159.  
  160. maintenance  and  have greatly reduced the  amount  of  assembler 
  161.  
  162. programming.  The description of C as "a PASCAL that's not afraid 
  163.  
  164. to get its hands dirty"(3) is very, very apt.
  165.  
  166.      If  there  is one single thing that delayed my entry in  the 
  167.  
  168. world of C it is probably the sparsity of references to C in  the 
  169.  
  170. microcomputer  journals.  There  have  been only  a   handful  of 
  171.  
  172. programs  published and often they are examples that don't really 
  173.  
  174. do anything useful (Small C in an obvious  exception).  Following 
  175.  
  176. is one small effort to correct this situation.
  177.  
  178. THE_PROGRAM
  179.  
  180.      DATEDEMO  is a collection of C functions which will format a 
  181.  
  182. string  with  the current date and current time  fetched  from  a 
  183.  
  184. Mountain Hardware 100,000 day clock S-100 board. Several date and 
  185.  
  186. time  formats  are  available  by specification  as  one  of  the 
  187.  
  188. arguments to the primary functions.
  189.  
  190.      The  date formats are those used by the nearly all "Western" 
  191.  
  192. countries outside North America (this is an appropriate point  to 
  193.  
  194. complain  loudly about software packages that lock the user  into 
  195.  
  196. MM/DD/YY date formats !!!) but are coded in a very general manner 
  197.  
  198. so  that  nearly  any  imaginable date  format  could  be  easily 
  199.  
  200. generated  and extra formats are easily added.  Time formats  are 
  201.  
  202. somewhat  arbritary  and  only support 24 hour types  (perhaps  if 
  203.  
  204. someone is keen they will do an AM/PM 12 hour type and contribute 
  205.  
  206. it to DDJ).
  207.  
  208.       In practice I use the functions to put date and time on  in 
  209.  
  210. the  page  headings  of  hardcopy output  and  in  some  business 
  211.  
  212. packages.  The  functions live in my standard C function  library 
  213.  
  214. and are linked in at compile time.  Normally the "#define" state
  215.  
  216. ments are loaded from a header file CLOCK.H but the are listed in 
  217.  
  218. full here.  While the program is written for compilation with the 
  219.  
  220. BDS  C compiler (Version 1.4X),  it should also compile with  the 
  221.  
  222. other  C  compilers.  The only non-standard library  function  is 
  223.  
  224. 'initw' which is the BDS kludge to allow array initialisation, as 
  225.  
  226. BDS  C does not support initialisers.  As far as I know all other 
  227.  
  228. library  functions are supported by other C compilers (maybe  not 
  229.  
  230. Small C). 
  231.  
  232. THE_FUNCTIONS
  233.  
  234. 'main' -      is  simply  an executable demonstration program  to 
  235.  
  236.               tie the actual working functions together,  all the 
  237.  
  238.               presently  supported  formats are displayed  and  a 
  239.  
  240.               typical  use of the formatted strings is  shown  on 
  241.  
  242.               the last line displayed.
  243.  
  244. 'date' -      Attempts  to get basic data to format a date string 
  245.  
  246.               form the clock,  if this fails an error message  is 
  247.  
  248.               displayed.   Else  the  month  and  week  name  are 
  249.  
  250.               determined and the string formatted.
  251.  
  252. 'get_date' -  tests  if  the clock board is in the  system.  Then 
  253.  
  254.               collects digits from the clock to get the number of 
  255.  
  256.               days  since  31/Dec/1977  (N.B.   this  day  was  a 
  257.  
  258.               Saturday,  so a simple MOD 7 on the total number of 
  259.  
  260.               days since then makes Sunday day 1).  This value is 
  261.  
  262.               then  turned  into the year, month and day  of  the 
  263.  
  264.               month.
  265.  
  266. 'month_day'-  Uses  an  initialised array to determine month  and 
  267.  
  268.               day of the month from year and day of the year.
  269.  
  270. 'ndays' -     Returns number of days in the specified year.
  271.  
  272. 'leap' -      Thorough test for leap years.
  273.  
  274. 'name_month'- Fills  a string with a month name given the  number 
  275.  
  276.               of the month in range 1 to 12.
  277.  
  278. 'name_week'-  Fills a string with day of the week name given  the 
  279.  
  280.               number of the day in range 0 to 6.
  281.  
  282. 'time' -      As  for  'date'  except that time needs  much  less 
  283.  
  284.               processing.
  285.  
  286. 'read_clock'- Tests  if  clock is in system (this  test  must  be 
  287.  
  288.               duplicated  from  'get_date' as the date  and  time 
  289.  
  290.               functions  may  be used separately),  then  fetches 
  291.  
  292.               digits  from the clock and puts them straight  into 
  293.  
  294.               an integer array.
  295.  
  296.  
  297. THE HARDWARE
  298.  
  299. The  MH  board  occupies a block of  16  I/O  ports.  The  values 
  300.  
  301. returned  from  each port are shown at the head of  the  listing. 
  302.  
  303. Each value is in the form of a BCD number and for the purposes of 
  304.  
  305. this program the high nybble of the port can be ignored. A simple 
  306.  
  307. test for presense of the board in the bus can be made by  looking 
  308.  
  309. at  the  base  port,  an FF Hex will indicate that the  board  is 
  310.  
  311. missing.  An  on-board  battery keeps the clock running  all  the 
  312.  
  313. time,  I only need to reset time for Daylight saving and  usually 
  314.  
  315. do  it  using DDT's inline assembler.  The board has  been  super 
  316.  
  317. reliable  and  functions  well in a system  set up  to  IEEE  696 
  318.  
  319. standards.
  320.  
  321.      The  rest  of the system is a North Star  Horizon  mainframe 
  322.  
  323. with a Morrow Designs "Wunderbuss" 12 slot motherboard (Yes it IS 
  324.  
  325. possible  to  get  another  motherboard  into  an  Horizon,   and 
  326.  
  327. attacking  your computer with a hammer can be a  very  theraputic 
  328.  
  329. experience,  but  that is a story for another time) Godbout  Z-80 
  330.  
  331. CPU, Morrow SuperRam memory, Morrow Disk Jockey 2D controller and 
  332.  
  333. Godbout Interfacer I Serial  I/O.                  
  334.  
  335. PROGRAM_AVAILABLITY
  336.  
  337.      This program will be available from the BDS C User's  Group, 
  338.  
  339. 409 E. Kansas, Yates Centre, KS 66783. Membership of the group is 
  340.  
  341. US$10:00 a year (US$20:00 a year foreign) and numerous disks of C 
  342.  
  343. software  are  available  from  the  group  for  nominal  copying 
  344.  
  345. charges.  Amongst  the  software available is Ron Cain's Small  C 
  346.  
  347. Compiler  and Run Time Library previously published in  DDJ,  and 
  348.  
  349. also  W.C.  Colley's  6800  & 1802  cross  assemblers  previously 
  350.  
  351. described in DDJ. 
  352.  
  353.  
  354.  
  355. *****************************************************************
  356.  
  357. References:
  358.  
  359. (1)  See  CPMLABLE.BAS on CP/M User's Group Disk 46,  one  of  my 
  360.  
  361. efforts at structure in BASIC 80.
  362.  
  363. (2)   "The  C programming Language" By  Brian  W.  Kernighan  and 
  364.  
  365. Dennis M.  Ritchie,  Published by Prentice-Hall 1978, ISBN 0-13-
  366.  
  367. 110163-3.  This is the "Bible" of C and essential to any study of 
  368.  
  369. the language. 
  370.  
  371. (3)  From "C Notes" by C.T Zahn, Published By Yourdon Press 1979, 
  372.  
  373. ISBN  0-917072-13-8.  The only other readily available work on C, 
  374.  
  375. interesting  as it was written by someone outside Bell  Labs  and 
  376.  
  377. has a slightly different perspective on the language.
  378.  
  379. *****************************************************************
  380.  
  381. THE_AUTHORP
  382.  
  383.      I  am employed as Senior Technical Officer at  Multicultural 
  384.  
  385. Television Channel 0/28, involved in the maintenance of both mini 
  386.  
  387. and  microcomputer systems as well as video  systems.  BASIC  and 
  388.  
  389. FORTRAN were learned as part of a college course in 1975 and that 
  390.  
  391. was  were I became interested in  computers.  Present  activities 
  392.  
  393. include  consulting work for CP/M system users,  cordinating 80AT 
  394.  
  395. (the Australian 8080/Z80 User's Group) and writing a regular CP/M 
  396.  
  397. column in a local microcomputer journal "Your Computer". 
  398.