|
| 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_POSIXCT_H_INCLUDED |
| 19 | +#define R_NANOARROW_VCTR_BUILDER_POSIXCT_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 PosixctBuilder : public DblBuilder { |
| 28 | + public: |
| 29 | + explicit PosixctBuilder(SEXP ptype_sexp) |
| 30 | + : DblBuilder(ptype_sexp, VECTOR_TYPE_POSIXCT), 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 | + |
| 36 | + ArrowTimeUnit time_unit = NANOARROW_TIME_UNIT_SECOND; |
| 37 | + switch (schema_view_.type) { |
| 38 | + case NANOARROW_TYPE_NA: |
| 39 | + break; |
| 40 | + case NANOARROW_TYPE_DATE64: |
| 41 | + time_unit = NANOARROW_TIME_UNIT_MILLI; |
| 42 | + break; |
| 43 | + case NANOARROW_TYPE_TIMESTAMP: |
| 44 | + time_unit = schema_view_.time_unit; |
| 45 | + break; |
| 46 | + default: |
| 47 | + StopCantConvert(); |
| 48 | + } |
| 49 | + |
| 50 | + scale_ = 1; |
| 51 | + |
| 52 | + switch (time_unit) { |
| 53 | + case NANOARROW_TIME_UNIT_SECOND: |
| 54 | + scale_ *= 1; |
| 55 | + break; |
| 56 | + case NANOARROW_TIME_UNIT_MILLI: |
| 57 | + scale_ *= 1e-3; |
| 58 | + break; |
| 59 | + case NANOARROW_TIME_UNIT_MICRO: |
| 60 | + scale_ *= 1e-6; |
| 61 | + break; |
| 62 | + case NANOARROW_TIME_UNIT_NANO: |
| 63 | + scale_ *= 1e-9; |
| 64 | + break; |
| 65 | + default: |
| 66 | + return EINVAL; |
| 67 | + } |
| 68 | + |
| 69 | + return NANOARROW_OK; |
| 70 | + } |
| 71 | + |
| 72 | + ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override { |
| 73 | + R_xlen_t value_size0 = value_size_; |
| 74 | + NANOARROW_RETURN_NOT_OK(DblBuilder::PushNext(array, error)); |
| 75 | + |
| 76 | + if (scale_ != 1) { |
| 77 | + double* result = REAL(value_); |
| 78 | + for (int64_t i = 0; i < array_view_.length; i++) { |
| 79 | + result[value_size0 + i] = result[value_size0 + i] * scale_; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return NANOARROW_OK; |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + double scale_; |
| 88 | +}; |
| 89 | + |
| 90 | +#endif |
0 commit comments