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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
18 skip_if_not_available("dataset")
20 library(lubridate, warn.conflicts = FALSE)
21 library(dplyr, warn.conflicts = FALSE)
23 # base::strptime() defaults to local timezone
24 # but arrow's strptime defaults to UTC.
25 # So that tests are consistent, set the local timezone to UTC
26 # TODO: consider reevaluating this workaround after ARROW-12980
27 withr::local_timezone("UTC")
29 # TODO: We should test on windows once ARROW-13168 is resolved.
30 if (tolower(Sys.info()[["sysname"]]) == "windows") {
31 test_date <- as.POSIXct("2017-01-01 00:00:11.3456789", tz = "")
33 test_date <- as.POSIXct("2017-01-01 00:00:11.3456789", tz = "Pacific/Marquesas")
37 test_df <- tibble::tibble(
38 # test_date + 1 turns the tzone = "" to NULL, which is functionally equivalent
39 # so we can run some tests on Windows, but this skirts around
40 # https://issues.apache.org/jira/browse/ARROW-13588
41 # That issue is tough because in C++, "" is the "no timezone" value
42 # due to static typing, so we can't distinguish a literal "" from NULL
43 datetime = c(test_date, NA) + 1,
44 date = c(as.Date("2021-09-09"), NA)
47 # These tests test component extraction from timestamp objects
49 test_that("extract year from timestamp", {
50 compare_dplyr_binding(
52 mutate(x = year(datetime)) %>%
58 test_that("extract isoyear from timestamp", {
59 compare_dplyr_binding(
61 mutate(x = isoyear(datetime)) %>%
67 test_that("extract quarter from timestamp", {
68 compare_dplyr_binding(
70 mutate(x = quarter(datetime)) %>%
76 test_that("extract month from timestamp", {
77 compare_dplyr_binding(
79 mutate(x = month(datetime)) %>%
85 test_that("extract isoweek from timestamp", {
86 compare_dplyr_binding(
88 mutate(x = isoweek(datetime)) %>%
94 test_that("extract epiweek from timestamp", {
95 compare_dplyr_binding(
97 mutate(x = epiweek(datetime)) %>%
103 test_that("extract day from timestamp", {
104 compare_dplyr_binding(
106 mutate(x = day(datetime)) %>%
112 test_that("extract wday from timestamp", {
113 compare_dplyr_binding(
115 mutate(x = wday(datetime)) %>%
120 compare_dplyr_binding(
122 mutate(x = wday(date, week_start = 3)) %>%
127 compare_dplyr_binding(
129 mutate(x = wday(date, week_start = 1)) %>%
134 skip_on_os("windows") # https://issues.apache.org/jira/browse/ARROW-13168
136 compare_dplyr_binding(
138 mutate(x = wday(date, label = TRUE)) %>%
139 mutate(x = as.character(x)) %>%
144 compare_dplyr_binding(
146 mutate(x = wday(datetime, label = TRUE, abbr = TRUE)) %>%
147 mutate(x = as.character(x)) %>%
153 test_that("extract yday from timestamp", {
154 compare_dplyr_binding(
156 mutate(x = yday(datetime)) %>%
162 test_that("extract hour from timestamp", {
163 compare_dplyr_binding(
165 mutate(x = hour(datetime)) %>%
171 test_that("extract minute from timestamp", {
172 compare_dplyr_binding(
174 mutate(x = minute(datetime)) %>%
180 test_that("extract second from timestamp", {
181 compare_dplyr_binding(
183 mutate(x = second(datetime)) %>%
186 # arrow supports nanosecond resolution but lubridate does not
191 # These tests test extraction of components from date32 objects
193 test_that("extract year from date", {
194 compare_dplyr_binding(
196 mutate(x = year(date)) %>%
202 test_that("extract isoyear from date", {
203 compare_dplyr_binding(
205 mutate(x = isoyear(date)) %>%
211 test_that("extract quarter from date", {
212 compare_dplyr_binding(
214 mutate(x = quarter(date)) %>%
220 test_that("extract month from date", {
221 compare_dplyr_binding(
223 mutate(x = month(date)) %>%
229 test_that("extract isoweek from date", {
230 compare_dplyr_binding(
232 mutate(x = isoweek(date)) %>%
238 test_that("extract epiweek from date", {
239 compare_dplyr_binding(
241 mutate(x = epiweek(date)) %>%
247 test_that("extract day from date", {
248 compare_dplyr_binding(
250 mutate(x = day(date)) %>%
256 test_that("extract wday from date", {
257 compare_dplyr_binding(
259 mutate(x = wday(date)) %>%
264 compare_dplyr_binding(
266 mutate(x = wday(date, week_start = 3)) %>%
271 compare_dplyr_binding(
273 mutate(x = wday(date, week_start = 1)) %>%
278 skip_on_os("windows") # https://issues.apache.org/jira/browse/ARROW-13168
280 compare_dplyr_binding(
282 mutate(x = wday(date, label = TRUE, abbr = TRUE)) %>%
283 mutate(x = as.character(x)) %>%
288 compare_dplyr_binding(
290 mutate(x = wday(date, label = TRUE)) %>%
291 mutate(x = as.character(x)) %>%
297 test_that("extract yday from date", {
298 compare_dplyr_binding(
300 mutate(x = yday(date)) %>%