| 25 | // Specialization of NumericArray::Make for arrow::TimestampType. |
| 26 | template <> |
| 27 | libmexclass::proxy::MakeResult TimestampArray::make( |
| 28 | const libmexclass::proxy::FunctionArguments& constructor_arguments) { |
| 29 | namespace mda = ::matlab::data; |
| 30 | using MatlabBuffer = arrow::matlab::buffer::MatlabBuffer; |
| 31 | using TimestampArray = arrow::TimestampArray; |
| 32 | using TimestampArrayProxy = |
| 33 | arrow::matlab::array::proxy::NumericArray<arrow::TimestampType>; |
| 34 | |
| 35 | mda::StructArray opts = constructor_arguments[0]; |
| 36 | |
| 37 | // Get the mxArray from constructor arguments |
| 38 | const mda::TypedArray<int64_t> timestamp_mda = opts[0]["MatlabArray"]; |
| 39 | const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"]; |
| 40 | |
| 41 | const mda::TypedArray<mda::MATLABString> timezone_mda = opts[0]["TimeZone"]; |
| 42 | const mda::TypedArray<mda::MATLABString> units_mda = opts[0]["TimeUnit"]; |
| 43 | |
| 44 | // extract the time zone string |
| 45 | const std::u16string& u16_timezone = timezone_mda[0]; |
| 46 | MATLAB_ASSIGN_OR_ERROR(const auto timezone, |
| 47 | arrow::util::UTF16StringToUTF8(u16_timezone), |
| 48 | error::UNICODE_CONVERSION_ERROR_ID); |
| 49 | |
| 50 | // extract the time unit |
| 51 | const std::u16string& u16_timeunit = units_mda[0]; |
| 52 | MATLAB_ASSIGN_OR_ERROR(const auto time_unit, |
| 53 | arrow::matlab::type::timeUnitFromString(u16_timeunit), |
| 54 | error::UNKNOWN_TIME_UNIT_ERROR_ID) |
| 55 | |
| 56 | // create the timestamp_type |
| 57 | auto data_type = arrow::timestamp(time_unit, timezone); |
| 58 | auto array_length = static_cast<int64_t>( |
| 59 | timestamp_mda.getNumberOfElements()); // cast size_t to int64_t |
| 60 | |
| 61 | auto data_buffer = std::make_shared<MatlabBuffer>(timestamp_mda); |
| 62 | |
| 63 | // Pack the validity bitmap values. |
| 64 | MATLAB_ASSIGN_OR_ERROR(auto packed_validity_bitmap, bit::packValid(validity_bitmap_mda), |
| 65 | error::BITPACK_VALIDITY_BITMAP_ERROR_ID); |
| 66 | |
| 67 | auto array_data = arrow::ArrayData::Make(data_type, array_length, |
| 68 | {packed_validity_bitmap, data_buffer}); |
| 69 | auto timestamp_array = |
| 70 | std::static_pointer_cast<TimestampArray>(arrow::MakeArray(array_data)); |
| 71 | return std::make_shared<TimestampArrayProxy>(std::move(timestamp_array)); |
| 72 | } |
| 73 | } // namespace arrow::matlab::array::proxy |