incr3.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * This returns the components of a type
  12. * (mode/from/to/fracsec) that can be used to construct a DateTime object that
  13. * can be used to increment the 'src'. Also see
  14. * <b>datetime_set_increment_type()</b>.
  15. * returns:
  16. * 0 dt is legal
  17. * !=0 why dt is illegal
  18. * Implemented as follows:
  19. \code
  20. *mode = RELATIVE
  21. *to = src.to
  22. *fracsec = src.fracsec
  23. if src.mode is ABSOLUTE
  24. if src.to is in {YEAR,MONTH} then
  25. *from = YEAR
  26. if src.to is in {DAY,HOUR,MINUTE,SECOND} then
  27. *from = DAY
  28. if src.mode is RELATIVE, then
  29. *from = src.from
  30. \endcode
  31. *
  32. * \param dt
  33. * \param mode
  34. * \param from
  35. * \param to
  36. * \param fracsec
  37. * \return int
  38. */
  39. int
  40. datetime_get_increment_type(const DateTime * dt, int *mode, int *from,
  41. int *to, int *fracsec)
  42. {
  43. if (!datetime_is_valid_type(dt))
  44. return datetime_error_code();
  45. *mode = DATETIME_RELATIVE;
  46. *to = dt->to;
  47. *fracsec = dt->fracsec;
  48. if (datetime_is_absolute(dt)) {
  49. if (datetime_in_interval_year_month(dt->to))
  50. *from = DATETIME_YEAR;
  51. else
  52. *from = DATETIME_DAY;
  53. }
  54. else {
  55. *from = dt->from;
  56. }
  57. return 0;
  58. }
  59. /*!
  60. * \brief
  61. *
  62. * src must be legal
  63. * This is a convenience routine which is implemented as follows:
  64. \code
  65. int mode, from ,to;
  66. int fracsec;
  67. if(<b>datetime_get_increment_type</b>(src, &mode, &from, &to, &fracsec))
  68. return <b>datetime_get_error_code()</b>;
  69. return <b>datetime_set_type</b> (incr, mode, from, to, fracsec);
  70. \endcode
  71. * Timezone Timezones are represented in minutes from GMT in the range
  72. * [-720,+780]. For a DateTime to have a timezone, it must be of type ABSOLUTE,
  73. * and "to" must be in {MINUTE,SECOND}.
  74. *
  75. * \param src
  76. * \param incr
  77. * \return int
  78. */
  79. int datetime_set_increment_type(const DateTime * src, DateTime * incr)
  80. {
  81. int mode, from, to, fracsec;
  82. if (datetime_get_increment_type(src, &mode, &from, &to, &fracsec) != 0)
  83. return datetime_error_code();
  84. return datetime_set_type(incr, mode, from, to, fracsec);
  85. }