local.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <time.h>
  8. #include <grass/datetime.h>
  9. extern struct tm *localtime();
  10. extern struct tm *gmtime();
  11. /*
  12. ** NOTE: the extern variable "timezone" seems to be treated
  13. ** differently by different OS, and the tm_zone element of struct tm
  14. ** is missing in some OS (IRIX), so we're converting localtime() and
  15. ** gmtime() structures to datetimes, then doing a difference to get the
  16. ** timezone offset. -Bill Brown 5/31/95
  17. */
  18. /*!
  19. * \brief
  20. *
  21. * Returns:
  22. * 0 OK
  23. * -1 local timezone info not available
  24. *
  25. * \param minutes
  26. * \return int
  27. */
  28. int datetime_get_local_timezone(int *minutes)
  29. {
  30. struct tm *local, *gm;
  31. time_t clock;
  32. DateTime dtl, dtg, dtdiff;
  33. time(&clock);
  34. local = localtime(&clock);
  35. datetime_set_type(&dtl, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
  36. 0);
  37. /* now put current {year,month,day,hour,minute,second} into local */
  38. datetime_set_year(&dtl, (int)local->tm_year + 1900);
  39. datetime_set_month(&dtl, (int)local->tm_mon + 1);
  40. datetime_set_day(&dtl, (int)local->tm_mday);
  41. datetime_set_hour(&dtl, (int)local->tm_hour);
  42. datetime_set_minute(&dtl, (int)local->tm_min);
  43. datetime_set_second(&dtl, (double)local->tm_sec);
  44. gm = gmtime(&clock);
  45. datetime_set_type(&dtg, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
  46. 0);
  47. /* now put current {year,month,day,hour,minute,second} into gmt */
  48. datetime_set_year(&dtg, (int)gm->tm_year + 1900);
  49. datetime_set_month(&dtg, (int)gm->tm_mon + 1);
  50. datetime_set_day(&dtg, (int)gm->tm_mday);
  51. datetime_set_hour(&dtg, (int)gm->tm_hour);
  52. datetime_set_minute(&dtg, (int)gm->tm_min);
  53. datetime_set_second(&dtg, (double)gm->tm_sec);
  54. datetime_set_type(&dtdiff, DATETIME_RELATIVE,
  55. DATETIME_DAY, DATETIME_SECOND, 0);
  56. datetime_difference(&dtl, &dtg, &dtdiff);
  57. datetime_change_from_to(&dtdiff, DATETIME_MINUTE, DATETIME_MINUTE, 0);
  58. *minutes = dtdiff.positive ? dtdiff.minute : -dtdiff.minute;
  59. return 0;
  60. }
  61. /*!
  62. * \brief
  63. *
  64. * set mode/from/to ABSOLUTE/YEAR/SECOND
  65. * set the local time into 'dt' does not set timezone.
  66. *
  67. * \param dt
  68. * \return void
  69. */
  70. void datetime_get_local_time(DateTime * dt)
  71. {
  72. time_t clock;
  73. struct tm *local;
  74. /* first set dt to absolute full date */
  75. datetime_set_type(dt, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
  76. 0);
  77. /* get the current date/time */
  78. time(&clock);
  79. local = localtime(&clock);
  80. /* now put current {year,month,day,hour,minute,second} into dt */
  81. datetime_set_year(dt, (int)local->tm_year + 1900);
  82. datetime_set_month(dt, (int)local->tm_mon + 1);
  83. datetime_set_day(dt, (int)local->tm_mday);
  84. datetime_set_hour(dt, (int)local->tm_hour);
  85. datetime_set_minute(dt, (int)local->tm_min);
  86. datetime_set_second(dt, (double)local->tm_sec);
  87. }