home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / FTSER10.ZIP / ADTDV2.ZIP / datelib.h < prev    next >
C/C++ Source or Header  |  1997-01-26  |  13KB  |  357 lines

  1. /*-----------------------------------------------------------------------*
  2.  *   File: datelib.h                             *
  3.  *   Date: 1-26-97                             *
  4.  *   Author: Brady Tippit                         *
  5.  *   Compiler: Borland (C++ 4.5, using small memory model)         *
  6.  *                                       *
  7.  *   References:  Peter Meyer, "Julian and Gregorian Calendars."     *
  8.  *                Dr. Dobb's Journal, March 1993.             *
  9.  *                                            *
  10.  *                                     *
  11.  *-----------------------------------------------------------------------*/
  12.  
  13. #ifndef DATELIB_H
  14. #define DATELIB_H
  15.  
  16. typedef struct td {
  17.   int td_year;
  18.   int td_mon;
  19.   int td_mday;
  20.   int td_hour;
  21.   int td_min;
  22.   int td_sec;   
  23. }tdtype;
  24.  
  25.  
  26.  
  27.  
  28. /*-------------[ Prototypes for the date and time routines ]--------------*/ 
  29.  
  30.  
  31.  
  32.  
  33.  
  34. /*-----------------------------------------------------------------------*
  35.  *  Function: TimeDate()                         *
  36.  *                                     *
  37.  *  Creates a date/time structure and initializes all fields with the     *
  38.  *  current system date AND time info.                     *
  39.  *                                     *
  40.  *  Parameters: none.                             *
  41.  *  Returns: a pointer to the date/time structure.             *
  42.  *-----------------------------------------------------------------------*/
  43.  
  44. tdtype * TimeDate(void);
  45.  
  46.  
  47.  
  48.  
  49. /*-----------------------------------------------------------------------*
  50.  *  Function: CreateDate()                         *
  51.  *                                     *
  52.  *  Dynamically allocates a td (time/date) structure.             *
  53.  *                                     *
  54.  *  NOTE: Neither this function nor LoadDate() are needed if all     *
  55.  *  you want to work with is the current date/time.             *
  56.  *  If this function is used, call LoadDate() to initialize it's         *
  57.  *  fields with the month, day, and year you want.             *
  58.  *  Call FreeDate() to return the memory back to the system.             *
  59.  *                                                                       *
  60.  *  Parameters: none.                             *
  61.  *  Returns: a pointer to the newly allocated td structure.         *
  62.  *-----------------------------------------------------------------------*/
  63.  
  64. tdtype * CreateDate(void);
  65.  
  66.  
  67.  
  68.  
  69. /*-----------------------------------------------------------------------*
  70.  *  Function: LoadDate()                         *
  71.  *                                     *
  72.  *  Loads a previously created td structure with a user specified year,     *
  73.  *  month, and day.                             *
  74.  *  NOTE: All functions expect months from 1-12, years from 1-32766, and *
  75.  *  days from 1-31.                             *
  76.  *                                                                       *
  77.  *  Parameters: pointer to the td struct, integer dates to load into the *
  78.  *  struct.                                         *
  79.  *  Returns: -1 on invalid date, 0 on success.                  *
  80.  *-----------------------------------------------------------------------*/
  81.  
  82. int LoadDate(tdtype *, int, int, int);
  83.  
  84.  
  85.  
  86.  
  87. /*-----------------------------------------------------------------------*
  88.  *  Function: FreeDate()                         *
  89.  *                                     *
  90.  *  Makes more memory available to the system by freeing the memory     *
  91.  *  that was allocated by the user calling CreateDate().          *
  92.  *  The pointer/s in the calling environment are set to NULL and should  *
  93.  *  not be used again without first calling CreateDate() again. duh!     *
  94.  *                                        *
  95.  *  Parameters: a pointer to the address of the previously              *
  96.  *  allocated td structure.                         *
  97.  *  Returns: nothing.                             *
  98.  *-----------------------------------------------------------------------*/
  99.  
  100. void FreeDate(tdtype **);
  101.  
  102.  
  103.  
  104.  
  105. /*-----------------------------------------------------------------------*
  106.  *  Function: CreateTime()                         *
  107.  *                                     *
  108.  *  Dynamically allocates a td time structure.                     *
  109.  *                                     *
  110.  *  NOTE: Neither this function nor LoadTime() are needed if all     *
  111.  *  you want to work with is the current time.                           *
  112.  *  ( Just call TimeDate() )                             *
  113.  *                                     *
  114.  *  If this function is used, call LoadTime() to initialize it's         *
  115.  *  fields with the hours, mins, and secs you want.             *
  116.  *  Call FreeTime() to return the memory back to the system.             *
  117.  *                                                                       *
  118.  *  Parameters: none.                             *
  119.  *  Returns: a pointer to the newly allocated td structure, which is now *
  120.  *  ready to be filled with the time info. that you supply.              *
  121.  *-----------------------------------------------------------------------*/
  122.  
  123. tdtype * CreateTime(void);
  124.  
  125.  
  126.  
  127.  
  128. /*-----------------------------------------------------------------------*
  129.  *  Function: LoadTime()                         *
  130.  *                                     *
  131.  *  Initializes the hour, min, and sec fields of a td time structure.     *
  132.  *  Used after calling CreateTime().                     *
  133.  *                                                                       *
  134.  *  Parameters: pointer to a td structure, hours, mins, secs.         *
  135.  *  Returns: 0 on success, -1 on invalid time.                           *
  136.  *-----------------------------------------------------------------------*/
  137.  
  138. int LoadTime(tdtype *, int, int, int);
  139.  
  140.  
  141.  
  142.  
  143. /*-----------------------------------------------------------------------*
  144.  *  Function: FreeTime()                         *
  145.  *                                     *
  146.  *  Makes more memory available to the system by freeing the memory     *
  147.  *  that was allocated by the user calling CreateTime().          *
  148.  *  The pointer/s in the calling environment are set to NULL and should  *
  149.  *  not be used again without first calling CreateTime() again.          *
  150.  *                                        *
  151.  *  Parameters: a pointer to the address of the previously allocated td  *
  152.  *  structure.                                         *
  153.  *  Returns: nothing.                             *
  154.  *-----------------------------------------------------------------------*/
  155.  
  156. void FreeTime(tdtype **);
  157.  
  158.  
  159.  
  160.  
  161. /*-----------------------------------------------------------------------*
  162.  *  Function: IsDateValid()                         *
  163.  *                                             *
  164.  *  Checks for valid dates. Earliest date allowed is Jan 1, 1 A.D.     *
  165.  *  Maximum date is Dec. 31, 32766                                       *
  166.  *  This is done automatically but CAN be called.                        *
  167.  *                                                                       *
  168.  *  Parameters: pointer to the td struct.                     *
  169.  *  Returns: 0 if date is not valid, otherwise 1.             *
  170.  *-----------------------------------------------------------------------*/
  171.  
  172. int IsDateValid(tdtype *);
  173.  
  174.  
  175.  
  176.  
  177. /*-----------------------------------------------------------------------*
  178.  *  Function: IsTimeValid()                         *
  179.  *                                     *
  180.  *  Checks the time fields for validity.                     *
  181.  *  This is done automatically but CAN be called.                        *
  182.  *                                                                       *
  183.  *  Returns: 1 if valid, otherwise 0.                     *
  184.  *-----------------------------------------------------------------------*/
  185.  
  186. int IsTimeValid(tdtype *);
  187.  
  188.  
  189.  
  190.  
  191. /*-----------------------------------------------------------------------*
  192.  *  Function: DateDiff()                         *
  193.  *                                     *
  194.  *  Calculates the difference, in days, between two given dates. Two td     *
  195.  *  structures are used.                         *
  196.  *                                       *
  197.  *  Parameters: pointers to the td structures.                         *
  198.  *  Returns: the number of days from date1 to date2. A negative result     *
  199.  *  indicates a decrease in the number of days.                 *
  200.  *  (No error check, you can check this yourself.)             *
  201.  *-----------------------------------------------------------------------*/
  202.  
  203. long DateDiff(tdtype *, tdtype *);
  204.  
  205.  
  206.  
  207.  
  208. /*-----------------------------------------------------------------------*
  209.  *  Function: DateToDays()                         *
  210.  *                                     *
  211.  *  Calculates the number of days from Jan 1, 1 A.D. to the date found     *
  212.  *  in the td structure.                         *
  213.  *                                     *
  214.  *  Parameters: pointer to the td struct.                     *
  215.  *  Returns: the number of days or -1 if date not valid.             *
  216.  *-----------------------------------------------------------------------*/
  217.  
  218. long DateToDays(tdtype *);
  219.  
  220.  
  221.  
  222.  
  223.  
  224. /*-----------------------------------------------------------------------*
  225.  *  Function: DaysToDate()                         *
  226.  *                                     *
  227.  *  Given some number of days from Jan 1, 1 A.D. , this function      *
  228.  *  calculates a year, month, and day-of-month for that many days     *
  229.  *  and loads the td struture with that year, month, and                 *
  230.  *  day-of-the-month.                                 *
  231.  *                                     *
  232.  *  Parameters: pointer to the td struct,                     *
  233.  *             the number of days since Jan 1, 1 A.D.             *
  234.  *  Returns: -1 on error, 0 on success.                     *
  235.  *-----------------------------------------------------------------------*/
  236.  
  237. int DaysToDate(tdtype *, long int);
  238.  
  239.  
  240.  
  241.  
  242. /*-----------------------------------------------------------------------*
  243.  *  Function: DayOfWeek()                         *
  244.  *                                     *
  245.  *  This function expects the YEAR, MONTH AND DAY fields in the          *
  246.  *  td structure to be the actual dates you want, NOT year - 1900! etc.  *
  247.  *                                     *
  248.  *  Parameters: pointer to the td struct.                     *
  249.  *  Returns:  -1 on error, otherwise the day of the week, (0-6).     *
  250.  *-----------------------------------------------------------------------*/
  251.  
  252. int DayOfWeek(tdtype *);
  253.  
  254.  
  255.  
  256.  
  257. /*-----------------------------------------------------------------------*
  258.  *  Function: DayOfYear()                         *
  259.  *                                     *
  260.  *  This function calculates the day of any given year starting from     *
  261.  *  Jan. 1st of that year. The td structure should be loaded normally     *
  262.  *  using LoadDate() ( or TimeDate() ) with a 1-12 month, 1-31 month-day *
  263.  *  and the full year desired.                         *
  264.  *                                     *
  265.  *  Parameters: pointer to the td struct.                     *
  266.  *  Returns: -1 on error or the day of the year from Jan 1.         *
  267.  *-----------------------------------------------------------------------*/
  268.  
  269. int DayOfYear(tdtype *);
  270.  
  271.  
  272.  
  273.  
  274. /*-----------------------------------------------------------------------*
  275.  *  Function: IsLeapYear()                         *
  276.  *                                     *
  277.  *  Determines whether a year is a leap year or not.             *
  278.  *  This is done automatically but CAN be called.                        *
  279.  *                                     *
  280.  *  Parameters: the year to check.                         *
  281.  *  Returns: 0 if not a leap year, otherwise 1.                     *
  282.  *-----------------------------------------------------------------------*/
  283.  
  284. int IsLeapYear(int);
  285.  
  286.  
  287.  
  288.  
  289. /*-----------------------------------------------------------------------*
  290.  *  Function: TimeDiff()                         *
  291.  *                                     *
  292.  *  Calculates the difference, in seconds, between two calendar dates     *
  293.  *  or times.                                 *
  294.  *                                     *
  295.  *  The advantage this function has over the ANSI difftime() is that     *
  296.  *  this function works for any date from 1 A.D.             *
  297.  *  And the same is true for my DateDiff() function.             *
  298.  *                                     *
  299.  *  Parameters: pointers to the two td structures.                     *
  300.  *                                     *
  301.  *  Returns: The difference, in seconds, between two dates or times.     *
  302.  *  If the seconds returned are negative, this reflects a decrease in     *
  303.  *  in time and positive for an increase in time.             *
  304.  *  The times or dates must be valid for the result to be valid.     *
  305.  *  No error is returned.                         *
  306.  *-----------------------------------------------------------------------*/
  307.  
  308. double TimeDiff(tdtype *, tdtype *);
  309.  
  310.  
  311.  
  312.  
  313. /*-----------------------------------------------------------------------*
  314.  *  Function: PrintCalendar()                         *
  315.  *                                     *
  316.  *  Prints a calendar month for any year from Jan 1, 1 A.D. to 32766      *
  317.  *  Parameters: month and year of calendar to print.                     *
  318.  *  Returns: nothing.                             *
  319.  *-----------------------------------------------------------------------*/
  320.  
  321. void PrintCalendar(int, int);
  322.  
  323.  
  324.  
  325.  
  326.  
  327. /*-----------------------------------------------------------------------*
  328.  *  Function: TDFormat()   (see howto.txt for format options)            *
  329.  *                                     *
  330.  *  Converts dates to various formats. For the "%Y" option, the year     *
  331.  *  should be entered in the precise form you desire, ie, 1996 for 1996     *
  332.  *  and 96 for the year 96 A.D. The upper limit for full year formatting *
  333.  *  is the year 32,766 (the maximum integer size - 1 on 2 byte systems). *
  334.  *                                     *
  335.  *  Unlike the hindered ANSI strftime(), which only works for dates from *
  336.  *  1900 or later, this function works for any date from Jan 1, 1 A.D.!     *
  337.  *                                     *
  338.  *  All format options are ANSI compliant with the exception of the two     *
  339.  *  format options %r and %n, which I use most often.             *
  340.  *  None of the format options are THE ANSI format options.         *
  341.  *                                     *
  342.  *  Parameters: a pointer to the td structure,                     *
  343.  *              a pointer to the output string,                 *
  344.  *              the size of the output string (800 bytes max),         *
  345.  *              the format of the output string.             *
  346.  *                                     *
  347.  *  Returns: -1 if out of memory, or the number of characters formatted, *
  348.  *  or 0 if all of the characters could not be formatted, or an error     *
  349.  *  message if the user specifies an illegal format option.         *
  350.  *-----------------------------------------------------------------------*/
  351.  
  352. int TDFormat(tdtype *, char *, int, char *);
  353.  
  354.  
  355. #endif
  356.  
  357.