Skip to content

Preserve names in as.IDate() and as.ITime() #7800

Open
venom1204 wants to merge 13 commits into
masterfrom
issue7252
Open

Preserve names in as.IDate() and as.ITime() #7800
venom1204 wants to merge 13 commits into
masterfrom
issue7252

Conversation

@venom1204

@venom1204 venom1204 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

closes #7252

updated as.IDate and as.ITime methods to preserve names using setattr(), matching as.Date() behavior.

hi @tdhock @joshhwuu can you please have a look when you got time.
thanks

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.85%. Comparing base (deb9acc) to head (d19dbb2).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7800   +/-   ##
=======================================
  Coverage   98.85%   98.85%           
=======================================
  Files          87       87           
  Lines       17140    17163   +23     
=======================================
+ Hits        16944    16967   +23     
  Misses        196      196           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Jun 20, 2026

Copy link
Copy Markdown

No obvious timing issues in HEAD=issue7252
Comparison Plot

Generated via commit d19dbb2

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 6 minutes and 22 seconds
Installing different package versions 11 minutes and 35 seconds
Running and plotting the test cases 5 minutes and 28 seconds

@venom1204 venom1204 requested review from joshhwuu and tdhock June 20, 2026 21:19
Comment thread R/IDateTime.R Outdated
if (is_utc(tz)) {
ans = as.integer(as.numeric(x) %/% 86400L)
setattr(ans, "class", c("IDate", "Date"))
setattr(ans, "names", names(x))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont we guard here with if (!is.null(nm))?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have introduced the copy_names helper at the top and updated all methods to use it. This ensures we always guard the name assignment with if (!is.null(nm)) as you requested.

Comment thread R/IDateTime.R
as.IDate(as.Date(x, tz = tz %||% '', ...))
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we remove the blank line here?

Comment thread inst/tests/tests.Rraw Outdated
test(2376.6, names(as.IDate(c(a = 18000))), "a")
test(2376.7, names(as.IDate(c(a = 1), origin = "2020-01-01")), "a")
test(2376.8, names(as.ITime(c(a = "12-00-00"), format = "%H-%M-%S")), "a")
test(2376.9, names(as.IDate(as.POSIXct(c(a = "2019-01-01"), tz="UTC"))), "a")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test for the non-UTC tz branch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i added the test and corrected that removal of the blank line have a check when you get tim.
thanks

@ben-schwen ben-schwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEWS is missing.

Maybe we should also introduce a helper like

copy_names = function(ans, nm) {
  if (!is.null(nm)) setattr(ans, "names", nm)
  ans
}

@venom1204 venom1204 requested a review from ben-schwen June 22, 2026 17:49

@ben-schwen ben-schwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are close. Names are now not dropped but overwritten, e.g.,

as.Date(c(a = 1), origin = c(b = "2020-01-01"))
#            b
# "2020-01-02"
as.IDate(c(a = 1), origin = c(b = "2020-01-01"))
#            a
# "2020-01-02"

@ben-schwen

Copy link
Copy Markdown
Member

Can you also please stop removing comments. We rarely comment things in our codebase and most comments are quite thoughtful and there for a reason.

@venom1204

Copy link
Copy Markdown
Contributor Author

Can you also please stop removing comments. We rarely comment things in our codebase and most comments are quite thoughtful and there for a reason.

yah i get this, and am about to submit my final version with the restored comments.

@venom1204 venom1204 requested a review from ben-schwen June 24, 2026 11:22
@venom1204

Copy link
Copy Markdown
Contributor Author

We are close. Names are now not dropped but overwritten, e.g.,

as.Date(c(a = 1), origin = c(b = "2020-01-01"))
#            b
# "2020-01-02"
as.IDate(c(a = 1), origin = c(b = "2020-01-01"))
#            a
# "2020-01-02"

i tired working on it, could you please take a look and let me know if anything still needs to be adjusted?
thanks

@ben-schwen

Copy link
Copy Markdown
Member
as.Date(c(a = 1), origin = c(b = "2020-01-01"))

Nah the test should be something like this

test(1, as.IDate(c(a = 1), origin = c(b = "2020-01-01")), as.IDate(as.Date(c(a = 1), origin = c(b = "2020-01-01"))))

to be compliant with Date

@ben-schwen ben-schwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As posted above. Please don't comments if you don't know exactly that they are redundant/unnecessary.

Comment thread R/IDateTime.R
copy_names(x, nm)
} else {
# only call expensive as.IDate.character if we have to
as.IDate(origin, ...) + as.integer(x)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot drop the as.integer cast, e.g. as.IDate(origin="2020-01-01", 20.5)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add this as regression test

Comment thread R/IDateTime.R
res = unclass(e1) + unclass(e2)
nm = names(res)
ans = as.integer(res)
(setattr(ans, "class", c("IDate", "Date"))) # () wrap to return visibly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as outlined above. the comments are there for reason.
In this case we want to print on return.
setattr return invisible, thats why it was wrapped in () to print. Now when we return with copy_names there is obviously no need for wrapping. Please think about this stuff before requesting feedback.

Comment thread R/IDateTime.R
nm = names(x)
secs = 86400L * (unclass(x) %% 1L)
secs = clip_msec(secs, ms)
(setattr(secs, "class", "ITime")) # the first line that creates sec will create a local copy so we can use setattr() to avoid potential copy of class()<-

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as outlined above, we probably dont need wrapping when copy_names returns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as.IDate() drops names

2 participants