diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -372,6 +372,7 @@ # time.h entrypoints libc.src.time.asctime libc.src.time.asctime_r + libc.src.time.difftime libc.src.time.gmtime libc.src.time.gmtime_r libc.src.time.mktime diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -443,6 +443,7 @@ # time.h entrypoints libc.src.time.asctime libc.src.time.asctime_r + libc.src.time.difftime libc.src.time.gmtime libc.src.time.gmtime_r libc.src.time.mktime diff --git a/libc/docs/date_and_time.rst b/libc/docs/date_and_time.rst --- a/libc/docs/date_and_time.rst +++ b/libc/docs/date_and_time.rst @@ -41,7 +41,7 @@ clock_settime ctime ctime_r -difftime +difftime |check| getdate gmtime |check| gmtime_r |check| diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt --- a/libc/src/time/CMakeLists.txt +++ b/libc/src/time/CMakeLists.txt @@ -45,6 +45,16 @@ libc.src.errno.errno ) +add_entrypoint_object( + difftime + SRCS + difftime.cpp + HDRS + difftime.h + DEPENDS + libc.include.time +) + add_entrypoint_object( gmtime SRCS diff --git a/libc/src/time/difftime.h b/libc/src/time/difftime.h new file mode 100644 --- /dev/null +++ b/libc/src/time/difftime.h @@ -0,0 +1,22 @@ +//===-- Implementation header of difftime -----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_TIME_DIFFTIME_H +#define LLVM_LIBC_SRC_TIME_DIFFTIME_H + +#include + +namespace __llvm_libc { + +double difftime(time_t end, time_t beginning); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_TIME_DIFFTIME_H + +#include "include/time.h" diff --git a/libc/src/time/difftime.cpp b/libc/src/time/difftime.cpp new file mode 100644 --- /dev/null +++ b/libc/src/time/difftime.cpp @@ -0,0 +1,18 @@ +//===-- Implementation of difftime function -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/time/difftime.h" +#include "src/__support/common.h" + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(double, difftime, (time_t end, time_t beginning)) { + return end - beginning; +} + +} // namespace __llvm_libc diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt --- a/libc/test/src/time/CMakeLists.txt +++ b/libc/test/src/time/CMakeLists.txt @@ -40,6 +40,16 @@ libc.src.time.clock_gettime ) +add_libc_unittest( + difftime + SUITE + libc_time_unittests + SRCS + difftime_test.cpp + DEPENDS + libc.src.time.difftime +) + add_libc_unittest( gmtime SUITE diff --git a/libc/test/src/time/difftime_test.cpp b/libc/test/src/time/difftime_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/time/difftime_test.cpp @@ -0,0 +1,40 @@ +//===-- Unittests for difftime --------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/__support/FPUtil/FPBits.h" +#include "src/time/difftime.h" +#include "src/time/time_utils.h" +#include "test/ErrnoSetterMatcher.h" +#include "utils/UnitTest/Test.h" + +#include + +using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; +using __llvm_libc::time_utils::TimeConstants; + +TEST(LlvmLibcDifftime, SmokeTest) { + time_t t1_seconds = TimeConstants::SECONDS_PER_HOUR; + time_t t2_seconds = 0; + + __llvm_libc::fputil::FPBits expected_fp = + __llvm_libc::fputil::FPBits(); + expected_fp = __llvm_libc::fputil::FPBits( + static_cast(t1_seconds)); + + double result = __llvm_libc::difftime(t1_seconds, t2_seconds); + + __llvm_libc::fputil::FPBits actual_fp = + __llvm_libc::fputil::FPBits(); + actual_fp = __llvm_libc::fputil::FPBits( + static_cast(result)); + + EXPECT_EQ(actual_fp.bits, expected_fp.bits); + EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign()); + EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent()); + EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa()); +}