[libc-commits] [libc] [libc] implement localtime (PR #110363)

via libc-commits libc-commits at lists.llvm.org
Sat Sep 28 06:57:27 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 104f3c180644c8872eaad0b3fcf6a6b948d92a71 6eea6286a65bac86d780b35b7232d8c8b9891398 --extensions cpp,h -- libc/src/time/localtime.cpp libc/src/time/localtime.h libc/src/time/localtime_r.cpp libc/src/time/localtime_r.h libc/test/src/time/localtime_r_test.cpp libc/test/src/time/localtime_test.cpp libc/src/time/time_utils.cpp libc/src/time/time_utils.h libc/test/src/time/ctime_r_test.cpp libc/test/src/time/ctime_test.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/src/time/localtime.cpp b/libc/src/time/localtime.cpp
index c08af2d8b2..48c8d4a6c6 100644
--- a/libc/src/time/localtime.cpp
+++ b/libc/src/time/localtime.cpp
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(struct tm *, localtime, (const time_t *t_ptr)) {
   if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
-      return nullptr;
+    return nullptr;
   }
 
   return time_utils::localtime(t_ptr);
diff --git a/libc/src/time/localtime_r.cpp b/libc/src/time/localtime_r.cpp
index 6ff3fb3d4f..64a107a729 100644
--- a/libc/src/time/localtime_r.cpp
+++ b/libc/src/time/localtime_r.cpp
@@ -14,9 +14,10 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(struct tm *, localtime_r, (const time_t *t_ptr, struct tm *tm)) {
+LLVM_LIBC_FUNCTION(struct tm *, localtime_r,
+                   (const time_t *t_ptr, struct tm *tm)) {
   if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
-      return nullptr;
+    return nullptr;
   }
 
   return time_utils::localtime_internal(t_ptr, tm);
diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp
index d00cc13947..9a4aacb8f8 100644
--- a/libc/src/time/time_utils.cpp
+++ b/libc/src/time/time_utils.cpp
@@ -6,11 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <stdio.h>
 #include "src/time/time_utils.h"
 #include "src/__support/CPP/limits.h" // INT_MIN, INT_MAX
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include <stdio.h>
 
 namespace LIBC_NAMESPACE_DECL {
 namespace time_utils {
@@ -143,10 +143,10 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
 
   int offset;
   if (internal::same_string(timezone, "UTC") == 0) {
-      offset = 0;
+    offset = 0;
   }
   if (internal::same_string(timezone, "Europe/Berlin") == 0) {
-      offset = 2;
+    offset = 2;
   }
 
   // All the data (years, month and remaining days) was calculated from
@@ -185,7 +185,7 @@ int calculate_dst(struct tm *tm) {
   } else if (tm->tm_mon > 3 && tm->tm_mon < 11) {
     return 1;
   } else if (tm->tm_mon == 3) {
-      return sunday >= 8;
+    return sunday >= 8;
   }
 
   return sunday <= 0;
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 97374ca5b1..87e0e0bae7 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -11,11 +11,11 @@
 
 #include <stddef.h> // For size_t.
 
+#include "src/__support/CPP/limits.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 #include "src/time/mktime.h"
-#include "src/__support/CPP/limits.h"
 
 #include <stdint.h>
 
@@ -175,8 +175,9 @@ LIBC_INLINE struct tm *localtime(const time_t *t_ptr) {
   return &result;
 }
 
-LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr, struct tm *result) {
-  //time_t time = *t;
+LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr,
+                                          struct tm *result) {
+  // time_t time = *t;
   int64_t t = *t_ptr;
 
   // Update the tm structure's year, month, day, etc. from seconds.
diff --git a/libc/test/src/time/localtime_r_test.cpp b/libc/test/src/time/localtime_r_test.cpp
index 83848b53fc..edc7fed030 100644
--- a/libc/test/src/time/localtime_r_test.cpp
+++ b/libc/test/src/time/localtime_r_test.cpp
@@ -13,17 +13,15 @@
 
 TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp0) {
   const time_t t_ptr = 1;
-  static struct tm input = (struct tm) {
-      .tm_sec = 0,
-      .tm_min = 0,
-      .tm_hour = 0,
-      .tm_mday = 0,
-      .tm_mon = 0,
-      .tm_year = 0,
-      .tm_wday = 0,
-      .tm_yday = 0,
-      .tm_isdst = 0
-  };
+  static struct tm input = (struct tm){.tm_sec = 0,
+                                       .tm_min = 0,
+                                       .tm_hour = 0,
+                                       .tm_mday = 0,
+                                       .tm_mon = 0,
+                                       .tm_year = 0,
+                                       .tm_wday = 0,
+                                       .tm_yday = 0,
+                                       .tm_isdst = 0};
   struct tm *result = LIBC_NAMESPACE::localtime_r(&t_ptr, &input);
   ASSERT_EQ(70, result->tm_year);
   ASSERT_EQ(0, result->tm_mon);
@@ -38,17 +36,15 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp0) {
 
 TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32Int) {
   time_t t_ptr = 2147483647;
-  static struct tm input = (struct tm) {
-      .tm_sec = 0,
-      .tm_min = 0,
-      .tm_hour = 0,
-      .tm_mday = 0,
-      .tm_mon = 0,
-      .tm_year = 0,
-      .tm_wday = 0,
-      .tm_yday = 0,
-      .tm_isdst = 0
-  };
+  static struct tm input = (struct tm){.tm_sec = 0,
+                                       .tm_min = 0,
+                                       .tm_hour = 0,
+                                       .tm_mday = 0,
+                                       .tm_mon = 0,
+                                       .tm_year = 0,
+                                       .tm_wday = 0,
+                                       .tm_yday = 0,
+                                       .tm_isdst = 0};
   struct tm *result = LIBC_NAMESPACE::localtime_r(&t_ptr, &input);
   ASSERT_EQ(138, result->tm_year);
   ASSERT_EQ(0, result->tm_mon);
@@ -63,17 +59,15 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32Int) {
 
 TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32IntDst) {
   time_t t_ptr = 1627225465;
-  static struct tm input = (struct tm) {
-      .tm_sec = 0,
-      .tm_min = 0,
-      .tm_hour = 0,
-      .tm_mday = 0,
-      .tm_mon = 0,
-      .tm_year = 0,
-      .tm_wday = 0,
-      .tm_yday = 0,
-      .tm_isdst = 0
-  };
+  static struct tm input = (struct tm){.tm_sec = 0,
+                                       .tm_min = 0,
+                                       .tm_hour = 0,
+                                       .tm_mday = 0,
+                                       .tm_mon = 0,
+                                       .tm_year = 0,
+                                       .tm_wday = 0,
+                                       .tm_yday = 0,
+                                       .tm_isdst = 0};
   struct tm *result = LIBC_NAMESPACE::localtime_r(&t_ptr, &input);
   ASSERT_EQ(121, result->tm_year);
   ASSERT_EQ(6, result->tm_mon);

``````````

</details>


https://github.com/llvm/llvm-project/pull/110363


More information about the libc-commits mailing list