Turn UTC timestamps into local days in R¶
Draft: not yet validated
The code on this page was run inside MESA RStudio Geospatial on 2026-11-16 (R 4.6.2), by the AI assistant that drafted it, and produced the output shown. No person has yet followed the steps, so a screen may differ. Tell us what you find.
Instruments usually record time in UTC. A "day" of UTC is not a day where the instrument stands: on the invented campus, which keeps US Mountain Time, the UTC date changes at six in the evening in July. Evening rain lands on the wrong day unless you convert first.
You will: have daily rain totals by local day that match the gauge's daily record.
Time: 10 minutes. You need: the readings and gauges tables from
Load a file from your Data Store in RStudio, in
MESA RStudio Geospatial.
Steps¶
-
In the Console, fetch the hourly observations into your practice folder:
download.file("https://idss-mesa.github.io/trainings/assets/practice/observations.csv", "observations.csv")You should see: a line that reads
downloaded 13 KB. -
Read the file and check its size:
obs <- read.csv("observations.csv") dim(obs)You should see:
[1] 336 4 -
Keep the rain rows and turn the text timestamps into real times. Say that they are UTC; the trailing
Zin the file means so, but R does not read it:rain <- obs[obs$measurement == "rain_mm", ] rain$time_utc <- as.POSIXct(rain$timestamp_utc, format = "%Y-%m-%dT%H:%M:%SZ", tz = "UTC") attr(rain$time_utc, "tzone")You should see:
[1] "UTC" -
Total the rain by UTC date:
rain$day_utc <- format(rain$time_utc, "%Y-%m-%d") aggregate(value ~ day_utc, data = rain, FUN = sum)You should see: eight dates for a seven-day week, the first sign of trouble.
day_utc value 1 2026-07-10 0.0 2 2026-07-11 32.4 3 2026-07-12 7.4 4 2026-07-13 17.6 5 2026-07-14 4.4 6 2026-07-15 22.1 7 2026-07-16 1.7 8 2026-07-17 0.0 -
Write each time as a date in the local zone, and total again:
rain$day_local <- format(rain$time_utc, "%Y-%m-%d", tz = "America/Denver") aggregate(value ~ day_local, data = rain, FUN = sum)You should see: seven dates, and different totals.
day_local value 1 2026-07-10 32.4 2 2026-07-11 0.0 3 2026-07-12 23.0 4 2026-07-13 6.4 5 2026-07-14 22.1 6 2026-07-15 1.7 7 2026-07-16 0.0 -
Compare with the same gauge's daily record:
week <- readings$date >= "2026-07-10" & readings$date <= "2026-07-16" readings[readings$gauge_id == "G03" & week, c("date", "value")]You should see: the seven totals from step 5, not those from step 4.
date value 69 2026-07-10 32.4 70 2026-07-11 0.0 71 2026-07-12 23.0 72 2026-07-13 6.4 73 2026-07-14 22.1 74 2026-07-15 1.7 75 2026-07-16 0.0
Check it worked¶
Your local-day totals equal the daily values in readings.csv for G03, day for day.
sum(rain$value) is 85.6 either way; only the days it falls on change.
If it didn't¶
The totals in step 4 already look like local days, or are off by hours. You left
out tz = "UTC" in step 3. R then takes the times to be in the app's own zone, which
in MESA RStudio Geospatial is not UTC, and gives no warning. Run step 3 again as
written.
Every time_utc is NA. The format does not match the text. It must describe
the timestamp exactly, including the T between date and time and the Z at the end.
Next¶
- Work with a long table in R, which uses the same file.
- Count records per day in R
- The same task in Python: Turn UTC timestamps into local days.
Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-16; the code was run there that day (R 4.6.2).
Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.