|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#ifndef R_NANOARROW_VCTR_BUILDER_DIFFTIME_H_INCLUDED |
| 19 | +#define R_NANOARROW_VCTR_BUILDER_DIFFTIME_H_INCLUDED |
| 20 | + |
| 21 | +#define R_NO_REMAP |
| 22 | +#include <R.h> |
| 23 | +#include <Rinternals.h> |
| 24 | + |
| 25 | +#include "vctr_builder_dbl.h" |
| 26 | + |
| 27 | +class DifftimeBuilder : public DblBuilder { |
| 28 | + public: |
| 29 | + explicit DifftimeBuilder(SEXP ptype_sexp, VectorType vector_type = VECTOR_TYPE_DIFFTIME) |
| 30 | + : DblBuilder(ptype_sexp, vector_type), scale_(0) {} |
| 31 | + |
| 32 | + ArrowErrorCode Init(const ArrowSchema* schema, VctrBuilderOptions options, |
| 33 | + ArrowError* error) override { |
| 34 | + NANOARROW_RETURN_NOT_OK(DblBuilder::Init(schema, options, error)); |
| 35 | + switch (schema_view_.type) { |
| 36 | + case NANOARROW_TYPE_NA: |
| 37 | + case NANOARROW_TYPE_TIME32: |
| 38 | + case NANOARROW_TYPE_TIME64: |
| 39 | + case NANOARROW_TYPE_DURATION: |
| 40 | + break; |
| 41 | + default: |
| 42 | + StopCantConvert(); |
| 43 | + } |
| 44 | + |
| 45 | + switch (GetTimeUnits(ptype_sexp_)) { |
| 46 | + case R_TIME_UNIT_MINUTES: |
| 47 | + scale_ = 1.0 / 60; |
| 48 | + break; |
| 49 | + case R_TIME_UNIT_HOURS: |
| 50 | + scale_ = 1.0 / (60 * 60); |
| 51 | + break; |
| 52 | + case R_TIME_UNIT_DAYS: |
| 53 | + scale_ = 1.0 / (60 * 60 * 24); |
| 54 | + break; |
| 55 | + case R_TIME_UNIT_WEEKS: |
| 56 | + scale_ = 1.0 / (60 * 60 * 24 * 7); |
| 57 | + break; |
| 58 | + default: |
| 59 | + scale_ = 1.0; |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + switch (schema_view_.time_unit) { |
| 64 | + case NANOARROW_TIME_UNIT_SECOND: |
| 65 | + scale_ *= 1; |
| 66 | + break; |
| 67 | + case NANOARROW_TIME_UNIT_MILLI: |
| 68 | + scale_ *= 1e-3; |
| 69 | + break; |
| 70 | + case NANOARROW_TIME_UNIT_MICRO: |
| 71 | + scale_ *= 1e-6; |
| 72 | + break; |
| 73 | + case NANOARROW_TIME_UNIT_NANO: |
| 74 | + scale_ *= 1e-9; |
| 75 | + break; |
| 76 | + default: |
| 77 | + return EINVAL; |
| 78 | + } |
| 79 | + |
| 80 | + return NANOARROW_OK; |
| 81 | + } |
| 82 | + |
| 83 | + virtual ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override { |
| 84 | + R_xlen_t value_size0 = value_size_; |
| 85 | + NANOARROW_RETURN_NOT_OK(DblBuilder::PushNext(array, error)); |
| 86 | + |
| 87 | + if (scale_ != 1) { |
| 88 | + double* result = REAL(value_); |
| 89 | + for (int64_t i = 0; i < array_view_.length; i++) { |
| 90 | + result[value_size0 + i] = result[value_size0 + i] * scale_; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return NANOARROW_OK; |
| 95 | + } |
| 96 | + |
| 97 | + private: |
| 98 | + double scale_; |
| 99 | + |
| 100 | + static RTimeUnits GetTimeUnits(SEXP ptype) { |
| 101 | + SEXP units_attr = Rf_getAttrib(ptype, Rf_install("units")); |
| 102 | + if (units_attr == R_NilValue || TYPEOF(units_attr) != STRSXP || |
| 103 | + Rf_length(units_attr) != 1) { |
| 104 | + Rf_error("Expected difftime 'units' attribute of type character(1)"); |
| 105 | + } |
| 106 | + |
| 107 | + const char* dst_units = Rf_translateCharUTF8(STRING_ELT(units_attr, 0)); |
| 108 | + if (strcmp(dst_units, "secs") == 0) { |
| 109 | + return R_TIME_UNIT_SECONDS; |
| 110 | + } else if (strcmp(dst_units, "mins") == 0) { |
| 111 | + return R_TIME_UNIT_MINUTES; |
| 112 | + } else if (strcmp(dst_units, "hours") == 0) { |
| 113 | + return R_TIME_UNIT_HOURS; |
| 114 | + } else if (strcmp(dst_units, "days") == 0) { |
| 115 | + return R_TIME_UNIT_DAYS; |
| 116 | + } else if (strcmp(dst_units, "weeks") == 0) { |
| 117 | + return R_TIME_UNIT_WEEKS; |
| 118 | + } else { |
| 119 | + Rf_error("Unexpected value for difftime 'units' attribute"); |
| 120 | + return R_TIME_UNIT_SECONDS; |
| 121 | + } |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +#endif |
0 commit comments