33 lines · cpp
1//===-- Implementation of strftime 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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/time/strftime.h"10#include "hdr/types/size_t.h"11#include "hdr/types/struct_tm.h"12#include "src/__support/common.h"13#include "src/__support/macros/config.h"14#include "src/stdio/printf_core/writer.h"15#include "src/time/strftime_core/strftime_main.h"16 17namespace LIBC_NAMESPACE_DECL {18 19LLVM_LIBC_FUNCTION(size_t, strftime,20 (char *__restrict buffer, size_t buffsz,21 const char *__restrict format, const tm *timeptr)) {22 printf_core::WriteBuffer<printf_core::Mode<23 printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>24 wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));25 printf_core::Writer writer(wb);26 auto ret = strftime_core::strftime_main(&writer, format, timeptr);27 if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.28 wb.buff[wb.buff_cur] = '\0';29 return (!ret.has_value() || ret.value() >= buffsz) ? 0 : ret.value();30}31 32} // namespace LIBC_NAMESPACE_DECL33