@@ -158,6 +158,109 @@ as.POSIXlt.clock_zoned_time <- function(x, ...) {
158158
159159# ------------------------------------------------------------------------------
160160
161+ # ' Convert to a date-time
162+ # '
163+ # ' @description
164+ # ' `as_date_time()` is a generic function that converts its input to a date-time
165+ # ' (POSIXct).
166+ # '
167+ # ' There are methods for converting dates (Date), calendars, time points, and
168+ # ' zoned-times to date-times.
169+ # '
170+ # ' @details
171+ # ' Note that clock always assumes that R's Date class is naive, so converting
172+ # ' a Date to a POSIXct will always attempt to retain the printed year, month,
173+ # ' and day. Where possible, the resulting time will be at midnight (`00:00:00`),
174+ # ' but in some rare cases this is not possible due to daylight saving time. If
175+ # ' that issue ever arises, an error will be thrown, which can be resolved by
176+ # ' explicitly supplying `nonexistent` or `ambiguous`.
177+ # '
178+ # ' @inheritParams as-zoned-time-naive-time
179+ # '
180+ # ' @param x `[vector]`
181+ # '
182+ # ' A vector.
183+ # '
184+ # ' @export
185+ # ' @examples
186+ # ' x <- as.Date("2019-01-01")
187+ # '
188+ # ' # `as.POSIXct()` will always treat Date as UTC, but will show the result
189+ # ' # of the conversion in your system time zone, which can be somewhat confusing
190+ # ' if (rlang::is_installed("withr")) {
191+ # ' withr::with_timezone("UTC", print(as.POSIXct(x)))
192+ # ' withr::with_timezone("Europe/Paris", print(as.POSIXct(x)))
193+ # ' withr::with_timezone("America/New_York", print(as.POSIXct(x)))
194+ # ' }
195+ # '
196+ # ' # `as_date_time()` will treat Date as naive, which means that the original
197+ # ' # printed date will attempt to be kept wherever possible, no matter the
198+ # ' # time zone. The time will be set to midnight.
199+ # ' as_date_time(x, "UTC")
200+ # ' as_date_time(x, "Europe/Paris")
201+ # ' as_date_time(x, "America/New_York")
202+ # '
203+ # ' # In some rare cases, this is not possible.
204+ # ' # For example, in Asia/Beirut, there was a DST gap from
205+ # ' # 2021-03-27 23:59:59 -> 2021-03-28 01:00:00,
206+ # ' # skipping the 0th hour entirely.
207+ # ' x <- as.Date("2021-03-28")
208+ # ' try(as_date_time(x, "Asia/Beirut"))
209+ # '
210+ # ' # To resolve this, set a `nonexistent` time resolution strategy
211+ # ' as_date_time(x, "Asia/Beirut", nonexistent = "roll-forward")
212+ # '
213+ # '
214+ # ' # You can also convert to date-time from other clock types
215+ # ' as_date_time(year_month_day(2019, 2, 3, 03), "America/New_York")
216+ as_date_time <- function (x , ... ) {
217+ UseMethod(" as_date_time" )
218+ }
219+
220+ # ' @rdname as_date_time
221+ # ' @export
222+ as_date_time.POSIXt <- function (x , ... ) {
223+ check_dots_empty()
224+ to_posixct(x )
225+ }
226+
227+ # ' @rdname as_date_time
228+ # ' @export
229+ as_date_time.Date <- function (x , zone , ... , nonexistent = NULL , ambiguous = NULL ) {
230+ check_dots_empty()
231+ as.POSIXct(as_naive_time(x ), tz = zone , nonexistent = nonexistent , ambiguous = ambiguous )
232+ }
233+
234+ # ' @rdname as_date_time
235+ # ' @export
236+ as_date_time.clock_calendar <- function (x , zone , ... , nonexistent = NULL , ambiguous = NULL ) {
237+ check_dots_empty()
238+ as.POSIXct(x , tz = zone , nonexistent = nonexistent , ambiguous = ambiguous )
239+ }
240+
241+ # ' @rdname as_date_time
242+ # ' @export
243+ as_date_time.clock_sys_time <- function (x , zone , ... ) {
244+ check_dots_empty()
245+ as.POSIXct(x , tz = zone )
246+ }
247+
248+ # ' @rdname as_date_time
249+ # ' @export
250+ as_date_time.clock_naive_time <- function (x , zone , ... , nonexistent = NULL , ambiguous = NULL ) {
251+ check_dots_empty()
252+ as.POSIXct(x , tz = zone , nonexistent = nonexistent , ambiguous = ambiguous )
253+ }
254+
255+ # ' @rdname as_date_time
256+ # ' @export
257+ as_date_time.clock_zoned_time <- function (x , ... ) {
258+ check_dots_empty()
259+ as.POSIXct(x )
260+ }
261+
262+ # ------------------------------------------------------------------------------
263+
161264# ' Getters: date-time
162265# '
163266# ' @description
0 commit comments