Get a dataframe of all service dates and their service IDs from a GTFS.
Source:R/gtfs_helpers.R
get_gtfs_service_dates.RdThis function returns a dataframe of each date covered by a GTFS and the
service_id run on this date. This data is extracted from the calendar.txt
and calendar_dates.txt files, depending on how the GTFS is structured. See
Details for a discussion.
Usage
get_gtfs_service_dates(
gtfs,
date_min = NULL,
date_max = NULL,
use_calendar_table = "calendar"
)Arguments
- gtfs
A tidygtfs object.
- date_min
Optional. The starting (earliest possible) Date object for the returned dataframe. Default is
NULL, where the earliest date in the GTFS will be used.- date_max
Optional. The ending (latest possible) Date object for the returned dataframe. Default is
NULL, where the latest date in the GTFS will be used.- use_calendar_table
Optional. Should the GTFS's
calendar.txtorcalendar_dates.txtbe used for the feasible date range? Must be"calendar"or"calendar_dates". Default is"calendar".
Details
The GTFS standard allows for two different structurings of calendar.txt
and calendar_dates.txt:
Standard service in
calendar.txt, with exceptions incalendar_dates.txt. Here,calendar.txtwill list the standard service ID by weekday (e.g., Monday, Tuesday, etc.), andcalendar_dates.txtlists specific dates which are exceptions to this. In this scenario,get_gtfs_service_dates()will get enumerate all weekdays and dates incalendar.txt, and assign the correctservice_idto it, depending on if the date is listed as an exception incalendar_dates.txt.All dates of service are enumerated in
calendar_dates.txt, andcalendar.txtis not used. In this scenario,get_gtfs_service_dates()will simply filter, clean, and return this table.
Use the input parameter use_calendar_table to control which method to use.
If use_calendar_table = "calendar", the former method will be used; if
use_calendar_table = "calendar_dates", the latter will be used. To
restrict the date enumeration to only a specific window, set date_min
and date_max.
Examples
# Set parameters
trb_start <- as.Date("2026-01-11")
trb_end <- as.Date("2026-01-15")
# Run function: get service ID by day in date range
trb_service_ids <- get_gtfs_service_dates(gtfs = wmata_gtfs,
date_min = trb_start,
date_max = trb_end,
use_calendar_table = "calendar")
print(trb_service_ids)
#> date service_id
#> 1 2026-01-11 2
#> 2 2026-01-12 9
#> 3 2026-01-13 9
#> 4 2026-01-14 10
#> 5 2026-01-15 9