home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol04 / 02b / project / classes / date.cls < prev    next >
Text File  |  1988-11-30  |  6KB  |  267 lines

  1. /* Object to hold a date -- created 8/26/88 acr
  2.  
  3.    11/30/88 mzu  -added a test in asString to use international
  4.                   date format.
  5.                  -asLongString, dayOfWeek are hard coded in english
  6. */!!
  7.  
  8. inherit(Magnitude, #Date, #(month   /*  Month 1-12             */
  9. day     /*  Day   1-31             */
  10. year    /*  Year                   */), 2, nil)!!
  11.  
  12. now(DateClass)!!
  13.  
  14. /* Create a new Date object and initialize. */
  15. Def new(self)
  16. {  ^init(new(self:Behavior))
  17. }!!
  18.  
  19. /* Return current date. */
  20. Def current(self | ds)
  21. {  ds := new(Struct, 16);
  22.    putMSB(ds, 0x2a, 4);
  23.    call(ds);
  24.  
  25.    ^date(atMSB(ds, 10), atLSB(ds, 10), wordAt(ds, 8))
  26. }!!
  27.  
  28. now(Date)!!
  29.  
  30. /* Return date in string in international date format */
  31. Def asString(self | iDate)
  32. {
  33.   iDate := getProfileString("intl", "iDate"); 
  34.   select
  35.     case iDate = "2"
  36.       ^asYYMMDDString(self);
  37.     endCase
  38.     case iDate = "1"
  39.       ^asDDMMYYString(self);
  40.     endCase
  41.     default            /* "0" or not found */
  42.       ^asMMDDYYString(self);
  43.     endSelect;
  44. }!!
  45.  
  46. /* Return date in abbreviated YY/MM/DD string format. */
  47. Def asYYMMDDString(self)
  48. {  ^asPaddedString(year mod 100, 2) + "/" +
  49.     asString(month) + "/" +
  50.     asString(day);
  51. }!!
  52.  
  53. /* Return date in abbreviated MM/DD/YY string format. */
  54. Def asMMDDYYString(self)
  55. {  ^asString(month) + "/" +
  56.     asString(day)   + "/" +
  57.     asPaddedString(year mod 100, 2)
  58. }!!
  59.  
  60. /* Return date in abbreviated DD/MM/YY string format. */
  61. Def asDDMMYYString(self)
  62. {  ^asString(day) + "/" +
  63.     asString(month)   + "/" +
  64.     asPaddedString(year mod 100, 2)
  65. }!!
  66.  
  67. /* Return date in long string format. */
  68. Def asLongString(self)
  69. {  ^#("January" "February" "March" "April" "May" "June" "July"
  70.      "August" "September" "October" "November" "December")[month-1] +
  71.      " "  + asString(day) + ", " + asString(year)
  72. }!!
  73.  
  74. /* Return date in 'day of the week' format. */
  75. Def asDayString(self)
  76. {  ^dayOfWeek(self) + ", " + asLongString(self)
  77. }!!
  78.  
  79. /* Return number of days since 1/1/1901. */
  80. Def asLong(self)
  81. {  ^asLong(
  82.       (year - 1901) * 365 +
  83.       daysBeforeMonth(self) +
  84.       day - 1 +
  85.       leapDays(self)
  86.    )
  87. }!!
  88.  
  89. /* Included so that addition and subtraction
  90.    will work with Longs or Ints.             */
  91. Def asInt(self)
  92. {  ^asLong(self)
  93. }!!
  94.  
  95. /* Infix operation.  Return 0 if x = self, otherwise returns nil. */
  96. Def =(self, x)
  97. {  if class(self) == class(x)
  98.       ^(x.day == day) cand (x.month == month) cand (x.year == year)
  99.    else
  100.       ^nil
  101.    endif
  102. }!!
  103.  
  104. /* Infix operation.  Return 0 if x < self, otherwise return nil. */
  105. Def <(self, x)
  106. {  select
  107.       case (x.year < year)
  108.          ^0
  109.       endCase
  110.       case (x.year == year) cand (x.month < month)
  111.          ^0
  112.       endCase
  113.       case (x.year == year) cand (x.month == month) cand (x.day < day)
  114.          ^0
  115.       endCase
  116.       default
  117.          ^nil
  118.    endSelect;
  119. }!!
  120.  
  121. /* Infix operation.  Return 0 if x <= self, otherwise return nil. */
  122. Def <=(self, x)
  123. {  ^(x < self) cor (x = self)
  124. }!!
  125.  
  126. /* Infix operation.  Return 0 if x > self, otherwise return nil. */
  127. Def >(self, x)
  128. {  select
  129.       case (x.year > year)
  130.          ^0
  131.       endCase
  132.       case (x.year == year) cand (x.month > month)
  133.          ^0
  134.       endCase
  135.       case (x.year == year) cand (x.month == month) cand (x.day > day)
  136.          ^0
  137.       endCase
  138.       default
  139.          ^nil
  140.    endSelect;
  141. }!!
  142.  
  143. /* Infix operation.  Return 0 if x >= self, otherwise return nil. */
  144. Def >=(self, x)
  145. {  ^(x > self) cor (x = self)
  146. }!!
  147.  
  148. /* Infix operation.  Add a Date to another Date, an Int, or a Long.
  149.    Return a Long.  You must use asDate() to convert back to a Date.   */
  150. Def +(self, x)
  151. {  ^asLong(x) + asLong(self):Long
  152. }!!
  153.  
  154. /* Infix operation.  Find difference between two dates, or
  155.    subtract a number of days (an Int or Long) from a Date.   */
  156. Def -(self, x)
  157. {  ^asLong(x) - asLong(self):Long
  158. }!!
  159.  
  160. /* Return number of days between two dates.  This
  161.    is a more efficient than date1 - date2.         */
  162. Def diff(self, y)
  163. {  ^(year - y.year) * 365 +
  164.       daysBeforeMonth(self) - daysBeforeMonth(y) + 
  165.       day - y.day +
  166.       leapDays(self) - leapDays(y)
  167. }!!
  168.  
  169. /* Increment a Date by one, modifying it directly. */
  170. Def inc(self)
  171. {  day := day + 1;
  172.  
  173.    if day > #(31 28 31 30 31 30 31 31 30 31 30 31)[month - 1]
  174.       if month ~= 2 cor not(year mod 4 == 0) cor day > 29
  175.          month := month + 1;
  176.          day := 1;
  177.          if month > 12
  178.             year := year + 1;
  179.             month := 1;
  180.          endif;
  181.       endif;
  182.    endif
  183. }!!
  184.  
  185. /* Decrement a Date by one, modifying it directly. */
  186. Def dec(self)
  187. {  day := day - 1;
  188.  
  189.    if day == 0
  190.       month := (month - 1);
  191.  
  192.       if month == 0
  193.          year := year - 1;
  194.          month := 12;
  195.       endif;
  196.  
  197.       day := #(0 31 28 31 30 31 30 31 31 30 31 30 31)[month];
  198.  
  199.       if (month == 2) cand (year mod 4 == 0)
  200.          day := 29
  201.       endif;
  202.    endif
  203. }!!
  204.  
  205. /* Given a date, return the next date.  Preserve original date. */
  206. Def next(self | aDate)
  207. {  aDate := new(Date);
  208.    aDate.day := day;
  209.    aDate.month := month;
  210.    aDate.year := year;
  211.    ^inc(aDate)
  212. }!!
  213.  
  214. /* Given a date, return the previous date.  Preserve original date. */
  215. Def previous(self | aDate)
  216. {  aDate := new(Date);
  217.    aDate.day := day;
  218.    aDate.month := month;
  219.    aDate.year := year;
  220.    ^dec(aDate)
  221. }!!
  222.  
  223. /* Return number of February 29s since 1/1/1901. */
  224. Def leapDays(self)
  225. {  if (month < 3) cand (year mod 4 == 0)
  226.       ^(year - 1900)/4 - 1
  227.    else 
  228.       ^(year - 1900)/4
  229.    endif
  230. }!!
  231.  
  232. /* Private method.  Return number of days in a year before this month. */
  233. Def daysBeforeMonth(self)
  234. {  ^#(0 31 59 90 120 151 181 212 243 273 304 334 365)[abs((month - 1) mod 13)]
  235. }!!
  236.  
  237. /* Initialize a date to 1/1/80. */
  238. Def init(self)
  239. {  month := 1;
  240.    day := 1;
  241.    year := 1980;
  242. }!!
  243.  
  244. /* Determine if a Date is valid.  Used by conversion
  245.    methods to guarantee that dates are always valid.  */
  246. Def isValid(self)
  247. {  if self <> asDate(asLong(self))
  248.       ^nil
  249.    endif
  250. }!!
  251.  
  252. /* Return day of the week. */
  253. Def dayOfWeek(self)
  254. {  ^#("Tuesday" "Wednesday" "Thursday"
  255.       "Friday" "Saturday" "Sunday" "Monday")[asInt(asLong(self) mod 7)]
  256. }!!
  257.  
  258. /* Print a date. */
  259. Def print(self)
  260. {  print(asString(self))
  261. }!!
  262.  
  263. /* Put a date onto the specified Stream. */
  264. Def printOn(self, aStrm)
  265. {  printOn(asString(self), aStrm)
  266. }!!
  267.