timedelta64#

class numpy::timedelta64#

numpy C++ class.

Example#

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

// Use timedelta64
timedelta64 obj;
// ... operations ...

Constructors#

Signature

Location

Example

timedelta64(int64_tvalue, DateTimeUnitunit)

NP_TIMEDELTA64.H:25

View

timedelta64(const std::string &delta_string)

NP_TIMEDELTA64.H:26

View

Operators#

Signature

Return Type

Location

Example

timedelta64 operator+(const timedelta64 &other)

timedelta64

NP_TIMEDELTA64.H:31

timedelta64 operator-(const timedelta64 &other)

timedelta64

NP_TIMEDELTA64.H:32

timedelta64 operator\*(int64_tscalar)

timedelta64

NP_TIMEDELTA64.H:33

timedelta64 operator/(int64_tscalar)

timedelta64

NP_TIMEDELTA64.H:34

timedelta64 operator-()

timedelta64

NP_TIMEDELTA64.H:36

bool operator==(const timedelta64 &other)

bool

NP_TIMEDELTA64.H:38

bool operator!=(const timedelta64 &other)

bool

NP_TIMEDELTA64.H:39

bool operator<(const timedelta64 &other)

bool

NP_TIMEDELTA64.H:40

bool operator<=(const timedelta64 &other)

bool

NP_TIMEDELTA64.H:41

bool operator>(const timedelta64 &other)

bool

NP_TIMEDELTA64.H:42

bool operator>=(const timedelta64 &other)

bool

NP_TIMEDELTA64.H:43

Indexing / Selection#

Signature

Return Type

Location

Example

DateTimeUnit getUnit()

DateTimeUnit

NP_TIMEDELTA64.H:29

View

int64_t getValue()

int64_t

NP_TIMEDELTA64.H:28

View

Math Operations#

Signature

Return Type

Location

Example

timedelta64 abs()

timedelta64

NP_TIMEDELTA64.H:52

View

Other Methods#

Signature

Return Type

Location

Example

timedelta64 NaT(DateTimeUnitunit = DateTimeUnit::Second)

timedelta64

NP_TIMEDELTA64.H:55

View

timedelta64 convertTo(DateTimeUnitnew_unit)

timedelta64

NP_TIMEDELTA64.H:57

View

timedelta64 fromSeconds(int64_tseconds, DateTimeUnitunit)

timedelta64

NP_TIMEDELTA64.H:22

View

bool isNaT()

bool

NP_TIMEDELTA64.H:54

View

int64_t parseDeltaString(const std::string &delta_string, DateTimeUnit &unit)

int64_t

NP_TIMEDELTA64.H:60

int64_t toDays()

int64_t

NP_TIMEDELTA64.H:47

View

int64_t toHours()

int64_t

NP_TIMEDELTA64.H:48

View

int64_t toMinutes()

int64_t

NP_TIMEDELTA64.H:49

View

int64_t toSeconds()

int64_t

NP_TIMEDELTA64.H:21

View

double toSecondsDouble()

double

NP_TIMEDELTA64.H:50

std::string toString()

std::string

NP_TIMEDELTA64.H:45

View

Code Examples#

The following examples are extracted from the test suite.

timedelta64 (np_test_1_all.cpp:7030)
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);
7031    timedelta64 result3 = valid_date - nat_dt;
7032
7033    // std::cout << "Valid date + NaT timedelta = " << result1.toString() << std::endl;
7034    // std::cout << "NaT datetime + valid timedelta = " << result2.toString() << std::endl;
7035    // std::cout << "Valid date - NaT datetime = " << result3.toString() << std::endl;
7036
7037    // NaT comparisons
7038    // std::cout << "NaT == NaT: " << (nat_dt == datetime64::NaT()) << std::endl;
7039    // std::cout << "Valid date == NaT: " << (valid_date == nat_dt) << std::endl;
7040    // std::cout << "NaT < Valid date: " << (nat_dt < valid_date) << std::endl;
timedelta64 (np_test_1_all.cpp:7030)
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);
7031    timedelta64 result3 = valid_date - nat_dt;
7032
7033    // std::cout << "Valid date + NaT timedelta = " << result1.toString() << std::endl;
7034    // std::cout << "NaT datetime + valid timedelta = " << result2.toString() << std::endl;
7035    // std::cout << "Valid date - NaT datetime = " << result3.toString() << std::endl;
7036
7037    // NaT comparisons
7038    // std::cout << "NaT == NaT: " << (nat_dt == datetime64::NaT()) << std::endl;
7039    // std::cout << "Valid date == NaT: " << (valid_date == nat_dt) << std::endl;
7040    // std::cout << "NaT < Valid date: " << (nat_dt < valid_date) << std::endl;
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;
abs (np_test_1_all.cpp:101)
 91        if (arr.getShape().size() == 1 && arr.getShape()[0] <= 10) {
 92            for (size_t i = 0; i < arr.getShape()[0]; ++i) {
 93                // std::cout << static_cast<int64_t>(arr.getElementAt({i})) << " ";
 94            }
 95        }
 96        // std::cout << std::endl;
 97    }
 98
 99    // Helper function for core array extensions tests
