misc.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
  3. *
  4. * This program is free software under the GPL (>=v2)
  5. * Read the file GPL.TXT coming with GRASS for details.
  6. */
  7. #include <grass/datetime.h>
  8. /*!
  9. * \brief
  10. *
  11. * \param year
  12. * \param ad
  13. * \return int
  14. */
  15. int datetime_is_leap_year(int year, int ad)
  16. {
  17. if (year == 0)
  18. return datetime_error(-1, "datetime_is_leap_year(): illegal year");
  19. if (!ad)
  20. return 0; /* BC */
  21. if (year < 0)
  22. return 0; /* ?? */
  23. return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  24. }
  25. /*!
  26. * \brief
  27. *
  28. * returns the number of days in 'year'
  29. *
  30. * \param year
  31. * \param ad
  32. * \return int
  33. */
  34. int datetime_days_in_year(int year, int ad)
  35. {
  36. if (year == 0)
  37. return datetime_error(-1, "datetime_days_in_year(): illegal year");
  38. if (datetime_is_leap_year(year, ad))
  39. return 366;
  40. else
  41. return 365;
  42. }
  43. /*!
  44. * \brief
  45. *
  46. * returns number of days in 'month' of a particular 'year'
  47. *
  48. * \param month
  49. * \param year
  50. * \param ad
  51. * \return int
  52. */
  53. int datetime_days_in_month(int year, int month, int ad)
  54. {
  55. static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  56. if (month < 1 || month > 12)
  57. return datetime_error(-1, "datetime_days_in_month(): illegal month");
  58. if (month == 2 && datetime_is_leap_year(year, ad))
  59. return (29);
  60. return (days[month - 1]);
  61. }