time.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/time.h>
  6. #if defined(__ANDROID__) && !defined(__LP64__)
  7. /*
  8. * This branch brought to you by the kind folks at google chromium. It's been modified a bit, but
  9. * gotta give credit where it's due.... I'm not a lawyer so I'm just gonna drop their copyright
  10. * notification here to avoid all of that.
  11. */
  12. /*
  13. * Copyright 2014 The Chromium Authors. All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are
  17. * met:
  18. *
  19. * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following disclaimer
  23. * in the documentation and/or other materials provided with the
  24. * distribution.
  25. * Neither the name of Google Inc. nor the names of its
  26. * contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. * From src/base/os_compat_android.cc:
  41. */
  42. # include <time64.h>
  43. static const time_t s_time_max = ~(1L << ((sizeof(time_t) * __CHAR_BIT__ - 1)));
  44. static const time_t s_time_min = (1L << ((sizeof(time_t)) * __CHAR_BIT__ - 1));
  45. /* 32-bit Android has only timegm64() and not timegm(). */
  46. time_t aws_timegm(struct tm *const t) {
  47. time64_t result = timegm64(t);
  48. if (result < s_time_min || result > s_time_max) {
  49. return -1;
  50. }
  51. return (time_t)result;
  52. }
  53. #else
  54. # ifndef __APPLE__
  55. /* glibc.... you disappoint me.. */
  56. extern time_t timegm(struct tm *);
  57. # endif
  58. time_t aws_timegm(struct tm *const t) {
  59. return timegm(t);
  60. }
  61. #endif /* defined(__ANDROID__) && !defined(__LP64__) */
  62. void aws_localtime(time_t time, struct tm *t) {
  63. localtime_r(&time, t);
  64. }
  65. void aws_gmtime(time_t time, struct tm *t) {
  66. gmtime_r(&time, t);
  67. }