Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow casting from Date and POSIXt objects #55

Open
billdenney opened this issue May 15, 2024 · 2 comments
Open

Allow casting from Date and POSIXt objects #55

billdenney opened this issue May 15, 2024 · 2 comments
Labels
change::minor Feature additions design (api) Design of user-facing api enhancement New feature or request

Comments

@billdenney
Copy link
Contributor

I think that it would be useful to be able to cast Date and POSIXt objects to parttime objects.

My initial thought would be something like the following:

library(parttime)
#> Warning: package 'parttime' was built under R version 4.3.3
#> Initializing default timezone offset, which is assumed when timezone
#> parts are missing.
#> 
#>     options("parttime.assume_tz_offset" = 0L)
#> 
#> Attaching package: 'parttime'
#> The following objects are masked from 'package:stats':
#> 
#>     end, start
#> The following objects are masked from 'package:base':
#> 
#>     pmax, pmin

as.parttime("2001")
#> <partial_time<YMDhms+tz>[1]> 
#> [1] "2001"
as.parttime(as.Date("2001-01-01"), missing_year = FALSE, missing_month = TRUE, missing_day = TRUE)
#> Error in vctrs::stop_incompatible_cast(x, to): argument "x_arg" is missing, with no default

Created on 2024-05-15 with reprex v2.1.0

@trevorld
Copy link

Note you can cast Date and POSIXt objects to {parttime} objects using {datetimeoffset} as an intermediary:

Sys.Date() |>
  datetimeoffset::as_datetimeoffset() |>
  parttime::as.parttime()
<partial_time<YMDhms+tz>[1]> 
[1] "2024-05-15"
Sys.time() |>
  datetimeoffset::as_datetimeoffset() |>
  parttime::as.parttime()
<partial_time<YMDhms+tz>[1]> 
[1] "2024-05-15 11:09:56.833+-7:00" 

@dgkf
Copy link
Owner

dgkf commented May 15, 2024

And if you prefer not to pull in new dependencies, you can also go through an as.character intermediate - though you may have to juggle things like timezones and formatting.

Sys.Date() |>
  as.character() |>
  parttime::as.parttime()

or

Sys.Date() |>
  as.POSIXct() |>
  as.character() |>
  parttime::as.parttime()

Even with a work-around available, I do agree that handling coercion from common base types should be handled out of the box.

Thanks for filing the report.

@dgkf dgkf added enhancement New feature or request design (api) Design of user-facing api change::minor Feature additions labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change::minor Feature additions design (api) Design of user-facing api enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants