[libc] Add implementation of difftime function.
authorRaman Tenneti <rtenneti@google.com>
Mon, 24 Oct 2022 18:42:43 +0000 (11:42 -0700)
committerRaman Tenneti <rtenneti@google.com>
Mon, 24 Oct 2022 22:14:26 +0000 (15:14 -0700)
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@google.com>
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D136631

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/src/time/difftime.cpp [new file with mode: 0644]
libc/src/time/difftime.h [new file with mode: 0644]
libc/test/src/time/CMakeLists.txt
libc/test/src/time/difftime_test.cpp [new file with mode: 0644]

index 92e3cdc..134146c 100644 (file)
@@ -372,6 +372,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # 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
index b2041d7..6e870bc 100644 (file)
@@ -443,6 +443,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # 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
index 7a2b897..9439da2 100644 (file)
@@ -41,7 +41,7 @@ clock_nanosleep
 clock_settime
 ctime
 ctime_r
-difftime
+difftime              |check|
 getdate
 gmtime                |check|
 gmtime_r              |check|
index 660b796..d3bb5d4 100644 (file)
@@ -46,6 +46,16 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  difftime
+  SRCS
+    difftime.cpp
+  HDRS
+    difftime.h
+  DEPENDS
+    libc.include.time
+)
+
+add_entrypoint_object(
   gmtime
   SRCS
     gmtime.cpp
diff --git a/libc/src/time/difftime.cpp b/libc/src/time/difftime.cpp
new file mode 100644 (file)
index 0000000..b42b1f4
--- /dev/null
@@ -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/src/time/difftime.h b/libc/src/time/difftime.h
new file mode 100644 (file)
index 0000000..feadef1
--- /dev/null
@@ -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 <time.h>
+
+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"
index 91c9cdc..10a2f4e 100644 (file)
@@ -41,6 +41,16 @@ add_libc_unittest(
 )
 
 add_libc_unittest(
+  difftime
+  SUITE
+    libc_time_unittests
+  SRCS
+    difftime_test.cpp
+  DEPENDS
+    libc.src.time.difftime
+)
+
+add_libc_unittest(
   gmtime
   SUITE
     libc_time_unittests
diff --git a/libc/test/src/time/difftime_test.cpp b/libc/test/src/time/difftime_test.cpp
new file mode 100644 (file)
index 0000000..4c07e3d
--- /dev/null
@@ -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 <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::difftime(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());
+}