Skip to main content
Solved

time conversions over midnight


kimo
Contributor
Forum|alt.badge.img+10
  • Contributor

The new DatetimeConverter works well for the gtfs stop times which are stored as only a time string field. But it rejects (naturally) any times greater or equal to 24:00:00. The gtfs convention is to roll over midnight for bus routes that do not finish before midnight. How might that be handled?

I am trying to calculate the total time of a route using Arrival_Time - Departure_Time.

I cannot just replace the 24 with 00 because I have to add up two intervals. I can see a clumsy three part conversion. There are 1.4M records so elegance and speed would be welcome.

Best answer by takashi

Just an idea. If you add a date (YYYYmmdd) to the time string as prefix after formatting the time string, you can calculate interval between two datetime values correctly. For example, add "20180101" if the formatted time is greater than "040000", "20180102" otherwise.

View original
Did this help you find an answer to your question?

3 replies

carsonlam
Safer
Forum|alt.badge.img
  • Safer
  • August 24, 2018

GTFS datetimes with hours >= 24 are not strictly valid datetimes. They're more like a date plus an interval value - this is the approach I use when I load GTFS data into databases. I'd consider separating the date part from the time part of the arrival/departure times, and then parsing the time part into a "seconds from midnight" value.


takashi
Supporter
  • Best Answer
  • August 24, 2018

Just an idea. If you add a date (YYYYmmdd) to the time string as prefix after formatting the time string, you can calculate interval between two datetime values correctly. For example, add "20180101" if the formatted time is greater than "040000", "20180102" otherwise.


kimo
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • August 26, 2018

Thanks the suggestions. I did a combination of these two approaches. The times are already split because they are a general trip record for any date. I used a test filter to split into three types: today, span_midnight, tomorrow. Today was left alone, tomorrow were split, 24 subtracted from the hour part and then the h,m,s reassembled into a string. Span_midnight were parsed like tomorrow and a dummy date added to each one, one day apart. Then the new time interval arithmetic gave me the duration in minutes. Since it is only the 'night buses' that are a problem there were only 700 out of the million so the extra processing was trivial.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings