incr3.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 mode
  33. * \param from
  34. * \param to
  35. * \param fracsec
  36. * \return int
  37. */
  38. int
  39. datetime_get_increment_type(const DateTime * dt, int *mode, int *from,
  40. int *to, int *fracsec)
  41. {
  42. if (!datetime_is_valid_type(dt))
  43. return datetime_error_code();
  44. *mode = DATETIME_RELATIVE;
  45. *to = dt->to;
  46. *fracsec = dt->fracsec;
  47. if (datetime_is_absolute(dt)) {
  48. if (datetime_in_interval_year_month(dt->to))
  49. *from = DATETIME_YEAR;
  50. else
  51. *from = DATETIME_DAY;
  52. }
  53. else {
  54. *from = dt->from;
  55. }
  56. return 0;
  57. }
  58. /*!
  59. * \brief
  60. *
  61. * src must be legal
  62. * This is a convenience routine which is implemented as follows:
  63. \code
  64. int mode, from ,to;
  65. int fracsec;
  66. if(<b>datetime_get_increment_type</b>(src, &mode, &from, &to, &fracsec))
  67. return <b>datetime_get_error_code()</b>;
  68. return <b>datetime_set_type</b> (incr, mode, from, to, fracsec);
  69. \endcode
  70. * Timezone Timezones are represented in minutes from GMT in the range
  71. * [-720,+780]. For a DateTime to have a timezone, it must be of type ABSOLUTE,
  72. * and "to" must be in {MINUTE,SECOND}.
  73. *
  74. * \param src
  75. * \param incr
  76. * \return int
  77. */
  78. int datetime_set_increment_type(const DateTime * src, DateTime * incr)
  79. {
  80. int mode, from, to, fracsec;
  81. if (datetime_get_increment_type(src, &mode, &from, &to, &fracsec) != 0)
  82. return datetime_error_code();
  83. return datetime_set_type(incr, mode, from, to, fracsec);
  84. }