Timestamp ========= .. cpp:class:: numpy::Timestamp numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Timestamp Timestamp obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``Timestamp(int64_tvalue, const std::string &tz = "")`` - NP_TIMESTAMP.H:155 - :ref:`View ` * - ``Timestamp(intyear, intmonth, intday, inthour = 0, intminute = 0, intsecond = 0, intmicrosecond = 0, intnanosecond = 0, const std::string &tz = "", intfold = 0)`` - NP_TIMESTAMP.H:163 - :ref:`View ` * - ``Timestamp(const std::string &ts_input, const std::string &tz = "")`` - NP_TIMESTAMP.H:212 - :ref:`View ` * - ``Timestamp(const datetime64 &dt, const std::string &tz = "")`` - NP_TIMESTAMP.H:313 - :ref:`View ` * - ``Timestamp(const std::chrono::time_point&tp, const std::string &tz = "")`` - NP_TIMESTAMP.H:331 - :ref:`View ` Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``timedelta64 operator-(const Timestamp &other)`` - timedelta64 - NP_TIMESTAMP.H:1389 - * - ``Timestamp operator+(const timedelta64 &delta)`` - Timestamp - NP_TIMESTAMP.H:1400 - * - ``Timestamp operator-(const timedelta64 &delta)`` - Timestamp - NP_TIMESTAMP.H:1413 - * - ``Timestamp & operator+=(const timedelta64 &delta)`` - Timestamp & - NP_TIMESTAMP.H:1425 - * - ``Timestamp & operator-=(const timedelta64 &delta)`` - Timestamp & - NP_TIMESTAMP.H:1435 - * - ``Timestamp operator+(const Timedelta &delta)`` - Timestamp - NP_TIMESTAMP.H:1446 - * - ``Timestamp operator-(const Timedelta &delta)`` - Timestamp - NP_TIMESTAMP.H:1458 - * - ``Timestamp & operator+=(const Timedelta &delta)`` - Timestamp & - NP_TIMESTAMP.H:1469 - * - ``Timestamp & operator-=(const Timedelta &delta)`` - Timestamp & - NP_TIMESTAMP.H:1479 - * - ``bool operator==(const Timestamp &other)`` - bool - NP_TIMESTAMP.H:1493 - * - ``bool operator!=(const Timestamp &other)`` - bool - NP_TIMESTAMP.H:1498 - * - ``bool operator<(const Timestamp &other)`` - bool - NP_TIMESTAMP.H:1503 - * - ``bool operator<=(const Timestamp &other)`` - bool - NP_TIMESTAMP.H:1508 - * - ``bool operator>(const Timestamp &other)`` - bool - NP_TIMESTAMP.H:1513 - * - ``bool operator>=(const Timestamp &other)`` - bool - NP_TIMESTAMP.H:1518 - Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Timestamp max()`` - Timestamp - NP_TIMESTAMP.H:655 - :ref:`View ` * - ``Timestamp min()`` - Timestamp - NP_TIMESTAMP.H:651 - :ref:`View ` * - ``int minute()`` - int - NP_TIMESTAMP.H:699 - :ref:`View ` * - ``int nanosecond()`` - int - NP_TIMESTAMP.H:717 - :ref:`View ` Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Timestamp ceil(const std::string &freq, const std::string &ambiguous = "raise", const std::string &nonexistent = "raise")`` - Timestamp - NP_TIMESTAMP.H:1176 - :ref:`View ` * - ``Timestamp floor(const std::string &freq, const std::string &ambiguous = "raise", const std::string &nonexistent = "raise")`` - Timestamp - NP_TIMESTAMP.H:1148 - :ref:`View ` * - ``Timestamp round(const std::string &freq, const std::string &ambiguous = "raise", const std::string &nonexistent = "raise")`` - Timestamp - NP_TIMESTAMP.H:1207 - :ref:`View ` Linear Algebra -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void invalidateCache()`` - void - NP_TIMESTAMP.H:56 - * - ``Timestamp normalize()`` - Timestamp - NP_TIMESTAMP.H:1233 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``datetime64 to_datetime64()`` - datetime64 - NP_TIMESTAMP.H:861 - :ref:`View ` * - ``double to_julian_date()`` - double - NP_TIMESTAMP.H:946 - :ref:`View ` * - ``int64_t to_numpy(const std::string &dtype = "datetime64[ns]", boolcopy = false)`` - int64_t - NP_TIMESTAMP.H:875 - :ref:`View ` * - ``std::tm to_pydatetime()`` - std::tm - NP_TIMESTAMP.H:882 - :ref:`View ` Special Functions ----------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Timestamp combine(const std::tuple&date, const std::tuple&time)`` - Timestamp - NP_TIMESTAMP.H:389 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_leap_year()`` - bool - NP_TIMESTAMP.H:795 - :ref:`View ` * - ``bool is_month_end()`` - bool - NP_TIMESTAMP.H:807 - :ref:`View ` * - ``bool is_month_start()`` - bool - NP_TIMESTAMP.H:801 - :ref:`View ` * - ``bool is_quarter_end()`` - bool - NP_TIMESTAMP.H:819 - :ref:`View ` * - ``bool is_quarter_start()`` - bool - NP_TIMESTAMP.H:813 - :ref:`View ` * - ``bool is_year_end()`` - bool - NP_TIMESTAMP.H:831 - :ref:`View ` * - ``bool is_year_start()`` - bool - NP_TIMESTAMP.H:825 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Timestamp NaT()`` - Timestamp - NP_TIMESTAMP.H:659 - :ref:`View ` * - ``Timestamp as_unit(const std::string &unit, boolround_ok = true)`` - Timestamp - NP_TIMESTAMP.H:1286 - :ref:`View ` * - ``datetime64 asm8()`` - datetime64 - NP_TIMESTAMP.H:867 - :ref:`View ` * - ``Timestamp astimezone(const std::string &tz_str)`` - Timestamp - NP_TIMESTAMP.H:1362 - :ref:`View ` * - ``int64_t componentsToNanos(intyear, intmonth, intday, inthour, intminute, intsecond, intmicrosecond, intnanosecond)`` - int64_t - NP_TIMESTAMP.H:104 - * - ``void computeComponents()`` - void - NP_TIMESTAMP.H:60 - * - ``std::string ctime()`` - std::string - NP_TIMESTAMP.H:1040 - :ref:`View ` * - ``int day()`` - int - NP_TIMESTAMP.H:687 - :ref:`View ` * - ``std::string day_name(const std::string &locale = "")`` - std::string - NP_TIMESTAMP.H:1045 - :ref:`View ` * - ``int day_of_week()`` - int - NP_TIMESTAMP.H:749 - :ref:`View ` * - ``int day_of_year()`` - int - NP_TIMESTAMP.H:766 - :ref:`View ` * - ``int dayofweek()`` - int - NP_TIMESTAMP.H:732 - :ref:`View ` * - ``int dayofyear()`` - int - NP_TIMESTAMP.H:752 - :ref:`View ` * - ``int days_in_month()`` - int - NP_TIMESTAMP.H:785 - :ref:`View ` * - ``timedelta64 dst()`` - timedelta64 - NP_TIMESTAMP.H:1376 - :ref:`View ` * - ``int fold()`` - int - NP_TIMESTAMP.H:852 - :ref:`View ` * - ``Timestamp fromisocalendar(intiso_year, intiso_week, intiso_weekday)`` - Timestamp - NP_TIMESTAMP.H:398 - :ref:`View ` * - ``Timestamp fromisoformat(const std::string &date_string)`` - Timestamp - NP_TIMESTAMP.H:428 - :ref:`View ` * - ``Timestamp fromordinal(int64_tordinal, const std::string &tz = "")`` - Timestamp - NP_TIMESTAMP.H:380 - :ref:`View ` * - ``Timestamp fromtimestamp(doublets, const std::string &tz = "")`` - Timestamp - NP_TIMESTAMP.H:369 - :ref:`View ` * - ``int hour()`` - int - NP_TIMESTAMP.H:693 - :ref:`View ` * - ``bool isNaT()`` - bool - NP_TIMESTAMP.H:667 - :ref:`View ` * - ``std::string isoformat(charsep = 'T', const std::string ×pec = "auto")`` - std::string - NP_TIMESTAMP.H:978 - :ref:`View ` * - ``int isoweekday()`` - int - NP_TIMESTAMP.H:1128 - :ref:`View ` * - ``int microsecond()`` - int - NP_TIMESTAMP.H:711 - :ref:`View ` * - ``int month()`` - int - NP_TIMESTAMP.H:681 - :ref:`View ` * - ``std::string month_name(const std::string &locale = "")`` - std::string - NP_TIMESTAMP.H:1053 - :ref:`View ` * - ``Timestamp now(const std::string &tz = "")`` - Timestamp - NP_TIMESTAMP.H:350 - :ref:`View ` * - ``int64_t parseFrequencyToNanos(const std::string &freq)`` - int64_t - NP_TIMESTAMP.H:133 - * - ``int quarter()`` - int - NP_TIMESTAMP.H:778 - :ref:`View ` * - ``Timestamp replace(intnew_year = -1, intnew_month = -1, intnew_day = -1, intnew_hour = -1, intnew_minute = -1, intnew_second = -1, intnew_microsecond = -1, intnew_nanosecond = -1, const std::string &new_tzinfo = "", intnew_fold = -1)`` - Timestamp - NP_TIMESTAMP.H:1255 - :ref:`View ` * - ``int second()`` - int - NP_TIMESTAMP.H:705 - :ref:`View ` * - ``std::string strftime(const std::string &format)`` - std::string - NP_TIMESTAMP.H:1031 - :ref:`View ` * - ``Timestamp strptime(const std::string &date_string, const std::string &format)`` - Timestamp - NP_TIMESTAMP.H:455 - :ref:`View ` * - ``double timestamp()`` - double - NP_TIMESTAMP.H:901 - :ref:`View ` * - ``std::string toString()`` - std::string - NP_TIMESTAMP.H:973 - :ref:`View ` * - ``Timestamp today(const std::string &tz = "")`` - Timestamp - NP_TIMESTAMP.H:358 - :ref:`View ` * - ``int toordinal()`` - int - NP_TIMESTAMP.H:955 - :ref:`View ` * - ``std::string tz()`` - std::string - NP_TIMESTAMP.H:841 - :ref:`View ` * - ``Timestamp tz_convert(const std::string &tz_str)`` - Timestamp - NP_TIMESTAMP.H:1340 - :ref:`View ` * - ``Timestamp tz_localize(const std::string &tz_str)`` - Timestamp - NP_TIMESTAMP.H:1315 - :ref:`View ` * - ``std::string tzinfo()`` - std::string - NP_TIMESTAMP.H:845 - :ref:`View ` * - ``std::string tzname()`` - std::string - NP_TIMESTAMP.H:847 - :ref:`View ` * - ``Timestamp utcfromtimestamp(doublets)`` - Timestamp - NP_TIMESTAMP.H:375 - :ref:`View ` * - ``Timestamp utcnow()`` - Timestamp - NP_TIMESTAMP.H:364 - :ref:`View ` * - ``timedelta64 utcoffset()`` - timedelta64 - NP_TIMESTAMP.H:1367 - :ref:`View ` * - ``int64_t value()`` - int64_t - NP_TIMESTAMP.H:723 - :ref:`View ` * - ``int week()`` - int - NP_TIMESTAMP.H:769 - :ref:`View ` * - ``int weekday()`` - int - NP_TIMESTAMP.H:1133 - :ref:`View ` * - ``int weekofyear()`` - int - NP_TIMESTAMP.H:775 - * - ``int year()`` - int - NP_TIMESTAMP.H:675 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-timestamp-timestamp-0: .. dropdown:: Timestamp (np_test_5_all.cpp:23235) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23225 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // Test untested Timestamp methods: constructor(int64_t), utcfromtimestamp, utctimetuple, timetz void np_test_timestamp_untested_methods() { std::cout << "========= timestamp: untested methods (int64_t ctor, utcfromtimestamp, utctimetuple, timetz) =="; auto pass = true; std::string fail_msg; // Test 1: Timestamp(int64_t value, tz) constructor - from nanoseconds since epoch // Verify round-trip: construct from nanos, convert back to value, should match // 1718454645000000000LL = 2024-06-15 12:30:45 UTC (verified with Python) int64_t nanos = 1718454645000000000LL; numpy::Timestamp ts_from_nanos(nanos, "UTC"); // Construct with UTC timezone // Verify the stored value matches input if (ts_from_nanos.value() != nanos) { pass = false; fail_msg = "Timestamp(int64_t) value() should return input nanos"; } // Verify we can extract correct UTC components .. _example-timestamp-timestamp-1: .. dropdown:: Timestamp (np_test_5_all.cpp:23235) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23225 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // Test untested Timestamp methods: constructor(int64_t), utcfromtimestamp, utctimetuple, timetz void np_test_timestamp_untested_methods() { std::cout << "========= timestamp: untested methods (int64_t ctor, utcfromtimestamp, utctimetuple, timetz) =="; auto pass = true; std::string fail_msg; // Test 1: Timestamp(int64_t value, tz) constructor - from nanoseconds since epoch // Verify round-trip: construct from nanos, convert back to value, should match // 1718454645000000000LL = 2024-06-15 12:30:45 UTC (verified with Python) int64_t nanos = 1718454645000000000LL; numpy::Timestamp ts_from_nanos(nanos, "UTC"); // Construct with UTC timezone // Verify the stored value matches input if (ts_from_nanos.value() != nanos) { pass = false; fail_msg = "Timestamp(int64_t) value() should return input nanos"; } // Verify we can extract correct UTC components .. _example-timestamp-timestamp-2: .. dropdown:: Timestamp (np_test_5_all.cpp:23235) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23225 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // Test untested Timestamp methods: constructor(int64_t), utcfromtimestamp, utctimetuple, timetz void np_test_timestamp_untested_methods() { std::cout << "========= timestamp: untested methods (int64_t ctor, utcfromtimestamp, utctimetuple, timetz) =="; auto pass = true; std::string fail_msg; // Test 1: Timestamp(int64_t value, tz) constructor - from nanoseconds since epoch // Verify round-trip: construct from nanos, convert back to value, should match // 1718454645000000000LL = 2024-06-15 12:30:45 UTC (verified with Python) int64_t nanos = 1718454645000000000LL; numpy::Timestamp ts_from_nanos(nanos, "UTC"); // Construct with UTC timezone // Verify the stored value matches input if (ts_from_nanos.value() != nanos) { pass = false; fail_msg = "Timestamp(int64_t) value() should return input nanos"; } // Verify we can extract correct UTC components .. _example-timestamp-timestamp-3: .. dropdown:: Timestamp (np_test_5_all.cpp:23235) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23225 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // Test untested Timestamp methods: constructor(int64_t), utcfromtimestamp, utctimetuple, timetz void np_test_timestamp_untested_methods() { std::cout << "========= timestamp: untested methods (int64_t ctor, utcfromtimestamp, utctimetuple, timetz) =="; auto pass = true; std::string fail_msg; // Test 1: Timestamp(int64_t value, tz) constructor - from nanoseconds since epoch // Verify round-trip: construct from nanos, convert back to value, should match // 1718454645000000000LL = 2024-06-15 12:30:45 UTC (verified with Python) int64_t nanos = 1718454645000000000LL; numpy::Timestamp ts_from_nanos(nanos, "UTC"); // Construct with UTC timezone // Verify the stored value matches input if (ts_from_nanos.value() != nanos) { pass = false; fail_msg = "Timestamp(int64_t) value() should return input nanos"; } // Verify we can extract correct UTC components .. _example-timestamp-timestamp-4: .. dropdown:: Timestamp (np_test_5_all.cpp:23235) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23225 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // Test untested Timestamp methods: constructor(int64_t), utcfromtimestamp, utctimetuple, timetz void np_test_timestamp_untested_methods() { std::cout << "========= timestamp: untested methods (int64_t ctor, utcfromtimestamp, utctimetuple, timetz) =="; auto pass = true; std::string fail_msg; // Test 1: Timestamp(int64_t value, tz) constructor - from nanoseconds since epoch // Verify round-trip: construct from nanos, convert back to value, should match // 1718454645000000000LL = 2024-06-15 12:30:45 UTC (verified with Python) int64_t nanos = 1718454645000000000LL; numpy::Timestamp ts_from_nanos(nanos, "UTC"); // Construct with UTC timezone // Verify the stored value matches input if (ts_from_nanos.value() != nanos) { pass = false; fail_msg = "Timestamp(int64_t) value() should return input nanos"; } // Verify we can extract correct UTC components .. _example-timestamp-max-5: .. dropdown:: max (np_test_1_all.cpp:7274) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7264 :emphasize-lines: 11 if (sizeof(uintp) == sizeof(void*)) { // std::cout << " -> uintp size matches pointer size"; } else { // std::cout << " ✗ uintp size doesn't match pointer size" << std::endl; } // Test range limits // std::cout << "Range Information:" << std::endl; // std::cout << " intp min: " << std::numeric_limits::min() << std::endl; // std::cout << " intp max: " << std::numeric_limits::max() << std::endl; // std::cout << " uintp max: " << std::numeric_limits::max() << std::endl; // std::cout << " longdouble digits: " << std::numeric_limits::digits << std::endl; std::cout << " -> tests passed" << std::endl; } void testComplexArithmeticExtendedTypes() { std::cout << "========= testComplexArithmeticExtendedTypes ======================="; clongdouble c1(3.0L, 4.0L); // 3 + 4i .. _example-timestamp-min-6: .. dropdown:: min (np_test_1_all.cpp:2350) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2340 :emphasize-lines: 11 if (i % 3 == 0) { large_array.setElementAt({i}, object_(static_cast(i))); } else if (i % 3 == 1) { large_array.setElementAt({i}, object_(static_cast(i) * 0.5)); } else { large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i))); } } // Verify pattern for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) { // Check first 100 object_ obj = large_array.getElementAt({i}); if (i % 3 == 0) { if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } else if (i % 3 == 1) { if (!(obj.is_type())) { std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; .. _example-timestamp-minute-7: .. dropdown:: minute (np_test_5_all.cpp:22277) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22267 :emphasize-lines: 11 // Default constructor (NaT) numpy::Timestamp nat; if (!nat.isNaT()) { pass = false; fail_msg = "Default constructor should create NaT"; } // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; } .. _example-timestamp-nanosecond-8: .. dropdown:: nanosecond (np_test_5_all.cpp:22284) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22274 :emphasize-lines: 11 // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; } // String constructor - ISO format numpy::Timestamp ts3("2024-06-15T14:30:45"); if (ts3.year() != 2024 || ts3.month() != 6 || ts3.day() != 15) { pass = false; fail_msg = "String constructor parse failed"; } .. _example-timestamp-ceil-9: .. dropdown:: ceil (np_test_2_all.cpp:5735) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5725 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] floor function works correctly\n"; // Test ceil auto ceil_result = ceil(values); if (!(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-timestamp-floor-10: .. dropdown:: floor (np_test_2_all.cpp:5721) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5711 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] round function works correctly\n"; // Test floor auto floor_result = floor(values); if (!(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-timestamp-round-11: .. dropdown:: round (np_test_1_all.cpp:23769) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23759 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(power_scalar_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(power_scalar_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Power functions have correct signatures\n"; // Test rounding functions with new decimals parameter auto round_result = round(array); // Default decimals=0 auto round_decimals_result = round(array, 2); // With decimals if (!(round_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(round_decimals_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_decimals_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-timestamp-normalize-12: .. dropdown:: normalize (np_test_5_all.cpp:22908) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22898 :emphasize-lines: 11 fail_msg = "ceil to hour failed"; } // round to hour (37 minutes -> round up) numpy::Timestamp rounded_h = ts.round("h"); if (rounded_h.hour() != 15) { pass = false; fail_msg = "round to hour should be 15 (37 > 30)"; } // normalize (floor to day) numpy::Timestamp normalized = ts.normalize(); if (normalized.hour() != 0 || normalized.minute() != 0 || normalized.second() != 0) { pass = false; fail_msg = "normalize should be midnight"; } // floor to day numpy::Timestamp floored_d = ts.floor("D"); if (floored_d.day() != 15 || floored_d.hour() != 0) { pass = false; .. _example-timestamp-to_datetime64-13: .. dropdown:: to_datetime64 (np_test_5_all.cpp:22746) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22736 :emphasize-lines: 11 void np_test_timestamp_conversions() { std::cout << "========= timestamp: conversion methods =========================="; auto pass = true; std::string fail_msg; numpy::Timestamp ts(2024, 6, 15, 12, 30, 45, 123456, 789); // to_datetime64 numpy::datetime64 dt = ts.to_datetime64(); if (dt.isNaT()) { pass = false; fail_msg = "to_datetime64 should not return NaT"; } // asm8 alias numpy::datetime64 asm8 = ts.asm8(); if (asm8.isNaT()) { pass = false; fail_msg = "asm8 should not return NaT"; } // to_numpy int64_t value = ts.to_numpy(); if (value == 0) { pass = false; fail_msg = "to_numpy should return non-zero"; } .. _example-timestamp-to_julian_date-14: .. dropdown:: to_julian_date (np_test_5_all.cpp:22778) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22768 :emphasize-lines: 11 if (y != 2024 || mo != 6) { pass = false; fail_msg = "timetuple values incorrect"; } // date/time tuples auto [dy, dm, dd] = ts.date(); if (dy != 2024) { pass = false; fail_msg = "date tuple year"; } auto [th, tmi, ts2, tus] = ts.time(); if (th != 12) { pass = false; fail_msg = "time tuple hour"; } // Julian date double jd = ts.to_julian_date(); if (jd < 2400000) { pass = false; fail_msg = "Julian date should be reasonable"; } // Ordinal int ord = ts.toordinal(); if (ord <= 0) { pass = false; fail_msg = "Ordinal should be positive"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_conversions() : " << fail_msg; throw std::runtime_error("np_test_timestamp_conversions failed: " + fail_msg); } .. _example-timestamp-to_numpy-15: .. dropdown:: to_numpy (np_test_5_all.cpp:21464) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21454 :emphasize-lines: 11 numpy::timedelta64 td64 = td.to_timedelta64(numpy::DateTimeUnit::Hour); if (td64.getValue() != 24) { std::cout << "[FAIL] np_test_timedelta_conversions: to_timedelta64(Hour) expected 24, got " << td64.getValue() << std::endl; errors++; } } // to_numpy { int64_t nanos = td.to_numpy(); if (nanos != numpy::Timedelta::NANOS_PER_DAY) { std::cout << "[FAIL] np_test_timedelta_conversions: to_numpy expected " << numpy::Timedelta::NANOS_PER_DAY << ", got " << nanos << std::endl; errors++; } } // to_chrono { std::chrono::nanoseconds dur = td.to_chrono(); .. _example-timestamp-to_pydatetime-16: .. dropdown:: to_pydatetime (np_test_5_all.cpp:22758) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22748 :emphasize-lines: 11 // asm8 alias numpy::datetime64 asm8 = ts.asm8(); if (asm8.isNaT()) { pass = false; fail_msg = "asm8 should not return NaT"; } // to_numpy int64_t value = ts.to_numpy(); if (value == 0) { pass = false; fail_msg = "to_numpy should return non-zero"; } // to_pydatetime std::tm tm = ts.to_pydatetime(); if (tm.tm_year != 124) { pass = false; fail_msg = "tm_year should be 124 (2024-1900)"; } if (tm.tm_mon != 5) { pass = false; fail_msg = "tm_mon should be 5 (June)"; } // timestamp double posix = ts.timestamp(); if (posix <= 0) { pass = false; fail_msg = "POSIX timestamp should be positive"; } // timetuple auto [y, mo, d, h, mi, s] = ts.timetuple(); if (y != 2024 || mo != 6) { pass = false; fail_msg = "timetuple values incorrect"; } .. _example-timestamp-combine-17: .. dropdown:: combine (np_test_5_all.cpp:22364) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22354 :emphasize-lines: 11 } // fromordinal numpy::Timestamp from_ord = numpy::Timestamp::fromordinal(738678); if (from_ord.isNaT()) { pass = false; fail_msg = "fromordinal should not return NaT"; } // combine numpy::Timestamp combined = numpy::Timestamp::combine({2024, 6, 15}, {12, 30, 45, 0}); if (combined.year() != 2024 || combined.hour() != 12) { pass = false; fail_msg = "combine() values incorrect"; } // Static values numpy::Timestamp min_ts = numpy::Timestamp::min(); numpy::Timestamp max_ts = numpy::Timestamp::max(); numpy::Timestamp nat_ts = numpy::Timestamp::NaT(); if (min_ts.isNaT() || max_ts.isNaT() || !nat_ts.isNaT()) { .. _example-timestamp-is_leap_year-18: .. dropdown:: is_leap_year (np_test_5_all.cpp:22668) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22658 :emphasize-lines: 11 // ============================================================================ void np_test_timestamp_booleans() { std::cout << "========= timestamp: boolean properties =========================="; auto pass = true; std::string fail_msg; // Leap year numpy::Timestamp leap(2024, 2, 29); if (!leap.is_leap_year()) { pass = false; fail_msg = "2024 is a leap year"; } numpy::Timestamp non_leap(2023, 2, 28); if (non_leap.is_leap_year()) { pass = false; fail_msg = "2023 is not a leap year"; } // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } .. _example-timestamp-is_month_end-19: .. dropdown:: is_month_end (np_test_5_all.cpp:22681) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22671 :emphasize-lines: 11 if (non_leap.is_leap_year()) { pass = false; fail_msg = "2023 is not a leap year"; } // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } numpy::Timestamp dec31(2024, 12, 31); if (!dec31.is_year_end()) { pass = false; fail_msg = "Dec 31 is year end"; } if (!dec31.is_month_end()) { pass = false; fail_msg = "Dec 31 is month end"; } if (!dec31.is_quarter_end()) { pass = false; fail_msg = "Dec 31 is quarter end"; } // Quarter boundaries numpy::Timestamp mar31(2024, 3, 31); if (!mar31.is_quarter_end()) { pass = false; fail_msg = "Mar 31 is quarter end"; } numpy::Timestamp apr1(2024, 4, 1); if (!apr1.is_quarter_start()) { pass = false; fail_msg = "Apr 1 is quarter start"; } if (!pass) { .. _example-timestamp-is_month_start-20: .. dropdown:: is_month_start (np_test_5_all.cpp:22676) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22666 :emphasize-lines: 11 // Leap year numpy::Timestamp leap(2024, 2, 29); if (!leap.is_leap_year()) { pass = false; fail_msg = "2024 is a leap year"; } numpy::Timestamp non_leap(2023, 2, 28); if (non_leap.is_leap_year()) { pass = false; fail_msg = "2023 is not a leap year"; } // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } numpy::Timestamp dec31(2024, 12, 31); if (!dec31.is_year_end()) { pass = false; fail_msg = "Dec 31 is year end"; } if (!dec31.is_month_end()) { pass = false; fail_msg = "Dec 31 is month end"; } if (!dec31.is_quarter_end()) { pass = false; fail_msg = "Dec 31 is quarter end"; } // Quarter boundaries numpy::Timestamp mar31(2024, 3, 31); if (!mar31.is_quarter_end()) { pass = false; fail_msg = "Mar 31 is quarter end"; } .. _example-timestamp-is_quarter_end-21: .. dropdown:: is_quarter_end (np_test_5_all.cpp:22682) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22672 :emphasize-lines: 11 // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } numpy::Timestamp dec31(2024, 12, 31); if (!dec31.is_year_end()) { pass = false; fail_msg = "Dec 31 is year end"; } if (!dec31.is_month_end()) { pass = false; fail_msg = "Dec 31 is month end"; } if (!dec31.is_quarter_end()) { pass = false; fail_msg = "Dec 31 is quarter end"; } // Quarter boundaries numpy::Timestamp mar31(2024, 3, 31); if (!mar31.is_quarter_end()) { pass = false; fail_msg = "Mar 31 is quarter end"; } numpy::Timestamp apr1(2024, 4, 1); if (!apr1.is_quarter_start()) { pass = false; fail_msg = "Apr 1 is quarter start"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_booleans() : " << fail_msg; .. _example-timestamp-is_quarter_start-22: .. dropdown:: is_quarter_start (np_test_5_all.cpp:22677) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22667 :emphasize-lines: 11 numpy::Timestamp leap(2024, 2, 29); if (!leap.is_leap_year()) { pass = false; fail_msg = "2024 is a leap year"; } numpy::Timestamp non_leap(2023, 2, 28); if (non_leap.is_leap_year()) { pass = false; fail_msg = "2023 is not a leap year"; } // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } numpy::Timestamp dec31(2024, 12, 31); if (!dec31.is_year_end()) { pass = false; fail_msg = "Dec 31 is year end"; } if (!dec31.is_month_end()) { pass = false; fail_msg = "Dec 31 is month end"; } if (!dec31.is_quarter_end()) { pass = false; fail_msg = "Dec 31 is quarter end"; } // Quarter boundaries numpy::Timestamp mar31(2024, 3, 31); if (!mar31.is_quarter_end()) { pass = false; fail_msg = "Mar 31 is quarter end"; } .. _example-timestamp-is_year_end-23: .. dropdown:: is_year_end (np_test_5_all.cpp:22680) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22670 :emphasize-lines: 11 numpy::Timestamp non_leap(2023, 2, 28); if (non_leap.is_leap_year()) { pass = false; fail_msg = "2023 is not a leap year"; } // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } numpy::Timestamp dec31(2024, 12, 31); if (!dec31.is_year_end()) { pass = false; fail_msg = "Dec 31 is year end"; } if (!dec31.is_month_end()) { pass = false; fail_msg = "Dec 31 is month end"; } if (!dec31.is_quarter_end()) { pass = false; fail_msg = "Dec 31 is quarter end"; } // Quarter boundaries numpy::Timestamp mar31(2024, 3, 31); if (!mar31.is_quarter_end()) { pass = false; fail_msg = "Mar 31 is quarter end"; } numpy::Timestamp apr1(2024, 4, 1); if (!apr1.is_quarter_start()) { pass = false; fail_msg = "Apr 1 is quarter start"; } .. _example-timestamp-is_year_start-24: .. dropdown:: is_year_start (np_test_5_all.cpp:22675) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22665 :emphasize-lines: 11 // Leap year numpy::Timestamp leap(2024, 2, 29); if (!leap.is_leap_year()) { pass = false; fail_msg = "2024 is a leap year"; } numpy::Timestamp non_leap(2023, 2, 28); if (non_leap.is_leap_year()) { pass = false; fail_msg = "2023 is not a leap year"; } // Year start/end numpy::Timestamp jan1(2024, 1, 1); if (!jan1.is_year_start()) { pass = false; fail_msg = "Jan 1 is year start"; } if (!jan1.is_month_start()) { pass = false; fail_msg = "Jan 1 is month start"; } if (!jan1.is_quarter_start()) { pass = false; fail_msg = "Jan 1 is quarter start"; } numpy::Timestamp dec31(2024, 12, 31); if (!dec31.is_year_end()) { pass = false; fail_msg = "Dec 31 is year end"; } if (!dec31.is_month_end()) { pass = false; fail_msg = "Dec 31 is month end"; } if (!dec31.is_quarter_end()) { pass = false; fail_msg = "Dec 31 is quarter end"; } // Quarter boundaries numpy::Timestamp mar31(2024, 3, 31); .. _example-timestamp-nat-25: .. dropdown:: NaT (np_test_1_all.cpp:7020) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7010 :emphasize-lines: 11 // std::cout << april_2024.getElementAt({i}).toString() << " "; } // std::cout << std::endl; std::cout << " -> tests passed" << std::endl; } void testNaTHandlingDateTime() { std::cout << "========= testNaTHandlingDateTime ======================="; datetime64 nat_dt = datetime64::NaT(); timedelta64 nat_td = timedelta64::NaT(); // std::cout << "NaT datetime: " << nat_dt.toString() << std::endl; // std::cout << "NaT timedelta: " << nat_td.toString() << std::endl; datetime64 valid_date("2024-01-01"); // NaT arithmetic datetime64 result1 = valid_date + nat_td; datetime64 result2 = nat_dt + timedelta64(1, DateTimeUnit::Day); .. _example-timestamp-as_unit-26: .. dropdown:: as_unit (np_test_5_all.cpp:22957) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22947 :emphasize-lines: 11 } // Replace multiple numpy::Timestamp multi = ts.replace(2025, 7, 20, 14); if (multi.year() != 2025 || multi.month() != 7 || multi.day() != 20 || multi.hour() != 14) { pass = false; fail_msg = "Replace multiple values failed"; } // as_unit numpy::Timestamp as_unit_ts = ts.as_unit("ns"); if (as_unit_ts.isNaT()) { pass = false; fail_msg = "as_unit should not return NaT"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_replace() : " << fail_msg; throw std::runtime_error("np_test_timestamp_replace failed: " + fail_msg); } .. _example-timestamp-asm8-27: .. dropdown:: asm8 (np_test_5_all.cpp:22750) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22740 :emphasize-lines: 11 auto pass = true; std::string fail_msg; numpy::Timestamp ts(2024, 6, 15, 12, 30, 45, 123456, 789); // to_datetime64 numpy::datetime64 dt = ts.to_datetime64(); if (dt.isNaT()) { pass = false; fail_msg = "to_datetime64 should not return NaT"; } // asm8 alias numpy::datetime64 asm8 = ts.asm8(); if (asm8.isNaT()) { pass = false; fail_msg = "asm8 should not return NaT"; } // to_numpy int64_t value = ts.to_numpy(); if (value == 0) { pass = false; fail_msg = "to_numpy should return non-zero"; } // to_pydatetime std::tm tm = ts.to_pydatetime(); if (tm.tm_year != 124) { pass = false; fail_msg = "tm_year should be 124 (2024-1900)"; } if (tm.tm_mon != 5) { pass = false; fail_msg = "tm_mon should be 5 (June)"; } .. _example-timestamp-astimezone-28: .. dropdown:: astimezone (np_test_5_all.cpp:22996) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22986 :emphasize-lines: 11 fail_msg = "tz_localize should set timezone"; } // tz_convert numpy::Timestamp converted = localized.tz_convert("Europe/London"); if (converted.tz() != "Europe/London") { pass = false; fail_msg = "tz_convert should change timezone"; } // astimezone (alias) numpy::Timestamp ast = localized.astimezone("UTC"); if (ast.tz() != "UTC") { pass = false; fail_msg = "astimezone should convert to UTC"; } // utcoffset numpy::timedelta64 offset = localized.utcoffset(); if (offset.isNaT()) { pass = false; .. _example-timestamp-ctime-29: .. dropdown:: ctime (np_test_5_all.cpp:22822) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22812 :emphasize-lines: 11 // isoformat with space separator std::string iso_space = ts.isoformat(' '); if (iso_space.find(" ") == std::string::npos) { pass = false; fail_msg = "isoformat with space separator"; } // strftime std::string fmt = ts.strftime("%Y/%m/%d"); if (fmt != "2024/06/15") { pass = false; fail_msg = "strftime format"; } // ctime std::string ct = ts.ctime(); if (ct.find("2024") == std::string::npos) { pass = false; fail_msg = "ctime should contain year"; } // day_name std::string dayname = ts.day_name(); if (dayname != "Saturday") { pass = false; fail_msg = "2024-06-15 is Saturday"; } // month_name std::string monthname = ts.month_name(); if (monthname != "June") { pass = false; fail_msg = "Month should be June"; } .. _example-timestamp-day-30: .. dropdown:: day (np_test_5_all.cpp:22276) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22266 :emphasize-lines: 11 // Default constructor (NaT) numpy::Timestamp nat; if (!nat.isNaT()) { pass = false; fail_msg = "Default constructor should create NaT"; } // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; .. _example-timestamp-day_name-31: .. dropdown:: day_name (np_test_5_all.cpp:22826) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22816 :emphasize-lines: 11 // strftime std::string fmt = ts.strftime("%Y/%m/%d"); if (fmt != "2024/06/15") { pass = false; fail_msg = "strftime format"; } // ctime std::string ct = ts.ctime(); if (ct.find("2024") == std::string::npos) { pass = false; fail_msg = "ctime should contain year"; } // day_name std::string dayname = ts.day_name(); if (dayname != "Saturday") { pass = false; fail_msg = "2024-06-15 is Saturday"; } // month_name std::string monthname = ts.month_name(); if (monthname != "June") { pass = false; fail_msg = "Month should be June"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_strings() : " << fail_msg; throw std::runtime_error("np_test_timestamp_strings failed: " + fail_msg); } .. _example-timestamp-day_of_week-32: .. dropdown:: day_of_week (np_test_5_all.cpp:22639) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22629 :emphasize-lines: 11 void np_test_timestamp_derived() { std::cout << "========= timestamp: derived properties =========================="; auto pass = true; std::string fail_msg; // 2024-06-15 is a Saturday numpy::Timestamp ts(2024, 6, 15); if (ts.dayofweek() != 5) { pass = false; fail_msg = "2024-06-15 should be Saturday (5)"; } if (ts.day_of_week() != 5) { pass = false; fail_msg = "day_of_week alias"; } if (ts.dayofyear() != 167) { pass = false; fail_msg = "2024-06-15 should be day 167"; } if (ts.day_of_year() != 167) { pass = false; fail_msg = "day_of_year alias"; } if (ts.quarter() != 2) { pass = false; fail_msg = "June is Q2"; } if (ts.days_in_month() != 30) { pass = false; fail_msg = "June has 30 days"; } int week = ts.week(); if (week < 1 || week > 53) { pass = false; fail_msg = "week should be 1-53"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_derived() : " << fail_msg; .. _example-timestamp-day_of_year-33: .. dropdown:: day_of_year (np_test_5_all.cpp:22641) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22631 :emphasize-lines: 11 auto pass = true; std::string fail_msg; // 2024-06-15 is a Saturday numpy::Timestamp ts(2024, 6, 15); if (ts.dayofweek() != 5) { pass = false; fail_msg = "2024-06-15 should be Saturday (5)"; } if (ts.day_of_week() != 5) { pass = false; fail_msg = "day_of_week alias"; } if (ts.dayofyear() != 167) { pass = false; fail_msg = "2024-06-15 should be day 167"; } if (ts.day_of_year() != 167) { pass = false; fail_msg = "day_of_year alias"; } if (ts.quarter() != 2) { pass = false; fail_msg = "June is Q2"; } if (ts.days_in_month() != 30) { pass = false; fail_msg = "June has 30 days"; } int week = ts.week(); if (week < 1 || week > 53) { pass = false; fail_msg = "week should be 1-53"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_derived() : " << fail_msg; throw std::runtime_error("np_test_timestamp_derived failed: " + fail_msg); } .. _example-timestamp-dayofweek-34: .. dropdown:: dayofweek (np_test_5_all.cpp:22638) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22628 :emphasize-lines: 11 void np_test_timestamp_derived() { std::cout << "========= timestamp: derived properties =========================="; auto pass = true; std::string fail_msg; // 2024-06-15 is a Saturday numpy::Timestamp ts(2024, 6, 15); if (ts.dayofweek() != 5) { pass = false; fail_msg = "2024-06-15 should be Saturday (5)"; } if (ts.day_of_week() != 5) { pass = false; fail_msg = "day_of_week alias"; } if (ts.dayofyear() != 167) { pass = false; fail_msg = "2024-06-15 should be day 167"; } if (ts.day_of_year() != 167) { pass = false; fail_msg = "day_of_year alias"; } if (ts.quarter() != 2) { pass = false; fail_msg = "June is Q2"; } if (ts.days_in_month() != 30) { pass = false; fail_msg = "June has 30 days"; } int week = ts.week(); if (week < 1 || week > 53) { pass = false; fail_msg = "week should be 1-53"; } if (!pass) { .. _example-timestamp-dayofyear-35: .. dropdown:: dayofyear (np_test_5_all.cpp:22640) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22630 :emphasize-lines: 11 std::cout << "========= timestamp: derived properties =========================="; auto pass = true; std::string fail_msg; // 2024-06-15 is a Saturday numpy::Timestamp ts(2024, 6, 15); if (ts.dayofweek() != 5) { pass = false; fail_msg = "2024-06-15 should be Saturday (5)"; } if (ts.day_of_week() != 5) { pass = false; fail_msg = "day_of_week alias"; } if (ts.dayofyear() != 167) { pass = false; fail_msg = "2024-06-15 should be day 167"; } if (ts.day_of_year() != 167) { pass = false; fail_msg = "day_of_year alias"; } if (ts.quarter() != 2) { pass = false; fail_msg = "June is Q2"; } if (ts.days_in_month() != 30) { pass = false; fail_msg = "June has 30 days"; } int week = ts.week(); if (week < 1 || week > 53) { pass = false; fail_msg = "week should be 1-53"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_derived() : " << fail_msg; throw std::runtime_error("np_test_timestamp_derived failed: " + fail_msg); .. _example-timestamp-days_in_month-36: .. dropdown:: days_in_month (np_test_5_all.cpp:22643) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22633 :emphasize-lines: 11 std::string fail_msg; // 2024-06-15 is a Saturday numpy::Timestamp ts(2024, 6, 15); if (ts.dayofweek() != 5) { pass = false; fail_msg = "2024-06-15 should be Saturday (5)"; } if (ts.day_of_week() != 5) { pass = false; fail_msg = "day_of_week alias"; } if (ts.dayofyear() != 167) { pass = false; fail_msg = "2024-06-15 should be day 167"; } if (ts.day_of_year() != 167) { pass = false; fail_msg = "day_of_year alias"; } if (ts.quarter() != 2) { pass = false; fail_msg = "June is Q2"; } if (ts.days_in_month() != 30) { pass = false; fail_msg = "June has 30 days"; } int week = ts.week(); if (week < 1 || week > 53) { pass = false; fail_msg = "week should be 1-53"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_derived() : " << fail_msg; throw std::runtime_error("np_test_timestamp_derived failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; .. _example-timestamp-dst-37: .. dropdown:: dst (np_test_5_all.cpp:23011) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23001 :emphasize-lines: 11 } // utcoffset numpy::timedelta64 offset = localized.utcoffset(); if (offset.isNaT()) { pass = false; fail_msg = "utcoffset should not be NaT"; } // dst numpy::timedelta64 dst_val = localized.dst(); if (dst_val.isNaT()) { pass = false; fail_msg = "dst should not be NaT"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_tz_methods() : " << fail_msg; throw std::runtime_error("np_test_timestamp_tz_methods failed: " + fail_msg); } .. _example-timestamp-fold-38: .. dropdown:: fold (np_test_5_all.cpp:22717) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22707 :emphasize-lines: 11 std::string fail_msg; // Naive timestamp numpy::Timestamp naive(2024, 6, 15); if (!naive.tz().empty()) { pass = false; fail_msg = "Naive should have empty tz"; } if (!naive.tzinfo().empty()) { pass = false; fail_msg = "Naive should have empty tzinfo"; } // UTC timestamp numpy::Timestamp utc(2024, 6, 15, 12, 0, 0, 0, 0, "UTC"); if (utc.tz() != "UTC") { pass = false; fail_msg = "Should have UTC timezone"; } if (utc.fold() != 0) { pass = false; fail_msg = "fold should be 0"; } // Named timezone numpy::Timestamp ny(2024, 6, 15, 12, 0, 0, 0, 0, "America/New_York"); if (ny.tz() != "America/New_York") { pass = false; fail_msg = "Should have NY timezone"; } std::string tzname = ny.tzname(); if (tzname != "EDT" && tzname != "EST") { pass = false; fail_msg = "Summer should be EDT or EST"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_timezone() : " << fail_msg; throw std::runtime_error("np_test_timestamp_timezone failed: " + fail_msg); .. _example-timestamp-fromisocalendar-39: .. dropdown:: fromisocalendar (np_test_5_all.cpp:22392) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22382 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // STATIC FACTORY METHODS - Plan 11 (fromisocalendar, fromisoformat, strptime) // ============================================================================ void np_test_timestamp_fromisocalendar() { std::cout << "========= Timestamp::fromisocalendar() ==============="; // Test 1: Basic ISO calendar to date conversion // 2024, week 1, Monday (Jan 1, 2024 was Monday of week 1) numpy::Timestamp ts1 = numpy::Timestamp::fromisocalendar(2024, 1, 1); if (ts1.year() != 2024 || ts1.month() != 1 || ts1.day() != 1) { throw std::runtime_error("fromisocalendar failed: 2024-W01-1 should be 2024-01-01"); } // Test 2: 2020, week 1, Wednesday (Jan 1, 2020 was Wednesday) numpy::Timestamp ts2 = numpy::Timestamp::fromisocalendar(2020, 1, 3); .. _example-timestamp-fromisoformat-40: .. dropdown:: fromisoformat (np_test_5_all.cpp:22449) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22439 :emphasize-lines: 11 threw = true; } if (!threw) { throw std::runtime_error("fromisocalendar failed: should throw for weekday 8"); } std::cout << " -> tests passed" << std::endl; } void np_test_timestamp_fromisoformat() { std::cout << "========= Timestamp::fromisoformat() =================="; // Test 1: Date only numpy::Timestamp ts1 = numpy::Timestamp::fromisoformat("2024-03-15"); if (ts1.year() != 2024 || ts1.month() != 3 || ts1.day() != 15) { throw std::runtime_error("fromisoformat failed: date only parsing"); } // Test 2: Date and time with T separator numpy::Timestamp ts2 = numpy::Timestamp::fromisoformat("2024-03-15T14:30:00"); if (ts2.year() != 2024 || ts2.month() != 3 || ts2.day() != 15 || .. _example-timestamp-fromordinal-41: .. dropdown:: fromordinal (np_test_5_all.cpp:22357) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22347 :emphasize-lines: 11 } // fromtimestamp - Unix epoch numpy::Timestamp epoch = numpy::Timestamp::fromtimestamp(0.0); if (epoch.year() != 1970 || epoch.month() != 1 || epoch.day() != 1) { pass = false; fail_msg = "fromtimestamp(0) should be epoch"; } // fromordinal numpy::Timestamp from_ord = numpy::Timestamp::fromordinal(738678); if (from_ord.isNaT()) { pass = false; fail_msg = "fromordinal should not return NaT"; } // combine numpy::Timestamp combined = numpy::Timestamp::combine({2024, 6, 15}, {12, 30, 45, 0}); if (combined.year() != 2024 || combined.hour() != 12) { pass = false; fail_msg = "combine() values incorrect"; .. _example-timestamp-fromtimestamp-42: .. dropdown:: fromtimestamp (np_test_5_all.cpp:22350) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22340 :emphasize-lines: 11 } // utcnow() numpy::Timestamp utcnow = numpy::Timestamp::utcnow(); if (utcnow.isNaT() || utcnow.tz() != "UTC") { pass = false; fail_msg = "utcnow() should have UTC timezone"; } // fromtimestamp - Unix epoch numpy::Timestamp epoch = numpy::Timestamp::fromtimestamp(0.0); if (epoch.year() != 1970 || epoch.month() != 1 || epoch.day() != 1) { pass = false; fail_msg = "fromtimestamp(0) should be epoch"; } // fromordinal numpy::Timestamp from_ord = numpy::Timestamp::fromordinal(738678); if (from_ord.isNaT()) { pass = false; fail_msg = "fromordinal should not return NaT"; .. _example-timestamp-hour-43: .. dropdown:: hour (np_test_5_all.cpp:22277) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22267 :emphasize-lines: 11 // Default constructor (NaT) numpy::Timestamp nat; if (!nat.isNaT()) { pass = false; fail_msg = "Default constructor should create NaT"; } // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; } .. _example-timestamp-isnat-44: .. dropdown:: isNaT (np_test_1_all.cpp:6821) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6811 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // DateTime Functions (merged from np_test_datetime.cpp) void testDatetime64CreationDateTime() { std::cout << "========= testDatetime64CreationDateTime ======================="; // Test default constructor datetime64 default_dt; if (!(default_dt.isNaT())) { std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl; // Test value constructor datetime64 epoch_day(0, DateTimeUnit::Day); // std::cout << "Epoch day: " << epoch_day.toString() << std::endl; .. _example-timestamp-isoformat-45: .. dropdown:: isoformat (np_test_5_all.cpp:21539) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21529 :emphasize-lines: 11 if (nat.toString() != "NaT") { std::cout << "[FAIL] np_test_timedelta_strings: toString(NaT) expected 'NaT', got '" << nat.toString() << "'" << std::endl; errors++; } } // isoformat { numpy::Timedelta td(1, 2, 30); std::string iso = td.isoformat(); if (iso.find("P1D") == std::string::npos) { std::cout << "[FAIL] np_test_timedelta_strings: isoformat expected 'P1D...', got '" << iso << "'" << std::endl; errors++; } } // components_str { numpy::Timedelta td(1, 2, 30, 45, 100, 200, 300); .. _example-timestamp-isoweekday-46: .. dropdown:: isoweekday (np_test_5_all.cpp:22859) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22849 :emphasize-lines: 11 std::string fail_msg; numpy::Timestamp ts(2024, 6, 15); // isocalendar auto [iso_year, iso_week, iso_day] = ts.isocalendar(); if (iso_year != 2024) { pass = false; fail_msg = "ISO year should be 2024"; } if (iso_week < 1 || iso_week > 53) { pass = false; fail_msg = "ISO week range"; } if (iso_day < 1 || iso_day > 7) { pass = false; fail_msg = "ISO day range 1-7"; } // isoweekday (1=Monday, 7=Sunday) int isowd = ts.isoweekday(); if (isowd != 6) { pass = false; fail_msg = "2024-06-15 is Saturday = 6"; } // weekday (0=Monday, 6=Sunday) int wd = ts.weekday(); if (wd != 5) { pass = false; fail_msg = "2024-06-15 is Saturday = 5"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_calendar() : " << fail_msg; throw std::runtime_error("np_test_timestamp_calendar failed: " + fail_msg); .. _example-timestamp-microsecond-47: .. dropdown:: microsecond (np_test_5_all.cpp:22284) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22274 :emphasize-lines: 11 // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; } // String constructor - ISO format numpy::Timestamp ts3("2024-06-15T14:30:45"); if (ts3.year() != 2024 || ts3.month() != 6 || ts3.day() != 15) { pass = false; fail_msg = "String constructor parse failed"; } .. _example-timestamp-month-48: .. dropdown:: month (np_test_5_all.cpp:22276) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22266 :emphasize-lines: 11 // Default constructor (NaT) numpy::Timestamp nat; if (!nat.isNaT()) { pass = false; fail_msg = "Default constructor should create NaT"; } // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; .. _example-timestamp-month_name-49: .. dropdown:: month_name (np_test_5_all.cpp:22830) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22820 :emphasize-lines: 11 // ctime std::string ct = ts.ctime(); if (ct.find("2024") == std::string::npos) { pass = false; fail_msg = "ctime should contain year"; } // day_name std::string dayname = ts.day_name(); if (dayname != "Saturday") { pass = false; fail_msg = "2024-06-15 is Saturday"; } // month_name std::string monthname = ts.month_name(); if (monthname != "June") { pass = false; fail_msg = "Month should be June"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_strings() : " << fail_msg; throw std::runtime_error("np_test_timestamp_strings failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; } .. _example-timestamp-now-50: .. dropdown:: now (np_test_1_all.cpp:3611) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3601 :emphasize-lines: 11 void test_broadcasting_performance() { std::cout << "========= test_broadcasting_performance ======================="; // std::cout << "Testing broadcasting performance..." << std::endl; // Create larger arrays for performance testing auto large_arr = NDArray::createOnes({100, 100}); auto broadcast_arr = NDArray::createOnes({1, 100}); // Time the operation (basic timing) auto start = std::chrono::high_resolution_clock::now(); auto result = large_arr.addArrays(broadcast_arr); auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(end - start); // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl; // Verify result shape if (!((result.getShape() == std::vector{100, 100}))) { std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector{100, 100}))"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-timestamp-quarter-51: .. dropdown:: quarter (np_test_5_all.cpp:22642) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22632 :emphasize-lines: 11 auto pass = true; std::string fail_msg; // 2024-06-15 is a Saturday numpy::Timestamp ts(2024, 6, 15); if (ts.dayofweek() != 5) { pass = false; fail_msg = "2024-06-15 should be Saturday (5)"; } if (ts.day_of_week() != 5) { pass = false; fail_msg = "day_of_week alias"; } if (ts.dayofyear() != 167) { pass = false; fail_msg = "2024-06-15 should be day 167"; } if (ts.day_of_year() != 167) { pass = false; fail_msg = "day_of_year alias"; } if (ts.quarter() != 2) { pass = false; fail_msg = "June is Q2"; } if (ts.days_in_month() != 30) { pass = false; fail_msg = "June has 30 days"; } int week = ts.week(); if (week < 1 || week > 53) { pass = false; fail_msg = "week should be 1-53"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_derived() : " << fail_msg; throw std::runtime_error("np_test_timestamp_derived failed: " + fail_msg); } .. _example-timestamp-replace-52: .. dropdown:: replace (np_test_1_all.cpp:6717) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6707 :emphasize-lines: 11 std::vector text = {"Hello world", "The world is big", "world peace"}; auto arr = array<64>(text); // std::cout << "Original text:" << std::endl; for (size_t i = 0; i < arr.size(); ++i) { // std::cout << "'" << arr[i] << "'"; } // Test replace auto replaced = replace(arr, "world", "universe"); // std::cout << "After replacing 'world' with 'universe':" << std::endl; for (size_t i = 0; i < replaced.size(); ++i) { // std::cout << "'" << replaced[i] << "'"; } // Test replace with count limit std::vector repeated_text = {"test test test", "more test cases", "test everything"}; auto repeated_arr = array<64>(repeated_text); auto limited_replace = replace(repeated_arr, "test", "example", 1); .. _example-timestamp-second-53: .. dropdown:: second (np_test_5_all.cpp:22277) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22267 :emphasize-lines: 11 // Default constructor (NaT) numpy::Timestamp nat; if (!nat.isNaT()) { pass = false; fail_msg = "Default constructor should create NaT"; } // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect"; } .. _example-timestamp-strftime-54: .. dropdown:: strftime (np_test_5_all.cpp:22818) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22808 :emphasize-lines: 11 // isoformat std::string iso = ts.isoformat(); if (iso.find("2024-06-15") == std::string::npos) { pass = false; fail_msg = "isoformat should contain date"; } // isoformat with space separator std::string iso_space = ts.isoformat(' '); if (iso_space.find(" ") == std::string::npos) { pass = false; fail_msg = "isoformat with space separator"; } // strftime std::string fmt = ts.strftime("%Y/%m/%d"); if (fmt != "2024/06/15") { pass = false; fail_msg = "strftime format"; } // ctime std::string ct = ts.ctime(); if (ct.find("2024") == std::string::npos) { pass = false; fail_msg = "ctime should contain year"; } // day_name std::string dayname = ts.day_name(); if (dayname != "Saturday") { pass = false; fail_msg = "2024-06-15 is Saturday"; } .. _example-timestamp-strptime-55: .. dropdown:: strptime (np_test_5_all.cpp:22519) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22509 :emphasize-lines: 11 threw = true; } if (!threw) { throw std::runtime_error("fromisoformat failed: should throw for too short string"); } std::cout << " -> tests passed" << std::endl; } void np_test_timestamp_strptime() { std::cout << "========= Timestamp::strptime() ======================="; // Test 1: Basic date parsing numpy::Timestamp ts1 = numpy::Timestamp::strptime("2024-03-15", "%Y-%m-%d"); if (ts1.year() != 2024 || ts1.month() != 3 || ts1.day() != 15) { throw std::runtime_error("strptime failed: basic date parsing"); } // Test 2: Date and time parsing numpy::Timestamp ts2 = numpy::Timestamp::strptime("2024-03-15 14:30:45", "%Y-%m-%d %H:%M:%S"); if (ts2.year() != 2024 || ts2.month() != 3 || ts2.day() != 15 || .. _example-timestamp-timestamp-56: .. dropdown:: timestamp (np_test_1_all.cpp:8336) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8326 :emphasize-lines: 11 uint file_size = 18446744073709551000ULL; // NumPy: np.uint longlong big_number = 9223372036854775807LL; // NumPy: np.longlong ulonglong huge_number = 18446744073709551615ULL; // NumPy: np.ulonglong // std::cout << " pixel_intensity (byte): " << static_cast(pixel_intensity) << std::endl; // std::cout << " rgb_value (ubyte): " << static_cast(rgb_value) << std::endl; // std::cout << " temperature (short_): " << temperature << std::endl; // std::cout << " elevation (ushort): " << elevation << std::endl; // std::cout << " count (intc): " << count << std::endl; // std::cout << " population (uintc): " << population << std::endl; // std::cout << " timestamp (int_): " << timestamp << std::endl; // std::cout << " file_size (uint): " << file_size << std::endl; // std::cout << " big_number (longlong): " << big_number << std::endl; // std::cout << " huge_number (ulonglong): " << huge_number << std::endl; // Test array creation with NumPy-style types auto numpy_style_array = NDArray::createZeros({3, 3}); // Like np.zeros((3,3), dtype=np.int_) // std::cout << "NumPy-style array creation:"; //numpy_style_array.printArray(); std::cout << " -> tests passed" << std::endl; .. _example-timestamp-tostring-57: .. dropdown:: toString (np_test_1_all.cpp:6826) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6816 :emphasize-lines: 11 void testDatetime64CreationDateTime() { std::cout << "========= testDatetime64CreationDateTime ======================="; // Test default constructor datetime64 default_dt; if (!(default_dt.isNaT())) { std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl; // Test value constructor datetime64 epoch_day(0, DateTimeUnit::Day); // std::cout << "Epoch day: " << epoch_day.toString() << std::endl; // Test string constructor datetime64 date_from_string("2024-01-15"); // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl; datetime64 datetime_from_string("2024-01-15T10:30:45"); .. _example-timestamp-today-58: .. dropdown:: today (np_test_1_all.cpp:6840) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6830 :emphasize-lines: 11 // std::cout << "Epoch day: " << epoch_day.toString() << std::endl; // Test string constructor datetime64 date_from_string("2024-01-15"); // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl; datetime64 datetime_from_string("2024-01-15T10:30:45"); // std::cout << "DateTime from string '2024-01-15T10:30:45': " << datetime_from_string.toString() << std::endl; // Test static methods datetime64 today = datetime64::today(); // std::cout << "Today: " << today.toString() << std::endl; datetime64 now = datetime64::now(); // std::cout << "Now: " << now.toString() << std::endl; std::cout << " -> tests passed" << std::endl; } void testTimedelta64CreationDateTime() { std::cout << "========= testTimedelta64CreationDateTime ======================="; .. _example-timestamp-toordinal-59: .. dropdown:: toordinal (np_test_5_all.cpp:22782) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22772 :emphasize-lines: 11 if (dy != 2024) { pass = false; fail_msg = "date tuple year"; } auto [th, tmi, ts2, tus] = ts.time(); if (th != 12) { pass = false; fail_msg = "time tuple hour"; } // Julian date double jd = ts.to_julian_date(); if (jd < 2400000) { pass = false; fail_msg = "Julian date should be reasonable"; } // Ordinal int ord = ts.toordinal(); if (ord <= 0) { pass = false; fail_msg = "Ordinal should be positive"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_conversions() : " << fail_msg; throw std::runtime_error("np_test_timestamp_conversions failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; } .. _example-timestamp-tz-60: .. dropdown:: tz (np_test_5_all.cpp:22305) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22295 :emphasize-lines: 11 // String with fractional seconds numpy::Timestamp ts4("2024-01-01T12:00:00.123456789"); if (ts4.microsecond() != 123456 || ts4.nanosecond() != 789) { pass = false; fail_msg = "Fractional seconds parse failed"; } // String with Z timezone numpy::Timestamp ts5("2024-06-15T12:00:00Z"); if (ts5.tz() != "UTC") { pass = false; fail_msg = "Z timezone should parse as UTC"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_constructors() : " << fail_msg; throw std::runtime_error("np_test_timestamp_constructors failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; .. _example-timestamp-tz_convert-61: .. dropdown:: tz_convert (np_test_5_all.cpp:22990) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22980 :emphasize-lines: 11 // tz_localize numpy::Timestamp naive(2024, 6, 15, 12, 0, 0); numpy::Timestamp localized = naive.tz_localize("America/New_York"); if (localized.tz() != "America/New_York") { pass = false; fail_msg = "tz_localize should set timezone"; } // tz_convert numpy::Timestamp converted = localized.tz_convert("Europe/London"); if (converted.tz() != "Europe/London") { pass = false; fail_msg = "tz_convert should change timezone"; } // astimezone (alias) numpy::Timestamp ast = localized.astimezone("UTC"); if (ast.tz() != "UTC") { pass = false; fail_msg = "astimezone should convert to UTC"; .. _example-timestamp-tz_localize-62: .. dropdown:: tz_localize (np_test_5_all.cpp:22983) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22973 :emphasize-lines: 11 // ============================================================================ void np_test_timestamp_tz_methods() { std::cout << "========= timestamp: timezone methods ============================"; auto pass = true; std::string fail_msg; // tz_localize numpy::Timestamp naive(2024, 6, 15, 12, 0, 0); numpy::Timestamp localized = naive.tz_localize("America/New_York"); if (localized.tz() != "America/New_York") { pass = false; fail_msg = "tz_localize should set timezone"; } // tz_convert numpy::Timestamp converted = localized.tz_convert("Europe/London"); if (converted.tz() != "Europe/London") { pass = false; fail_msg = "tz_convert should change timezone"; .. _example-timestamp-tzinfo-63: .. dropdown:: tzinfo (np_test_5_all.cpp:22712) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22702 :emphasize-lines: 11 void np_test_timestamp_timezone() { std::cout << "========= timestamp: timezone properties ========================="; auto pass = true; std::string fail_msg; // Naive timestamp numpy::Timestamp naive(2024, 6, 15); if (!naive.tz().empty()) { pass = false; fail_msg = "Naive should have empty tz"; } if (!naive.tzinfo().empty()) { pass = false; fail_msg = "Naive should have empty tzinfo"; } // UTC timestamp numpy::Timestamp utc(2024, 6, 15, 12, 0, 0, 0, 0, "UTC"); if (utc.tz() != "UTC") { pass = false; fail_msg = "Should have UTC timezone"; } if (utc.fold() != 0) { pass = false; fail_msg = "fold should be 0"; } // Named timezone numpy::Timestamp ny(2024, 6, 15, 12, 0, 0, 0, 0, "America/New_York"); if (ny.tz() != "America/New_York") { pass = false; fail_msg = "Should have NY timezone"; } std::string tzname = ny.tzname(); .. _example-timestamp-tzname-64: .. dropdown:: tzname (np_test_5_all.cpp:22722) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22712 :emphasize-lines: 11 if (!naive.tzinfo().empty()) { pass = false; fail_msg = "Naive should have empty tzinfo"; } // UTC timestamp numpy::Timestamp utc(2024, 6, 15, 12, 0, 0, 0, 0, "UTC"); if (utc.tz() != "UTC") { pass = false; fail_msg = "Should have UTC timezone"; } if (utc.fold() != 0) { pass = false; fail_msg = "fold should be 0"; } // Named timezone numpy::Timestamp ny(2024, 6, 15, 12, 0, 0, 0, 0, "America/New_York"); if (ny.tz() != "America/New_York") { pass = false; fail_msg = "Should have NY timezone"; } std::string tzname = ny.tzname(); if (tzname != "EDT" && tzname != "EST") { pass = false; fail_msg = "Summer should be EDT or EST"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_timezone() : " << fail_msg; throw std::runtime_error("np_test_timestamp_timezone failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; } .. _example-timestamp-utcfromtimestamp-65: .. dropdown:: utcfromtimestamp (np_test_5_all.cpp:23259) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23249 :emphasize-lines: 11 } if (pass && ts_from_nanos.month() != 6) { pass = false; fail_msg = "Timestamp(int64_t) month should be 6, got " + std::to_string(ts_from_nanos.month()); } if (pass && ts_from_nanos.day() != 15) { pass = false; fail_msg = "Timestamp(int64_t) day should be 15, got " + std::to_string(ts_from_nanos.day()); } // Test 2: utcfromtimestamp(double ts) - creates UTC timestamp from POSIX timestamp // 1718454645.0 = 2024-06-15 12:30:45 UTC (verified with Python) double posix_ts = 1718454645.0; numpy::Timestamp utc_ts = numpy::Timestamp::utcfromtimestamp(posix_ts); if (pass && utc_ts.year() != 2024) { pass = false; fail_msg = "utcfromtimestamp year mismatch, got " + std::to_string(utc_ts.year()); } if (pass && utc_ts.month() != 6) { pass = false; fail_msg = "utcfromtimestamp month mismatch, got " + std::to_string(utc_ts.month()); } if (pass && utc_ts.day() != 15) { pass = false; fail_msg = "utcfromtimestamp day mismatch, got " + std::to_string(utc_ts.day()); } if (pass && utc_ts.hour() != 12) { pass = false; fail_msg = "utcfromtimestamp hour mismatch, got " + std::to_string(utc_ts.hour()); } if (pass && utc_ts.minute() != 30) { pass = false; fail_msg = "utcfromtimestamp minute mismatch, got " + std::to_string(utc_ts.minute()); } if (pass && utc_ts.second() != 45) { pass = false; fail_msg = "utcfromtimestamp second mismatch, got " + std::to_string(utc_ts.second()); } if (pass && utc_ts.tz() != "UTC") { pass = false; fail_msg = "utcfromtimestamp should have UTC timezone, got '" + utc_ts.tz() + "'"; } .. _example-timestamp-utcnow-66: .. dropdown:: utcnow (np_test_5_all.cpp:22342) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22332 :emphasize-lines: 11 fail_msg = "now() should not return NaT"; } // today() numpy::Timestamp today = numpy::Timestamp::today(); if (today.hour() != 0 || today.minute() != 0 || today.second() != 0) { pass = false; fail_msg = "today() should be at midnight"; } // utcnow() numpy::Timestamp utcnow = numpy::Timestamp::utcnow(); if (utcnow.isNaT() || utcnow.tz() != "UTC") { pass = false; fail_msg = "utcnow() should have UTC timezone"; } // fromtimestamp - Unix epoch numpy::Timestamp epoch = numpy::Timestamp::fromtimestamp(0.0); if (epoch.year() != 1970 || epoch.month() != 1 || epoch.day() != 1) { pass = false; .. _example-timestamp-utcoffset-67: .. dropdown:: utcoffset (np_test_5_all.cpp:23004) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22994 :emphasize-lines: 11 } // astimezone (alias) numpy::Timestamp ast = localized.astimezone("UTC"); if (ast.tz() != "UTC") { pass = false; fail_msg = "astimezone should convert to UTC"; } // utcoffset numpy::timedelta64 offset = localized.utcoffset(); if (offset.isNaT()) { pass = false; fail_msg = "utcoffset should not be NaT"; } // dst numpy::timedelta64 dst_val = localized.dst(); if (dst_val.isNaT()) { pass = false; fail_msg = "dst should not be NaT"; .. _example-timestamp-value-68: .. dropdown:: value (np_test_1_all.cpp:7786) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7776 :emphasize-lines: 11 } void testFloat16PrecisionFloat16() { std::cout << "========= testFloat16PrecisionFloat16 ======================="; // Test precision limits float16 small_val(0.0001f); float16 large_val(30000.0f); float16 very_small(1e-8f); // This might underflow to zero // std::cout << "Small value (0.0001): " << small_val << std::endl; // std::cout << "Large value (30000): " << large_val << std::endl; // std::cout << "Very small value (1e-8): " << very_small << std::endl; // Test conversion accuracy float original = 1.234f; float16 converted(original); float back_converted = static_cast(converted); // std::cout << "Original float: " << std::setprecision(10) << original << std::endl; // std::cout << "Float16 version: " << converted << std::endl; .. _example-timestamp-week-69: .. dropdown:: week (np_test_1_all.cpp:27347) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27337 :emphasize-lines: 11 std::cout << "========= busday_offset: forward offset ======================="; // Start on Monday, offset by 5 business days -> should be next Monday NDArray dates({ 1 }); NDArray offsets({ 1 }); dates.setElementAt({ 0 }, datetime64(19723, DateTimeUnit::Day)); // 2024-01-01 (Monday) offsets.setElementAt({ 0 }, int64_t(5)); auto result = busday_offset(dates, offsets); // Expected: Monday + 5 business days = Monday next week (skipping weekend) datetime64 expected(19730, DateTimeUnit::Day); // 2024-01-08 (Monday) if (result.getElementAt({ 0 }) != expected) { std::cout << " [FAIL] : in test_busday_offset_forward() : incorrect offset result"; throw std::runtime_error("busday_offset forward test failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-timestamp-weekday-70: .. dropdown:: weekday (np_test_5_all.cpp:22863) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22853 :emphasize-lines: 11 // isocalendar auto [iso_year, iso_week, iso_day] = ts.isocalendar(); if (iso_year != 2024) { pass = false; fail_msg = "ISO year should be 2024"; } if (iso_week < 1 || iso_week > 53) { pass = false; fail_msg = "ISO week range"; } if (iso_day < 1 || iso_day > 7) { pass = false; fail_msg = "ISO day range 1-7"; } // isoweekday (1=Monday, 7=Sunday) int isowd = ts.isoweekday(); if (isowd != 6) { pass = false; fail_msg = "2024-06-15 is Saturday = 6"; } // weekday (0=Monday, 6=Sunday) int wd = ts.weekday(); if (wd != 5) { pass = false; fail_msg = "2024-06-15 is Saturday = 5"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_calendar() : " << fail_msg; throw std::runtime_error("np_test_timestamp_calendar failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; } .. _example-timestamp-year-71: .. dropdown:: year (np_test_5_all.cpp:22276) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22266 :emphasize-lines: 11 // Default constructor (NaT) numpy::Timestamp nat; if (!nat.isNaT()) { pass = false; fail_msg = "Default constructor should create NaT"; } // Component constructor numpy::Timestamp ts1(2024, 6, 15, 14, 30, 45); if (ts1.year() != 2024 || ts1.month() != 6 || ts1.day() != 15 || ts1.hour() != 14 || ts1.minute() != 30 || ts1.second() != 45) { pass = false; fail_msg = "Component constructor values incorrect"; } // With microseconds and nanoseconds numpy::Timestamp ts2(2024, 1, 1, 12, 0, 0, 123456, 789); if (ts2.microsecond() != 123456 || ts2.nanosecond() != 789) { pass = false; fail_msg = "Microsecond/nanosecond values incorrect";