datetime64#

class numpy::datetime64#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use datetime64
datetime64 obj;
// ... operations ...

Constructors#

Signature

Location

Example

datetime64(int64_tvalue, DateTimeUnitunit)

NP_DATETIME64.H:42

View

datetime64(const std::string &iso_string)

NP_DATETIME64.H:43

View

Operators#

Signature

Return Type

Location

Example

timedelta64 operator-(const datetime64 &other)

timedelta64

NP_DATETIME64.H:48

datetime64 operator+(const timedelta64 &delta)

datetime64

NP_DATETIME64.H:49

datetime64 operator-(const timedelta64 &delta)

datetime64

NP_DATETIME64.H:50

bool operator==(const datetime64 &other)

bool

NP_DATETIME64.H:52

bool operator!=(const datetime64 &other)

bool

NP_DATETIME64.H:53

bool operator<(const datetime64 &other)

bool

NP_DATETIME64.H:54

bool operator<=(const datetime64 &other)

bool

NP_DATETIME64.H:55

bool operator>(const datetime64 &other)

bool

NP_DATETIME64.H:56

bool operator>=(const datetime64 &other)

bool

NP_DATETIME64.H:57

Indexing / Selection#

Signature

Return Type

Location

Example

DateTimeUnit getUnit()

DateTimeUnit

NP_DATETIME64.H:46

View

int64_t getValue()

int64_t

NP_DATETIME64.H:45

View

Other Methods#

Signature

Return Type

Location

Example

datetime64 NaT(DateTimeUnitunit = DateTimeUnit::Second)

datetime64

NP_DATETIME64.H:66

View

datetime64 convertTo(DateTimeUnitnew_unit)

datetime64

NP_DATETIME64.H:68

View

int daysInMonth(intyear, intmonth)

int

NP_DATETIME64.H:72

View

datetime64 fromEpochSeconds(int64_tseconds, DateTimeUnitunit)

datetime64

NP_DATETIME64.H:39

View

bool isLeapYear(intyear)

bool

NP_DATETIME64.H:71

View

bool isNaT()

bool

NP_DATETIME64.H:65

View

datetime64 now(DateTimeUnitunit = DateTimeUnit::Second)

datetime64

NP_DATETIME64.H:62

View

int64_t parseISO8601(const std::string &iso_string, DateTimeUnit &unit)

int64_t

NP_DATETIME64.H:70

int64_t toEpochSeconds()

int64_t

NP_DATETIME64.H:38

View

std::string toString()

std::string

NP_DATETIME64.H:59

View

std::tm toTm()

std::tm

NP_DATETIME64.H:60

datetime64 today()

datetime64

NP_DATETIME64.H:63

View

Code Examples#

The following examples are extracted from the test suite.

