Date and Time Zone Helpers in R With lubridate

Quick Overview

Write R helpers with lubridate that parse timestamp strings in a given time zone, convert instants between zones, extract integer date fields with Monday as day 1, and compute hour differences. It tests time zone semantics, weekday conventions, difftime units and daylight saving behavior.

Date and Time Zone Helpers in R With lubridate

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement date and time helpers in R using `lubridate`. Every timestamp string uses the exact format `"YYYY-MM-DD HH:MM:SS"`. - `parse_times(x, tz = "UTC")`: return a `POSIXct` of the same length as `x`, parsed in `tz` (its `tzone` attribute equals `tz`). - `to_timezone(times, tz)`: return the same instants displayed in `tz` using `lubridate::with_tz`, so only `tzone` changes. - `describe_times(x, tz = "UTC")`: return a `data.frame` (`stringsAsFactors = FALSE`) with one row per input and integer columns in the order `year, month, day, hour, minute, weekday`. `weekday` runs 1 to 7 with Monday = 1 and Sunday = 7 (`week_start = 1`). - `hours_between(start, end, tz = "UTC")`: take character inputs (recycled to a common length) and return the numeric difference `end - start` in hours, which may be fractional or negative. Example: ```r describe_times("2026-03-09 14:30:00", tz = "UTC") #> year month day hour minute weekday #> 1 2026 3 9 14 30 1 hours_between("2026-01-01 00:00:00", "2026-01-01 06:30:00") #> [1] 6.5 ``` March 9, 2026 is a Monday, so `weekday` is 1. The two timestamps in the second call are six and a half hours apart. ```hint Parse once, then read fields Parse in the requested zone first, so the field accessors read wall-clock values in that zone. Check which day lubridate treats as the first of the week by default. ``` ```hint Watch the difference units Subtracting two date-times picks a unit on its own based on the size of the gap. ``` ### Constraints & Assumptions - Input strings always use the exact format `"YYYY-MM-DD HH:MM:SS"`. - `tz` is an Olson time zone name such as `"UTC"` or `"America/New_York"`. - `lubridate` is installed. ### Clarifying Questions to Ask - Should a string in any other shape fail and return `NA`, or may it be parsed leniently? - In a zone with daylight saving time, what should happen to a wall-clock time that does not exist or that occurs twice? - Should `hours_between` measure elapsed time, or the difference between wall-clock readings across a daylight saving change? ### What a Strong Answer Covers - Parsing that returns `POSIXct` with the requested `tzone` and keeps the input length - The difference between changing the display zone and changing the instant - Integer field extraction in the requested zone, with Monday as day 1 - An hour difference with explicit units, recycling and the correct sign - Behavior around daylight saving transitions and unparseable input ### Follow-up Questions - What is the difference between `with_tz` and `force_tz`? - How would you handle daylight saving time gaps or ambiguous times when parsing? - How would you count the calendar days between two timestamps in a given zone?

Overview: Write R helpers with lubridate that parse timestamp strings in a given time zone, convert instants between zones, extract integer date fields with Monday as day 1, and compute hour differences. It tests time zone semantics, weekday conventions, difftime units and daylight saving behavior.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement date and time helpers in R using lubridate. Every timestamp string uses the exact format "YYYY-MM-DD HH:MM:SS".

  • parse_times(x, tz = "UTC") : return a POSIXct of the same length as x , parsed in tz (its tzone attribute equals tz ).
  • to_timezone(times, tz) : return the same instants displayed in tz using lubridate::with_tz , so only tzone changes.
  • describe_times(x, tz = "UTC") : return a data.frame ( stringsAsFactors = FALSE ) with one row per input and integer columns in the order year, month, day, hour, minute, weekday . weekday runs 1 to 7 with Monday = 1 and Sunday = 7 ( week_start = 1 ).
  • hours_between(start, end, tz = "UTC") : take character inputs (recycled to a common length) and return the numeric difference end - start in hours, which may be fractional or negative.

Example:

describe_times("2026-03-09 14:30:00", tz = "UTC")
#>   year month day hour minute weekday
#> 1 2026     3   9   14     30       1

hours_between("2026-01-01 00:00:00", "2026-01-01 06:30:00")
#> [1] 6.5

March 9, 2026 is a Monday, so weekday is 1. The two timestamps in the second call are six and a half hours apart.

Constraints & Assumptions

  • Input strings always use the exact format "YYYY-MM-DD HH:MM:SS" .
  • tz is an Olson time zone name such as "UTC" or "America/New_York" .
  • lubridate is installed.

Clarifying Questions to Ask Guidance

  • Should a string in any other shape fail and return NA , or may it be parsed leniently?
  • In a zone with daylight saving time, what should happen to a wall-clock time that does not exist or that occurs twice?
  • Should hours_between measure elapsed time, or the difference between wall-clock readings across a daylight saving change?

What a Strong Answer Covers Guidance

  • Parsing that returns POSIXct with the requested tzone and keeps the input length
  • The difference between changing the display zone and changing the instant
  • Integer field extraction in the requested zone, with Monday as day 1
  • An hour difference with explicit units, recycling and the correct sign
  • Behavior around daylight saving transitions and unparseable input

Follow-up Questions Guidance

  • What is the difference between with_tz and force_tz ?
  • How would you handle daylight saving time gaps or ambiguous times when parsing?
  • How would you count the calendar days between two timestamps in a given zone?