Issue with converting date format in R using lubridate while handling time zones
I tried several approaches but none seem to work. I've been struggling with this for a few days now and could really use some help. I'm having trouble converting a character vector of dates into POSIXct objects using the `lubridate` package in R, particularly when it comes to different time zones. I have a character vector that contains dates in the format 'YYYY-MM-DD HH:MM:SS' and I need to convert these to POSIXct while ensuring that the appropriate time zone is set. However, some of the dates seem to default to UTC and others are off by several hours, which leads to incorrect timestamps in my analysis. Here's the code I'm currently using: ```r library(lubridate) # Sample character vector with different time zones char_dates <- c('2023-10-01 12:00:00', '2023-10-02 14:00:00') # Attempting to convert to POSIXct with time zone set posix_dates <- ymd_hms(char_dates, tz = 'America/New_York') print(posix_dates) ``` I expected the output to correctly reflect the New York time zone, but I'm getting: ``` [1] "2023-10-01 12:00:00 EDT" "2023-10-02 14:00:00 EDT" ``` This seems correct for the first date, but when I change the time zone to 'America/Los_Angeles', it incorrectly shifts the time as well. I tried using `with_tz` to convert after the initial parsing, but it still seems to mess up the hours: ```r posix_dates_los_angeles <- with_tz(posix_dates, tzone = 'America/Los_Angeles') print(posix_dates_los_angeles) ``` The output shows: ``` [1] "2023-10-01 09:00:00 PDT" "2023-10-02 11:00:00 PDT" ``` This shift is not what I anticipated. I also ensured that the installation of `lubridate` is the latest version (1.8.0). Any suggestions on how to manage this conversion correctly while keeping the time zones accurate? This is part of a larger application I'm building. What's the best practice here? For context: I'm using R on Windows. How would you solve this?