Skip to content

fix: Fix timezone conversion for invalid timestamps with tz_out_convert = "force" - #1474

Merged
krlmlr merged 5 commits into
mainfrom
copilot/fix-1470
Sep 7, 2025
Merged

krlmlr merged 5 commits into
mainfrom
copilot/fix-1470

Conversation

Copilot AI commented Sep 7, 2025 •

Copy link
Copy Markdown
Contributor

When using tz_out_convert = "force" with DuckDB R package, if any returned timestamps are invalid (e.g., during DST transitions), then all times get converted incorrectly, with some losing their time component and becoming date-only values.

The issue occurs in the tz_force_one() function in R/Result.R which calls as.POSIXct(ct, tz = timezone) without specifying a format parameter. When as.POSIXct doesn't have an explicit format, it tries various formats from tryFormats until finding one that returns no NA values. For invalid timestamps during DST transitions, this can result in using the "%Y-%m-%d" format, which only captures dates and loses time information.

Example of the issue:

library(duckdb)
con <- dbConnect(duckdb(), timezone_out = "Europe/London", tz_out_convert = "force")

# Single valid timestamp works correctly
dbGetQuery(con, "VALUES ('2025-03-30 00:59:00'::TIMESTAMP);")
#>                  col0
#> 1 2025-03-30 00:59:00

# But mixing valid and invalid timestamps causes time loss
# 2025-03-30 01:00:00 does not exist in Europe/London (DST transition)
dbGetQuery(con, "VALUES ('2025-03-30 00:59:00'::TIMESTAMP), ('2025-03-30 01:00:00'::TIMESTAMP);")
#>         col0
#> 1 2025-03-30  # Time component lost!
#> 2 2025-03-30  # Time component lost!

This fix adds the explicit format = "%Y-%m-%d %H:%M:%OS" parameter to the as.POSIXct() call in tz_force_one(), preventing it from trying multiple formats and ensuring consistent behavior. Valid timestamps retain their full time information, while invalid timestamps during DST transitions will be handled appropriately rather than causing time loss for all timestamps.

The fix also includes a test case that validates the expected behavior for DST transition scenarios.

Fixes #1470.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cran.r-project.org
    • Triggering command: /usr/lib/R/bin/exec/R --vanilla -e ~n~install.packages(c(&#39;testthat&#39;,~&#43;~&#39;DBI&#39;,~&#43;~&#39;withr&#39;),~&#43;~repos=&#39;REDACTED&#39;,~&#43;~quiet=TRUE)~n~ (dns block)
  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

… as.POSIXct

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copilot AI changed the title [WIP] tz_out_convert = force sets all times to midnight in TIMESTAMPS whenever any supplied datetimes are invalid Fix tz_out_convert=force timezone conversion losing time component for invalid timestamps Sep 7, 2025
Copilot AI requested a review from krlmlr September 7, 2025 13:03
@krlmlr krlmlr changed the title Fix tz_out_convert=force timezone conversion losing time component for invalid timestamps fix: Fix timezone conversion for invalid timestamps with tz_out_convert = "force" Sep 7, 2025
@krlmlr
krlmlr marked this pull request as ready for review September 7, 2025 13:19
@krlmlr
krlmlr enabled auto-merge September 7, 2025 13:20
@krlmlr
krlmlr merged commit dc23e9a into main Sep 7, 2025
@krlmlr
krlmlr deleted the copilot/fix-1470 branch September 7, 2025 14:44
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tz_out_convert = force sets all times to midnight in TIMESTAMPS whenever any supplied datetimes are invalid

2 participants