home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / pascal / 8582 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  4.8 KB

  1. Path: sparky!uunet!tcsi.com!iat.holonet.net!news.cerf.net!usc!howland.reston.ans.net!spool.mu.edu!darwin.sura.net!sgiblab!munnari.oz.au!metro!grivel!metz.une.edu.au!ddavidso
  2. From: ddavidso@metz.une.edu.au (Dean Davidson)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: Date/time manipulation
  5. Keywords: dat,time
  6. Message-ID: <2845@grivel.une.edu.au>
  7. Date: 26 Jan 93 22:03:18 GMT
  8. References: <currin.27.727945571@unpsun1.cc.unp.ac.za>
  9. Sender: usenet@grivel.une.edu.au
  10. Organization: University of New England, Armidale, Australia
  11. Lines: 184
  12.  
  13. In article <currin.27.727945571@unpsun1.cc.unp.ac.za> currin@unpsun1.cc.unp.ac.za (Michael Currin) writes:
  14. >Hi All
  15. >
  16. >I have a line of text that is generated by a package that we use that looks 
  17. >like :-
  18. >
  19. >  23/01/93, 00:01:31, 31, NAME OF SOMEONE
  20. >
  21. >Does anyone have any code that takes a minutes figure away from the date and 
  22. >time ?
  23. >
  24. The following should do the trick.  Note that it uses a non-TP-standard
  25. date/time record structure, but you could modify it if you wanted to.
  26.  
  27. Good luck,
  28. Dean
  29.  
  30. ------------------------------------------------------------------------------
  31.  
  32. unit timeadj;
  33.  
  34. interface
  35.  
  36. type
  37.  
  38. timtyp  = record             {time record}
  39.             hour  : byte;
  40.             min   : byte;
  41.           end;
  42.  
  43. dattyp  = record             {date record}
  44.             year : integer;
  45.             mon  : byte;
  46.             day  : byte;
  47.             dayno: byte;
  48.           end;
  49.  
  50. dttyp   = record             {date time record}
  51.             time : timtyp;
  52.             date : dattyp;
  53.           end;
  54.  
  55. function adjtime(od : dttyp ; nmins : integer ; var nd : dttyp) : boolean;
  56.             {add/subtract nmins to od to give nd}
  57.             {return T if day change}
  58.  
  59. implementation
  60.  
  61. {Date/Julian Day conversion routines
  62.  Valid from 1582 onwards
  63.  from James Miller G3RUH, Cambridge, England}
  64.  
  65. const
  66. {days in a month}
  67. monthd  : array [1..12] of byte = (31,28,31,30,31,30,31,31,30,31,30,31);
  68.  
  69. d0 : longint = -428; {James defines this as the general day number}
  70.  
  71. procedure date2jul(var dn : longint ; dat : dattyp);
  72. {calc julian date DN from date DAT}
  73. var
  74. m : byte;
  75.  
  76. begin
  77.   with dat do
  78.     begin
  79.       m := mon;
  80.       if m <= 2 then
  81.         begin
  82.           m := m + 12;
  83.           dec(year);
  84.         end;
  85.       dn := d0 + day + trunc(30.61 * (m + 1)) + trunc(365.25 * year) +
  86.       {the next line may be omitted if only used from Jan 1900 to Feb 2100}
  87.             trunc(year / 400) - trunc(year / 100) + 15;
  88.     end
  89. end; {date2jul}
  90.  
  91. procedure jul2date(dn : longint ; var dat : dattyp);
  92. {calc date DAT from julian date DN}
  93. var
  94. d : longint;
  95.  
  96. begin
  97.   with dat do
  98.     begin
  99.       d := dn - d0;
  100.       dayno := (d + 5) mod 7;
  101.       {the next line may be omitted if only used from Jan 1900 to Feb 2100}
  102.       d := d + trunc( 0.75 * trunc(1.0 * (d + 36387) / 36524.25)) - 15;
  103.       year := trunc((1.0 * d - 122.1) / 365.25);
  104.       d := d - trunc(365.25 * year);
  105.       mon := trunc(d / 30.61);
  106.       day := d - trunc(30.61 * mon);
  107.       dec(mon);
  108.       if mon > 12 then
  109.         begin
  110.           mon := mon - 12;
  111.           inc(year);
  112.         end;
  113.     end;
  114. end;  {jul2date}
  115.  
  116. function juld2date(jul : word ; var jd : dattyp) : boolean;
  117. {convert julian day  to date}
  118. {ret T if no err}
  119.  
  120. var
  121. sum : integer;
  122. j : longint;
  123.  
  124. begin
  125.   if jul > 366 then
  126.     begin
  127.       juld2date := False;
  128.       exit;
  129.     end
  130.   else
  131.     juld2date := True;
  132.   if (jd.year mod 4) = 0 then
  133.     monthd[2] := 29
  134.   else
  135.     monthd[2] := 28;
  136.   sum := 0;
  137.   jd.mon := 0;
  138.   repeat
  139.     inc(jd.mon);
  140.     sum := sum + monthd[jd.mon];
  141.   until sum >= jul;
  142.   sum := sum - monthd[jd.mon];
  143.   jd.day := jul - sum;
  144.   date2jul(j,jd);
  145.   jul2date(j,jd);
  146. end; {juld2date}
  147.  
  148. procedure adjdate(od : dattyp ; ndays : integer ; var nd : dattyp);
  149.             {add/subtract ndays to od to give nd}
  150.  
  151. var
  152. j : longint;
  153.  
  154. begin
  155.   date2jul(j,od);
  156.   j := j + ndays;
  157.   jul2date(j,nd);
  158. end;
  159.  
  160. function adjtime(od : dttyp ; nmins : integer ; var nd : dttyp) : boolean;
  161.             {add/subtract nmins to od to give nd}
  162.             {return T if day change}
  163. var
  164. emins : integer;
  165. tnd   : dttyp; {needed in case routine called with od & nd the same}
  166.  
  167. begin
  168.   adjtime := False;
  169.   tnd := od;
  170.   emins := od.time.hour*60 + od.time.min + nmins;
  171.   if emins > 1439 then
  172.     begin
  173.       adjtime :=  True;
  174.       emins := emins - 1440;
  175.       adjdate(od.date,1,tnd.date);
  176.     end;
  177.   if emins < 0 then
  178.     begin
  179.       adjtime :=  True;
  180.       emins := emins + 1440;
  181.       adjdate(od.date,-1,tnd.date);
  182.     end;
  183.   tnd.time.hour := emins div 60;
  184.   tnd.time.min  := emins mod 60;
  185.   nd := tnd;
  186. end;   {adjtime}
  187.  
  188. end.
  189.  
  190. -----------------------------------------------------------------------------
  191.  
  192. -- 
  193. Dean Davidson                                 ddavidso@metz.une.edu.au
  194. Dept Psychology                               Phone 61 67 73 2585
  195. University of New England                     Fax   61 67 72 9816
  196. Armidale NSW 2351 Australia                   VK2 ZID
  197.