home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TOTDOC.ZIP / CHAPT13.TXT < prev    next >
Encoding:
Text File  |  1991-02-11  |  16.9 KB  |  425 lines

  1.                                                                         Managing
  2.                                                                            Dates
  3.  
  4.  
  5.  
  6.  
  7.  
  8.          "The brain is a wonderful organ; it starts the minute you get up in the
  9.          morning and does not stop until you get to the office."
  10.  
  11.                                                                     Robert Frost
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.          Date manipulation has always been a problem for computer programmers.
  20.          If only there were ten days in a week, ten weeks in a year, and ten
  21.          years in a century! Date mathematics is just plain clumsy when you
  22.          think of dates in days, months and years. Try computing the date one
  23.          hundred days after February 20, 1956! Not so easy.
  24.  
  25.          Date mathematics is greatly simplified when you convert dates into
  26.          their Julian form. Over the centuries, many mathematicians have devised
  27.          methods of accurately computing dates. Most modern computer systems
  28.          (including this Toolkit!) use the Julian system. The Julian period was
  29.          devised in 1582 by Joseph Scaliger, and was named after his father
  30.          Julias (not after the Julian calendar). The Julian system assigns each
  31.          day its own unique number. Day 1 began at noon, January 1, 4713 B.C.
  32.          Every day since that time has its own sequential number, and February
  33.          11, 1991 is Julian period 2448299.
  34.  
  35.          The totDATE unit provides a variety of functions for converting dates
  36.          to and from Julian format. To compute the date one hundred days after
  37.          February 20, 1956, you would convert the date to Julian, add one
  38.          hundred to it, and convert it back to a string. This can be achieved
  39.          with the following compound statement using Toolkit functions.
  40.  
  41.                   FancyDateStr(GregToJul(2,20,1956)+100,true,true);
  42.  
  43.          This uses the function GregToJul to convert Month/Day/Year to a Julian
  44.          period. A hundred is then added to it, and this value is passed to the
  45.          function FancyDateStr, which returns a text string showing the day and
  46.          date represented by the Julian period. In this example, the following
  47.          date would be returned:
  48.  
  49.                   Wednesday May 30, 1956
  50.  
  51.          The Toolkit supports dates in eight different formats. Using M, D and Y
  52.          to represent months, days and years respectively, the supported formats
  53.          are:
  54.  
  55.                                   MMDDYY
  56.                                   MMDDYYYY
  57.                                   MMYY
  58.                                   MMYYYY
  59.                                   DDMMYY
  60.  
  61. 13-2                                                                User's Guide
  62.  
  63. --------------------------------------------------------------------------------
  64.  
  65.                                   DDMMYYYY
  66.                                   YYMMDD
  67.                                   YYYYMMDD
  68.  
  69.          Many of the totDATE functions need to know in which format the date
  70.          strings are, and so the unit includes the enumerated type tDate, which
  71.          (not surprisingly) has the following members: MMDDYY, MMDDYYYY, MMYY,
  72.          MMYYYY, DDMMYY, DDMMYYYY, YYMMDD and YYYYMMDD.
  73.  
  74.  
  75.  
  76.  
  77.            Note: if you plan to delve into ancient history with your date
  78.            calculations, you need to be aware of some strange facts. For
  79.            example, different countries decided to drop certain days to
  80.            adjust for inaccuracies in the lunar calendar. In 1752, the Brit-
  81.            ish decreed that the day following September 2 would be September
  82.            14. Further back in 1582, Pope Gregory XIII (of Gregorian calendar
  83.            fame!) decreed that October 4 would become October 15. In 1793 the
  84.            French .... well, you get the idea. For a good summary of these
  85.            calendar aberrations, refer to any copy of The World Almanac and
  86.            Book of Facts.
  87.  
  88.  
  89.  
  90.  
  91. Using DateTOT
  92.  
  93.          The totDATE unit includes the object DateOBJ, and a global instance of
  94.          this object, DateTOT. This instance is used to control two of the
  95.          default date configurations. The following two methods control these
  96.          defaults:
  97.  
  98.  
  99.          SetLastYearNextCentury(yr:byte);
  100.  
  101.          When a year is specified with only two digits, the Toolkit has to
  102.          decide to which century the year refers. This method is used to set the
  103.          largest year, which will be interpreted as falling in the next century.
  104.          For example, if this method is passed a value of 50, then the two digit
  105.          year 50 will be assumed to be 2050, and the two digit year 51 will be
  106.          assumed to be 1951. By default, this year is set to 20.
  107.  
  108.  
  109.          SetSeparator(Sep:char);
  110.  
  111.          When dates are displayed in their numeric form, a character is used to
  112.          separate the days from the months from the years, e.g. 12-01-1990. This
  113.          default is used to control which character will be used as the separa-
  114.          tor. By default, the character is a slash, i.e. '/'.
  115.  
  116.  
  117.  
  118.          The following two function methods return the current settings:
  119.  
  120.  
  121.  
  122. Managing Dates                                                              13-3
  123.  
  124. --------------------------------------------------------------------------------
  125.  
  126.          GetLastYearNextCentury: byte;
  127.          GetSeparator: char;
  128.  
  129.  
  130.  
  131.          Another purpose of the DateOBJ object is to control how the days of the
  132.          week and months are spelled. This feature is provided for international
  133.          users who want to use a different language. The following DateOBJ meth-
  134.          ods are supported:
  135.  
  136.  
  137.          SetMonths(M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12: StrShort);
  138.  
  139.          This method defines how the months will be spelled. The method is
  140.          passed twelve strings representing the months January through December.
  141.  
  142.  
  143.          SetDays(D0,D1,D2,D3,D4,D5,D6:StrShort);
  144.  
  145.          This method defines how the days will be spelled. The method is passed
  146.          seven strings representing the days Sunday through Saturday.
  147.  
  148.  
  149.          GetMonth(Mth:byte):string;
  150.  
  151.          This function method returns a string representing a specific month.
  152.          The method is passed a byte parameter, with a value between one and
  153.          twelve, to indicate which month you want returned.
  154.  
  155.  
  156.          GetDay(Day:byte):string;
  157.  
  158.          This function method returns a string representing a specific day. The
  159.          method is passed a byte parameter, with a value between zero and six,
  160.          to indicate which month you want returned. Day zero represents Sunday.
  161.  
  162.  
  163.  
  164. Date Functions
  165.  
  166.          Listed below are the functions used to manipulate date numbers and
  167.          strings:
  168.  
  169. GregToJul
  170.  
  171.          GregToJul(M,D,Y:longint): longint;
  172.  
  173.          Returns the Julian value of a Gregorian date. The function is passed
  174.          three parameters of type byte, shortint, integer, word or longint; the
  175.          first parameter is the month, the second is the day, and the third is
  176.          the four-digit year. For example:
  177.  
  178.                   MyJul := GregToJul(3,21,1957);
  179.  
  180.          Assigns the value 1741959 to MyJul.
  181.  
  182.  
  183. 13-4                                                                User's Guide
  184.  
  185. --------------------------------------------------------------------------------
  186.  
  187. JulToGreg
  188.  
  189.          JulToGreg(Jul:longint; var M,D,Y:longint);
  190.  
  191.          This procedure converts a Julian date to Gregorian. The function is
  192.          passed four parameters; the first is the source Julian date, and the
  193.          remaining three parameters must be variables of type longint. The ini-
  194.          tial values of these three variables are ignored, and they are updated
  195.          with the month, day and year equivalent of the Julian value. For
  196.          example:
  197.  
  198.                   var
  199.                      Day,Mon,Yr: longint;
  200.                   {...}
  201.                   JulToGreg(2447654,Mon,Day,Yr);
  202.  
  203.          Assigns the values 5, 7 and 1989 to Mon, Day and Yr, respectively.
  204.  
  205.  
  206.  
  207. GregToStr
  208.  
  209.          GregToStr(M,D,Y:longint; Format:tDate):string;
  210.  
  211.          Returns a string representing the Gregorian date specified. The func-
  212.          tion is passed four parameters; the first three parameters may be of
  213.          type byte, shortint, word, integer or longint, representing the month,
  214.          day and year. The fourth parameter is a member of the enumerated type
  215.          tDate representing the desired string format. For example:
  216.  
  217.                   DStr := GregToStr(2,11,1991,DDMMYY);
  218.  
  219.          Assigns the value '11/02/91' to DStr.
  220.  
  221.  
  222.  
  223. JulToStr
  224.  
  225.          JulToStr(Jul:longint;Format:tDate):string;
  226.  
  227.          Returns a string representing a Julian date. The function is passed two
  228.          parameters; the Julian date, and the date format. For example:
  229.  
  230.                   DStr := JulToStr(2448299,YYYYMMDD);
  231.  
  232.          Assigns the value '1991/02/11' to DStr.
  233.  
  234. DOWJul
  235.  
  236.          DOWJul(Jul:longint):byte;
  237.  
  238.          Returns a byte representing the day of the week for a Julian date. The
  239.          function is passed one parameter; the Julian date. Zero represents
  240.          Sunday, and six represents Saturday, all other days falling in between
  241.          these two. For example:
  242.  
  243. Managing Dates                                                              13-5
  244.  
  245. --------------------------------------------------------------------------------
  246.  
  247.                   TheDay := DOWJul(2448299);
  248.  
  249.          Assigns the value 1 to TheDay, i.e. Monday.
  250.  
  251.  
  252.  
  253. DOWStr
  254.  
  255.          DOWStr(DStr:string; Format:tDate):byte;
  256.  
  257.          This function is similar to DOWJul in as much as it returns the day of
  258.          the week for a specified date. In this case, however, the date is
  259.          specified in string form. The function is passed two parameters; the
  260.          date in string form, and the date format. For example:
  261.  
  262.                   TheDay := DOWStr('02/11/91',MMDDYY);
  263.  
  264.          Assigns the value 1 to TheDay.
  265.  
  266.  
  267.  
  268. Day
  269.  
  270.          Day(DStr:string; Format:tDate): word;
  271.  
  272.          This function returns the day represented by a date string. The func-
  273.          tion is passed two parameters; the date in string form, and the date
  274.          format. For example:
  275.  
  276.                   TheDay := Day('05/30/1956',MMDDYYYY);
  277.  
  278.          Assigns the value 30 to TheDay.
  279.  
  280.  
  281. Month
  282.  
  283.          Month(DStr:string; Format:tDate): word;
  284.  
  285.          This function returns the month represented by a date string. The func-
  286.          tion is passed two parameters; the date in string form, and the date
  287.          format. For example:
  288.  
  289.                   TheMon := Month('30/05/1956',DDMMYYYY);
  290.  
  291.          Assigns the value 5 to TheMon.
  292.  
  293.  
  294. Year
  295.  
  296.          Year(DStr:string; Format:tDate): word;
  297.  
  298.          This function returns the year represented by a date string. The func-
  299.          tion is passed two parameters; the date in string form, and the date
  300.          format. For example:
  301.  
  302.                   TheYr := Year('1956/05/30',YYYYMMDD);
  303.  
  304. 13-6                                                                User's Guide
  305.  
  306. --------------------------------------------------------------------------------
  307.  
  308.          Assigns the value 1956 to TheYr.
  309.  
  310.  
  311. ValidDate
  312.  
  313.          ValidDate(M,D,Y:longint):boolean;
  314.  
  315.          This function returns true if the Gregorian date specified is valid,
  316.          i.e. the month is between one and twelve, and the day is valid for the
  317.          specified month. The function is passed three parameters of type byte,
  318.          shortint, word, integer or longint, representing the month, day and
  319.          year, respectively. For example:
  320.  
  321.                   OK := ValidDate(2,31,1991);
  322.  
  323.          Assigns the value false to OK.
  324.  
  325.  
  326. ValidDateStr
  327.  
  328.          ValidDateStr(DStr:string;Format:tDate):boolean;
  329.  
  330.          This function is similar to ValidDate, except that it validates a
  331.          string date. The function is passed two parameters; the date in string
  332.          form, and the date format. For example:
  333.  
  334.                   OK := ValidDateStr('02/28/1991',MMDDYYYY);
  335.  
  336.          Assigns the value true to OK.
  337.  
  338.  
  339. TodayInJul
  340.  
  341.          TodayInJul:longint;
  342.  
  343.          This function calls a DOS interrupt, and returns the system's date in
  344.          Julian form. It is passed no parameters. For example:
  345.  
  346.                   writeln(JulToStr(TodayInJul,DDMMYY));
  347.  
  348.          Writes the value of the system's date in string form.
  349.  
  350.  
  351. StartOfYear
  352.  
  353.          StartOfYear(Jul:longint):longint;
  354.  
  355.          Returns the Julian date of January 1 of the year in which the passed
  356.          date falls. The function is passed one parameter; the Julian date of
  357.          the year to be returned. For example:
  358.  
  359.                   JanJul := StartOfYear(2448299);
  360.  
  361.          Assigns the value 2448258, representing the date Jan 1 1991, to JanJul.
  362.  
  363. Managing Dates                                                              13-7
  364.  
  365. --------------------------------------------------------------------------------
  366.  
  367. EndOfYear
  368.  
  369.          EndOfYear(Jul:longint):longint;
  370.  
  371.          Returns the Julian date of December 31 of the year in which the passed
  372.          date falls. The function is passed one parameter; the Julian date of
  373.          the year to be returned. For example:
  374.  
  375.                   DecJul := EndOfYear(2448299);
  376.  
  377.          Assigns the value 2448622, representing the date Dec 31 1991, to Dec-
  378.          Jul.
  379.  
  380.  
  381.  
  382. RelativeDate
  383.  
  384.          RelativeDate(DStr:string; Format:tDate; Delta:longint):string;
  385.  
  386.          Returns a string representing a date, which is a specified number of
  387.          days before or after a base date. The function is passed three parame-
  388.          ters; the base date in string form, the date format, and the number of
  389.          days from the base date. The last parameter may be positive or
  390.          negative, depending on whether you want a date after or before the base
  391.          date. For example:
  392.  
  393.                   DatStr := RelativeDate('04/05/91',MMDDYY,-7);
  394.  
  395.          Assigns the value '03/29/91' to DatStr.
  396.  
  397.  
  398.  
  399. StripDateStr
  400.  
  401.          StripDateStr(DStr:string; Format:tDate):string;
  402.  
  403.          This function returns a date string with the date separator removed.
  404.          The function is passed two parameters; the date in string form, and the
  405.          date format. For example:
  406.  
  407.                   SlimStr := StripDateStr('02/11/91',MMDDYY);
  408.  
  409.          Assigns the value '021191' to SlimStr.
  410.  
  411.  
  412.  
  413. FancyDateStr
  414.  
  415.          FancyDateStr(Jul:longint; Long,day:boolean): string;
  416.  
  417.          This function returns an attractively formatted string date. The func-
  418.          tion is passed three parameters; the Julian date, and two boolean
  419.          parameters to indicate the desired format. The first boolean parameter
  420.          should be set to true if the month and day of week should be spelled
  421.          out in their long form, or false to use the three character abbrevi-
  422.  
  423.  
  424. 13-8                                                                User's Guide
  425.  
  426. --------------------------------------------------------------------------------
  427.  
  428.          ation. The second boolean parameter should be set to true if the day of
  429.          the week is to be included, or false if it is not required. For
  430.          example:
  431.  
  432.                   FStr := FancyDateStr(2448299,true,true);
  433.  
  434.          Assigns the value 'Monday February 11, 1991' to FStr.
  435.  
  436.  
  437.  
  438. DateFormat
  439.  
  440.          DateFormat(Format:tDate):string;
  441.  
  442.          This function is passed a date format, and returns the string represen-
  443.          tation of that format. For example:
  444.  
  445.                   MyStr := DateFormat(MMDDYYYY);
  446.  
  447.          Assigns the value 'MMDDYYYY' to MyStr.
  448.  
  449.  
  450.  
  451. Example
  452.  
  453.          Listed below is the demo program DEMDT2.PAS, followed by figure 13.1
  454.          showing an example of the generated output.
  455.  
  456.          program DemoDateTwo;
  457.          {DEMDT2 - example date statements}
  458.  
  459.          uses DOS,CRT, totDATE;
  460.  
  461.          var
  462.           M,D,Y:longint;
  463.  
  464.          begin
  465.             ClrScr;
  466.             writeln(TodayInJul);
  467.             writeln(JultoStr(TodayInJul,DDMMYYYY));
  468.             writeln(FancyDateStr(TodayInJul,false,false));
  469.             writeln(FancyDateStr(TodayInJul,true,false));
  470.             writeln(FancyDateStr(TodayInJul,false,true));
  471.             writeln(FancyDateStr(TodayInJul,true,true));
  472.             writeln;
  473.             writeln(RelativeDate('02/20/56',MMDDYY,90));
  474.             writeln;
  475.             writeln(JulToStr(StartOfYear(StrTo-
  476.          Jul('30/06/1990',DDMMYY)),DDMMYY));
  477.             writeln(JulToStr(EndOfYear(StrToJul('30/06/1990',DDMMYY)),DDMMYY));
  478.             writeln;
  479.             writeln(ValidDateStr('02/29/90',MMDDYY));
  480.             writeln(ValidDate(2,29,90));
  481.             writeln(DateFormat(MMDDYY));
  482.          end.
  483.  
  484.  
  485. Managing Dates                                                              13-9
  486.  
  487. --------------------------------------------------------------------------------
  488.  
  489. Figure 13.1                                                             [SCREEN]
  490. Date
  491. Functions
  492.  
  493.  
  494.  
  495.