墨尔本用户技术问询:如何获取AEST时区日期时间而非AEDT/UTC?
Hey there! Let's sort out this timezone issue you're facing. I’ve dealt with similar frustrations when needing a fixed AEST (UTC+10) instead of Melbourne’s default timezone that flips to AEDT in summer. Here’s how to make it work, both globally and per-function call:
Global Default Timezone (All Functions Use AEST)
If you want every date/time function in your R session to default to AEST, set your system timezone to one that doesn’t observe daylight savings. The easiest and most intuitive option is "Australia/Brisbane"—Brisbane sticks to AEST year-round, no DST switches.
Run this once at the start of your session:
# Set global timezone to permanent AEST Sys.setenv(TZ = "Australia/Brisbane") # Verify it works Sys.time() # Should show AEST timezone lubridate::now() # Same here
Alternatively, you can use Etc/GMT-10 (directly maps to UTC+10, no DST), but note the counterintuitive naming: GMT-10 means "10 hours ahead of GMT/UTC". Stick with Brisbane’s timezone if you want something more readable.
Per-Function Timezone Specification
If you don’t want to change the global setting, just explicitly specify the AEST timezone every time you call a date/time function:
With lubridate
library(lubridate) # Get current AEST time current_aest <- now(tzone = "Australia/Brisbane") print(current_aest) # Parse a date string into AEST event_date <- ymd_hms("2024-07-15 09:30:00", tz = "Australia/Brisbane") print(event_date)
With POSIXct/POSIXlt
# Convert current time to AEST posix_aest <- as.POSIXct(Sys.time(), tz = "Australia/Brisbane") print(posix_aest) # Create a POSIXct date directly in AEST fixed_date <- as.POSIXct("2024-12-25 12:00:00", tz = "Australia/Brisbane") print(fixed_date)
Why Your Previous Attempts Might Have Failed
Chances are you used tz = "Australia/Melbourne"—this timezone automatically switches between AEST (winter) and AEDT (summer), which is why you weren’t getting consistent AEST results. By switching to Australia/Brisbane or Etc/GMT-10, you lock in UTC+10 permanently.
内容的提问来源于stack exchange,提问作者Deepak Agarwal

