FDate.H 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* -*-C++-*-
  2. "$Id: FDate.H,v 1.4 2000/09/29 21:49:35 clip Exp $"
  3. Copyright 1999-2000 by the Flek development team.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  15. USA.
  16. Please report all bugs and problems to "[email protected]".
  17. */
  18. #ifndef __FDATE_H__
  19. #define __FDATE_H__
  20. #include <FLU/FBase.H>
  21. // CET - libstdc++ ios evil on Linux
  22. //#include <iostream.h>
  23. typedef enum {
  24. SUNDAY,
  25. MONDAY,
  26. TUESDAY,
  27. WEDNESDAY,
  28. THURSDAY,
  29. FRIDAY,
  30. SATURDAY
  31. } FWeekday;
  32. typedef enum {
  33. JANUARY = 1,
  34. FEBRUARY,
  35. MARCH,
  36. APRIL,
  37. MAY,
  38. JUNE,
  39. JULY,
  40. AUGUST,
  41. SEPTEMBER,
  42. OCTOBER,
  43. NOVEMBER,
  44. DECEMBER
  45. } FMonth;
  46. /** @package libflek_core
  47. * FDate provides a date class that stores the day, month and year.
  48. */
  49. class FDate : public FBase
  50. {
  51. public:
  52. /**
  53. * Default constructor.
  54. */
  55. FDate ();
  56. /**
  57. * Three argument constructor. Initializes this object with the
  58. * year, month and day.
  59. *
  60. * @param y The year.
  61. * @param m The month.
  62. * @param d The day.
  63. */
  64. FDate (int y, int m, int d);
  65. /**
  66. * Copy constructor.
  67. *
  68. * @param src The FDate object to copy initial values from.
  69. */
  70. FDate (const FDate& src);
  71. /* (ScanDoc doesn't like this one)
  72. * Copy method. This virtual method makes a new copy of this object.
  73. *
  74. * @return The pointer (FBase::Ptr) to the new FDate instance.
  75. */
  76. virtual FBase::Ptr copy () const;
  77. /**
  78. * Sets the date from the year, month, and day.
  79. *
  80. * @param y The year.
  81. * @param m The month, 1 through 12.
  82. * @param d The day, 1 through the numbers of days in that month.
  83. */
  84. void set_date (int y, int m, int d);
  85. /**
  86. * Sets the date value from another FDate object.
  87. *
  88. * @param src The FDate object to set values from.
  89. */
  90. void set_date (const FDate& src);
  91. /**
  92. * Sets the date format.
  93. *
  94. * @param fmt The date format.
  95. */
  96. void set_format (int fmt);
  97. /**
  98. * Set the date value to today's date.
  99. */
  100. void today ();
  101. /**
  102. * Sets the year.
  103. *
  104. * @param y The year to associate with this object.
  105. */
  106. void year (int);
  107. /**
  108. * Gets the year.
  109. *
  110. * @return The year associated with this object.
  111. */
  112. int year ();
  113. /**
  114. * Sets the month.
  115. *
  116. * @param m The month to associate with this object.
  117. */
  118. void month (int m);
  119. /**
  120. * Gets the month.
  121. *
  122. * @return The month associated with this object.
  123. */
  124. int month ();
  125. /**
  126. * Sets the day.
  127. *
  128. * @param d The day of the month to associate with this object.
  129. */
  130. void day (int d);
  131. /**
  132. * Gets the day.
  133. *
  134. * @return The day of the month associated with this object.
  135. */
  136. int day ();
  137. /**
  138. * Gets the Julian date.
  139. *
  140. * @return The julian date value for this object.
  141. */
  142. double julian_date ();
  143. /**
  144. * Equality comparison of two dates.
  145. *
  146. * @param src The FDate object to compare this one to.
  147. * @return True if this object and src are equal. False otherwise.
  148. */
  149. bool operator== (const FDate& src);
  150. /**
  151. * Inequality comparison of two dates.
  152. *
  153. * @param src The FDate object to compare this one to.
  154. * @return True if this object and src are <b>not</b> equal. False otherwise.
  155. */
  156. bool operator!= (const FDate& src);
  157. /**
  158. * Comparison of two dates.
  159. *
  160. * @param src The FDate object to compare this one to.
  161. * @return True if this FDate object is less than the src object. False otherwise.
  162. */
  163. bool operator< (const FDate& src);
  164. /**
  165. * Comparison of two dates.
  166. *
  167. * @param src The FDate object to compare this one to.
  168. * @return True if this FDate object is greater than the src object. False otherwise.
  169. */
  170. bool operator> (const FDate& src);
  171. /**
  172. * Sets one date equal to another date.
  173. *
  174. * @param src The FDate object to set values from.
  175. */
  176. void operator= (const FDate& src);
  177. /**
  178. * Adds days to the date.
  179. *
  180. * @param d The number of days to add to this date.
  181. */
  182. const FDate &operator+= (int d);
  183. /**
  184. * Pre-increment operator. Increment the date by one day.
  185. */
  186. FDate &operator++ ();
  187. /**
  188. * Post-increment operator. Increment the date by one day.
  189. */
  190. FDate operator++ (int);
  191. /**
  192. * ostream operator.
  193. */
  194. // CET - libstdc++ ios evil on Linux
  195. // friend ostream& operator << (ostream &, const FDate &);
  196. /**
  197. * Gets wether day d is at the end of the month.
  198. *
  199. * @param d The day to check.
  200. * @return True if d is at the end of the month. False otherwise.
  201. */
  202. bool end_of_month (int d);
  203. /**
  204. * Gets wether year y is a leap year.
  205. *
  206. * @param y The year to check.
  207. * @return True if the year is a leap year. False otherwise.
  208. */
  209. static bool leap_year (int y);
  210. /**
  211. * Gets wether the year associated with this object is a leap year.
  212. *
  213. * @return True if the year is a leap year. False otherwise.
  214. */
  215. bool leap_year ()
  216. { return leap_year (Year); }
  217. /**
  218. * Gets wether the passed date is valid.
  219. *
  220. * @param y The year.
  221. * @param m The month, 1 through 12.
  222. * @param d The day, 1 through the numbers of days in that month.
  223. * @return True if the year, month and day make a valid date. False otherwise.
  224. */
  225. static bool valid (int y, int m, int d);
  226. /**
  227. * Gets wether the current date is valid.
  228. *
  229. * @return True if the year, month and day make a valid date. False otherwise.
  230. */
  231. bool valid ()
  232. { return valid (Year, Month, Day); }
  233. /**
  234. * Gets the number of days in the month for a given month and leap value.
  235. *
  236. * @param m The month, 1 through 12.
  237. * @param leap Should be 1 if the associated year is a leap year, 0 otherwise.
  238. * @return The number of days in the month.
  239. */
  240. static int days_in_month (int m, int leap);
  241. /**
  242. * Gets the number of days in the month for this date.
  243. *
  244. * @return The number of days in the month.
  245. */
  246. int days_in_month ()
  247. { return days_in_month (Month, leap_year (Year)); }
  248. /**
  249. * Gets the day of the year for the passed date.
  250. *
  251. * @param y The year.
  252. * @param m The month, 1 through 12.
  253. * @param d The day, 1 through the numbers of days in that month.
  254. * @return The day of the year.
  255. */
  256. static int day_of_year (int y, int m, int d);
  257. /**
  258. * Gets the day of the year for this date.
  259. *
  260. * @return The day of the year.
  261. */
  262. int day_of_year ()
  263. { return day_of_year (Year, Month, Day); }
  264. /**
  265. * Gets the day of the epoch for the passed date.
  266. *
  267. * @param y The year.
  268. * @param m The month, 1 through 12.
  269. * @param d The day, 1 through the numbers of days in that month.
  270. * @return The day of the epoch.
  271. */
  272. static int day_of_epoch (int y, int m, int d);
  273. /**
  274. * Gets the day of the epoch for this date.
  275. *
  276. * @return The day of the epoch.
  277. */
  278. int day_of_epoch ()
  279. { return day_of_epoch (Year, Month, Day); }
  280. /**
  281. * Gets the day of the week for the passed date.
  282. *
  283. * @param y The year.
  284. * @param m The month, 1 through 12.
  285. * @param d The day, 1 through the numbers of days in that month.
  286. * @return The day of the week, 1 through 7.
  287. */
  288. static int day_of_week (int y, int m, int d);
  289. /**
  290. * Gets the day of the week for this date.
  291. *
  292. * @return The day of the week, 1 through 7.
  293. */
  294. int day_of_week ()
  295. { return day_of_week (Year, Month, Day); }
  296. /**
  297. * Decrement the date by one month.
  298. */
  299. void previous_month ();
  300. /**
  301. * Increment the date by one month.
  302. */
  303. void next_month ();
  304. /**
  305. * Decrement the date by one year.
  306. */
  307. void previous_year ();
  308. /**
  309. * Increment the date by one year.
  310. */
  311. void next_year ();
  312. /**
  313. * Convert the date to a string.
  314. *
  315. * @param fmt The format to use during conversion.
  316. */
  317. char* to_string (int fmt) const;
  318. /**
  319. * Convert the date to a string using the current format.
  320. */
  321. char* to_string () const;
  322. /**
  323. * An array of days of the month for a non leap year, 1-12. (0 is ignored.)
  324. */
  325. static const int days[];
  326. /**
  327. * An array of total days since the beginning of the year for
  328. * each month, 1-12. (0 is ignored.)
  329. */
  330. static const int julian_days[2][13];
  331. /**
  332. * An array of month names.
  333. */
  334. static const char* month_name[];
  335. private:
  336. int Year;
  337. int Month;
  338. int Day;
  339. int Fmt;
  340. void help_increment ();
  341. };
  342. #endif