timegm.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* timegm.c - libc replacement function
  2. * Copyright (C) 2004 Free Software Foundation, Inc.
  3. *
  4. * This file is part of GnuPG.
  5. *
  6. * GnuPG is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GnuPG is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  19. * USA.
  20. */
  21. /*
  22. timegm() is a GNU function that might not be available everywhere.
  23. It's basically the inverse of gmtime() - you give it a struct tm,
  24. and get back a time_t. It differs from mktime() in that it handles
  25. the case where the struct tm is UTC and the local environment isn't.
  26. Some BSDs don't handle the putenv("foo") case properly, so we use
  27. unsetenv if the platform has it to remove environment variables.
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. # include "config.h"
  31. #endif // HAVE_CONFIG_H
  32. #include <time.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. time_t
  36. timegm(struct tm *tm)
  37. {
  38. time_t answer;
  39. char *zone;
  40. zone=getenv("TZ");
  41. putenv("TZ=UTC");
  42. tzset();
  43. answer=mktime(tm);
  44. if(zone)
  45. {
  46. char *old_zone;
  47. old_zone=malloc(3+strlen(zone)+1);
  48. if(old_zone)
  49. {
  50. strcpy(old_zone,"TZ=");
  51. strcat(old_zone,zone);
  52. putenv(old_zone);
  53. }
  54. }
  55. else
  56. #ifdef HAVE_UNSETENV
  57. unsetenv("TZ");
  58. #else
  59. putenv("TZ=");
  60. #endif
  61. tzset();
  62. return answer;
  63. }