home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol04 / 02b / project / act / date.act next >
Text File  |  1988-11-30  |  4KB  |  168 lines

  1. /*
  2.    Miscellaneous methods to load along with DATE.CLS.
  3.  
  4.    Adam Rauch
  5.    8/26/88
  6.  
  7.    11/30/88 mzu  -added test in String:asDate to use international
  8.                   date format (e.g. MMDDYY or DDMMYY etc).
  9. */
  10.  
  11. now(Stream)!!
  12.  
  13. /* Parse an integer from a Stream.  If not found,
  14.    return the default value.                        */
  15. Def parseInt(self, delimiter, def | word result)
  16. {  if (word := word(self:Stream, delimiter))
  17.    cand (result := asInt("0" + word, 10))
  18.       ^result
  19.    else
  20.       ^def
  21.    endif
  22. }!!
  23.  
  24. now(Long)!!
  25.  
  26. /* Return date -- self is number of days since 1/1/1901. */
  27. Def asDate(self | aDate daysIntoYear remain withinLeap afterLeap)
  28. {  aDate := new(Date);
  29.    remain := asInt((self + 366L) mod 1461L);
  30.    withinLeap := afterLeap := 0;
  31.  
  32.    if remain > 58
  33.       afterLeap := 1;
  34.       if remain < 366
  35.          withinLeap := 1
  36.       endif;
  37.    endif;
  38.  
  39.    if remain < 366
  40.       daysIntoYear := remain
  41.    else
  42.       daysIntoYear := (remain - 1) mod 365
  43.    endif;
  44.  
  45.    aDate.month := daysIntoYear / 31 + 2;
  46.  
  47.    if daysBeforeMonth(aDate:Date) > (daysIntoYear - withinLeap:Int):Int
  48.       aDate.month := aDate.month - 1
  49.    endif;
  50.  
  51.    if remain == 59
  52.       withinLeap := 0
  53.    endif;
  54.  
  55.    aDate.year := asInt(((self + 366L)/1461L):Long)*4 + ((remain - afterLeap:Int)/365):Int + 1900;
  56.    aDate.day := daysIntoYear - withinLeap:Int - daysBeforeMonth(aDate:Date):Int + 1;
  57.    ^aDate
  58. }!!
  59.  
  60. now(Int)!!
  61.  
  62. /* Return a Date given month, day, and year.  Assume
  63.    twentieth century if a two-digit year is given.    */
  64. Def date(self, day, year | aDate)
  65. {  aDate := new(Date);
  66.  
  67.    aDate.month := self;
  68.    aDate.day := day;
  69.  
  70.    if year < 100
  71.       aDate.year := year + 1900
  72.    else
  73.       aDate.year := year
  74.    endif;
  75.  
  76.    ^isValid(aDate:Date)
  77. }!!
  78.  
  79. now(String)!!
  80.  
  81. /* Return a Date object parsed according to international date format */
  82. Def asDate(self | iDate)
  83. {
  84.   iDate := getProfileString("intl", "iDate"); 
  85.   select
  86.     case iDate = "2"
  87.       ^asYYMMDDDate(self);
  88.     endCase
  89.     case iDate = "1"
  90.       ^asDDMMYYDate(self);
  91.     endCase
  92.     default            /* "0" or not found */
  93.       ^asMMDDYYDate(self);
  94.     endSelect;
  95. }!!
  96.  
  97. /* Return a Date object parsed from a short form MM/DD/YY string like
  98.   "12/31/87" or "12/31/1987".  Assume twentieth century if a two-digit
  99.   year is given.      */
  100. Def asMMDDYYDate(self | aDate aStream)
  101. {  aDate := new(Date);
  102.    aStream := streamOver(self:String);
  103.  
  104.    aDate.month := parseInt(aStream:Stream, '/', 0);
  105.    aDate.day := parseInt(aStream:Stream, '/', 0);
  106.    aDate.year := parseInt(aStream:Stream, '/', 0);
  107.  
  108.    if aDate.year < 100
  109.       aDate.year := aDate.year + 1900
  110.    endif;
  111.  
  112.    ^isValid(aDate:Date)
  113. }!!
  114.  
  115. /* Return a Date object parsed from a short form DD/MM/YY string like
  116.   "31/12/87" or "31/12/1987".  Assume twentieth century if a two-digit
  117.   year is given.      */
  118. Def asDDMMYYDate(self | aDate aStream)
  119. {  aDate := new(Date);
  120.    aStream := streamOver(self:String);
  121.  
  122.    aDate.day := parseInt(aStream:Stream, '/', 0);
  123.    aDate.month := parseInt(aStream:Stream, '/', 0);
  124.    aDate.year := parseInt(aStream:Stream, '/', 0);
  125.  
  126.    if aDate.year < 100
  127.       aDate.year := aDate.year + 1900
  128.    endif;
  129.  
  130.    ^isValid(aDate:Date)
  131. }!!
  132.  
  133. /* Return a Date object parsed from a short form YY/MM/DD string like
  134.   "87/12/31" or "1987/12/31".  Assume twentieth century if a two-digit
  135.   year is given.      */
  136. Def asYYMMDDDate(self | aDate aStream)
  137. {  aDate := new(Date);
  138.    aStream := streamOver(self:String);
  139.  
  140.    aDate.year := parseInt(aStream:Stream, '/', 0);
  141.    aDate.month := parseInt(aStream:Stream, '/', 0);
  142.    aDate.day := parseInt(aStream:Stream, '/', 0);
  143.  
  144.    if aDate.year < 100
  145.       aDate.year := aDate.year + 1900
  146.    endif;
  147.  
  148.    ^isValid(aDate:Date)
  149. }!!
  150.  
  151. /* Return the string from win.ini for this app(self)/key.
  152.    If not found, return zero-length string.
  153.    This method is in Actor 1.2A and later. */
  154. Def getProfileString(self, key | str len dStr ans)
  155. { str := new(String, 80);
  156.   dStr := "";
  157.   len := Call GetProfileString(lP(self), lP(key),
  158.     lP(dStr), lP(str), 80
  159.   );
  160.   ans := subString(getText(str), 0, len);
  161.   freeHandle(self);
  162.   freeHandle(key);
  163.   freeHandle(dStr);
  164.   freeHandle(str);
  165.   ^ans;
  166. }!!
  167.  
  168.