datetime64 (np_test_1_all.cpp:6990)
6980}
6981
6982void testBusinessDateRangeDateTime() {
6983    std::cout << "========= testBusinessDateRangeDateTime =======================";
6984
6985    datetime64 start("2024-03-11");  // Monday
6986    datetime64 end("2024-03-22");    // Friday (2 weeks later)
6987
6988    // Create holidays set
6989    std::set<datetime64> holidays;
6990    holidays.insert(datetime64("2024-03-15"));  // Friday holiday
6991
6992    auto business_dates = createBusinessDateRange(start, end, holidays);
6993    // std::cout << "Business dates from " << start.toString()
6994              // << " to " << end.toString() << " (excluding 2024-03-15):" << std::endl;
6995    //business_dates.printArray();
6996
6997        std::cout << " -> tests passed" << std::endl;
6998}
6999
7000void testMonthRangeDateTime() {
datetime64 (np_test_1_all.cpp:6990)
6980}
6981
6982void testBusinessDateRangeDateTime() {
6983    std::cout << "========= testBusinessDateRangeDateTime =======================";
6984
6985    datetime64 start("2024-03-11");  // Monday
6986    datetime64 end("2024-03-22");    // Friday (2 weeks later)
6987
6988    // Create holidays set
6989    std::set<datetime64> holidays;
6990    holidays.insert(datetime64("2024-03-15"));  // Friday holiday
6991
6992    auto business_dates = createBusinessDateRange(start, end, holidays);
6993    // std::cout << "Business dates from " << start.toString()
6994              // << " to " << end.toString() << " (excluding 2024-03-15):" << std::endl;
6995    //business_dates.printArray();
6996
6997        std::cout << " -> tests passed" << std::endl;
6998}
6999
7000void testMonthRangeDateTime() {
getUnit (np_test_1_all.cpp:28800)
28790      std::cout << " -> tests passed" << std::endl;
28791    }
28792
28793    void np_test_datetime64_accessors() {
28794      std::cout << "========= datetime64 accessors: getValue/getUnit =======================";
28795
28796      auto dt = numpy::datetime64("2024-01-15");
28797
28798      // Test getValue and getUnit
28799      int64_t value = dt.getValue();
28800      numpy::DateTimeUnit unit = dt.getUnit();
28801
28802      bool passed = (value != 0);
28803
28804      if (!passed) {
28805        std::cout << "  [FAIL] : in np_test_datetime64_accessors() : Accessor methods failed";
28806        throw std::runtime_error("np_test_datetime64_accessors failed");
28807      }
28808
28809      std::cout << " -> tests passed" << std::endl;
28810    }
getValue (np_test_1_all.cpp:28799)
28789      std::cout << " -> tests passed" << std::endl;
28790    }
28791
28792    void np_test_datetime64_accessors() {
28793      std::cout << "========= datetime64 accessors: getValue/getUnit =======================";
28794
28795      auto dt = numpy::datetime64("2024-01-15");
28796
28797      // Test getValue and getUnit
28798      int64_t value = dt.getValue();
28799      numpy::DateTimeUnit unit = dt.getUnit();
28800
28801      bool passed = (value != 0);
28802
28803      if (!passed) {
28804        std::cout << "  [FAIL] : in np_test_datetime64_accessors() : Accessor methods failed";
28805        throw std::runtime_error("np_test_datetime64_accessors failed");
28806      }
28807
28808      std::cout << " -> tests passed" << std::endl;
NaT (np_test_1_all.cpp:7020)
7010        // std::cout << april_2024.getElementAt({i}).toString() << " ";
7011    }
7012    // std::cout << std::endl;
7013
7014        std::cout << " -> tests passed" << std::endl;
7015}
7016
7017void testNaTHandlingDateTime() {
7018    std::cout << "========= testNaTHandlingDateTime =======================";
7019
7020    datetime64 nat_dt = datetime64::NaT();
7021    timedelta64 nat_td = timedelta64::NaT();
7022
7023    // std::cout << "NaT datetime: " << nat_dt.toString() << std::endl;
7024    // std::cout << "NaT timedelta: " << nat_td.toString() << std::endl;
7025
7026    datetime64 valid_date("2024-01-01");
7027
7028    // NaT arithmetic
7029    datetime64 result1 = valid_date + nat_td;
7030    datetime64 result2 = nat_dt + timedelta64(1, DateTimeUnit::Day);
convertTo (np_test_1_all.cpp:7051)
7041        std::cout << " -> tests passed" << std::endl;
7042}
7043
7044void testConversionsDateTime() {
7045    std::cout << "========= testConversionsDateTime =======================";
7046
7047    datetime64 dt_seconds("2024-01-01T12:30:45");
7048    // std::cout << "Original (seconds): " << dt_seconds.toString() << std::endl;
7049
7050    datetime64 dt_days = dt_seconds.convertTo(DateTimeUnit::Day);
7051    // std::cout << "Converted to days: " << dt_days.toString() << std::endl;
7052
7053    datetime64 dt_hours = dt_seconds.convertTo(DateTimeUnit::Hour);
7054    // std::cout << "Converted to hours: " << dt_hours.toString() << std::endl;
7055
7056    timedelta64 td_hours(24, DateTimeUnit::Hour);
7057    // std::cout << "24 hours: " << td_hours.toString() << std::endl;
7058
7059    timedelta64 td_days = td_hours.convertTo(DateTimeUnit::Day);
7060    // std::cout << "Converted to days: " << td_days.toString() << std::endl;
daysInMonth (np_test_1_all.cpp:6977)
6967    // std::cout << "Business days from " << start_bday.toString()
6968              // << " to " << end_bday.toString() << ": " << business_days << std::endl;
6969
6970    datetime64 offset_date = datetime_utils::businessDayOffset(start_bday, 3);
6971    // std::cout << "3 business days after " << start_bday.toString()
6972              // << ": " << offset_date.toString() << std::endl;
6973
6974    // Test leap year
6975    // std::cout << "2024 is leap year: " << datetime_utils::isLeapYear(2024) << std::endl;
6976    // std::cout << "2023 is leap year: " << datetime_utils::isLeapYear(2023) << std::endl;
6977    // std::cout << "Days in February 2024: " << datetime_utils::daysInMonth(2024, 2) << std::endl;
6978
6979        std::cout << " -> tests passed" << std::endl;
6980}
6981
6982void testBusinessDateRangeDateTime() {
6983    std::cout << "========= testBusinessDateRangeDateTime =======================";
6984
6985    datetime64 start("2024-03-11");  // Monday
6986    datetime64 end("2024-03-22");    // Friday (2 weeks later)
fromEpochSeconds (np_test_1_all.cpp:28776)
28766      std::cout << " -> tests passed" << std::endl;
28767    }
28768
28769    // ============================================================================
28770    // DATETIME64 METHODS TESTS
28771    // ============================================================================
28772
28773    void np_test_datetime64_epoch_conversion() {
28774      std::cout << "========= datetime64 epoch conversion: fromEpochSeconds/toEpochSeconds =======================";
28775
28776      // Test fromEpochSeconds (needs two parameters: seconds and unit)
28777      int64_t epoch_seconds = 1704067200; // Jan 1, 2024 00:00:00 UTC
28778      auto dt = numpy::datetime64::fromEpochSeconds(epoch_seconds, numpy::DateTimeUnit::Second);
28779
28780      // Test toEpochSeconds
28781      int64_t converted = dt.toEpochSeconds();
28782
28783      bool passed = (converted == epoch_seconds);
28784
28785      if (!passed) {
28786        std::cout << "  [FAIL] : in np_test_datetime64_epoch_conversion() : Epoch conversion failed";
isLeapYear (np_test_1_all.cpp:6975)
6965    int64_t business_days = datetime_utils::businessDayCount(start_bday, end_bday);
6966    // std::cout << "Business days from " << start_bday.toString()
6967              // << " to " << end_bday.toString() << ": " << business_days << std::endl;
6968
6969    datetime64 offset_date = datetime_utils::businessDayOffset(start_bday, 3);
6970    // std::cout << "3 business days after " << start_bday.toString()
6971              // << ": " << offset_date.toString() << std::endl;
6972
6973    // Test leap year
6974    // std::cout << "2024 is leap year: " << datetime_utils::isLeapYear(2024) << std::endl;
6975    // std::cout << "2023 is leap year: " << datetime_utils::isLeapYear(2023) << std::endl;
6976    // std::cout << "Days in February 2024: " << datetime_utils::daysInMonth(2024, 2) << std::endl;
6977
6978        std::cout << " -> tests passed" << std::endl;
6979}
6980
6981void testBusinessDateRangeDateTime() {
6982    std::cout << "========= testBusinessDateRangeDateTime =======================";
6983
6984    datetime64 start("2024-03-11");  // Monday
isNaT (np_test_1_all.cpp:6821)
6811    std::cout << " -> tests passed" << std::endl;
6812}
6813
6814// DateTime Functions (merged from np_test_datetime.cpp)
6815void testDatetime64CreationDateTime() {
6816    std::cout << "========= testDatetime64CreationDateTime =======================";
6817
6818    // Test default constructor
6819    datetime64 default_dt;
6820    if (!(default_dt.isNaT())) {
6821        std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6822        std::cout << std::string("[FAIL] ") + description << std::endl;
6823        throw std::runtime_error(description);
6824    }
6825    // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6826
6827    // Test value constructor
6828    datetime64 epoch_day(0, DateTimeUnit::Day);
6829    // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
now (np_test_1_all.cpp:3611)
3601void test_broadcasting_performance() {
3602    std::cout << "========= test_broadcasting_performance =======================";
3603    // std::cout << "Testing broadcasting performance..." << std::endl;
3604
3605    // Create larger arrays for performance testing
3606    auto large_arr = NDArray<double>::createOnes({100, 100});
3607    auto broadcast_arr = NDArray<double>::createOnes({1, 100});
3608
3609    // Time the operation (basic timing)
3610    auto start = std::chrono::high_resolution_clock::now();
3611    auto result = large_arr.addArrays(broadcast_arr);
3612    auto end = std::chrono::high_resolution_clock::now();
3613
3614    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
3615    // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl;
3616
3617    // Verify result shape
3618    if (!((result.getShape() == std::vector<size_t>{100, 100}))) {
3619        std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector<size_t>{100, 100}))";
3620        std::cout << std::string("[FAIL] ") + description << std::endl;
toEpochSeconds (np_test_1_all.cpp:28781)
28771    // ============================================================================
28772
28773    void np_test_datetime64_epoch_conversion() {
28774      std::cout << "========= datetime64 epoch conversion: fromEpochSeconds/toEpochSeconds =======================";
28775
28776      // Test fromEpochSeconds (needs two parameters: seconds and unit)
28777      int64_t epoch_seconds = 1704067200; // Jan 1, 2024 00:00:00 UTC
28778      auto dt = numpy::datetime64::fromEpochSeconds(epoch_seconds, numpy::DateTimeUnit::Second);
28779
28780      // Test toEpochSeconds
28781      int64_t converted = dt.toEpochSeconds();
28782
28783      bool passed = (converted == epoch_seconds);
28784
28785      if (!passed) {
28786        std::cout << "  [FAIL] : in np_test_datetime64_epoch_conversion() : Epoch conversion failed";
28787        throw std::runtime_error("np_test_datetime64_epoch_conversion failed");
28788      }
28789
28790      std::cout << " -> tests passed" << std::endl;
28791    }
toString (np_test_1_all.cpp:6826)
6816void testDatetime64CreationDateTime() {
6817    std::cout << "========= testDatetime64CreationDateTime =======================";
6818
6819    // Test default constructor
6820    datetime64 default_dt;
6821    if (!(default_dt.isNaT())) {
6822        std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6823        std::cout << std::string("[FAIL] ") + description << std::endl;
6824        throw std::runtime_error(description);
6825    }
6826    // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6827
6828    // Test value constructor
6829    datetime64 epoch_day(0, DateTimeUnit::Day);
6830    // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
6831
6832    // Test string constructor
6833    datetime64 date_from_string("2024-01-15");
6834    // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl;
6835
6836    datetime64 datetime_from_string("2024-01-15T10:30:45");
today (np_test_1_all.cpp:6840)
6830    // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
6831
6832    // Test string constructor
6833    datetime64 date_from_string("2024-01-15");
6834    // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl;
6835
6836    datetime64 datetime_from_string("2024-01-15T10:30:45");
6837    // std::cout << "DateTime from string '2024-01-15T10:30:45': " << datetime_from_string.toString() << std::endl;
6838
6839    // Test static methods
6840    datetime64 today = datetime64::today();
6841    // std::cout << "Today: " << today.toString() << std::endl;
6842
6843    datetime64 now = datetime64::now();
6844    // std::cout << "Now: " << now.toString() << std::endl;
6845
6846        std::cout << " -> tests passed" << std::endl;
6847}
6848
6849void testTimedelta64CreationDateTime() {
6850    std::cout << "========= testTimedelta64CreationDateTime =======================";