100    bool isApproxEqualExt(double a, double b, double tolerance = 1e-10) {
101        return std::abs(a - b) < tolerance;
102    }
103}
104
105void f_nothing() {
106    // This function does nothing and tests nothing
107    // It's a placeholder test function
108}
109
110// ------ merging np_test_advanced_indexing.cpp -- number of functions merged=16 --------------------------------
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;
fromSeconds (np_test_1_all.cpp:28819)
28809      std::cout << " -> tests passed" << std::endl;
28810    }
28811
28812    // ============================================================================
28813    // TIMEDELTA64 METHODS TESTS
28814    // ============================================================================
28815
28816    void np_test_timedelta64_conversions() {
28817      std::cout << "========= timedelta64 conversions: fromSeconds/toSeconds/toDays =======================";
28818
28819      // Test fromSeconds (needs two parameters: seconds and unit)
28820      int64_t seconds = 86400; // 1 day
28821      auto td = numpy::timedelta64::fromSeconds(seconds, numpy::DateTimeUnit::Second);
28822
28823      // Test toSeconds
28824      int64_t converted_seconds = td.toSeconds();
28825
28826      // Test toDays (returns int64_t)
28827      int64_t days = td.toDays();
28828
28829      bool passed = (converted_seconds == seconds && days == 1);
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;
toDays (np_test_1_all.cpp:28826)
28816    void np_test_timedelta64_conversions() {
28817      std::cout << "========= timedelta64 conversions: fromSeconds/toSeconds/toDays =======================";
28818
28819      // Test fromSeconds (needs two parameters: seconds and unit)
28820      int64_t seconds = 86400; // 1 day
28821      auto td = numpy::timedelta64::fromSeconds(seconds, numpy::DateTimeUnit::Second);
28822
28823      // Test toSeconds
28824      int64_t converted_seconds = td.toSeconds();
28825
28826      // Test toDays (returns int64_t)
28827      int64_t days = td.toDays();
28828
28829      bool passed = (converted_seconds == seconds && days == 1);
28830
28831      if (!passed) {
28832        std::cout << "  [FAIL] : in np_test_timedelta64_conversions() : Conversion failed - seconds: "
28833          << converted_seconds << ", days: " << days << std::endl;
28834        throw std::runtime_error("np_test_timedelta64_conversions failed");
28835      }
toHours (np_test_1_all.cpp:6863)
6853    timedelta64 one_day(1, DateTimeUnit::Day);
6854    // std::cout << "One day: " << one_day.toString() << std::endl;
6855
6856    timedelta64 five_hours(5, DateTimeUnit::Hour);
6857    // std::cout << "Five hours: " << five_hours.toString() << std::endl;
6858
6859    timedelta64 from_string("30 minutes");
6860    // std::cout << "From string '30 minutes': " << from_string.toString() << std::endl;
6861
6862    // Test conversions
6863    // std::cout << "One day in hours: " << one_day.toHours() << std::endl;
6864    // std::cout << "Five hours in minutes: " << five_hours.toMinutes() << std::endl;
6865
6866        std::cout << " -> tests passed" << std::endl;
6867}
6868
6869void testDatetimeArithmeticDateTime() {
6870    std::cout << "========= testDatetimeArithmeticDateTime =======================";
6871
6872    datetime64 start_date("2024-01-01");
6873    timedelta64 one_week(7, DateTimeUnit::Day);
toMinutes (np_test_1_all.cpp:6864)
6854    // std::cout << "One day: " << one_day.toString() << std::endl;
6855
6856    timedelta64 five_hours(5, DateTimeUnit::Hour);
6857    // std::cout << "Five hours: " << five_hours.toString() << std::endl;
6858
6859    timedelta64 from_string("30 minutes");
6860    // std::cout << "From string '30 minutes': " << from_string.toString() << std::endl;
6861
6862    // Test conversions
6863    // std::cout << "One day in hours: " << one_day.toHours() << std::endl;
6864    // std::cout << "Five hours in minutes: " << five_hours.toMinutes() << std::endl;
6865
6866        std::cout << " -> tests passed" << std::endl;
6867}
6868
6869void testDatetimeArithmeticDateTime() {
6870    std::cout << "========= testDatetimeArithmeticDateTime =======================";
6871
6872    datetime64 start_date("2024-01-01");
6873    timedelta64 one_week(7, DateTimeUnit::Day);
6874    timedelta64 three_days(3, DateTimeUnit::Day);
toSeconds (np_test_1_all.cpp:28824)
28814    // ============================================================================
28815
28816    void np_test_timedelta64_conversions() {
28817      std::cout << "========= timedelta64 conversions: fromSeconds/toSeconds/toDays =======================";
28818
28819      // Test fromSeconds (needs two parameters: seconds and unit)
28820      int64_t seconds = 86400; // 1 day
28821      auto td = numpy::timedelta64::fromSeconds(seconds, numpy::DateTimeUnit::Second);
28822
28823      // Test toSeconds
28824      int64_t converted_seconds = td.toSeconds();
28825
28826      // Test toDays (returns int64_t)
28827      int64_t days = td.toDays();
28828
28829      bool passed = (converted_seconds == seconds && days == 1);
28830
28831      if (!passed) {
28832        std::cout << "  [FAIL] : in np_test_timedelta64_conversions() : Conversion failed - seconds: "
28833          << converted_seconds << ", days: " << days << std::endl;
28834        throw std::runtime_error("np_test_timedelta64_conversions failed");
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");