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