1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 #include <aws/core/platform/Time.h>
7 
8 #include <limits.h>
9 #include <time.h>
10 
11 namespace Aws
12 {
13 namespace Time
14 {
15 
16 #if defined(__ANDROID__) && !defined(__LP64__)
17 
18 // TODO: verify this is ok legally, push into separate file?
19 
20 // timegm doesn't exist for some forms of android.  Chromium has a substitute
21 // implementation that we cut-and-paste here,
22 // including the full license notice from Chromium
23 
24 // Copyright 2014 The Chromium Authors. All rights reserved.
25 //
26 // Redistribution and use in source and binary forms, with or without
27 // modification, are permitted provided that the following conditions are
28 // met:
29 //
30 //    * Redistributions of source code must retain the above copyright
31 // notice, this list of conditions and the following disclaimer.
32 //    * Redistributions in binary form must reproduce the above
33 // copyright notice, this list of conditions and the following disclaimer
34 // in the documentation and/or other materials provided with the
35 // distribution.
36 //    * Neither the name of Google Inc. nor the names of its
37 // contributors may be used to endorse or promote products derived from
38 // this software without specific prior written permission.
39 //
40 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 
52 // From src/base/os_compat_android.cc:
53 
54 
55 #include <time64.h>
56 
57 // 32-bit Android has only timegm64() and not timegm().
58 // We replicate the behaviour of timegm() when the result overflows time_t.
TimeGM(struct tm * const t)59 time_t TimeGM(struct tm* const t)
60 {
61     // time_t is signed on Android.
62     static const time_t kTimeMax = ~(1L << (sizeof(time_t) * CHAR_BIT - 1));
63     static const time_t kTimeMin = (1L << (sizeof(time_t) * CHAR_BIT - 1));
64     time64_t result = timegm64(t);
65     if (result < kTimeMin || result > kTimeMax)
66     {
67         return -1;
68     }
69     return result;
70 }
71 
72 #else
73 
74 time_t TimeGM(struct tm* const t)
75 {
76     return timegm(t);
77 }
78 
79 #endif
80 
LocalTime(tm * t,std::time_t time)81 void LocalTime(tm* t, std::time_t time)
82 {
83     localtime_r(&time, t);
84 }
85 
GMTime(tm * t,std::time_t time)86 void GMTime(tm* t, std::time_t time)
87 {
88     gmtime_r(&time, t);
89 }
90 
91 } // namespace Time
92 } // namespace Aws
93