xref: /llvm-project/libc/src/time/ctime.cpp (revision f9c2377fb68e5051b3061186c507f7b87db2a8b2)
1 //===-- Implementation of ctime function ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/time/ctime.h"
10 #include "src/__support/CPP/limits.h"
11 #include "src/__support/common.h"
12 #include "src/__support/macros/config.h"
13 #include "src/time/time_constants.h"
14 #include "src/time/time_utils.h"
15 
16 namespace LIBC_NAMESPACE_DECL {
17 
18 LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) {
19   if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
20     return nullptr;
21   }
22   static char buffer[time_constants::ASCTIME_BUFFER_SIZE];
23   return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
24                              time_constants::ASCTIME_MAX_BYTES);
25 }
26 
27 } // namespace LIBC_NAMESPACE_DECL
28