[libc-commits] [libc] 9ad0ac8 - [libc] Add Month enum, refactor mktime_test

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Tue Feb 21 09:24:40 PST 2023


Author: Jeff Bailey
Date: 2023-02-21T17:24:33Z
New Revision: 9ad0ac8eb3d0bc03cac9c8871d0ee771156a67b4

URL: https://github.com/llvm/llvm-project/commit/9ad0ac8eb3d0bc03cac9c8871d0ee771156a67b4
DIFF: https://github.com/llvm/llvm-project/commit/9ad0ac8eb3d0bc03cac9c8871d0ee771156a67b4.diff

LOG: [libc] Add Month enum, refactor mktime_test

Introduce Month enum.

Use designated initializers instead of comments to ensure that tm_data
is initialized correctly.  Use Month enum for initializing .tm_mon to
avoid off-by-one errors.

Co-authored-by: Raman Tenneti <rtenneti at google.com>

Reviewed By: rtenneti

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

Added: 
    

Modified: 
    libc/src/time/time_utils.h
    libc/test/src/time/mktime_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index a4d51d8042db5..317195da7f61a 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -22,10 +22,26 @@
 namespace __llvm_libc {
 namespace time_utils {
 
+enum Month : int {
+  JANUARY,
+  FEBRUARY,
+  MARCH,
+  APRIL,
+  MAY,
+  JUNE,
+  JULY,
+  AUGUST,
+  SEPTEMBER,
+  OCTOBER,
+  NOVEMBER,
+  DECEMBER
+};
+
 struct TimeConstants {
   static constexpr int SECONDS_PER_MIN = 60;
   static constexpr int SECONDS_PER_HOUR = 3600;
   static constexpr int SECONDS_PER_DAY = 86400;
+  static constexpr int MINUTES_PER_HOUR = 60;
   static constexpr int DAYS_PER_WEEK = 7;
   static constexpr int MONTHS_PER_YEAR = 12;
   static constexpr int DAYS_PER_NON_LEAP_YEAR = 365;

diff  --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp
index 6793298fc0731..231696d45fcb8 100644
--- a/libc/test/src/time/mktime_test.cpp
+++ b/libc/test/src/time/mktime_test.cpp
@@ -18,413 +18,418 @@
 
 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
-using __llvm_libc::time_utils::TimeConstants;
+using __llvm_libc::time_utils::Month;
 
-static inline time_t call_mktime(struct tm *tm_data, int year, int month,
-                                 int mday, int hour, int min, int sec, int wday,
-                                 int yday) {
-  __llvm_libc::tmhelper::testing::initialize_tm_data(
-      tm_data, year, month, mday, hour, min, sec, wday, yday);
-  return __llvm_libc::mktime(tm_data);
+static inline constexpr int tm_year(int year) {
+  return year - TimeConstants::TIME_YEAR_BASE;
 }
 
 TEST(LlvmLibcMkTime, FailureSetsErrno) {
-  struct tm tm_data;
-  __llvm_libc::tmhelper::testing::initialize_tm_data(
-      &tm_data, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, -1, 0, 0);
+  struct tm tm_data {
+    .tm_sec = INT_MAX, .tm_min = INT_MAX, .tm_hour = INT_MAX,
+    .tm_mday = INT_MAX, .tm_mon = INT_MAX - 1, .tm_year = tm_year(INT_MAX),
+    .tm_wday = 0, .tm_yday = 0
+  };
   EXPECT_THAT(__llvm_libc::mktime(&tm_data), Fails(EOVERFLOW));
 }
 
 TEST(LlvmLibcMkTime, InvalidSeconds) {
-  struct tm tm_data;
-  // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          1,    // day
-                          0,    // hr
-                          0,    // min
-                          -1,   // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(-1));
-  EXPECT_TM_EQ((tm{59,     // sec
-                   59,     // min
-                   23,     // hr
-                   31,     // day
-                   12 - 1, // tm_mon starts with 0 for Jan
-                   1969 - TimeConstants::TIME_YEAR_BASE, // year
-                   3,                                    // wday
-                   364,                                  // yday
-                   0}),
-               tm_data);
-  // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          1,    // day
-                          0,    // hr
-                          0,    // min
-                          60,   // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(60));
-  EXPECT_TM_EQ((tm{0, // sec
-                   1, // min
-                   0, // hr
-                   1, // day
-                   0, // tm_mon starts with 0 for Jan
-                   1970 - TimeConstants::TIME_YEAR_BASE, // year
-                   4,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59.
+    struct tm tm_data {
+      .tm_sec = -1, .tm_min = 0, .tm_hour = 0, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data), Succeeds(-1));
+    EXPECT_TM_EQ((tm{.tm_sec = 59,
+                     .tm_min = 59,
+                     .tm_hour = 23,
+                     .tm_mday = 31,
+                     .tm_mon = Month::DECEMBER,
+                     .tm_year = tm_year(1969),
+                     .tm_wday = 3,
+                     .tm_yday = 364}),
+                 tm_data);
+  }
+
+  {
+    // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00.
+    struct tm tm_data {
+      .tm_sec = 60, .tm_min = 0, .tm_hour = 0, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data), Succeeds(60));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 1,
+                     .tm_hour = 0,
+                     .tm_mday = 1,
+                     .tm_mon = Month::JANUARY,
+                     .tm_year = tm_year(1970),
+                     .tm_wday = 4,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 }
 
 TEST(LlvmLibcMkTime, InvalidMinutes) {
-  struct tm tm_data;
-  // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          1,    // day
-                          0,    // hr
-                          -1,   // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(-TimeConstants::SECONDS_PER_MIN));
-  EXPECT_TM_EQ((tm{0,  // sec
-                   59, // min
-                   23, // hr
-                   31, // day
-                   11, // tm_mon starts with 0 for Jan
-                   1969 - TimeConstants::TIME_YEAR_BASE, // year
-                   3,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
-  // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          1,    // day
-                          0,    // hr
-                          60,   // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(60 * TimeConstants::SECONDS_PER_MIN));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   1, // hr
-                   1, // day
-                   0, // tm_mon starts with 0 for Jan
-                   1970 - TimeConstants::TIME_YEAR_BASE, // year
-                   4,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = -1, .tm_hour = 0, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(-TimeConstants::SECONDS_PER_MIN));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 59,
+                     .tm_hour = 23,
+                     .tm_mday = 31,
+                     .tm_mon = Month::DECEMBER,
+                     .tm_year = tm_year(1969),
+                     .tm_wday = 3,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
+
+  {
+    // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 60, .tm_hour = 0, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(60 * TimeConstants::SECONDS_PER_MIN));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 1,
+                     .tm_mday = 1,
+                     .tm_mon = Month::JANUARY,
+                     .tm_year = tm_year(1970),
+                     .tm_wday = 4,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 }
 
 TEST(LlvmLibcMkTime, InvalidHours) {
-  struct tm tm_data;
-  // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          1,    // day
-                          -1,   // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(-TimeConstants::SECONDS_PER_HOUR));
-  EXPECT_TM_EQ((tm{0,  // sec
-                   0,  // min
-                   23, // hr
-                   31, // day
-                   11, // tm_mon starts with 0 for Jan
-                   1969 - TimeConstants::TIME_YEAR_BASE, // year
-                   3,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
-  // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          1,    // day
-                          24,   // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(24 * TimeConstants::SECONDS_PER_HOUR));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   0, // hr
-                   2, // day
-                   0, // tm_mon starts with 0 for Jan
-                   1970 - TimeConstants::TIME_YEAR_BASE, // year
-                   5,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = -1, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(-TimeConstants::SECONDS_PER_HOUR));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 23,
+                     .tm_mday = 31,
+                     .tm_mon = Month::DECEMBER,
+                     .tm_year = tm_year(1969),
+                     .tm_wday = 3,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
+
+  {
+    // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 24, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(24 * TimeConstants::SECONDS_PER_HOUR));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 2,
+                     .tm_mon = Month::JANUARY,
+                     .tm_year = tm_year(1970),
+                     .tm_wday = 5,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 }
 
 TEST(LlvmLibcMkTime, InvalidYear) {
-  struct tm tm_data;
   // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1969, // year
-                          1,    // month
-                          1,    // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
+  struct tm tm_data {
+    .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1,
+    .tm_mon = Month::JANUARY, .tm_year = tm_year(1969), .tm_wday = 0,
+    .tm_yday = 0
+  };
+  EXPECT_THAT(__llvm_libc::mktime(&tm_data),
               Succeeds(-TimeConstants::DAYS_PER_NON_LEAP_YEAR *
                        TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   0, // hr
-                   1, // day
-                   0, // tm_mon starts with 0 for Jan
-                   1969 - TimeConstants::TIME_YEAR_BASE, // year
-                   3,                                    // wday
-                   0,                                    // yday
-                   0}),
+  EXPECT_TM_EQ((tm{.tm_sec = 0,
+                   .tm_min = 0,
+                   .tm_hour = 0,
+                   .tm_mday = 1,
+                   .tm_mon = Month::JANUARY,
+                   .tm_year = tm_year(1969),
+                   .tm_wday = 3,
+                   .tm_yday = 0}),
                tm_data);
 }
 
 TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
   if (sizeof(size_t) != 4)
     return;
-  struct tm tm_data;
-  // 2038-01-19 03:14:08 tests overflow of the second in 2038.
-  EXPECT_THAT(call_mktime(&tm_data, 2038, 1, 19, 3, 14, 8, 0, 0),
-              Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
-  // 2038-01-19 03:15:07 tests overflow of the minute in 2038.
-  EXPECT_THAT(call_mktime(&tm_data, 2038, 1, 19, 3, 15, 7, 0, 0),
-              Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
-  // 2038-01-19 04:14:07 tests overflow of the hour in 2038.
-  EXPECT_THAT(call_mktime(&tm_data, 2038, 1, 19, 4, 14, 7, 0, 0),
-              Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
-  // 2038-01-20 03:14:07 tests overflow of the day in 2038.
-  EXPECT_THAT(call_mktime(&tm_data, 2038, 1, 20, 3, 14, 7, 0, 0),
-              Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
-  // 2038-02-19 03:14:07 tests overflow of the month in 2038.
-  EXPECT_THAT(call_mktime(&tm_data, 2038, 2, 19, 3, 14, 7, 0, 0),
-              Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
-  // 2039-01-19 03:14:07 tests overflow of the year.
-  EXPECT_THAT(call_mktime(&tm_data, 2039, 1, 19, 3, 14, 7, 0, 0),
-              Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  {
+    // 2038-01-19 03:14:08 tests overflow of the second in 2038.
+    struct tm tm_data {
+      .tm_sec = 8, .tm_min = 14, .tm_hour = 3, .tm_mday = 19,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  }
+
+  {
+    // 2038-01-19 03:15:07 tests overflow of the minute in 2038.
+    struct tm tm_data {
+      .tm_sec = 7, .tm_min = 15, .tm_hour = 3, .tm_mday = 19,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  }
+
+  {
+    // 2038-01-19 04:14:07 tests overflow of the hour in 2038.
+    struct tm tm_data {
+      .tm_sec = 7, .tm_min = 14, .tm_hour = 4, .tm_mday = 19,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  }
+
+  {
+    // 2038-01-20 03:14:07 tests overflow of the day in 2038.
+    struct tm tm_data {
+      .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 20,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  }
+
+  {
+    // 2038-02-19 03:14:07 tests overflow of the month in 2038.
+    struct tm tm_data {
+      .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19,
+      .tm_mon = Month::FEBRUARY, .tm_year = tm_year(2038), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  }
+
+  {
+    // 2039-01-19 03:14:07 tests overflow of the year.
+    struct tm tm_data {
+      .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2039), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::OUT_OF_RANGE_RETURN_VALUE));
+  }
 }
 
 TEST(LlvmLibcMkTime, InvalidMonths) {
-  struct tm tm_data;
-  // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          0,    // month
-                          1,    // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(-31 * TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0,      // sec
-                   0,      // min
-                   0,      // hr
-                   1,      // day
-                   12 - 1, // tm_mon starts with 0 for Jan
-                   1969 - TimeConstants::TIME_YEAR_BASE, // year
-                   1,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
-  // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          13,   // month
-                          1,    // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(TimeConstants::DAYS_PER_NON_LEAP_YEAR *
-                       TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   0, // hr
-                   1, // day
-                   0, // tm_mon starts with 0 for Jan
-                   1971 - TimeConstants::TIME_YEAR_BASE, // year
-                   5,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 0, .tm_mon = -1,
+      .tm_year = tm_year(1970), .tm_wday = 0, .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(-32 * TimeConstants::SECONDS_PER_DAY));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 1,
+                     .tm_mon = Month::DECEMBER,
+                     .tm_year = tm_year(1969),
+                     .tm_wday = 1,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
+
+  {
+    // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_mon = 12,
+      .tm_year = tm_year(1970), .tm_wday = 0, .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(TimeConstants::DAYS_PER_NON_LEAP_YEAR *
+                         TimeConstants::SECONDS_PER_DAY));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 1,
+                     .tm_mon = Month::JANUARY,
+                     .tm_year = tm_year(1971),
+                     .tm_wday = 5,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 }
 
 TEST(LlvmLibcMkTime, InvalidDays) {
-  struct tm tm_data;
-  // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          0,    // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(-1 * TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0,  // sec
-                   0,  // min
-                   0,  // hr
-                   31, // day
-                   11, // tm_mon starts with 0 for Jan
-                   1969 - TimeConstants::TIME_YEAR_BASE, // year
-                   3,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = (1 - 1),
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(-1 * TimeConstants::SECONDS_PER_DAY));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 31,
+                     .tm_mon = Month::DECEMBER,
+                     .tm_year = tm_year(1969),
+                     .tm_wday = 3,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 
-  // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          1,    // month
-                          32,   // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(31 * TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   0, // hr
-                   1, // day
-                   0, // tm_mon starts with 0 for Jan
-                   1970 - TimeConstants::TIME_YEAR_BASE, // year
-                   0,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 32,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(31 * TimeConstants::SECONDS_PER_DAY));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 1,
+                     .tm_mon = Month::FEBRUARY,
+                     .tm_year = tm_year(1970),
+                     .tm_wday = 0,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 
-  // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1970, // year
-                          2,    // month
-                          29,   // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(59 * TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   0, // hr
-                   1, // day
-                   2, // tm_mon starts with 0 for Jan
-                   1970 - TimeConstants::TIME_YEAR_BASE, // year
-                   0,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 29,
+      .tm_mon = Month::FEBRUARY, .tm_year = tm_year(1970), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(59 * TimeConstants::SECONDS_PER_DAY));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 1,
+                     .tm_mon = Month::MARCH,
+                     .tm_year = tm_year(1970),
+                     .tm_wday = 0,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 
-  // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          1972, // year
-                          2,    // month
-                          30,   // day
-                          0,    // hr
-                          0,    // min
-                          0,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) *
-                       TimeConstants::SECONDS_PER_DAY));
-  EXPECT_TM_EQ((tm{0, // sec
-                   0, // min
-                   0, // hr
-                   1, // day
-                   2, // tm_mon starts with 0 for Jan
-                   1972 - TimeConstants::TIME_YEAR_BASE, // year
-                   3,                                    // wday
-                   0,                                    // yday
-                   0}),
-               tm_data);
+  {
+    // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00.
+    struct tm tm_data {
+      .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 30,
+      .tm_mon = Month::FEBRUARY, .tm_year = tm_year(1972), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data),
+                Succeeds(((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) *
+                         TimeConstants::SECONDS_PER_DAY));
+    EXPECT_TM_EQ((tm{.tm_sec = 0,
+                     .tm_min = 0,
+                     .tm_hour = 0,
+                     .tm_mday = 1,
+                     .tm_mon = Month::MARCH,
+                     .tm_year = tm_year(1972),
+                     .tm_wday = 3,
+                     .tm_yday = 0}),
+                 tm_data);
+  }
 }
 
 TEST(LlvmLibcMkTime, EndOf32BitEpochYear) {
-  struct tm tm_data;
   // Test for maximum value of a signed 32-bit integer.
   // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
-  EXPECT_THAT(call_mktime(&tm_data,
-                          2038, // year
-                          1,    // month
-                          19,   // day
-                          3,    // hr
-                          14,   // min
-                          7,    // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(0x7FFFFFFF));
-  EXPECT_TM_EQ((tm{7,  // sec
-                   14, // min
-                   3,  // hr
-                   19, // day
-                   0,  // tm_mon starts with 0 for Jan
-                   2038 - TimeConstants::TIME_YEAR_BASE, // year
-                   2,                                    // wday
-                   7,                                    // yday
-                   0}),
+  struct tm tm_data {
+    .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19,
+    .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0,
+    .tm_yday = 0
+  };
+  EXPECT_THAT(__llvm_libc::mktime(&tm_data), Succeeds(0x7FFFFFFF));
+  EXPECT_TM_EQ((tm{.tm_sec = 7,
+                   .tm_min = 14,
+                   .tm_hour = 3,
+                   .tm_mday = 19,
+                   .tm_mon = Month::JANUARY,
+                   .tm_year = tm_year(2038),
+                   .tm_wday = 2,
+                   .tm_yday = 7}),
                tm_data);
 }
 
 TEST(LlvmLibcMkTime, Max64BitYear) {
   if (sizeof(time_t) == 4)
     return;
-  // Mon Jan 1 12:50:50 2170 (200 years from 1970),
-  struct tm tm_data;
-  EXPECT_THAT(call_mktime(&tm_data,
-                          2170, // year
-                          1,    // month
-                          1,    // day
-                          12,   // hr
-                          50,   // min
-                          50,   // sec
-                          0,    // wday
-                          0),   // yday
-              Succeeds(6311479850));
-  EXPECT_TM_EQ((tm{50, // sec
-                   50, // min
-                   12, // hr
-                   1,  // day
-                   0,  // tm_mon starts with 0 for Jan
-                   2170 - TimeConstants::TIME_YEAR_BASE, // year
-                   1,                                    // wday
-                   50,                                   // yday
-                   0}),
-               tm_data);
+  {
+    // Mon Jan 1 12:50:50 2170 (200 years from 1970),
+    struct tm tm_data {
+      .tm_sec = 50, .tm_min = 50, .tm_hour = 12, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2170), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data), Succeeds(6311479850));
+    EXPECT_TM_EQ((tm{.tm_sec = 50,
+                     .tm_min = 50,
+                     .tm_hour = 12,
+                     .tm_mday = 1,
+                     .tm_mon = Month::JANUARY,
+                     .tm_year = tm_year(2170),
+                     .tm_wday = 1,
+                     .tm_yday = 50}),
+                 tm_data);
+  }
 
-  // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
-  EXPECT_THAT(call_mktime(&tm_data, 2147483647, 1, 1, 12, 50, 50, 0, 0),
-              Succeeds(67767976202043050));
-  EXPECT_TM_EQ((tm{50, // sec
-                   50, // min
-                   12, // hr
-                   1,  // day
-                   0,  // tm_mon starts with 0 for Jan
-                   2147483647 - TimeConstants::TIME_YEAR_BASE, // year
-                   2,                                          // wday
-                   50,                                         // yday
-                   0}),
-               tm_data);
+  {
+    // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
+    struct tm tm_data {
+      .tm_sec = 50, .tm_min = 50, .tm_hour = 12, .tm_mday = 1,
+      .tm_mon = Month::JANUARY, .tm_year = tm_year(2147483647), .tm_wday = 0,
+      .tm_yday = 0
+    };
+    EXPECT_THAT(__llvm_libc::mktime(&tm_data), Succeeds(67767976202043050));
+    EXPECT_TM_EQ((tm{.tm_sec = 50,
+                     .tm_min = 50,
+                     .tm_hour = 12,
+                     .tm_mday = 1,
+                     .tm_mon = Month::JANUARY,
+                     .tm_year = tm_year(2147483647),
+                     .tm_wday = 2,
+                     .tm_yday = 50}),
+                 tm_data);
+  }
 }


        


More information about the libc-commits mailing list