tz2.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * if dt has a timezone, increment dt by minutes-dt.tz MINUTES and set dt.tz = minutes
  12. * Returns:
  13. * 0 OK
  14. * <b>datetime_check_timezone</b> (dt) if not
  15. * -4 if minutes invalid
  16. *
  17. * \param dt
  18. * \param minutes
  19. * \return int
  20. */
  21. int datetime_change_timezone(DateTime * dt, int minutes)
  22. { /* new timezone in minutes */
  23. int stat;
  24. int old_minutes, diff_minutes;
  25. DateTime incr;
  26. stat = datetime_get_timezone(dt, &old_minutes);
  27. if (stat != 0)
  28. return stat;
  29. if (!datetime_is_valid_timezone(minutes))
  30. return datetime_error(-4, "invalid datetime timezone");
  31. /* create a relative minute increment */
  32. datetime_set_type(&incr, DATETIME_RELATIVE, DATETIME_MINUTE,
  33. DATETIME_MINUTE, 0);
  34. /* BB - needed to set SIGN here */
  35. diff_minutes = minutes - old_minutes;
  36. if (diff_minutes >= 0) {
  37. datetime_set_minute(&incr, diff_minutes);
  38. }
  39. else {
  40. datetime_invert_sign(&incr);
  41. datetime_set_minute(&incr, -diff_minutes);
  42. }
  43. return datetime_increment(dt, &incr);
  44. }
  45. /*!
  46. * \brief
  47. *
  48. * Return <b>datetime_change_timezone</b> (dt, 0);
  49. *
  50. * \param dt
  51. * \return int
  52. */
  53. int datetime_change_to_utc(DateTime * dt)
  54. {
  55. return datetime_change_timezone(dt, 0);
  56. }
  57. /*!
  58. * \brief
  59. *
  60. * tz = abs(tz)
  61. * *hour = tz/60
  62. * *minute = tz%60
  63. * Note: hour,minute are non-negative. Must look at sign of tz itself to see if
  64. * the tz is negative offset or not. This routine would be used to format tz for
  65. * output. For example if tz=-350 this would be hour=5 minute=50, but negative.
  66. * Output might encode this as -0550: printf ("%s%02d%02d", tz<0?"-":"",
  67. * hour, minute)
  68. *
  69. * \param tz
  70. * \param hours
  71. * \param minutes
  72. * \return void
  73. */
  74. void datetime_decompose_timezone(int tz, int *hours, int *minutes)
  75. {
  76. if (tz < 0)
  77. tz = -tz;
  78. *hours = tz / 60;
  79. *minutes = tz % 60;
  80. }