[libc-commits] [libc] 1220442 - [libc] Add implementation of difftime function.
Raman Tenneti via libc-commits
libc-commits at lists.llvm.org
Mon Oct 24 15:14:31 PDT 2022
Author: Raman Tenneti
Date: 2022-10-24T15:14:26-07:00
New Revision: 12204429f2f9e1ab47e3fcb7a2ec4a262c800818
URL: https://github.com/llvm/llvm-project/commit/12204429f2f9e1ab47e3fcb7a2ec4a262c800818
DIFF: https://github.com/llvm/llvm-project/commit/12204429f2f9e1ab47e3fcb7a2ec4a262c800818.diff
LOG: [libc] Add implementation of difftime function.
The difftime function computes the difference between two calendar
times: time1 - time0 as per as per 7.27.2.2 section in
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2478.pdf.
double difftime(time_t time1, time_t time0);
Tested:
Unit tests
Co-authored-by: Jeff Bailey <jeffbailey at google.com>
Reviewed By: jeffbailey
Differential Revision: https://reviews.llvm.org/D136631
Added:
libc/src/time/difftime.cpp
libc/src/time/difftime.h
libc/test/src/time/difftime_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/docs/date_and_time.rst
libc/src/time/CMakeLists.txt
libc/test/src/time/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 92e3cdce6a19f..134146c6d8215 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -372,6 +372,7 @@ if(LLVM_LIBC_FULL_BUILD)
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
+ libc.src.time.
diff time
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
index b2041d7ad3876..6e870bc834b2f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -443,6 +443,7 @@ if(LLVM_LIBC_FULL_BUILD)
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
+ libc.src.time.
diff time
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
index 7a2b8975285b1..9439da26e0d21 100644
--- a/libc/docs/date_and_time.rst
+++ b/libc/docs/date_and_time.rst
@@ -41,7 +41,7 @@ clock_nanosleep
clock_settime
ctime
ctime_r
-
diff time
+
diff time |check|
getdate
gmtime |check|
gmtime_r |check|
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index 660b79678b524..d3bb5d4378763 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -45,6 +45,16 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+
diff time
+ SRCS
+
diff time.cpp
+ HDRS
+
diff time.h
+ DEPENDS
+ libc.include.time
+)
+
add_entrypoint_object(
gmtime
SRCS
diff --git a/libc/src/time/
diff time.cpp b/libc/src/time/
diff time.cpp
new file mode 100644
index 0000000000000..b42b1f4fd1300
--- /dev/null
+++ b/libc/src/time/
diff time.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of
diff time 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/
diff time.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(double,
diff time, (time_t end, time_t beginning)) {
+ return end - beginning;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/time/
diff time.h b/libc/src/time/
diff time.h
new file mode 100644
index 0000000000000..feadef135933a
--- /dev/null
+++ b/libc/src/time/
diff time.h
@@ -0,0 +1,22 @@
+//===-- Implementation header of
diff time -----------------------*- 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 <time.h>
+
+namespace __llvm_libc {
+
+double
diff time(time_t end, time_t beginning);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_TIME_DIFFTIME_H
+
+#include "include/time.h"
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index 91c9cdc38e057..10a2f4ee7ec03 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -40,6 +40,16 @@ add_libc_unittest(
libc.src.time.clock_gettime
)
+add_libc_unittest(
+
diff time
+ SUITE
+ libc_time_unittests
+ SRCS
+
diff time_test.cpp
+ DEPENDS
+ libc.src.time.
diff time
+)
+
add_libc_unittest(
gmtime
SUITE
diff --git a/libc/test/src/time/
diff time_test.cpp b/libc/test/src/time/
diff time_test.cpp
new file mode 100644
index 0000000000000..4c07e3d235aeb
--- /dev/null
+++ b/libc/test/src/time/
diff time_test.cpp
@@ -0,0 +1,40 @@
+//===-- Unittests for
diff time --------------------------------------------===//
+//
+// 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/
diff time.h"
+#include "src/time/time_utils.h"
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+
+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<long double> expected_fp =
+ __llvm_libc::fputil::FPBits<long double>();
+ expected_fp = __llvm_libc::fputil::FPBits<long double>(
+ static_cast<long double>(t1_seconds));
+
+ double result = __llvm_libc::
diff time(t1_seconds, t2_seconds);
+
+ __llvm_libc::fputil::FPBits<long double> actual_fp =
+ __llvm_libc::fputil::FPBits<long double>();
+ actual_fp = __llvm_libc::fputil::FPBits<long double>(
+ static_cast<long double>(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());
+}
More information about the libc-commits
mailing list