xref: /llvm-project/libc/src/time/ctime_r.cpp (revision f9c2377fb68e5051b3061186c507f7b87db2a8b2)
1000a3f0aSЗишан Мирза //===-- Implementation of ctime_r function --------------------------------===//
2000a3f0aSЗишан Мирза //
3000a3f0aSЗишан Мирза // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4000a3f0aSЗишан Мирза // See https://llvm.org/LICENSE.txt for license information.
5000a3f0aSЗишан Мирза // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6000a3f0aSЗишан Мирза //
7000a3f0aSЗишан Мирза //===----------------------------------------------------------------------===//
8000a3f0aSЗишан Мирза 
9*f9c2377fSMichael Jones #include "src/time/ctime_r.h"
10000a3f0aSЗишан Мирза #include "src/__support/CPP/limits.h"
11000a3f0aSЗишан Мирза #include "src/__support/common.h"
12000a3f0aSЗишан Мирза #include "src/__support/macros/config.h"
13*f9c2377fSMichael Jones #include "src/time/time_constants.h"
14*f9c2377fSMichael Jones #include "src/time/time_utils.h"
15000a3f0aSЗишан Мирза 
16000a3f0aSЗишан Мирза namespace LIBC_NAMESPACE_DECL {
17000a3f0aSЗишан Мирза 
18000a3f0aSЗишан Мирза LLVM_LIBC_FUNCTION(char *, ctime_r, (const time_t *t_ptr, char *buffer)) {
19000a3f0aSЗишан Мирза   if (t_ptr == nullptr || buffer == nullptr ||
20000a3f0aSЗишан Мирза       *t_ptr > cpp::numeric_limits<int32_t>::max()) {
21000a3f0aSЗишан Мирза     return nullptr;
22000a3f0aSЗишан Мирза   }
23000a3f0aSЗишан Мирза 
24000a3f0aSЗишан Мирза   return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
25*f9c2377fSMichael Jones                              time_constants::ASCTIME_MAX_BYTES);
26000a3f0aSЗишан Мирза }
27000a3f0aSЗишан Мирза 
28000a3f0aSЗишан Мирза } // namespace LIBC_NAMESPACE_DECL
29