xref: /aosp_15_r20/external/pigweed/pw_chrono/wrap_time_build_time.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <time.h>
16 
17 #include "pw_chrono/build_time.h"
18 
19 #if __has_include(<sys/time.h>)
20 #include <sys/time.h>
21 #endif  // __has_include(<sys/time.h>)
22 
23 namespace pw::chrono {
24 namespace {
25 constexpr uint64_t kMicrosecondsPerSecond = 1'000'000;
26 }
27 
__wrap_time(time_t * t)28 extern "C" time_t __wrap_time(time_t* t) {
29   time_t ret =
30       static_cast<time_t>(kBuildTimeMicrosecondsUTC / kMicrosecondsPerSecond);
31   if (t) {
32     *t = ret;
33   }
34   return ret;
35 }
36 
37 #if __has_include(<sys/time.h>)
38 
__wrap_gettimeofday(struct timeval * tv,void * tz)39 extern "C" int __wrap_gettimeofday(struct timeval* tv, void* tz) {
40   // The use of the timezone structure is obsolete (see docs "man
41   // gettimeofday"). Thus we don't consider it.
42   (void)tz;
43   tv->tv_sec = kBuildTimeMicrosecondsUTC / kMicrosecondsPerSecond;
44   tv->tv_usec = kBuildTimeMicrosecondsUTC % kMicrosecondsPerSecond;
45   return 0;
46 }
47 
48 #endif  // __has_include(<sys/time.h>)
49 
50 }  // namespace pw::chrono
51