¿Cómo analizar los intervalos de tiempo en Ruby?

I have time interval strings like 6:30 am – 2:30 pm or 5:30–11:00 pm (notice the omitted "pm" for the start time here) that I want to parse into Time objects in Ruby. Is there an elegant way to do that or do I have to catch all different cases manually?

preguntado el 31 de enero de 12 a las 08:01

4 Respuestas

Respondido el 31 de enero de 12 a las 12:01

Perhaps using a time library like Crónico podría ayudarte?

Respondido el 31 de enero de 12 a las 12:01

Thanks. My problem is not the parsing of individual time strings but of the intervals, sometimes with omitted am/pm for the start time. Doesn't look like Chronic would be of help for that. - Ortwin Gentz

Thanks for the Chronic recommendation, alas, it doesn't provide interval parsing. Ended up using Time.parse and parsing the interval manually.

Respondido 01 Feb 12, 12:02

Here's a snippet that does this. It'll take in an iso8601 format interval string (of the format start,end) and returns you the start and finish times from the interval.

iso8601_interval_text = "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
start, finish = iso8601_interval_text.split(/\//).map {|t| Time.parse(t)}

respondido 11 mar '14, 02:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.