xref: /llvm-project/libc/src/time/time.cpp (revision f9c2377fb68e5051b3061186c507f7b87db2a8b2)
1e6cf5d28SSchrodinger ZHU Yifan //===-- Linux implementation of the time function -------------------------===//
2e6cf5d28SSchrodinger ZHU Yifan //
3e6cf5d28SSchrodinger ZHU Yifan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6cf5d28SSchrodinger ZHU Yifan // See https://llvm.org/LICENSE.txt for license information.
5e6cf5d28SSchrodinger ZHU Yifan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6cf5d28SSchrodinger ZHU Yifan //
7e6cf5d28SSchrodinger ZHU Yifan //===----------------------------------------------------------------------===//
8e6cf5d28SSchrodinger ZHU Yifan 
9*f9c2377fSMichael Jones #include "src/time/time_func.h"
10*f9c2377fSMichael Jones 
11e6cf5d28SSchrodinger ZHU Yifan #include "hdr/time_macros.h"
12e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/common.h"
13e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/macros/config.h"
14e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/time/clock_gettime.h"
15e6cf5d28SSchrodinger ZHU Yifan #include "src/errno/libc_errno.h"
16e6cf5d28SSchrodinger ZHU Yifan 
17e6cf5d28SSchrodinger ZHU Yifan namespace LIBC_NAMESPACE_DECL {
18e6cf5d28SSchrodinger ZHU Yifan // avoid inconsitent clang-format behavior
19e6cf5d28SSchrodinger ZHU Yifan using time_ptr_t = time_t *;
20e6cf5d28SSchrodinger ZHU Yifan LLVM_LIBC_FUNCTION(time_t, time, (time_ptr_t tp)) {
21e6cf5d28SSchrodinger ZHU Yifan   struct timespec ts;
22e6cf5d28SSchrodinger ZHU Yifan   auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
23e6cf5d28SSchrodinger ZHU Yifan   if (!result.has_value()) {
24e6cf5d28SSchrodinger ZHU Yifan     libc_errno = result.error();
25e6cf5d28SSchrodinger ZHU Yifan     return -1;
26e6cf5d28SSchrodinger ZHU Yifan   }
27e6cf5d28SSchrodinger ZHU Yifan 
28e6cf5d28SSchrodinger ZHU Yifan   if (tp != nullptr)
29e6cf5d28SSchrodinger ZHU Yifan     *tp = time_t(ts.tv_sec);
30e6cf5d28SSchrodinger ZHU Yifan   return time_t(ts.tv_sec);
31e6cf5d28SSchrodinger ZHU Yifan }
32e6cf5d28SSchrodinger ZHU Yifan 
33e6cf5d28SSchrodinger ZHU Yifan } // namespace LIBC_NAMESPACE_DECL
34